Skip to content

Commit

Permalink
feat(sdk-coin-apt): improvements in code
Browse files Browse the repository at this point in the history
TICKET: COIN-2894
  • Loading branch information
baltiyal committed Jan 28, 2025
1 parent dca736f commit 490c681
Show file tree
Hide file tree
Showing 8 changed files with 37 additions and 55 deletions.
4 changes: 4 additions & 0 deletions modules/sdk-coin-apt/src/lib/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,7 @@ 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 = '0x1::primary_fungible_store::transfer';

export const FUNGIBLE_ASSET_TYPE_ARGS = '0x1::fungible_asset::Metadata';
2 changes: 1 addition & 1 deletion modules/sdk-coin-apt/src/lib/iface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,5 @@ export interface TxData {
gasUsed: number;
expirationTime: number;
feePayer: string;
assetId?: string;
assetId: string;
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,20 @@ import {
} from '@aptos-labs/ts-sdk';
import { InvalidTransactionError, TransactionRecipient, TransactionType } from '@bitgo/sdk-core';
import { BaseCoin as CoinConfig, NetworkType } from '@bitgo/statics';
import { FUNGIBLE_ASSET, FUNGIBLE_ASSET_TYPE_ARGS } from '../constants';

export class FungibleAssetTransaction extends Transaction {
constructor(coinConfig: Readonly<CoinConfig>) {
super(coinConfig);
this._type = TransactionType.SendToken;
this._assetId = AccountAddress.ZERO.toString();
}

protected parseTransactionPayload(payload: TransactionPayload): void {
if (!(payload instanceof TransactionPayloadEntryFunction) || payload.entryFunction.args.length !== 3) {
if (
!(payload instanceof TransactionPayloadEntryFunction) ||
payload.entryFunction.args.length !== 3 ||
FUNGIBLE_ASSET_TYPE_ARGS !== payload.entryFunction.type_args[0].toString()
) {
throw new InvalidTransactionError('Invalid transaction payload');
}
const entryFunction = payload.entryFunction;
Expand Down Expand Up @@ -50,8 +54,8 @@ export class FungibleAssetTransaction extends Transaction {
const simpleTxn = await aptos.transaction.build.simple({
sender: senderAddress,
data: {
function: '0x1::primary_fungible_store::transfer',
typeArguments: ['0x1::fungible_asset::Metadata'],
function: FUNGIBLE_ASSET,
typeArguments: [FUNGIBLE_ASSET_TYPE_ARGS],
functionArguments: [fungibleTokenAddress, recipientAddress, this.recipient.amount],
abi: faTransferAbi,
},
Expand Down
1 change: 1 addition & 0 deletions modules/sdk-coin-apt/src/lib/transaction/transaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ export abstract class Transaction extends BaseTransaction {
this._expirationTime = Math.floor(Date.now() / 1e3) + SECONDS_PER_WEEK;
this._sequenceNumber = 0;
this._sender = AccountAddress.ZERO.toString();
this._assetId = AccountAddress.ZERO.toString();
this._senderSignature = {
publicKey: {
pub: Hex.fromHexInput(Transaction.EMPTY_PUBLIC_KEY).toString(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ import { TransactionBuilder } from './transactionBuilder';
import { BaseCoin as CoinConfig } from '@bitgo/statics';
import { FungibleAssetTransaction } from '../transaction/fungibleAssetTransaction';
import { TransactionType } from '@bitgo/sdk-core';
import { Transaction } from '../transaction/transaction';
import BigNumber from 'bignumber.js';
import utils from '../utils';
import { TransactionPayload, TransactionPayloadEntryFunction } from '@aptos-labs/ts-sdk';
import { FUNGIBLE_ASSET_TYPE_ARGS } from '../constants';

export class FungibleAssetTransactionBuilder extends TransactionBuilder {
constructor(_coinConfig: Readonly<CoinConfig>) {
Expand All @@ -17,11 +17,6 @@ export class FungibleAssetTransactionBuilder extends TransactionBuilder {
return TransactionType.SendToken;
}

/** @inheritdoc */
initBuilder(tx: FungibleAssetTransaction): void {
this._transaction = tx;
}

/** @inheritdoc */
validateTransaction(transaction?: FungibleAssetTransaction): void {
if (!transaction) {
Expand All @@ -35,7 +30,11 @@ export class FungibleAssetTransactionBuilder extends TransactionBuilder {

protected isValidTransactionPayload(payload: TransactionPayload) {
try {
if (!(payload instanceof TransactionPayloadEntryFunction) || payload.entryFunction.args.length !== 3) {
if (
!(payload instanceof TransactionPayloadEntryFunction) ||
payload.entryFunction.args.length !== 3 ||
FUNGIBLE_ASSET_TYPE_ARGS !== payload.entryFunction.type_args[0].toString()
) {
console.error('invalid transaction payload');
return false;
}
Expand All @@ -54,18 +53,4 @@ export class FungibleAssetTransactionBuilder extends TransactionBuilder {
return false;
}
}

/** @inheritdoc */
protected fromImplementation(rawTransaction: string): Transaction {
this.transaction.fromRawTransaction(rawTransaction);
this.transaction.transactionType = this.transactionType;
return this.transaction;
}

/** @inheritdoc */
protected async buildImplementation(): Promise<Transaction> {
this.transaction.transactionType = this.transactionType;
await this.transaction.build();
return this.transaction;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,9 @@ export abstract class TransactionBuilder extends BaseTransactionBuilder {
*
* @param {Transaction} tx the transaction data
*/
abstract initBuilder(tx: Transaction): void;
initBuilder(tx: Transaction): void {
this._transaction = tx;
}

/** @inheritdoc */
protected get transaction(): Transaction {
Expand All @@ -45,8 +47,6 @@ export abstract class TransactionBuilder extends BaseTransactionBuilder {
this._transaction = transaction;
}

abstract validateTransaction(transaction?: Transaction): void;

/**
* Sets the sender of this transaction.
*
Expand Down Expand Up @@ -104,6 +104,20 @@ export abstract class TransactionBuilder extends BaseTransactionBuilder {
this.transaction.addFeePayerSignature(publicKey, signature);
}

/** @inheritdoc */
protected fromImplementation(rawTransaction: string): Transaction {
this.transaction.fromRawTransaction(rawTransaction);
this.transaction.transactionType = this.transactionType;
return this.transaction;
}

/** @inheritdoc */
protected async buildImplementation(): Promise<Transaction> {
this.transaction.transactionType = this.transactionType;
await this.transaction.build();
return this.transaction;
}

// region Validators
/** @inheritdoc */
validateAddress(address: BaseAddress, addressFormat?: string): void {
Expand Down
24 changes: 0 additions & 24 deletions modules/sdk-coin-apt/src/lib/transactionBuilder/transferBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { TransactionBuilder } from './transactionBuilder';
import { BaseCoin as CoinConfig } from '@bitgo/statics';
import { TransactionType } from '@bitgo/sdk-core';
import { TransferTransaction } from '../transaction/transferTransaction';
import { Transaction } from '../transaction/transaction';
import BigNumber from 'bignumber.js';
import utils from '../utils';
import { TransactionPayload, TransactionPayloadEntryFunction } from '@aptos-labs/ts-sdk';
Expand All @@ -17,15 +16,6 @@ export class TransferBuilder extends TransactionBuilder {
return TransactionType.Send;
}

/**
* Initialize the transaction builder fields using the decoded transaction data
*
* @param {Transaction} tx the transaction data
*/
initBuilder(tx: TransferTransaction): void {
this._transaction = tx;
}

/** @inheritdoc */
validateTransaction(transaction?: TransferTransaction): void {
if (!transaction) {
Expand All @@ -52,18 +42,4 @@ export class TransferBuilder extends TransactionBuilder {
return false;
}
}

/** @inheritdoc */
protected fromImplementation(rawTransaction: string): Transaction {
this.transaction.fromRawTransaction(rawTransaction);
this.transaction.transactionType = this.transactionType;
return this.transaction;
}

/** @inheritdoc */
protected async buildImplementation(): Promise<Transaction> {
this.transaction.transactionType = this.transactionType;
await this.transaction.build();
return this.transaction;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,6 @@ describe('Apt Token Transfer Builder', () => {
it('should build and send a signed tx', async function () {
const txBuilder = factory.from(testData.FUNGIBLE_TOKEN_TRANSFER);
const tx = (await txBuilder.build()) as FungibleAssetTransaction;
console.log('----- tx: ', tx);
should.equal(tx.type, TransactionType.SendToken);
tx.inputs.length.should.equal(1);
tx.inputs[0].should.deepEqual({
address: testData.sender2.address,
Expand Down

0 comments on commit 490c681

Please sign in to comment.