Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fixed wrong result posibilty #20

Merged
merged 1 commit into from
Jun 4, 2024
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
2 changes: 1 addition & 1 deletion packages/networks/tron/package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
40 changes: 34 additions & 6 deletions packages/networks/tron/src/models/Transaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,27 @@
}
}

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<TransactionData> {
/**
* Each transaction has its own unique ID defined by the user
Expand Down Expand Up @@ -160,13 +181,20 @@
}

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

Check warning on line 197 in packages/networks/tron/src/models/Transaction.ts

View check run for this annotation

Codecov / codecov/patch

packages/networks/tron/src/models/Transaction.ts#L197

Added line #L197 was not covered by tests
} else if (data.raw_data.contract[0].type === 'TransferContract') {
return TransactionTypeEnum.COIN
}
Expand Down
22 changes: 21 additions & 1 deletion packages/networks/tron/tests/models.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
})
Expand Down Expand Up @@ -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
Expand All @@ -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)
Expand Down Expand Up @@ -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
Expand Down