-
Notifications
You must be signed in to change notification settings - Fork 283
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(sdk-coin-apt): fungible asset transfer transaction
TICKET: COIN-2894
- Loading branch information
Showing
17 changed files
with
527 additions
and
188 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
72 changes: 72 additions & 0 deletions
72
modules/sdk-coin-apt/src/lib/transaction/fungibleAssetTransaction.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
import { Transaction } from './transaction'; | ||
import { | ||
AccountAddress, | ||
Aptos, | ||
AptosConfig, | ||
EntryFunctionABI, | ||
Network, | ||
parseTypeTag, | ||
TransactionPayload, | ||
TransactionPayloadEntryFunction, | ||
TypeTagAddress, | ||
TypeTagU64, | ||
} 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_FUNCTION } from '../constants'; | ||
|
||
export class FungibleAssetTransaction extends Transaction { | ||
constructor(coinConfig: Readonly<CoinConfig>) { | ||
super(coinConfig); | ||
this._type = TransactionType.SendToken; | ||
} | ||
|
||
protected parseTransactionPayload(payload: TransactionPayload): void { | ||
if ( | ||
!(payload instanceof TransactionPayloadEntryFunction) || | ||
payload.entryFunction.args.length !== 3 || | ||
payload.entryFunction.type_args.length === 0 || | ||
FUNGIBLE_ASSET !== payload.entryFunction.type_args[0].toString() | ||
) { | ||
throw new InvalidTransactionError('Invalid transaction payload'); | ||
} | ||
const entryFunction = payload.entryFunction; | ||
if (!this._recipient) { | ||
this._recipient = {} as TransactionRecipient; | ||
} | ||
this._assetId = entryFunction.args[0].toString(); | ||
this._recipient.address = entryFunction.args[1].toString(); | ||
const amountBuffer = Buffer.from(entryFunction.args[2].bcsToBytes()); | ||
this._recipient.amount = amountBuffer.readBigUint64LE().toString(); | ||
} | ||
|
||
protected async buildRawTransaction(): Promise<void> { | ||
const network: Network = this._coinConfig.network.type === NetworkType.MAINNET ? Network.MAINNET : Network.TESTNET; | ||
const aptos = new Aptos(new AptosConfig({ network })); | ||
const senderAddress = AccountAddress.fromString(this._sender); | ||
const recipientAddress = AccountAddress.fromString(this._recipient.address); | ||
const fungibleTokenAddress = this._assetId; | ||
|
||
const faTransferAbi: EntryFunctionABI = { | ||
typeParameters: [{ constraints: [] }], | ||
parameters: [parseTypeTag('0x1::object::Object'), new TypeTagAddress(), new TypeTagU64()], | ||
}; | ||
|
||
const simpleTxn = await aptos.transaction.build.simple({ | ||
sender: senderAddress, | ||
data: { | ||
function: FUNGIBLE_ASSET_FUNCTION, | ||
typeArguments: [FUNGIBLE_ASSET], | ||
functionArguments: [fungibleTokenAddress, recipientAddress, this.recipient.amount], | ||
abi: faTransferAbi, | ||
}, | ||
options: { | ||
maxGasAmount: this.maxGasAmount, | ||
gasUnitPrice: this.gasUnitPrice, | ||
expireTimestamp: this.expirationTime, | ||
accountSequenceNumber: this.sequenceNumber, | ||
}, | ||
}); | ||
this._rawTransaction = simpleTxn.rawTransaction; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
71 changes: 15 additions & 56 deletions
71
modules/sdk-coin-apt/src/lib/transaction/transferTransaction.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
55 changes: 55 additions & 0 deletions
55
modules/sdk-coin-apt/src/lib/transactionBuilder/fungibleAssetTransactionBuilder.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
import { TransactionBuilder } from './transactionBuilder'; | ||
import { BaseCoin as CoinConfig } from '@bitgo/statics'; | ||
import { FungibleAssetTransaction } from '../transaction/fungibleAssetTransaction'; | ||
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'; | ||
|
||
export class FungibleAssetTransactionBuilder extends TransactionBuilder { | ||
constructor(_coinConfig: Readonly<CoinConfig>) { | ||
super(_coinConfig); | ||
this._transaction = new FungibleAssetTransaction(_coinConfig); | ||
} | ||
|
||
protected get transactionType(): TransactionType { | ||
return TransactionType.SendToken; | ||
} | ||
|
||
/** @inheritdoc */ | ||
validateTransaction(transaction?: FungibleAssetTransaction): void { | ||
if (!transaction) { | ||
throw new Error('fungible asset transaction not defined'); | ||
} | ||
super.validateTransaction(transaction); | ||
this.validateAddress({ address: transaction.assetId }); | ||
} | ||
|
||
protected isValidTransactionPayload(payload: TransactionPayload) { | ||
try { | ||
if ( | ||
!(payload instanceof TransactionPayloadEntryFunction) || | ||
payload.entryFunction.args.length !== 3 || | ||
payload.entryFunction.type_args.length === 0 || | ||
FUNGIBLE_ASSET !== payload.entryFunction.type_args[0].toString() | ||
) { | ||
console.error('invalid transaction payload'); | ||
return false; | ||
} | ||
const entryFunction = payload.entryFunction; | ||
const fungibleTokenAddress = entryFunction.args[0].toString(); | ||
const recipientAddress = entryFunction.args[1].toString(); | ||
const amountBuffer = Buffer.from(entryFunction.args[2].bcsToBytes()); | ||
const recipientAmount = new BigNumber(amountBuffer.readBigUint64LE().toString()); | ||
return ( | ||
utils.isValidAddress(recipientAddress) && | ||
utils.isValidAddress(fungibleTokenAddress) && | ||
!recipientAmount.isLessThan(0) | ||
); | ||
} catch (e) { | ||
console.error('invalid transaction payload', e); | ||
return false; | ||
} | ||
} | ||
} |
Oops, something went wrong.