Skip to content

Commit

Permalink
eth_getTokenSymbol (#873)
Browse files Browse the repository at this point in the history
* fix optional blockNumber property in eth_getTransactionCount

* stash

* Tron getBalance implemented

* some code style fixes

* removed TronWeb from dependencies
reworked processTRC20TokenBalanceDetails
added decodeUInt256 method

* some code style fixes

* rolled back yarn.lock

* fixed issue with empty trc20 token balance and with multiply trc20 tokens

* fixed issue with empty trc20 token balance and with multiply trc20 tokens

* fixed code style

* added getTokenSymbol in eth rpc node helpers
fixed decodeUInt256
added decodeHexString

* fix code style
  • Loading branch information
PhilipMantrov authored Aug 2, 2023
1 parent dd5d8cb commit 0da427f
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 32 deletions.
2 changes: 2 additions & 0 deletions src/dto/rpc/EvmBasedRpcInterface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,8 @@ export interface EvmBasedRpcInterface {

getTokenDecimals(tokenAddress: string): Promise<JsonRpcResponse<BigNumber>>

getTokenSymbol(tokenAddress: string): Promise<JsonRpcResponse<string>>

getContractAddress(txHash: string): Promise<string | null>

// web3_ methods
Expand Down
23 changes: 9 additions & 14 deletions src/service/address/address.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import { CONFIG, Constant, ErrorUtils, ResponseDto, Utils } from '../../util'
import { EvmRpc, GenericRpc, TronRpc } from '../rpc'
import { Network, TatumConfig } from '../tatum'
import { AddressBalance, AddressTransaction, GetAddressTransactionsQuery } from './address.dto'
import { decodeUInt256 } from '../../util/decode'
import { decodeHexString, decodeUInt256 } from '../../util/decode';

@Service({
factory: (data: { id: string }) => {
Expand Down Expand Up @@ -124,17 +124,12 @@ export class Address {
})
}

private async processTRC20TokenBalanceDetails(tokenBalances: {[key: string]: string}) {
const balances = Object.entries(tokenBalances[0])
const serializedTokenBalance: Array<unknown> = []
for (let i = 0; i < balances.length; i++) {
const asset = await Utils.getRpc<TronRpc>(this.id, this.config).triggerConstantContract(
balances[i][0], balances[i][0], 'symbol()', '', { visible: true }
).then(r => decodeUInt256(r.constant_result[0]))
const decimals = await Utils.getRpc<TronRpc>(this.id, this.config).triggerConstantContract(
balances[i][0], balances[i][0], 'decimals()', '', { visible: true }
).then(r => decodeUInt256(r.constant_result[0]))
const balance = balances[i][1]
private async processTRC20TokenBalanceDetails(tokenBalances: [{[key: string]: string}]) {
const serializedTokenBalance = [];
for (let i = 0; i < tokenBalances.length; i++) {
const asset = await Utils.getRpc<TronRpc>(this.id, this.config).triggerConstantContract(Object.keys(tokenBalances[i])[0], Object.keys(tokenBalances[i])[0], 'symbol()', '', { visible: true }).then(r => decodeHexString(r.constant_result[0]))
const decimals = await Utils.getRpc<TronRpc>(this.id, this.config).triggerConstantContract(Object.keys(tokenBalances[i])[0], Object.keys(tokenBalances[i])[0], 'decimals()', '', { visible: true }).then(r => decodeUInt256(r.constant_result[0]))
const balance = Object.values(tokenBalances[i])[0];
serializedTokenBalance.push({
asset,
decimals,
Expand Down Expand Up @@ -271,7 +266,7 @@ export class Address {
key: string,
value: number,
}]
trc20: {[key: string]: string}
trc20: [{[key: string]: string}]
freeNetLimit: number,
bandwidth: number,
}>({
Expand All @@ -281,7 +276,7 @@ export class Address {
{
return Object.create({
nativeBalance: r.balance.toString(),
tokenBalances: await this.processTRC20TokenBalanceDetails(r.trc20),
tokenBalances: r.trc20.length > 0 ? await this.processTRC20TokenBalanceDetails(r.trc20) : [],
})
})
}
Expand Down
12 changes: 12 additions & 0 deletions src/service/rpc/evm/AbstractEvmRpc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
TraceType,
TxPayload,
} from '../../../dto'
import { decodeHexString } from '../../../util/decode';

@Service()
export abstract class AbstractEvmRpc implements EvmBasedRpcInterface {
Expand Down Expand Up @@ -150,6 +151,17 @@ export abstract class AbstractEvmRpc implements EvmBasedRpcInterface {
return response
}

async getTokenSymbol(tokenAddress: string): Promise<JsonRpcResponse<string>> {
const response = await this.rpcCall<JsonRpcResponse<string>>('eth_call', [
{ to: tokenAddress, data: '0x95d89b41' },
'latest',
])
if (response.result) {
response.result = decodeHexString(response.result)
}
return response
}

async getContractAddress(txHash: string): Promise<string | null> {
try {
const txReceipt = await this.getTransactionReceipt(txHash)
Expand Down
32 changes: 14 additions & 18 deletions src/util/decode.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,21 @@
export async function decodeUInt256(uint256Hex: string): Promise<string | number> {
const hexString = uint256Hex.replace(/^0+/, '') // Remove leading zeros
const byteLength = hexString.length / 2
export function decodeUInt256(hex: string): number {
const formattedHex = hex.replace(/^0x/, '') // Remove 0x
return Number('0x' + formattedHex)
}

export function decodeHexString(hex: string): string {
const formattedHex = hex.replace(/^(0x)?0+/, '') // Remove 0x and leading zeros
const byteLength = formattedHex.length / 2
const bytes = []

for (let i = 0; i < byteLength; i++) {
const byte = parseInt(hexString.substr(i * 2, 2), 16) // Get the current byte
const byte = parseInt(formattedHex.substr(i * 2, 2), 16) // Get the current byte
bytes.push(byte)
}

const firstByte = BigInt(bytes[0]) // Convert the first byte to a BigInt
let decodedData

if (firstByte < BigInt(10)) {
// If the first byte is in the range of ASCII digits (0-9), decode as number
decodedData = Number('0x' + uint256Hex)
} else {
decodedData = bytes
.map(byte => String.fromCharCode(byte))
.filter(char => /[a-zA-Z0-9]/.test(char))
.join('')
}

return decodedData
return bytes
.map(byte => String.fromCharCode(byte))
.filter(char => /[a-zA-Z0-9]/.test(char))
.join('')
}

0 comments on commit 0da427f

Please sign in to comment.