-
Notifications
You must be signed in to change notification settings - Fork 302
feat(sdk-coin-tempo): add tests for TIP-20 transaction builder #8149
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
Merged
+535
−259
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 |
|---|---|---|
| @@ -1,121 +1,213 @@ | ||
| /** | ||
| * TIP-20 Integration Tests | ||
| * | ||
| * End-to-end flows for TIP-20 token transactions on Tempo using testnet token addresses. | ||
| */ | ||
|
|
||
| import assert from 'assert'; | ||
| import { describe, it } from 'mocha'; | ||
| import { ethers } from 'ethers'; | ||
| import { coins } from '@bitgo/statics'; | ||
| import { Tip20TransactionBuilder } from '../../src/lib/transactionBuilder'; | ||
| import { Tip20Transaction } from '../../src/lib/transaction'; | ||
| import { Address } from '../../src/lib/types'; | ||
| import { coins } from '@bitgo/statics'; | ||
| import { AA_TRANSACTION_TYPE, TEMPO_CHAIN_IDS } from '../../src/lib/constants'; | ||
| import { TESTNET_TOKENS, TX_PARAMS, SIGNATURE_TEST_DATA, TEST_RECIPIENT_ADDRESS } from '../resources/tempo'; | ||
|
|
||
| const mockCoinConfig = coins.get('ttempo'); | ||
|
|
||
| const ALPHA_USD_TOKEN = TESTNET_TOKENS.alphaUSD.address as Address; | ||
| const BETA_USD_TOKEN = TESTNET_TOKENS.betaUSD.address as Address; | ||
| const THETA_USD_TOKEN = TESTNET_TOKENS.thetaUSD.address as Address; | ||
| const RECEIVER_ADDRESS = TEST_RECIPIENT_ADDRESS as Address; | ||
|
|
||
| describe('TIP-20 Integration Tests', () => { | ||
| const ALPHA_USD_TOKEN = '0x...' as Address; | ||
| const BETA_USD_TOKEN = '0x...' as Address; | ||
| const THETA_USD_TOKEN = '0x...' as Address; | ||
| const RECEIVER_ADDRESS = '0x...' as Address; | ||
| describe('Single Transfer', () => { | ||
| it('should build and serialize single TIP-20 transfer', async () => { | ||
| const builder = new Tip20TransactionBuilder(mockCoinConfig); | ||
|
|
||
| describe.skip('Single Transfer', () => { | ||
| it('should build single TIP-20 transfer without memo', async () => { | ||
| builder | ||
| .addOperation({ | ||
| token: ALPHA_USD_TOKEN, | ||
| to: RECEIVER_ADDRESS, | ||
| amount: '1', | ||
| memo: '1', | ||
| }) | ||
| .feeToken(ALPHA_USD_TOKEN) | ||
| .nonce(0) | ||
| .gas(TX_PARAMS.defaultGas) | ||
| .maxFeePerGas(TX_PARAMS.defaultMaxFeePerGas) | ||
| .maxPriorityFeePerGas(TX_PARAMS.defaultMaxPriorityFeePerGas); | ||
|
|
||
| const tx = (await builder.build()) as Tip20Transaction; | ||
|
|
||
| assert.ok(tx instanceof Tip20Transaction); | ||
| assert.strictEqual(tx.getOperationCount(), 1); | ||
| assert.strictEqual(tx.isBatch(), false); | ||
| assert.strictEqual(tx.getOperations()[0].memo, '1'); | ||
|
|
||
| const serialized = await tx.serialize(); | ||
| assert.ok(serialized.startsWith(AA_TRANSACTION_TYPE)); | ||
| }); | ||
| }); | ||
|
|
||
| describe('Batch Transfer', () => { | ||
| it('should build multi-token batch transfer', async () => { | ||
| const builder = new Tip20TransactionBuilder(mockCoinConfig); | ||
| builder.addOperation({ | ||
| token: ALPHA_USD_TOKEN, | ||
| to: RECEIVER_ADDRESS, | ||
| amount: '1.0', | ||
| }); | ||
| builder.feeToken(ALPHA_USD_TOKEN); | ||
| // TODO: const tx = await builder.build(); | ||
|
|
||
| builder | ||
| .addOperation({ token: ALPHA_USD_TOKEN, to: RECEIVER_ADDRESS, amount: '15', memo: '1' }) | ||
| .addOperation({ token: BETA_USD_TOKEN, to: RECEIVER_ADDRESS, amount: '20', memo: '2' }) | ||
| .addOperation({ token: THETA_USD_TOKEN, to: RECEIVER_ADDRESS, amount: '7', memo: '3' }) | ||
| .feeToken(BETA_USD_TOKEN) | ||
| .nonce(10) | ||
| .gas(400000n) | ||
| .maxFeePerGas(TX_PARAMS.defaultMaxFeePerGas) | ||
| .maxPriorityFeePerGas(TX_PARAMS.defaultMaxPriorityFeePerGas); | ||
|
|
||
| const tx = (await builder.build()) as Tip20Transaction; | ||
|
|
||
| assert.strictEqual(tx.getOperationCount(), 3); | ||
| assert.strictEqual(tx.isBatch(), true); | ||
| assert.strictEqual(tx.getFeeToken(), BETA_USD_TOKEN); | ||
|
|
||
| const operations = tx.getOperations(); | ||
| assert.strictEqual(operations[0].token, ALPHA_USD_TOKEN); | ||
| assert.strictEqual(operations[1].token, BETA_USD_TOKEN); | ||
| assert.strictEqual(operations[2].token, THETA_USD_TOKEN); | ||
| }); | ||
| }); | ||
|
|
||
| describe('Transaction Signing', () => { | ||
| it('should produce unsigned transaction with correct RLP structure', async () => { | ||
| const builder = new Tip20TransactionBuilder(mockCoinConfig); | ||
|
|
||
| builder | ||
| .addOperation({ token: ALPHA_USD_TOKEN, to: RECEIVER_ADDRESS, amount: '1' }) | ||
| .feeToken(ALPHA_USD_TOKEN) | ||
| .nonce(0) | ||
| .gas(TX_PARAMS.defaultGas) | ||
| .maxFeePerGas(TX_PARAMS.defaultMaxFeePerGas) | ||
| .maxPriorityFeePerGas(TX_PARAMS.defaultMaxPriorityFeePerGas); | ||
|
|
||
| const tx = (await builder.build()) as Tip20Transaction; | ||
| const unsignedHex = await tx.serialize(); | ||
|
|
||
| assert.ok(unsignedHex.startsWith(AA_TRANSACTION_TYPE)); | ||
| assert.strictEqual(tx.getSignature(), undefined); | ||
|
|
||
| // Verify RLP structure (13 fields for unsigned) | ||
| const rlpPart = '0x' + unsignedHex.slice(4); | ||
| const decoded = ethers.utils.RLP.decode(rlpPart); | ||
| assert.strictEqual(decoded.length, 13); | ||
| }); | ||
|
|
||
| it('should build single TIP-20 transfer with memo', async () => { | ||
| it('should apply signature and produce broadcast format', async () => { | ||
| const builder = new Tip20TransactionBuilder(mockCoinConfig); | ||
| builder.addOperation({ | ||
| token: ALPHA_USD_TOKEN, | ||
| to: RECEIVER_ADDRESS, | ||
| amount: '1.0', | ||
| memo: '12345', | ||
| }); | ||
| builder.feeToken(ALPHA_USD_TOKEN); | ||
| // TODO: const tx = await builder.build(); | ||
|
|
||
| builder | ||
| .addOperation({ token: ALPHA_USD_TOKEN, to: RECEIVER_ADDRESS, amount: '1' }) | ||
| .feeToken(ALPHA_USD_TOKEN) | ||
| .nonce(0) | ||
| .gas(TX_PARAMS.defaultGas) | ||
| .maxFeePerGas(TX_PARAMS.defaultMaxFeePerGas) | ||
| .maxPriorityFeePerGas(TX_PARAMS.defaultMaxPriorityFeePerGas); | ||
|
|
||
| const tx = (await builder.build()) as Tip20Transaction; | ||
| tx.setSignature(SIGNATURE_TEST_DATA.validSignature); | ||
|
|
||
| const signedHex = await tx.toBroadcastFormat(); | ||
|
|
||
| // Verify it includes signature (14 fields) | ||
| assert.ok(signedHex.startsWith(AA_TRANSACTION_TYPE)); | ||
| const rlpPart = '0x' + signedHex.slice(4); | ||
| const decoded = ethers.utils.RLP.decode(rlpPart); | ||
| assert.strictEqual(decoded.length, 14); | ||
|
|
||
| const storedSig = tx.getSignature(); | ||
| assert.ok(storedSig); | ||
| assert.strictEqual(storedSig?.r, SIGNATURE_TEST_DATA.validSignature.r); | ||
| }); | ||
| }); | ||
|
|
||
| describe.skip('Batch Transfer', () => { | ||
| it('should build batch transfer with multiple memos', async () => { | ||
| describe('Fee Token Selection', () => { | ||
| it('should pay fees with different token than transfer', async () => { | ||
| const builder = new Tip20TransactionBuilder(mockCoinConfig); | ||
|
|
||
| builder | ||
| .addOperation({ | ||
| token: ALPHA_USD_TOKEN, | ||
| to: RECEIVER_ADDRESS, | ||
| amount: '0.5', | ||
| memo: '1001', | ||
| }) | ||
| .addOperation({ | ||
| token: ALPHA_USD_TOKEN, | ||
| to: RECEIVER_ADDRESS, | ||
| amount: '0.3', | ||
| memo: '1002', | ||
| }) | ||
| .addOperation({ | ||
| token: ALPHA_USD_TOKEN, | ||
| to: RECEIVER_ADDRESS, | ||
| amount: '0.2', | ||
| memo: '1003', | ||
| }); | ||
| builder.feeToken(ALPHA_USD_TOKEN); | ||
| // TODO: const tx = await builder.build(); | ||
| .addOperation({ token: ALPHA_USD_TOKEN, to: RECEIVER_ADDRESS, amount: '1' }) | ||
| .feeToken(BETA_USD_TOKEN) | ||
| .nonce(0) | ||
| .gas(TX_PARAMS.defaultGas) | ||
| .maxFeePerGas(TX_PARAMS.defaultMaxFeePerGas) | ||
| .maxPriorityFeePerGas(TX_PARAMS.defaultMaxPriorityFeePerGas); | ||
|
|
||
| const tx = (await builder.build()) as Tip20Transaction; | ||
|
|
||
| assert.strictEqual(tx.getOperations()[0].token, ALPHA_USD_TOKEN); | ||
| assert.strictEqual(tx.getFeeToken(), BETA_USD_TOKEN); | ||
| }); | ||
|
|
||
| it('should build multi-token batch transfer', async () => { | ||
| it('should build transaction without explicit fee token', async () => { | ||
| const builder = new Tip20TransactionBuilder(mockCoinConfig); | ||
|
|
||
| builder | ||
| .addOperation({ | ||
| token: ALPHA_USD_TOKEN, | ||
| to: RECEIVER_ADDRESS, | ||
| amount: '1.5', | ||
| memo: '2001', | ||
| }) | ||
| .addOperation({ | ||
| token: BETA_USD_TOKEN, | ||
| to: RECEIVER_ADDRESS, | ||
| amount: '2.0', | ||
| memo: '2002', | ||
| }) | ||
| .addOperation({ | ||
| token: THETA_USD_TOKEN, | ||
| to: RECEIVER_ADDRESS, | ||
| amount: '0.75', | ||
| memo: '2003', | ||
| }); | ||
| builder.feeToken(BETA_USD_TOKEN); | ||
| // TODO: const tx = await builder.build(); | ||
| .addOperation({ token: ALPHA_USD_TOKEN, to: RECEIVER_ADDRESS, amount: '1' }) | ||
| .nonce(0) | ||
| .gas(TX_PARAMS.defaultGas) | ||
| .maxFeePerGas(TX_PARAMS.defaultMaxFeePerGas) | ||
| .maxPriorityFeePerGas(TX_PARAMS.defaultMaxPriorityFeePerGas); | ||
|
|
||
| const tx = (await builder.build()) as Tip20Transaction; | ||
|
|
||
| assert.strictEqual(tx.getFeeToken(), undefined); | ||
| const serialized = await tx.serialize(); | ||
| assert.ok(serialized.startsWith(AA_TRANSACTION_TYPE)); | ||
| }); | ||
| }); | ||
|
|
||
| describe.skip('Transaction Signing', () => { | ||
| it('should sign and serialize transaction', async () => { | ||
| describe('Transaction JSON', () => { | ||
| it('should produce complete JSON representation', async () => { | ||
| const builder = new Tip20TransactionBuilder(mockCoinConfig); | ||
| builder.addOperation({ | ||
| token: ALPHA_USD_TOKEN, | ||
| to: RECEIVER_ADDRESS, | ||
| amount: '1.0', | ||
| memo: '9999', | ||
| }); | ||
| builder.feeToken(ALPHA_USD_TOKEN); | ||
| // TODO: Implement signing with ethers.js Wallet | ||
| // TODO: const tx = await builder.build(); | ||
| // TODO: tx.setSignature(signature); | ||
| // TODO: const serialized = await tx.toBroadcastFormat(); | ||
|
|
||
| builder | ||
| .addOperation({ token: ALPHA_USD_TOKEN, to: RECEIVER_ADDRESS, amount: '123456', memo: '5' }) | ||
| .feeToken(BETA_USD_TOKEN) | ||
| .nonce(42) | ||
| .gas(150000n) | ||
| .maxFeePerGas(3000000000n) | ||
| .maxPriorityFeePerGas(1500000000n); | ||
|
|
||
| const tx = (await builder.build()) as Tip20Transaction; | ||
| const json = tx.toJson(); | ||
|
|
||
| assert.strictEqual(json.type, AA_TRANSACTION_TYPE); | ||
| assert.strictEqual(json.chainId, TEMPO_CHAIN_IDS.TESTNET); | ||
| assert.strictEqual(json.nonce, 42); | ||
| assert.strictEqual(json.gas, '150000'); | ||
| assert.strictEqual(json.feeToken, BETA_USD_TOKEN); | ||
|
|
||
| const ops = json.operations as any[]; | ||
| assert.strictEqual(ops[0].token, ALPHA_USD_TOKEN); | ||
| assert.strictEqual(ops[0].amount, '123456'); | ||
| assert.strictEqual(ops[0].memo, '5'); | ||
| }); | ||
| }); | ||
|
|
||
| describe.skip('Fee Token Selection', () => { | ||
| it('should pay fees with different token than transfer', async () => { | ||
| describe('canBroadcast Check', () => { | ||
| it('should return true for valid transaction', async () => { | ||
| const builder = new Tip20TransactionBuilder(mockCoinConfig); | ||
| builder.addOperation({ | ||
| token: ALPHA_USD_TOKEN, | ||
| to: RECEIVER_ADDRESS, | ||
| amount: '1.0', | ||
| memo: '5555', | ||
| }); | ||
| builder.feeToken(BETA_USD_TOKEN); | ||
| // TODO: const tx = await builder.build(); | ||
|
|
||
| builder | ||
| .addOperation({ token: ALPHA_USD_TOKEN, to: RECEIVER_ADDRESS, amount: '1' }) | ||
| .feeToken(ALPHA_USD_TOKEN) | ||
| .nonce(0) | ||
| .gas(TX_PARAMS.defaultGas) | ||
| .maxFeePerGas(TX_PARAMS.defaultMaxFeePerGas) | ||
| .maxPriorityFeePerGas(TX_PARAMS.defaultMaxPriorityFeePerGas); | ||
|
|
||
| const tx = (await builder.build()) as Tip20Transaction; | ||
| assert.strictEqual(tx.canBroadcast(), true); | ||
| }); | ||
| }); | ||
| }); |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.