Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(sdk-coin-apt): transaction building changes for apt and legacy coin #5435

Merged
merged 1 commit into from
Jan 31, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions modules/sdk-coin-apt/src/lib/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ export const SECONDS_PER_WEEK = 7 * 24 * 60 * 60; // Days * Hours * Minutes * Se
export const APTOS_ACCOUNT_MODULE = 'aptos_account';
export const FUNGIBLE_ASSET_MODULE = 'primary_fungible_store';

export const FUNGIBLE_ASSET_FUNCTION = '0x1::primary_fungible_store::transfer';
export const FUNGIBLE_ASSET_TRANSFER_FUNCTION = '0x1::primary_fungible_store::transfer';
export const COIN_TRANSFER_FUNCTION = '0x1::aptos_account::transfer_coins';

export const FUNGIBLE_ASSET = '0x1::fungible_asset::Metadata';
export const FUNGIBLE_ASSET_TYPE_ARGUMENT = '0x1::fungible_asset::Metadata';
bhavidhingra marked this conversation as resolved.
Show resolved Hide resolved
export const APTOS_COIN = '0x1::aptos_coin::AptosCoin';
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import {
} from '@aptos-labs/ts-sdk';
bhavidhingra marked this conversation as resolved.
Show resolved Hide resolved
import { InvalidTransactionError, TransactionRecipient, TransactionType } from '@bitgo/sdk-core';
import { BaseCoin as CoinConfig, NetworkType } from '@bitgo/statics';
import { FUNGIBLE_ASSET, FUNGIBLE_ASSET_FUNCTION } from '../constants';
import { FUNGIBLE_ASSET_TYPE_ARGUMENT, FUNGIBLE_ASSET_TRANSFER_FUNCTION } from '../constants';

export class FungibleAssetTransaction extends Transaction {
constructor(coinConfig: Readonly<CoinConfig>) {
Expand All @@ -26,7 +26,7 @@ export class FungibleAssetTransaction extends Transaction {
!(payload instanceof TransactionPayloadEntryFunction) ||
payload.entryFunction.args.length !== 3 ||
payload.entryFunction.type_args.length !== 1 ||
FUNGIBLE_ASSET !== payload.entryFunction.type_args[0].toString()
FUNGIBLE_ASSET_TYPE_ARGUMENT !== payload.entryFunction.type_args[0].toString()
) {
throw new InvalidTransactionError('Invalid transaction payload');
}
Expand Down Expand Up @@ -55,8 +55,8 @@ export class FungibleAssetTransaction extends Transaction {
const simpleTxn = await aptos.transaction.build.simple({
sender: senderAddress,
data: {
function: FUNGIBLE_ASSET_FUNCTION,
typeArguments: [FUNGIBLE_ASSET],
function: FUNGIBLE_ASSET_TRANSFER_FUNCTION,
typeArguments: [FUNGIBLE_ASSET_TYPE_ARGUMENT],
functionArguments: [fungibleTokenAddress, recipientAddress, this.recipient.amount],
abi: faTransferAbi,
},
Expand Down
15 changes: 11 additions & 4 deletions modules/sdk-coin-apt/src/lib/transaction/transferTransaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,29 @@ import {
} from '@aptos-labs/ts-sdk';

import { BaseCoin as CoinConfig, NetworkType } from '@bitgo/statics';
import { APTOS_COIN, COIN_TRANSFER_FUNCTION } from '../constants';

export class TransferTransaction extends Transaction {
constructor(coinConfig: Readonly<CoinConfig>) {
super(coinConfig);
this._type = TransactionType.Send;
this._assetId = APTOS_COIN;
}

protected parseTransactionPayload(payload: TransactionPayload): void {
if (!(payload instanceof TransactionPayloadEntryFunction)) {
if (
!(payload instanceof TransactionPayloadEntryFunction) ||
payload.entryFunction.args.length !== 2 ||
payload.entryFunction.type_args.length !== 1 ||
payload.entryFunction.type_args[0].toString().length === 0
) {
throw new InvalidTransactionError('Invalid transaction payload');
}
const entryFunction = payload.entryFunction;
if (!this._recipient) {
this._recipient = {} as TransactionRecipient;
}
this._assetId = entryFunction.type_args[0].toString();
this._recipient.address = entryFunction.args[0].toString();
const amountBuffer = Buffer.from(entryFunction.args[1].bcsToBytes());
this._recipient.amount = amountBuffer.readBigUint64LE().toString();
Expand All @@ -35,12 +43,11 @@ export class TransferTransaction extends Transaction {
const aptos = new Aptos(new AptosConfig({ network }));
const senderAddress = AccountAddress.fromString(this._sender);
const recipientAddress = AccountAddress.fromString(this._recipient.address);

const simpleTxn = await aptos.transaction.build.simple({
sender: senderAddress,
data: {
function: '0x1::aptos_account::transfer_coins',
typeArguments: ['0x1::aptos_coin::AptosCoin'],
function: COIN_TRANSFER_FUNCTION,
typeArguments: [this.assetId],
functionArguments: [recipientAddress, this.recipient.amount],
},
options: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { TransactionType } from '@bitgo/sdk-core';
import BigNumber from 'bignumber.js';
import utils from '../utils';
import { TransactionPayload, TransactionPayloadEntryFunction } from '@aptos-labs/ts-sdk';
import { FUNGIBLE_ASSET } from '../constants';
import { FUNGIBLE_ASSET_TYPE_ARGUMENT } from '../constants';

export class FungibleAssetTransactionBuilder extends TransactionBuilder {
constructor(_coinConfig: Readonly<CoinConfig>) {
Expand All @@ -16,6 +16,12 @@ export class FungibleAssetTransactionBuilder extends TransactionBuilder {
protected get transactionType(): TransactionType {
return TransactionType.SendToken;
}
//TODO: Ticket: COIN-2941 : Check Statics based asset validation and if possible, implement it
assetId(assetId: string): TransactionBuilder {
this.validateAddress({ address: assetId });
bhavidhingra marked this conversation as resolved.
Show resolved Hide resolved
this.transaction.assetId = assetId;
return this;
}

/** @inheritdoc */
validateTransaction(transaction?: FungibleAssetTransaction): void {
Expand All @@ -32,7 +38,7 @@ export class FungibleAssetTransactionBuilder extends TransactionBuilder {
!(payload instanceof TransactionPayloadEntryFunction) ||
payload.entryFunction.args.length !== 3 ||
payload.entryFunction.type_args.length !== 1 ||
FUNGIBLE_ASSET !== payload.entryFunction.type_args[0].toString()
FUNGIBLE_ASSET_TYPE_ARGUMENT !== payload.entryFunction.type_args[0].toString()
bhavidhingra marked this conversation as resolved.
Show resolved Hide resolved
) {
console.error('invalid transaction payload');
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,11 +84,7 @@ export abstract class TransactionBuilder extends BaseTransactionBuilder {
return this;
}

assetId(assetId: string): TransactionBuilder {
this.validateAddress({ address: assetId });
this.transaction.assetId = assetId;
return this;
}
abstract assetId(assetId: string): TransactionBuilder;

/** @inheritdoc */
protected signImplementation(key: BaseKey): Transaction {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,32 @@ export class TransferBuilder extends TransactionBuilder {
return TransactionType.Send;
}

validatePartsOfAssetId(assetId: string): void {
const parts = assetId.split('::');
bhavidhingra marked this conversation as resolved.
Show resolved Hide resolved
if (parts.length !== 3 || ('0x1' !== parts[0] && !utils.isValidAddress(parts[0]))) {
throw new Error('Invalid asset ID');
}
}

assetId(assetId: string): TransactionBuilder {
this.validatePartsOfAssetId(assetId);
this.transaction.assetId = assetId;
bhavidhingra marked this conversation as resolved.
Show resolved Hide resolved
return this;
}

protected isValidTransactionPayload(payload: TransactionPayload) {
try {
if (!(payload instanceof TransactionPayloadEntryFunction)) {
if (
!(payload instanceof TransactionPayloadEntryFunction) ||
payload.entryFunction.args.length !== 2 ||
payload.entryFunction.type_args.length !== 1 ||
payload.entryFunction.type_args[0].toString().length === 0
) {
console.error('invalid transaction payload');
return false;
}
const entryFunction = payload.entryFunction;
this.validatePartsOfAssetId(entryFunction.type_args[0].toString());
const recipientAddress = entryFunction.args[0].toString();
const amountBuffer = Buffer.from(entryFunction.args[1].bcsToBytes());
const recipientAmount = new BigNumber(amountBuffer.readBigUint64LE().toString());
Expand Down
2 changes: 1 addition & 1 deletion modules/sdk-coin-apt/src/lib/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ export class Utils implements BaseUtils {
throw new Error('Invalid Payload: Expected TransactionPayloadEntryFunction');
}
const entryFunction = payload.entryFunction;
const moduleIdentifier = entryFunction.module_name.name.identifier.trim();
const moduleIdentifier = entryFunction.module_name.name.identifier;
switch (moduleIdentifier) {
case APTOS_ACCOUNT_MODULE:
bhavidhingra marked this conversation as resolved.
Show resolved Hide resolved
return TransactionType.Send;
Expand Down
5 changes: 5 additions & 0 deletions modules/sdk-coin-apt/test/resources/apt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,9 @@ export const invalidRecipients: Recipient[] = [
export const TRANSFER =
'0x1aed808916ab9b1b30b07abb53561afd46847285ce28651221d406173a37244992000000000000000200000000000000000000000000000000000000000000000000000000000000010d6170746f735f6163636f756e740e7472616e736665725f636f696e73010700000000000000000000000000000000000000000000000000000000000000010a6170746f735f636f696e094170746f73436f696e000220f7405c28a02cf5bab4ea4498240bb3579db45951794eb1c843bef0534c093ad908e803000000000000400d0300000000006400000000000000979390670000000002030020f73836f42257240e43d439552471fc9dbcc3f1af5bd0b4ed83f44b5f6614644240caeb90efd4b7ecd922c97bb3163e6a9de1fbb2ee0fc0d56af484f4af9b0015c5831341550af29b3686713b6657c821d894635fe13c7933f06ee043728f040b090000dbc87a1c816d9bcd06b683c37e80c7162e4d48da7812198b830e4d5d8e0629f200205223396c531f13e031a9f0cb26d459d799a52e51be9a1cb9e871afb4c31f91ff4013e7e8a1325ee5f656c93baa3d0206a1d9bd6da5abdc6f5d9b8bbbb0926ddac68f3e57a915dd217d2d43e776a6cc01af72f895ea712acc836d30349f29a3c606';

export const TRANSACTION_USING_TRANSFER_COINS =
'0x1aed808916ab9b1b30b07abb53561afd46847285ce28651221d406173a37244992000000000000000200000000000000000000000000000000000000000000000000000000000000010d6170746f735f6163636f756e740e7472616e736665725f636f696e73010700000000000000000000000000000000000000000000000000000000000000010a6170746f735f636f696e094170746f73436f696e000220f7405c28a02cf5bab4ea4498240bb3579db45951794eb1c843bef0534c093ad908e803000000000000400d0300000000006400000000000000979390670000000002030020f73836f42257240e43d439552471fc9dbcc3f1af5bd0b4ed83f44b5f6614644240caeb90efd4b7ecd922c97bb3163e6a9de1fbb2ee0fc0d56af484f4af9b0015c5831341550af29b3686713b6657c821d894635fe13c7933f06ee043728f040b090000dbc87a1c816d9bcd06b683c37e80c7162e4d48da7812198b830e4d5d8e0629f200205223396c531f13e031a9f0cb26d459d799a52e51be9a1cb9e871afb4c31f91ff4013e7e8a1325ee5f656c93baa3d0206a1d9bd6da5abdc6f5d9b8bbbb0926ddac68f3e57a915dd217d2d43e776a6cc01af72f895ea712acc836d30349f29a3c606';

export const INVALID_TRANSFER =
'AAAAAAAAAAAAA6e7361637469bc4a58e500b9e64cb6547ee9b403000000000000002064ba1fb2f2fbd2938a350015d601f4db89cd7e8e2370d0dd9ae3ac4f635c1581111b8a49f67370bc4a58e500b9e64cb6462e39b802000000000000002064ba1fb2f2fbd2938a350015d601f4db89cd7e8e2370d0dd9ae3ac47aa1ff81f01c4173a804406a365e69dfb297d4eaaf002546ebd016400000000000000cba4a48bb0f8b586c167e5dcefaa1c5e96ab3f0836d6ca08f2081732944d1e5b6b406a4a462e39b8030000000000000020b9490ede63215262c434e03f606d9799f3ba704523ceda184b386d47aa1ff81f01000000000000006400000000000000';

Expand All @@ -73,3 +76,5 @@ export const fungibleTokenAddress = {

export const FUNGIBLE_TOKEN_TRANSFER =
'0x1aed808916ab9b1b30b07abb53561afd46847285ce28651221d406173a372449a700000000000000020000000000000000000000000000000000000000000000000000000000000001167072696d6172795f66756e6769626c655f73746f7265087472616e73666572010700000000000000000000000000000000000000000000000000000000000000010e66756e6769626c655f6173736574084d65746164617461000320d5d0d561493ea2b9410f67da804653ae44e793c2423707d4f11edb2e3819205020f7405c28a02cf5bab4ea4498240bb3579db45951794eb1c843bef0534c093ad9080100000000000000400d0300000000006400000000000000e42696670000000002030020f73836f42257240e43d439552471fc9dbcc3f1af5bd0b4ed83f44b5f661464424029665cd4c94658a0d83907bbed7e761794b25bccc03fc87e6dd63a543accdddfd7a6f1e7a15e8681547ca437ff99b58c92f816e35a0f333d7f1fd1330c21ad0a0000dbc87a1c816d9bcd06b683c37e80c7162e4d48da7812198b830e4d5d8e0629f200205223396c531f13e031a9f0cb26d459d799a52e51be9a1cb9e871afb4c31f91ff40de7b0bb86ca346031942e9cf21ff9604c7c08c2c662e38a0afe3dd640512c0441396c0697cd8bbbcf39694d6f88e35f6bed9fb34bd209b0479b5e8bd0cf3eb0b';

export const LEGACY_COIN = '0x4fb379c10c763a13e724064ecfb7d946690bea519ba982c81b518d1c11dd23fe::fa_test::Coinz';
Loading