Skip to content

Commit 3e9cece

Browse files
committed
feat(sdk-coin-apt): addition of test case for apt transaction builder
Ticket: COIN-2258
1 parent c2c10e6 commit 3e9cece

File tree

3 files changed

+84
-51
lines changed

3 files changed

+84
-51
lines changed

modules/sdk-coin-apt/src/lib/transactionBuilder.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,9 @@ export abstract class TransactionBuilder extends BaseTransactionBuilder {
139139

140140
/** @inheritdoc */
141141
validateValue(value: BigNumber): void {
142-
if (value.isLessThan(0)) {
142+
if (value.isNaN()) {
143+
throw new BuildTransactionError('Invalid amount format');
144+
} else if (value.isLessThan(0)) {
143145
throw new BuildTransactionError('Value cannot be less than zero');
144146
}
145147
}

modules/sdk-coin-apt/test/resources/apt.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,5 +32,20 @@ export const recipients: Recipient[] = [
3232
},
3333
];
3434

35+
export const invalidRecipients: Recipient[] = [
36+
{
37+
address: addresses.invalidAddresses[0],
38+
amount: AMOUNT.toString(),
39+
},
40+
{
41+
address: addresses.validAddresses[0],
42+
amount: '-919191',
43+
},
44+
{
45+
address: addresses.validAddresses[0],
46+
amount: 'invalidAmount',
47+
},
48+
];
49+
3550
export const TRANSFER =
3651
'0xc8f02d25aa698b3e9fbd8a08e8da4c8ee261832a25a4cde8731b5ec356537d09170000000000000002000000000000000000000000000000000000000000000000000000000000000104636f696e087472616e73666572010700000000000000000000000000000000000000000000000000000000000000010a6170746f735f636f696e094170746f73436f696e000220f7405c28a02cf5bab4ea4498240bb3579db45951794eb1c843bef0534c093ad908e803000000000000400d0300000000006400000000000000207c7667000000000200202121dcd098069ae535697dd019cfd8677ca7aba0adac1d1959cbce6dc54b12594010f340ec153b724c4dc1c9a435d0fafed1775d851c1e8d965925a7879550c69a4677925d9198334a72ae7ce8998226ff0a83743c7ba8a2831136c072bf21c404';

modules/sdk-coin-apt/test/unit/transferBuilder.ts

Lines changed: 66 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -10,61 +10,77 @@ import { TransferTransaction } from '../../src';
1010
describe('Apt Transfer Transaction', () => {
1111
const factory = new TransactionBuilderFactory(coins.get('tapt'));
1212

13-
it('should build a transfer tx', async function () {
14-
const transaction = new TransferTransaction(coins.get('tapt'));
15-
const txBuilder = factory.getTransferBuilder(transaction);
16-
txBuilder.sender(testData.sender2.address);
17-
txBuilder.recipient(testData.recipients[0]);
18-
txBuilder.gasData({
19-
maxGasAmount: 200000,
20-
gasUnitPrice: 100,
13+
describe('Succeed', () => {
14+
it('should build a transfer tx', async function () {
15+
const transaction = new TransferTransaction(coins.get('tapt'));
16+
const txBuilder = factory.getTransferBuilder(transaction);
17+
txBuilder.sender(testData.sender2.address);
18+
txBuilder.recipient(testData.recipients[0]);
19+
txBuilder.gasData({
20+
maxGasAmount: 200000,
21+
gasUnitPrice: 100,
22+
});
23+
txBuilder.sequenceNumber(14);
24+
txBuilder.expirationTime(1736246155);
25+
const tx = await txBuilder.build();
26+
should.equal(tx.type, TransactionType.Send);
27+
tx.inputs.length.should.equal(1);
28+
tx.inputs[0].should.deepEqual({
29+
address: testData.sender2.address,
30+
value: testData.recipients[0].amount,
31+
coin: 'tapt',
32+
});
33+
tx.outputs.length.should.equal(1);
34+
tx.outputs[0].should.deepEqual({
35+
address: testData.recipients[0].address,
36+
value: testData.recipients[0].amount,
37+
coin: 'tapt',
38+
});
39+
const rawTx = tx.toBroadcastFormat();
40+
should.equal(utils.isValidRawTransaction(rawTx), true);
41+
rawTx.should.equal(
42+
'0xc8f02d25aa698b3e9fbd8a08e8da4c8ee261832a25a4cde8731b5ec356537d090e0000000000000002000000000000000000000000000000000000000000000000000000000000000104636f696e087472616e73666572010700000000000000000000000000000000000000000000000000000000000000010a6170746f735f636f696e094170746f73436f696e000220f7405c28a02cf5bab4ea4498240bb3579db45951794eb1c843bef0534c093ad908e803000000000000400d03000000000064000000000000008b037d670000000002002000000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000'
43+
);
2144
});
22-
txBuilder.sequenceNumber(14);
23-
txBuilder.expirationTime(1736246155);
24-
const tx = await txBuilder.build();
25-
should.equal(tx.type, TransactionType.Send);
26-
tx.inputs.length.should.equal(1);
27-
tx.inputs[0].should.deepEqual({
28-
address: testData.sender2.address,
29-
value: testData.recipients[0].amount,
30-
coin: 'tapt',
31-
});
32-
tx.outputs.length.should.equal(1);
33-
tx.outputs[0].should.deepEqual({
34-
address: testData.recipients[0].address,
35-
value: testData.recipients[0].amount,
36-
coin: 'tapt',
45+
46+
it('should build and send a signed tx', async function () {
47+
const txHex = testData.TRANSFER;
48+
const transaction = new TransferTransaction(coins.get('tapt'));
49+
transaction.fromRawTransaction(txHex);
50+
51+
const txBuilder = factory.getTransferBuilder(transaction);
52+
const tx = await txBuilder.build();
53+
should.equal(tx.type, TransactionType.Send);
54+
tx.inputs.length.should.equal(1);
55+
tx.inputs[0].should.deepEqual({
56+
address: testData.sender2.address,
57+
value: testData.recipients[0].amount,
58+
coin: 'tapt',
59+
});
60+
tx.outputs.length.should.equal(1);
61+
tx.outputs[0].should.deepEqual({
62+
address: testData.recipients[0].address,
63+
value: testData.recipients[0].amount,
64+
coin: 'tapt',
65+
});
66+
const rawTx = tx.toBroadcastFormat();
67+
should.equal(utils.isValidRawTransaction(rawTx), true);
68+
should.equal(rawTx, testData.TRANSFER);
3769
});
38-
const rawTx = tx.toBroadcastFormat();
39-
should.equal(utils.isValidRawTransaction(rawTx), true);
40-
rawTx.should.equal(
41-
'0xc8f02d25aa698b3e9fbd8a08e8da4c8ee261832a25a4cde8731b5ec356537d090e0000000000000002000000000000000000000000000000000000000000000000000000000000000104636f696e087472616e73666572010700000000000000000000000000000000000000000000000000000000000000010a6170746f735f636f696e094170746f73436f696e000220f7405c28a02cf5bab4ea4498240bb3579db45951794eb1c843bef0534c093ad908e803000000000000400d03000000000064000000000000008b037d670000000002002000000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000'
42-
);
4370
});
4471

45-
it('should build and send a signed tx', async function () {
46-
const txHex = testData.TRANSFER;
47-
const transaction = new TransferTransaction(coins.get('tapt'));
48-
transaction.fromRawTransaction(txHex);
49-
// const explainedTx = transaction.explainTransaction(); // can be used for verification
50-
const txBuilder = factory.getTransferBuilder(transaction);
51-
const tx = await txBuilder.build();
52-
should.equal(tx.type, TransactionType.Send);
53-
tx.inputs.length.should.equal(1);
54-
tx.inputs[0].should.deepEqual({
55-
address: testData.sender2.address,
56-
value: testData.recipients[0].amount,
57-
coin: 'tapt',
72+
describe('Fail', () => {
73+
it('should fail for invalid sender', async function () {
74+
const transaction = new TransferTransaction(coins.get('tapt'));
75+
const builder = factory.getTransferBuilder(transaction);
76+
should(() => builder.sender('randomString')).throwError('Invalid address randomString');
5877
});
59-
tx.outputs.length.should.equal(1);
60-
tx.outputs[0].should.deepEqual({
61-
address: testData.recipients[0].address,
62-
value: testData.recipients[0].amount,
63-
coin: 'tapt',
78+
79+
it('should fail for invalid recipient', async function () {
80+
const builder = factory.getTransferBuilder();
81+
should(() => builder.recipient(testData.invalidRecipients[0])).throwError('Invalid address randomString');
82+
should(() => builder.recipient(testData.invalidRecipients[1])).throwError('Value cannot be less than zero');
83+
should(() => builder.recipient(testData.invalidRecipients[2])).throwError('Invalid amount format');
6484
});
65-
const rawTx = tx.toBroadcastFormat();
66-
should.equal(utils.isValidRawTransaction(rawTx), true);
67-
should.equal(rawTx, testData.TRANSFER);
6885
});
69-
// Invalid or Failing Test Case can be added if needed.
7086
});

0 commit comments

Comments
 (0)