Skip to content

Commit bff8c96

Browse files
Merge pull request #6428 from BitGo/COIN-4742-add-flush-token-tx-builder
fix(sdk-coin-vet): fix issue in fromRawTransaction for vechain
2 parents 637577a + 3502030 commit bff8c96

File tree

6 files changed

+75
-35
lines changed

6 files changed

+75
-35
lines changed

modules/sdk-coin-vet/src/lib/transaction/addressInitializationTransaction.ts

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
import { TransactionType, InvalidTransactionError } from '@bitgo/sdk-core';
2-
import { getCreateForwarderParamsAndTypes, calculateForwarderV1Address } from '@bitgo/abstract-eth';
3-
import { BaseCoin as CoinConfig } from '@bitgo/statics';
2+
import {
3+
getCreateForwarderParamsAndTypes,
4+
calculateForwarderV1Address,
5+
decodeForwarderCreationData,
6+
getProxyInitcode,
7+
} from '@bitgo/abstract-eth';
8+
import { BaseCoin as CoinConfig, EthereumNetwork } from '@bitgo/statics';
49
import * as ethUtil from 'ethereumjs-util';
510
import EthereumAbi from 'ethereumjs-abi';
611
import { Transaction as VetTransaction, Secp256k1 } from '@vechain/sdk-core';
@@ -62,7 +67,6 @@ export class AddressInitializationTransaction extends Transaction {
6267
/** @inheritdoc */
6368
async build(): Promise<void> {
6469
super.build();
65-
6670
if (this._salt && this._initCode) {
6771
const saltBuffer = ethUtil.setLengthLeft(ethUtil.toBuffer(this._salt), 32);
6872

@@ -134,6 +138,15 @@ export class AddressInitializationTransaction extends Transaction {
134138
// Set data from clauses
135139
this.contract = body.clauses[0]?.to || '0x0';
136140
this.transactionData = body.clauses[0]?.data || '0x0';
141+
this.type = TransactionType.AddressInitialization;
142+
const { baseAddress, addressCreationSalt, feeAddress } = decodeForwarderCreationData(this.transactionData);
143+
144+
this.baseAddress = baseAddress as string;
145+
this.salt = addressCreationSalt as string;
146+
this.feeAddress = feeAddress as string;
147+
const forwarderImplementationAddress = (this._coinConfig.network as EthereumNetwork)
148+
.forwarderImplementationAddress as string;
149+
this.initCode = getProxyInitcode(forwarderImplementationAddress);
137150

138151
// Set sender address
139152
if (signedTx.origin) {

modules/sdk-coin-vet/src/lib/transaction/flushTokenTransaction.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { TransactionType, InvalidTransactionError } from '@bitgo/sdk-core';
22
import { BaseCoin as CoinConfig } from '@bitgo/statics';
33
import { Transaction as VetTransaction, Secp256k1 } from '@vechain/sdk-core';
4+
import { decodeFlushTokensData } from '@bitgo/abstract-eth';
45

56
import { Transaction } from './transaction';
67
import { VetTransactionData } from '../iface';
@@ -84,6 +85,9 @@ export class FlushTokenTransaction extends Transaction {
8485
// Set data from clauses
8586
this.contract = body.clauses[0]?.to || '0x0';
8687
this.transactionData = body.clauses[0]?.data || '0x0';
88+
this.type = TransactionType.FlushTokens;
89+
const { tokenAddress } = decodeFlushTokensData(this.transactionData, this.contract);
90+
this.tokenAddress = tokenAddress;
8791

8892
// Set sender address
8993
if (signedTx.origin) {

modules/sdk-coin-vet/src/lib/transaction/transaction.ts

Lines changed: 18 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -398,37 +398,24 @@ export class Transaction extends BaseTransaction {
398398
}
399399

400400
loadInputsAndOutputs(): void {
401-
switch (this.type) {
402-
case TransactionType.AddressInitialization:
403-
this._type = TransactionType.AddressInitialization;
404-
break;
405-
case TransactionType.FlushTokens:
406-
this._type = TransactionType.FlushTokens;
407-
break;
408-
case TransactionType.Send:
409-
this._type = TransactionType.Send;
410-
const totalAmount = this._recipients.reduce(
411-
(accumulator, current) => accumulator.plus(current.amount),
412-
new BigNumber('0')
413-
);
414-
this._inputs = [
415-
{
416-
address: this.sender,
417-
value: totalAmount.toString(),
418-
coin: this._coinConfig.name,
419-
},
420-
];
421-
this._outputs = this._recipients.map((recipient) => {
422-
return {
423-
address: recipient.address,
424-
value: recipient.amount as string,
425-
coin: this._coinConfig.name,
426-
};
427-
});
428-
break;
429-
default:
430-
throw new InvalidTransactionError(`Unsupported transaction type: ${this.type}`);
431-
}
401+
const totalAmount = this._recipients.reduce(
402+
(accumulator, current) => accumulator.plus(current.amount),
403+
new BigNumber('0')
404+
);
405+
this._inputs = [
406+
{
407+
address: this.sender,
408+
value: totalAmount.toString(),
409+
coin: this._coinConfig.name,
410+
},
411+
];
412+
this._outputs = this._recipients.map((recipient) => {
413+
return {
414+
address: recipient.address,
415+
value: recipient.amount as string,
416+
coin: this._coinConfig.name,
417+
};
418+
});
432419
}
433420

434421
fromRawTransaction(rawTransaction: string): void {

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

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ export const ADDRESS_INITIALIZATION_SIGNABLE_PAYLOAD =
2323

2424
export const FORWARDER_ADDRESS = '0x043fde2fa0ece77c2461a9571f3b1b1c6faca120';
2525

26+
export const FORWARDER_ADDRESS_NEW = '0x604a42898388080d56bc85f815965d1f64beb1a6';
27+
2628
export const FLUSH_TOKEN_TRANSACTION =
2729
'0xf8542788014ead140e77bbc140f83df83b94043fde2fa0ece77c2461a9571f3b1b1c6faca12080a43ef133670000000000000000000000000000000000000000000000000000456e6572677981808252088082faf8c0';
2830

@@ -99,6 +101,12 @@ export const BASE_ADDRESS = '0x55b00b5c807d5696197b48d4affa40bb876df240';
99101

100102
export const FEE_ADDRESS = '0x7c87b9ffc6fd6c167c0e4fa9418720f3d659358e';
101103

102-
export const TOKEN_ADDRESS = '0x0000000000000000000000000000456E65726779';
104+
export const TOKEN_ADDRESS = '0x0000000000000000000000000000456e65726779';
103105

104106
export const SALT = '0x1';
107+
108+
export const SERIALIZED_SIGNED_ADDRESS_INIT_TX =
109+
'0xf8d927880152c906531784bd40f87ef87c9465343e18c376d2fc8c3cf10cd146d63e2e0dc9ef80b86413b2f75c00000000000000000000000055b00b5c807d5696197b48d4affa40bb876df2400000000000000000000000007c87b9ffc6fd6c167c0e4fa9418720f3d659358e00000000000000000000000000000000000000000000000000000000000000048180830249f08082faf8c0b84196577046ecbba640d8f5914a7b4cb99f9ab479359bcda5368cc9afdfb55994a0469af071cdeb4cac92caaf0be7fedf492d2215f83baa00d4abac31cf48eeddd900';
110+
111+
export const SERIALIZED_SIGNED_FLUSH_TOKEN_TX =
112+
'0xf89827880152cf455536ebc740f83df83b94604a42898388080d56bc85f815965d1f64beb1a680a43ef133670000000000000000000000000000000000000000000000000000456e657267798180830249f08082faf9c0b841b5225a0d5c63fd4ceee2fefe94759d20e37c29ebcd15fc03eb699d4af5bb210c258583f9e8e26e872751e255c2f0fe9d44d52ce65b6b76040476e285074a4b5901';

modules/sdk-coin-vet/test/transactionBuilder/addressInitializationBuilder.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,20 @@ describe('Address Initialisation Transaction', () => {
8383
should.equal(toJson.deployedAddress, testData.FORWARDER_ADDRESS);
8484
should.equal(toJson.value, '0');
8585
});
86+
87+
it('should build properly from serialized', async () => {
88+
const txBuilder = factory.from(testData.SERIALIZED_SIGNED_ADDRESS_INIT_TX);
89+
const signedTx = await txBuilder.build();
90+
const signedtxJson = signedTx.toJson();
91+
should.equal(signedtxJson.gas, 150000);
92+
93+
txBuilder.gas(180000);
94+
const tx = await txBuilder.build();
95+
const txJson = tx.toJson();
96+
should.equal(txJson.to, testData.FORWARDER_FACTORY_ADDRESS);
97+
should.equal(txJson.gas, 180000);
98+
console.log('txJson:', txJson);
99+
});
86100
});
87101

88102
describe('Fail', () => {

modules/sdk-coin-vet/test/transactionBuilder/flushTokenTransactionBuilder.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,20 @@ describe('Flush Token Transaction', () => {
7575
should.equal(toJson.tokenAddress, testData.TOKEN_ADDRESS);
7676
should.equal(toJson.value, '0');
7777
});
78+
79+
it('should build properly from serialized', async () => {
80+
const txBuilder = factory.from(testData.SERIALIZED_SIGNED_FLUSH_TOKEN_TX);
81+
const signedTx = await txBuilder.build();
82+
const signedtxJson = signedTx.toJson();
83+
should.equal(signedtxJson.gas, 150000);
84+
85+
txBuilder.gas(180000);
86+
const tx = await txBuilder.build();
87+
const txJson = tx.toJson();
88+
should.equal(txJson.to, testData.FORWARDER_ADDRESS_NEW);
89+
should.equal(txJson.gas, 180000);
90+
console.log('txJson:', txJson);
91+
});
7892
});
7993

8094
describe('Fail', () => {

0 commit comments

Comments
 (0)