Skip to content

Commit 9fa37a3

Browse files
authored
Merge pull request #7415 from BitGo/WIN-7579-2
chore(sdk-coin-polyx): use token instead of coin in tx decoding
2 parents fd3af3c + c36c663 commit 9fa37a3

File tree

3 files changed

+33
-6
lines changed

3 files changed

+33
-6
lines changed

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

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { Transaction as SubstrateTransaction, utils, KeyPair } from '@bitgo/abst
22
import { InvalidTransactionError, TransactionType } from '@bitgo/sdk-core';
33
import { construct, decode } from '@substrate/txwrapper-polkadot';
44
import { decodeAddress } from '@polkadot/keyring';
5+
import { coins, PolyxCoin } from '@bitgo/statics';
56
import {
67
DecodedTx,
78
RegisterDidWithCDDArgs,
@@ -24,6 +25,28 @@ export class Transaction extends SubstrateTransaction {
2425
return polyxUtils.getAddressFormat(this._coinConfig.name);
2526
}
2627

28+
/**
29+
* Helper method to find a token name by assetId
30+
* @param assetId The assetId to search for
31+
* @returns The token name if found
32+
* @throws {InvalidTransactionError} If no matching token is found
33+
*/
34+
private getTokenNameByAssetId(assetId: string): string {
35+
// Search through all coins to find the one with matching assetId
36+
let foundToken: string | undefined;
37+
coins.forEach((coin) => {
38+
if (coin instanceof PolyxCoin && coin.assetId === assetId) {
39+
foundToken = coin.name;
40+
}
41+
});
42+
43+
if (!foundToken) {
44+
throw new InvalidTransactionError(`No token found for assetId: ${assetId}`);
45+
}
46+
47+
return foundToken;
48+
}
49+
2750
/** @inheritdoc */
2851
toJson(): TxData {
2952
if (!this._substrateTransaction) {
@@ -128,19 +151,21 @@ export class Transaction extends SubstrateTransaction {
128151
}
129152

130153
private decodeInputsAndOutputsForPreApproveAsset(decodedTx: DecodedTx) {
154+
const txMethod = decodedTx.method.args as PreApproveAssetArgs;
131155
const sender = decodedTx.address;
132156
const value = '0'; // Pre-approval does not transfer any value
157+
const tokenName = this.getTokenNameByAssetId(txMethod.assetId);
133158

134159
this._inputs.push({
135160
address: sender,
136161
value,
137-
coin: this._coinConfig.name,
162+
coin: tokenName,
138163
});
139164

140165
this._outputs.push({
141166
address: sender, // In pre-approval, the output is the same as the input
142167
value,
143-
coin: this._coinConfig.name,
168+
coin: tokenName,
144169
});
145170
}
146171

@@ -149,17 +174,19 @@ export class Transaction extends SubstrateTransaction {
149174
const fromDID = txMethod.legs[0].fungible.sender.did;
150175
const toDID = txMethod.legs[0].fungible.receiver.did;
151176
const amount = txMethod.legs[0].fungible.amount.toString();
177+
const assetId = txMethod.legs[0].fungible.assetId;
178+
const tokenName = this.getTokenNameByAssetId(assetId);
152179

153180
this._inputs.push({
154181
address: fromDID,
155182
value: amount,
156-
coin: this._coinConfig.name,
183+
coin: tokenName,
157184
});
158185

159186
this._outputs.push({
160187
address: toDID,
161188
value: amount,
162-
coin: this._coinConfig.name,
189+
coin: tokenName,
163190
});
164191
}
165192

modules/sdk-coin-polyx/test/unit/transactionBuilder/preApproveAssetBuilder.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ describe('Polyx Pre Approve Asset Builder - Testnet', () => {
1010
let builder: PreApproveAssetBuilder;
1111

1212
const sender = accounts.rbitgoTokenOwner;
13-
const assetId = '0x2ffe769d862a89948e1ccf1423bfc7f8';
13+
const assetId = '0x780602887b358cf48989d0d9aa6c8d28';
1414

1515
beforeEach(() => {
1616
const config = buildTestConfig();

modules/sdk-coin-polyx/test/unit/transactionBuilder/tokenTransferBuilder.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ describe('Polyx token transfer Builder - Testnet', () => {
1212
const sender = accounts.rbitgoTokenOwner;
1313
const senderDID = '0x28e8649fec23dd688090b9b5bb950fd34bf20a014cf05542e3ad0264915ee775';
1414
const receiverDID = '0x9202856204a721d2f5e8b85408067d54f1ca84390bf4f558b5615a5a6d3bddb8';
15-
const assetId = '0x2ffe769d862a89948e1ccf1423bfc7f8';
15+
const assetId = '0x780602887b358cf48989d0d9aa6c8d28';
1616

1717
beforeEach(() => {
1818
const config = buildTestConfig();

0 commit comments

Comments
 (0)