Skip to content
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
1 change: 1 addition & 0 deletions .env
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,7 @@ VITE_STARKNET_NODE_URL=https://rpc.starknet.lava.build
VITE_STORY_NODE_URL=https://mainnet.storyrpc.io
VITE_SUI_NODE_URL=https://fullnode.mainnet.sui.io:443
VITE_TON_NODE_URL=https://toncenter.com/api/v2/jsonRPC
VITE_TRON_GRID_API_KEY=17430894-392e-44e8-b015-4a9c4fe17546
Comment thread
coderabbitai[bot] marked this conversation as resolved.
VITE_TRON_NODE_URL=https://api.trongrid.io
VITE_UNICHAIN_NODE_URL=https://mainnet.unichain.org
VITE_WORLDCHAIN_NODE_URL=https://worldchain-mainnet.g.alchemy.com/public
Expand Down
205 changes: 0 additions & 205 deletions packages/chain-adapters/src/tron/TRON_FEE_ESTIMATION_ISSUES.md

This file was deleted.

25 changes: 14 additions & 11 deletions packages/chain-adapters/src/tron/TronChainAdapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ export interface ChainAdapterArgs {
http: unchained.tron.TronApi
}
rpcUrl: string
apiKey?: string
}

export class ChainAdapter implements IChainAdapter<KnownChainIds.TronMainnet> {
Expand All @@ -57,18 +58,24 @@ export class ChainAdapter implements IChainAdapter<KnownChainIds.TronMainnet> {
}

protected readonly rpcUrl: string
private readonly apiKey: string
private requestQueue: PQueue

constructor(args: ChainAdapterArgs) {
this.providers = args.providers
this.rpcUrl = args.rpcUrl
this.apiKey = args.apiKey ?? ''
this.requestQueue = new PQueue({
intervalCap: 1,
interval: 400,
concurrency: 1,
})
}

private get tronGridHeaders(): Record<string, string> {
return this.apiKey ? { 'TRON-PRO-API-KEY': this.apiKey } : {}
}

private assertSupportsChain(wallet: HDWallet): asserts wallet is TronWallet {
if (!supportsTron(wallet)) {
throw new ChainAdapterError(`wallet does not support: ${this.getDisplayName()}`, {
Expand Down Expand Up @@ -192,6 +199,7 @@ export class ChainAdapter implements IChainAdapter<KnownChainIds.TronMainnet> {
// Create TronWeb instance once and reuse
const tronWeb = new TronWeb({
fullHost: this.rpcUrl,
headers: this.tronGridHeaders,
})

let txData: TronUnsignedTx
Expand Down Expand Up @@ -239,7 +247,7 @@ export class ChainAdapter implements IChainAdapter<KnownChainIds.TronMainnet> {
() =>
fetch(`${this.rpcUrl}/wallet/createtransaction`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
headers: { 'Content-Type': 'application/json', ...this.tronGridHeaders },
body: JSON.stringify(requestBody),
}),
{ throwOnTimeout: true },
Expand Down Expand Up @@ -324,7 +332,7 @@ export class ChainAdapter implements IChainAdapter<KnownChainIds.TronMainnet> {
() =>
fetch(`${this.rpcUrl}/wallet/triggersmartcontract`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
headers: { 'Content-Type': 'application/json', ...this.tronGridHeaders },
body: JSON.stringify(requestBody),
}),
{ throwOnTimeout: true },
Expand Down Expand Up @@ -456,22 +464,17 @@ export class ChainAdapter implements IChainAdapter<KnownChainIds.TronMainnet> {
}
}

// TODO: CRITICAL - Fix fee estimation for TRC20 tokens
// Current implementation returns FIXED 0.268 TRX for all transactions
// Reality: TRC20 transfers cost 6-15 TRX (energy + bandwidth + memo)
// This causes UI to show wrong fees and transactions to fail on-chain
// See TRON_FEE_ESTIMATION_ISSUES.md for detailed analysis and fix
async getFeeData(
input: GetFeeDataInput<KnownChainIds.TronMainnet>,
): Promise<FeeDataEstimate<KnownChainIds.TronMainnet>> {
try {
const { to, value, chainSpecific: { from, contractAddress, memo } = {} } = input

// Get live network prices from chain parameters
const tronWeb = new TronWeb({ fullHost: this.rpcUrl })
const tronWeb = new TronWeb({ fullHost: this.rpcUrl, headers: this.tronGridHeaders })
const params = await this.requestQueue.add(() => tronWeb.trx.getChainParameters(), {
throwOnTimeout: true,
})

const bandwidthPrice = params.find(p => p.key === 'getTransactionFee')?.value ?? 1000
const energyPrice = params.find(p => p.key === 'getEnergyFee')?.value ?? 100

Expand Down Expand Up @@ -540,7 +543,7 @@ export class ChainAdapter implements IChainAdapter<KnownChainIds.TronMainnet> {
() =>
fetch(`${this.rpcUrl}/wallet/getaccount`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
headers: { 'Content-Type': 'application/json', ...this.tronGridHeaders },
body: JSON.stringify({
address: to,
visible: true,
Expand Down Expand Up @@ -731,7 +734,7 @@ export class ChainAdapter implements IChainAdapter<KnownChainIds.TronMainnet> {
const TRANSFER_EVENT_SIGNATURE =
'ddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef'
const ZERO_ADDRESS = 'T9yD14Nj9j7xAB4dbGeiX9h8unkKHxuWwb'
const tronWeb = new TronWeb({ fullHost: this.rpcUrl })
const tronWeb = new TronWeb({ fullHost: this.rpcUrl, headers: this.tronGridHeaders })

for (const log of tx.log) {
try {
Expand Down
1 change: 1 addition & 0 deletions packages/public-api/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ export const getServerConfig = (): SwapperConfig => ({
VITE_TENDERLY_API_KEY: env.TENDERLY_API_KEY,
VITE_TENDERLY_ACCOUNT_SLUG: env.TENDERLY_ACCOUNT_SLUG,
VITE_TENDERLY_PROJECT_SLUG: env.TENDERLY_PROJECT_SLUG,
VITE_TRON_GRID_API_KEY: env.TRON_GRID_API_KEY,
VITE_SUI_NODE_URL: env.SUI_NODE_URL,
VITE_ACROSS_API_URL: env.ACROSS_API_URL,
VITE_ACROSS_INTEGRATOR_ID: env.ACROSS_INTEGRATOR_ID,
Expand Down
1 change: 1 addition & 0 deletions packages/public-api/src/env.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ const envSchema = z.object({
TENDERLY_API_KEY: z.string().min(1),
TENDERLY_ACCOUNT_SLUG: z.string().min(1),
TENDERLY_PROJECT_SLUG: z.string().min(1),
TRON_GRID_API_KEY: z.string().default(''),

// Feature flags
FEATURE_THORCHAINSWAP_LONGTAIL: flag,
Expand Down
8 changes: 7 additions & 1 deletion packages/public-api/src/swapperDeps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -248,8 +248,14 @@ const solanaAdapter = new adapters.solana.ChainAdapter({
})

const tronAdapter = new adapters.tron.ChainAdapter({
providers: { http: new unchained.tron.TronApi({ rpcUrl: env.TRON_NODE_URL }) },
providers: {
http: new unchained.tron.TronApi({
rpcUrl: env.TRON_NODE_URL,
apiKey: env.TRON_GRID_API_KEY,
}),
},
rpcUrl: env.TRON_NODE_URL,
apiKey: env.TRON_GRID_API_KEY,
})

const suiAdapter = new adapters.sui.ChainAdapter({ rpcUrl: env.SUI_NODE_URL })
Expand Down
Loading
Loading