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

fix(@fireblocks/recovery-utility): 🐛 fix trc20 withdrawals #127

Merged
merged 2 commits into from
Feb 12, 2025
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
14 changes: 11 additions & 3 deletions apps/recovery-relay/lib/wallets/TRC20/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,11 +62,19 @@ export class TRC20 extends BaseTron implements ConnectedWallet {
const balance = ((await this.getBalance()) / 10 ** this.decimals) as number;
const trxBalance = await this.getTrxBalance();

const blockData = await this.tronWeb!.fullNode.request('wallet/getblock', { detail: false }, 'post');
const metadata = {
ref_block_bytes: blockData.block_header.raw_data.number.toString(16).slice(-4).padStart(4, '0'),
ref_block_hash: blockData.blockID.slice(16, 32),
expiration: blockData.block_header.raw_data.timestamp + 600 * 1000,
timestamp: blockData.block_header.raw_data.timestamp,
};

const extraParams = new Map<string, any>();

extraParams.set('t', this.tokenAddress);
extraParams.set('d', this.decimals);
extraParams.set('r', this.rpcURL);
extraParams.set(this.KEY_TOKEN_ADDRESS, this.tokenAddress);
extraParams.set(this.KEY_DECIMALS, this.decimals);
extraParams.set(this.KEY_METADATA, metadata);

const feeRate = (await this.estimateGas()) ?? 40_000_000;

Expand Down
64 changes: 38 additions & 26 deletions apps/recovery-utility/renderer/lib/wallets/TRC20/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,37 +9,49 @@ import { promisify } from 'util';
export class TRC20 extends BaseTron implements SigningWallet {
public async generateTx({ to, amount, feeRate, extraParams }: GenerateTxInput): Promise<TxPayload> {
try {
const rpcUrl = extraParams?.get('r');
const tronWeb = require('tronweb');
const metadata = extraParams?.get(this.KEY_METADATA);
metadata.fee_limit = feeRate;
const decimals = extraParams?.get(this.KEY_DECIMALS);
const tokenAddress = extraParams?.get(this.KEY_TOKEN_ADDRESS);
const fixedAmount = amount * 10 ** decimals;

// eslint-disable-next-line global-require
const TronWeb = require('tronweb');
const { HttpProvider } = TronWeb.providers;
const fullNode = new HttpProvider(rpcUrl);
const solidityNode = new HttpProvider(rpcUrl);
const eventServer = new HttpProvider(rpcUrl);
const tronWeb = new TronWeb(fullNode, solidityNode, eventServer, this.privateKey?.replace('0x', ''));
//serialized data - functionSelector(transfer) + toAddress + amount
const data = `a9059cbb${tronWeb.address.toHex(to).replace('/^(41)/', '0x').padStart(64, '0')}${fixedAmount
.toString(16)
.padStart(64, '0')}`;
this.relayLogger.debug('TRC20: Data:', data);
const tx = {
visible: false,
txID: '',
raw_data_hex: '',
raw_data: {
contract: [
{
parameter: {
value: {
data,
owner_address: tronWeb.address.toHex(this.address),
contract_address: tronWeb.address.toHex(tokenAddress),
},
type_url: 'type.googleapis.com/protocol.TriggerSmartContract',
},
type: 'TriggerSmartContract',
},
],
...metadata,
},
};

const decimals = extraParams?.get('d');
const tokenAddress = extraParams?.get('t');
const pb = tronWeb.utils.transaction.txJsonToPb(tx);

const functionSelector = 'transfer(address,uint256)';
const parameter = [
{ type: 'address', value: to },
{ type: 'uint256', value: amount * 10 ** decimals },
];
this.utilityLogger.logSigningTx('Tron', tx);

const tx = await tronWeb.transactionBuilder.triggerSmartContract(
tokenAddress,
functionSelector,
{ feeLimit: feeRate },
parameter,
);
tx.txID = tronWeb.utils.transaction.txPbToTxID(pb).replace(/^0x/, '');
tx.raw_data_hex = tronWeb.utils.transaction.txPbToRawDataHex(pb).toLowerCase();
const signedTx = tronWeb.utils.crypto.signTransaction(Buffer.from(this.privateKey!.replace('0x', ''), 'hex'), tx);

const signedTx = await tronWeb.trx.sign(tx.transaction);

this.utilityLogger.logSigningTx('TRC20', signedTx);

//encode and compress for qr code
//encode and compress to fit qr code limits
const gzip = promisify(zlib.gzip);
const compressedTx = (await gzip(JSON.stringify(signedTx))).toString('base64');

Expand Down
4 changes: 4 additions & 0 deletions packages/wallet-derivation/wallets/chains/Tron.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,8 @@ export class Tron extends ECDSAWallet {
protected readonly KEY_TX = 't';

protected readonly KEY_METADATA = 'm';

protected readonly KEY_TOKEN_ADDRESS = 't';

protected readonly KEY_DECIMALS = 'd';
}