-
Notifications
You must be signed in to change notification settings - Fork 284
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): test cases for legacy coin
TICKET: COIN-2914
- Loading branch information
Showing
9 changed files
with
7,164 additions
and
4,864 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
69 changes: 69 additions & 0 deletions
69
modules/sdk-coin-apt/src/lib/transaction/nonFungibleAssetTransaction.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,69 @@ | ||
import { Transaction } from './transaction'; | ||
import { | ||
AccountAddress, | ||
Aptos, | ||
AptosConfig, | ||
EntryFunctionABI, | ||
MoveAbility, | ||
Network, | ||
objectStructTag, | ||
TransactionPayload, | ||
TransactionPayloadEntryFunction, | ||
TypeTagAddress, | ||
TypeTagGeneric, | ||
TypeTagStruct, | ||
} from '@aptos-labs/ts-sdk'; | ||
import { InvalidTransactionError, TransactionRecipient, TransactionType } from '@bitgo/sdk-core'; | ||
import { BaseCoin as CoinConfig, NetworkType } from '@bitgo/statics'; | ||
import { NON_FUNGIBLE_ASSET_TRANSFER_SUPPLY_PER_TRANSACTION } from '../constants'; | ||
|
||
export class NonFungibleAssetTransaction extends Transaction { | ||
constructor(coinConfig: Readonly<CoinConfig>) { | ||
super(coinConfig); | ||
this._type = TransactionType.SendNFT; | ||
this._assetId = AccountAddress.ZERO.toString(); | ||
} | ||
|
||
protected parseTransactionPayload(payload: TransactionPayload): void { | ||
if (!(payload instanceof TransactionPayloadEntryFunction) || payload.entryFunction.args.length !== 2) { | ||
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(); | ||
this._recipient.amount = NON_FUNGIBLE_ASSET_TRANSFER_SUPPLY_PER_TRANSACTION; | ||
} | ||
|
||
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 nonFungibleAssetAccountAddress = AccountAddress.fromString(this._assetId); | ||
|
||
const transferDigitalAssetAbi: EntryFunctionABI = { | ||
typeParameters: [{ constraints: [MoveAbility.KEY] }], | ||
parameters: [new TypeTagStruct(objectStructTag(new TypeTagGeneric(0))), new TypeTagAddress()], | ||
}; | ||
|
||
const simpleTxn = await aptos.transaction.build.simple({ | ||
sender: senderAddress, | ||
data: { | ||
function: '0x1::object::transfer', | ||
typeArguments: ['0x4::token::Token'], | ||
functionArguments: [nonFungibleAssetAccountAddress, recipientAddress], | ||
abi: transferDigitalAssetAbi, | ||
}, | ||
options: { | ||
maxGasAmount: this.maxGasAmount, | ||
gasUnitPrice: this.gasUnitPrice, | ||
expireTimestamp: this.expirationTime, | ||
accountSequenceNumber: this.sequenceNumber, | ||
}, | ||
}); | ||
this._rawTransaction = simpleTxn.rawTransaction; | ||
} | ||
} |
70 changes: 70 additions & 0 deletions
70
modules/sdk-coin-apt/src/lib/transactionBuilder/nonFungibleAssetTransactionBuilder.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,70 @@ | ||
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 utils from '../utils'; | ||
import { TransactionPayload, TransactionPayloadEntryFunction } from '@aptos-labs/ts-sdk'; | ||
import { NonFungibleAssetTransaction } from '../transaction/nonFungibleAssetTransaction'; | ||
|
||
export class NonFungibleAssetTransactionBuilder extends TransactionBuilder { | ||
constructor(_coinConfig: Readonly<CoinConfig>) { | ||
super(_coinConfig); | ||
this._transaction = new NonFungibleAssetTransaction(_coinConfig); | ||
} | ||
|
||
protected get transactionType(): TransactionType { | ||
return TransactionType.SendNFT; | ||
} | ||
|
||
assetId(assetId: string): TransactionBuilder { | ||
this.validateAddress({ address: assetId }); | ||
this.transaction.assetId = assetId; | ||
return this; | ||
} | ||
|
||
/** @inheritdoc */ | ||
initBuilder(tx: FungibleAssetTransaction): void { | ||
this._transaction = tx; | ||
} | ||
|
||
/** @inheritdoc */ | ||
validateTransaction(transaction?: FungibleAssetTransaction): void { | ||
if (!transaction) { | ||
throw new Error('transaction not defined'); | ||
} | ||
this.validateAddress({ address: transaction.sender }); | ||
this.validateAddress({ address: transaction.recipient.address }); | ||
this.validateAddress({ address: transaction.assetId }); | ||
} | ||
|
||
protected isValidTransactionPayload(payload: TransactionPayload) { | ||
try { | ||
if (!(payload instanceof TransactionPayloadEntryFunction) || payload.entryFunction.args.length !== 2) { | ||
console.error('invalid transaction payload'); | ||
return false; | ||
} | ||
const entryFunction = payload.entryFunction; | ||
const nonFungibleAssetAddress = entryFunction.args[0].toString(); | ||
const recipientAddress = entryFunction.args[1].toString(); | ||
return utils.isValidAddress(recipientAddress) && utils.isValidAddress(nonFungibleAssetAddress); | ||
} catch (e) { | ||
console.error('invalid transaction payload', e); | ||
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; | ||
} | ||
} |
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
Oops, something went wrong.