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 1 commit
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
10 changes: 9 additions & 1 deletion 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('m', metadata);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

key should be in the base class of BaseTron


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

Expand Down
63 changes: 37 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,35 +9,46 @@ 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');

// 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', ''));

const tronWeb = require('tronweb');
const metadata = extraParams?.get('m');
metadata.fee_limit = feeRate;
const decimals = extraParams?.get('d');
const tokenAddress = extraParams?.get('t');

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

const tx = await tronWeb.transactionBuilder.triggerSmartContract(
tokenAddress,
functionSelector,
{ feeLimit: feeRate },
parameter,
);

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

this.utilityLogger.logSigningTx('TRC20', signedTx);
const fixedAmount = amount * 10 ** decimals;
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 pb = tronWeb.utils.transaction.txJsonToPb(tx);

this.utilityLogger.logSigningTx('Tron', tx);

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);

//encode and compress for qr code
const gzip = promisify(zlib.gzip);
Expand Down