Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 31 additions & 4 deletions modules/sdk-coin-polyx/src/lib/transaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { Transaction as SubstrateTransaction, utils, KeyPair } from '@bitgo/abst
import { InvalidTransactionError, TransactionType } from '@bitgo/sdk-core';
import { construct, decode } from '@substrate/txwrapper-polkadot';
import { decodeAddress } from '@polkadot/keyring';
import { coins, PolyxCoin } from '@bitgo/statics';
import {
DecodedTx,
RegisterDidWithCDDArgs,
Expand All @@ -24,6 +25,28 @@ export class Transaction extends SubstrateTransaction {
return polyxUtils.getAddressFormat(this._coinConfig.name);
}

/**
* Helper method to find a token name by assetId
* @param assetId The assetId to search for
* @returns The token name if found
* @throws {InvalidTransactionError} If no matching token is found
*/
private getTokenNameByAssetId(assetId: string): string {
// Search through all coins to find the one with matching assetId
let foundToken: string | undefined;
coins.forEach((coin) => {
if (coin instanceof PolyxCoin && coin.assetId === assetId) {
foundToken = coin.name;
}
});

if (!foundToken) {
throw new InvalidTransactionError(`No token found for assetId: ${assetId}`);
}

return foundToken;
}

/** @inheritdoc */
toJson(): TxData {
if (!this._substrateTransaction) {
Expand Down Expand Up @@ -128,19 +151,21 @@ export class Transaction extends SubstrateTransaction {
}

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

this._inputs.push({
address: sender,
value,
coin: this._coinConfig.name,
coin: tokenName,
});

this._outputs.push({
address: sender, // In pre-approval, the output is the same as the input
value,
coin: this._coinConfig.name,
coin: tokenName,
});
}

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

this._inputs.push({
address: fromDID,
value: amount,
coin: this._coinConfig.name,
coin: tokenName,
});

this._outputs.push({
address: toDID,
value: amount,
coin: this._coinConfig.name,
coin: tokenName,
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ describe('Polyx Pre Approve Asset Builder - Testnet', () => {
let builder: PreApproveAssetBuilder;

const sender = accounts.rbitgoTokenOwner;
const assetId = '0x2ffe769d862a89948e1ccf1423bfc7f8';
const assetId = '0x780602887b358cf48989d0d9aa6c8d28';

beforeEach(() => {
const config = buildTestConfig();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ describe('Polyx token transfer Builder - Testnet', () => {
const sender = accounts.rbitgoTokenOwner;
const senderDID = '0x28e8649fec23dd688090b9b5bb950fd34bf20a014cf05542e3ad0264915ee775';
const receiverDID = '0x9202856204a721d2f5e8b85408067d54f1ca84390bf4f558b5615a5a6d3bddb8';
const assetId = '0x2ffe769d862a89948e1ccf1423bfc7f8';
const assetId = '0x780602887b358cf48989d0d9aa6c8d28';

beforeEach(() => {
const config = buildTestConfig();
Expand Down