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
74 changes: 74 additions & 0 deletions packages/chain-adapters/src/tron/TronChainAdapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,80 @@ export class ChainAdapter implements IChainAdapter<KnownChainIds.TronMainnet> {
}
}

async buildCustomApiTx(input: {
from: string
to: string
accountNumber: number
data: string
value: string
method?: string
args?: { type: string; value: unknown }[]
}): Promise<TronSignTx> {
try {
const { from, to, accountNumber, data, value } = input

// Always use raw data field instead of method/args to ensure correct method selector
// TronWeb's triggerSmartContract computes method selectors differently than expected
const callData = data.startsWith('0x') ? data.slice(2) : data
let txData: TronUnsignedTx

const requestBody = {
owner_address: from,
contract_address: to,
data: callData,
fee_limit: 100_000_000,
call_value: Number(value) || 0,
visible: true,
}

const response = await this.requestQueue.add(() =>
fetch(`${this.rpcUrl}/wallet/triggersmartcontract`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(requestBody),
}),
)

const result = await response.json()

if (result.Error || !result.transaction) {
throw new Error(`TronGrid API error: ${result.Error || 'No transaction returned'}`)
}

txData = result.transaction

if (!txData.raw_data_hex) {
throw new Error('Failed to create transaction')
}

const rawDataHexValue = txData.raw_data_hex
const rawDataHex =
typeof rawDataHexValue === 'string'
? rawDataHexValue
: Buffer.isBuffer(rawDataHexValue)
? (rawDataHexValue as Buffer).toString('hex')
: Array.isArray(rawDataHexValue)
? Buffer.from(rawDataHexValue).toString('hex')
: (() => {
throw new Error(`Unexpected raw_data_hex type: ${typeof rawDataHexValue}`)
})()

if (!/^[0-9a-fA-F]+$/.test(rawDataHex)) {
throw new Error(`Invalid raw_data_hex format: ${rawDataHex.slice(0, 100)}`)
}

return {
addressNList: toAddressNList(this.getBip44Params({ accountNumber })),
rawDataHex,
transaction: txData,
}
} catch (err) {
return ErrorHandler(err, {
translation: 'chainAdapters.errors.buildTransaction',
})
}
}

async buildSendTransaction(input: BuildSendTxInput<KnownChainIds.TronMainnet>): Promise<{
txToSign: TronSignTx
}> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ export const getTradeQuote = async (
)
}

// TODO: Debug why same-chain Tron swaps revert (swapAndCall method works on EVM but 0 successful on Tron)

// Yes, this is supposed to be supported as per checks above, but currently, Butter doesn't yield any quotes for BTC sells
if (sellAsset.assetId === btcAssetId) {
return Err(
Expand Down Expand Up @@ -259,13 +261,15 @@ export const getTradeQuote = async (
buyAsset,
sellAsset,
accountNumber,
allowanceContract: route.contract ?? '0x0',
allowanceContract: sellAsset.chainId === tronChainId ? buildTx.to : route.contract ?? '0x0',
estimatedExecutionTimeMs: route.timeEstimated * 1000,
butterSwapTransactionMetadata: {
to: buildTx.to,
data: buildTx.data,
value: buildTx.value,
gasLimit: bnOrZero(route.gasEstimatedTarget).toFixed(),
method: buildTx.method,
args: buildTx.args,
},
...(solanaTransactionMetadata && {
solanaTransactionMetadata,
Expand Down
29 changes: 24 additions & 5 deletions packages/swapper/src/tron-utils/getUnsignedTronTransaction.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { tronAssetId } from '@shapeshiftoss/caip'
import { contractAddressOrUndefined } from '@shapeshiftoss/utils'

import type { GetUnsignedTronTransactionArgs } from '../types'
Expand All @@ -23,15 +24,33 @@ export const getUnsignedTronTransaction = ({

const adapter = assertGetTronChainAdapter(sellAsset.chainId)

const to =
relayTransactionMetadata?.to ??
nearIntentsSpecific?.depositAddress ??
butterSwapTransactionMetadata?.to
if (butterSwapTransactionMetadata) {
const { to, data, method, args, value: butterValue } = butterSwapTransactionMetadata

if (!to) throw new Error('Missing Butter swap contract address')
if (!data) throw new Error('Missing Butter swap transaction data')

// Use Butter's value field which includes swap fees for same-chain swaps
// For native TRX sells, also include the sell amount
const isNativeTron = sellAsset.assetId === tronAssetId
const value = isNativeTron ? step.sellAmountIncludingProtocolFeesCryptoBaseUnit : butterValue

return adapter.buildCustomApiTx({
from,
to,
accountNumber,
data,
value,
method,
args,
})
}

const to = relayTransactionMetadata?.to ?? nearIntentsSpecific?.depositAddress
if (!to) throw new Error('Missing transaction destination address')

const value = step.sellAmountIncludingProtocolFeesCryptoBaseUnit

// Extract contract address for TRC20 tokens
const contractAddress = contractAddressOrUndefined(sellAsset.assetId)

return adapter.buildSendApiTransaction({
Expand Down
2 changes: 2 additions & 0 deletions packages/swapper/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -434,6 +434,8 @@ export type TradeQuoteStep = {
data: string
value: Hex
gasLimit: string
method?: string
args?: { type: string; value: unknown }[]
}
sunioTransactionMetadata?: {
route: {
Expand Down