diff --git a/packages/networks/tron/package.json b/packages/networks/tron/package.json index ee450c4..ef2ccc1 100644 --- a/packages/networks/tron/package.json +++ b/packages/networks/tron/package.json @@ -1,6 +1,6 @@ { "name": "@multiplechain/tron", - "version": "0.4.5", + "version": "0.4.6", "type": "module", "main": "dist/index.cjs", "module": "dist/index.es.js", diff --git a/packages/networks/tron/src/models/Transaction.ts b/packages/networks/tron/src/models/Transaction.ts index 7c7d96e..bf9171c 100644 --- a/packages/networks/tron/src/models/Transaction.ts +++ b/packages/networks/tron/src/models/Transaction.ts @@ -72,6 +72,27 @@ export interface TransactionData { } } +const selectors = { + // TRC20 + [TransactionTypeEnum.TOKEN]: [ + 'a9059cbb', // transfer(address,uint256) + '095ea7b3', // approve(address,uint256) + '23b872dd' // transferFrom(address,address,uint256) + ], + // TRC721, TRC1155 + [TransactionTypeEnum.NFT]: [ + // TRC721 + '23b872dd', // transferFrom(address,address,uint256) + '095ea7b3', // approve(address,uint256) + '42842e0e', // safeTransferFrom(address,address,uint256) + 'b88d4fde', // safeTransferFrom(address,address,uint256,bytes) + // TRC1155 + 'f242432a', // safeTransferFrom(address,address,uint256,uint256,bytes) + '2eb2c2d6', // safeBatchTransferFrom(address,address,uint256[],uint256[],bytes) + '29535c7e' // setApprovalForAll(address,bool) + ] +} + export class Transaction implements TransactionInterface { /** * Each transaction has its own unique ID defined by the user @@ -160,13 +181,20 @@ export class Transaction implements TransactionInterface { } if (data.raw_data.contract[0].type === 'TriggerSmartContract') { - const tryNft = new NFT(data.raw_data.contract[0].parameter.value.contract_address ?? '') - try { - await tryNft.getApproved(1) - return TransactionTypeEnum.NFT - } catch { - return TransactionTypeEnum.TOKEN + const val = data.raw_data.contract[0].parameter.value + const type = Object.entries(selectors).find(([_key, values]) => { + return values.includes(val.data?.slice(0, 8) ?? '') + }) + if (type !== undefined) { + const tryNft = new NFT(val.contract_address ?? '') + try { + await tryNft.getApproved(1) + return TransactionTypeEnum.NFT + } catch { + return TransactionTypeEnum.TOKEN + } } + return TransactionTypeEnum.CONTRACT } else if (data.raw_data.contract[0].type === 'TransferContract') { return TransactionTypeEnum.COIN } diff --git a/packages/networks/tron/tests/models.spec.ts b/packages/networks/tron/tests/models.spec.ts index ab60208..454fd14 100644 --- a/packages/networks/tron/tests/models.spec.ts +++ b/packages/networks/tron/tests/models.spec.ts @@ -4,7 +4,11 @@ import { Transaction } from '../src/models/Transaction' import { NftTransaction } from '../src/models/NftTransaction' import { CoinTransaction } from '../src/models/CoinTransaction' import { TokenTransaction } from '../src/models/TokenTransaction' -import { AssetDirectionEnum, TransactionStatusEnum } from '@multiplechain/types' +import { + AssetDirectionEnum, + TransactionStatusEnum, + TransactionTypeEnum +} from '@multiplechain/types' const nftId = Number(process.env.TRON_NFT_ID) const tokenAmount = Number(process.env.TRON_TOKEN_AMOUNT) @@ -37,6 +41,10 @@ describe('Transaction', () => { ) }) + it('Type', async () => { + expect(await tx.getType()).toBe(TransactionTypeEnum.COIN) + }) + it('Sender', async () => { expect(await tx.getSigner()).toBe(testSender) }) @@ -73,6 +81,10 @@ describe('Coin Transaction', () => { expect(await tx.getAmount()).toBe(coinAmount) }) + it('Type', async () => { + expect(await tx.getType()).toBe(TransactionTypeEnum.COIN) + }) + it('Verify Transfer', async () => { expect(await tx.verifyTransfer(AssetDirectionEnum.INCOMING, testReceiver, coinAmount)).toBe( TransactionStatusEnum.CONFIRMED @@ -99,6 +111,10 @@ describe('Token Transaction', () => { expect(await tx.getAmount()).toBe(tokenAmount) }) + it('Type', async () => { + expect(await tx.getType()).toBe(TransactionTypeEnum.TOKEN) + }) + it('Verify Transfer', async () => { expect( await tx.verifyTransfer(AssetDirectionEnum.INCOMING, testReceiver, tokenAmount) @@ -133,6 +149,10 @@ describe('NFT Transaction', () => { expect(await tx.getNftId()).toBe(nftId) }) + it('Type', async () => { + expect(await tx.getType()).toBe(TransactionTypeEnum.NFT) + }) + it('Verify Transfer', async () => { expect(await tx.verifyTransfer(AssetDirectionEnum.INCOMING, testReceiver, nftId)).toBe( TransactionStatusEnum.CONFIRMED