Skip to content

Commit 569b4cc

Browse files
authored
Merge pull request #63 from router-protocol/tron-wc-tx
Tron wc tx
2 parents 8360823 + 209b345 commit 569b4cc

File tree

12 files changed

+1190
-55
lines changed

12 files changed

+1190
-55
lines changed

example-next/src/components/ConnectedAccounts.tsx

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,17 @@
1+
import { buildNitroTransaction } from '@/txArgs/buildNitroTx';
2+
import { pfData } from '@/txArgs/getPfData';
3+
import { PathfinderTransaction } from '@/txArgs/types';
14
import {
25
ConnectedAccount,
36
SOL_ADDRESS,
7+
tronMainnet,
48
useAccounts,
59
useChain,
610
useConnect,
11+
useConnectionOrConfig,
12+
useCurrentWallet,
713
useDisconnect,
14+
useSendTransaction,
815
useTokenForAccount,
916
useWallet,
1017
} from '@tangled3/react';
@@ -52,7 +59,20 @@ const ConnectedAccountItem = ({ account }: { account: ConnectedAccount }) => {
5259
const { connect } = useConnect();
5360
const wallet = useWallet(account.chainType, account.wallet);
5461
const chain = useChain(account.chainId);
62+
const connectionOrConfig = useConnectionOrConfig();
5563

64+
const { mutateAsync: sendTransaction } = useSendTransaction();
65+
const currentWallet = useCurrentWallet();
66+
const walletInstance = useWallet(currentWallet?.type, currentWallet?.id);
67+
68+
const handleSendTx = async () => {
69+
const nitroTx = await buildNitroTransaction(pfData as unknown as PathfinderTransaction, tronMainnet);
70+
71+
console.log('tx data ', nitroTx, account);
72+
73+
const tx = await sendTransaction(nitroTx);
74+
console.log('[tron] tx = ', tx);
75+
};
5676
return (
5777
<tr className='border-b border-gray-700'>
5878
<td className='min-w-8 w-[5ch] h-8 px-4 py-2'>
@@ -85,6 +105,12 @@ const ConnectedAccountItem = ({ account }: { account: ConnectedAccount }) => {
85105
>
86106
Switch
87107
</button>
108+
<button
109+
onClick={handleSendTx}
110+
className='bg-emerald-500 hover:bg-emerald-600 text-white px-4 py-2 rounded'
111+
>
112+
Send Txn
113+
</button>
88114
</td>
89115
</tr>
90116
);

example-next/src/components/Providers.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ const Providers = ({ children }: { children: ReactNode }) => {
2222
},
2323
},
2424
},
25-
testnet: true,
25+
testnet: false,
2626
projectId: '41980758771052df3f01be0a46f172a5',
2727
bitcoinNetwork: 'mainnet',
2828
}}
Lines changed: 179 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,179 @@
1+
import { type ChainData, type SendTransactionMutationParams, type TransactionArgs } from '@tangled3/react';
2+
import type { TransactionData } from './types';
3+
import { type PathfinderTransaction, type TxnDataBitcoin } from './types';
4+
5+
export const buildNitroTransaction = async (
6+
pfTransaction: PathfinderTransaction,
7+
sourceChain: ChainData,
8+
): Promise<SendTransactionMutationParams> => {
9+
const transactionData = pfTransaction.txn as TransactionData;
10+
if (sourceChain.type === 'evm') {
11+
return {
12+
chain: sourceChain,
13+
value: BigInt(transactionData.value as string),
14+
to: transactionData.to,
15+
from: transactionData.from,
16+
args: { calldata: transactionData.data as string },
17+
overrides: undefined,
18+
};
19+
}
20+
if (sourceChain.type === 'tron') {
21+
if (!transactionData.raw_data_hex) {
22+
throw new Error('Tron transaction has no raw data hex');
23+
}
24+
const args: TransactionArgs<'tron'> = {
25+
calldata: JSON.stringify(transactionData),
26+
};
27+
return {
28+
chain: sourceChain,
29+
value: BigInt((transactionData.value as string) ?? '0'),
30+
to: 'TTZLTNUMMJ1AxEXa2oQBQzarNHccqYB3Ye',
31+
from: 'TABd1oGDnR5SntsXxiKZYGfYQ1kNfyLUHx',
32+
args,
33+
overrides: undefined,
34+
};
35+
}
36+
37+
// if (sourceChain.type === "cosmos") {
38+
// if (sourceChain.id === "router_9600-1") {
39+
// if (connector.type === "cosmos") {
40+
// return {
41+
// chain: sourceChain,
42+
// to: transactionData.to,
43+
// from: transactionData.from,
44+
// value: 0n,
45+
// args: {
46+
// messages: [],
47+
// routerChainArgs: {
48+
// executeMsg: transactionData.data as TxnDataRouter,
49+
// funds: isCosmosCoinArray(transactionData.value ?? []) ? transactionData.value : [transactionData.value],
50+
// },
51+
// } as TransactionArgs<"cosmos">,
52+
// overrides: {
53+
// walletType: "keplr",
54+
// },
55+
// };
56+
// } else {
57+
// return {
58+
// chain: routerEvm,
59+
// to: transactionData.to,
60+
// from: transactionData.from,
61+
// value: 0n,
62+
// args: {
63+
// calldata: "",
64+
// routerChainArgs: {
65+
// executeMsg: transactionData.data as TxnDataRouter,
66+
// funds: isCosmosCoinArray(transactionData.value ?? []) ? transactionData.value : [transactionData.value],
67+
// },
68+
// } as TransactionArgs<"evm">,
69+
// overrides: {
70+
// walletType: "evm",
71+
// },
72+
// };
73+
// }
74+
// }
75+
76+
// let messages:
77+
// | {
78+
// typeUrl: string;
79+
// value: Record<string, unknown>;
80+
// }
81+
// | undefined;
82+
83+
// if (sourceChain.id === "osmosis-1") {
84+
// messages = {
85+
// typeUrl: "/cosmwasm.wasm.v1.MsgExecuteContract",
86+
// value: {
87+
// sender: transactionData.from,
88+
// contract: transactionData.to,
89+
// msg: toUtf8(JSON.stringify(transactionData.data)),
90+
// funds: isCosmosCoinArray(transactionData.value) ? [...transactionData.value] : [],
91+
// },
92+
// };
93+
// } else if (sourceChain.id === "self-1") {
94+
// if (!isCosmosCoinArray(transactionData.value ?? [])) {
95+
// throw new Error("Expected CosmosCoin array for value in self-1 chain");
96+
// }
97+
98+
// if (!isTxnDataSelf(transactionData.data)) {
99+
// throw new Error("Expected TxnDataSelf for data in self-1 chain");
100+
// }
101+
102+
// const txnValue = transactionData.value as CosmosCoin[];
103+
// if (!txnValue[0]) {
104+
// throw new Error("Expected at least one coin in value array");
105+
// }
106+
107+
// const coin: CosmosCoin = {
108+
// denom: txnValue[0].denom,
109+
// amount: txnValue[0].amount,
110+
// };
111+
112+
// messages = {
113+
// typeUrl: "/ibc.applications.transfer.v1.MsgTransfer",
114+
// value: {
115+
// sourcePort: transactionData.data.srcPort,
116+
// sourceChannel: transactionData.data.srcChannel,
117+
// token: coin,
118+
// sender: transactionData.from,
119+
// receiver: transactionData.to,
120+
// timeoutTimestamp: (Date.now() + 240 * 1000) * 1_000_000,
121+
// memo: transactionData.data.memo,
122+
// },
123+
// };
124+
// }
125+
126+
// if (messages) {
127+
// const args: TransactionArgs<"cosmos"> = {
128+
// messages: [messages],
129+
// memo: undefined,
130+
// routerChainArgs: undefined,
131+
// };
132+
133+
// const txnValue =
134+
// isCosmosCoinArray(transactionData.value) && transactionData.value.length > 0 && transactionData.value[0]
135+
// ? BigInt(transactionData.value[0].amount)
136+
// : BigInt(transactionData.value as string);
137+
138+
// return {
139+
// chain: sourceChain,
140+
// value: txnValue,
141+
// to: transactionData.to,
142+
// from: transactionData.from,
143+
// args,
144+
// overrides: {},
145+
// };
146+
// }
147+
// }
148+
149+
if (sourceChain.type === 'bitcoin') {
150+
const args: TransactionArgs<'bitcoin'> = {
151+
memo: (transactionData.data as TxnDataBitcoin).params[0].memo,
152+
};
153+
return {
154+
chain: sourceChain,
155+
value: BigInt((transactionData.data as TxnDataBitcoin).params[0].amount.amount),
156+
to: transactionData.to,
157+
from: transactionData.from,
158+
args,
159+
overrides: undefined,
160+
};
161+
}
162+
163+
if (sourceChain.type === 'sui') {
164+
// const byteArray = hexToUint8Array(transactionData.data as string);
165+
// const args: TransactionArgs<"sui"> = {
166+
// tx: SuiTransaction.from(byteArray),
167+
// };
168+
// return {
169+
// chain: sourceChain,
170+
// value: 0n,
171+
// to: pfTransaction.receiverAddress,
172+
// from: transactionData.from,
173+
// args,
174+
// overrides: undefined,
175+
// };
176+
}
177+
178+
throw new Error('Unsupported chain type');
179+
};
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
export const pfData = {
2+
flowType: 'none',
3+
isTransfer: 'false',
4+
isWrappedToken: false,
5+
allowanceTo: '0xA4C516EB09A847E05ED41B8A3F2340B9634BFF08',
6+
bridgeFee: {},
7+
fuelTransfer: null,
8+
fromTokenAddress: '0xeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee',
9+
toTokenAddress: '0xa614f803b6fd780986a42c78ec9c7f77e6ded13c',
10+
source: {
11+
chainId: '728126428',
12+
chainType: 'tron',
13+
asset: {
14+
decimals: 6,
15+
symbol: 'TRX',
16+
name: 'WTRX',
17+
chainId: '728126428',
18+
address: '0x891cdb91d149f23B1a45D9c5Ca78a88d0cB44C18',
19+
resourceID: '',
20+
isMintable: false,
21+
isWrappedAsset: false,
22+
isReserveAsset: false,
23+
tokenInstance: {
24+
decimals: 6,
25+
symbol: 'TRX',
26+
name: 'WTRX',
27+
chainId: 728126428,
28+
address: '0x891cdb91d149f23B1a45D9c5Ca78a88d0cB44C18',
29+
},
30+
},
31+
stableReserveAsset: null,
32+
tokenAmount: '10000',
33+
stableReserveAmount: '2257',
34+
path: ['0xeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee', '0xa614f803b6fd780986a42c78ec9c7f77e6ded13c'],
35+
flags: ['4005'],
36+
priceImpact: '0.00',
37+
tokenPath: 'undefined->USDT',
38+
dataTx: [
39+
'0x9871efa40000000000000000000199fa891cdb91d149f23b1a45d9c5ca78a88d0cb44c18000000000000000000000000000000000000000000000000000000000000271000000000000000000000000000000000000000000000000000000000000008a30000000000000000000000000000000000000000000000000000000000000080000000000000000000000000000000000000000000000000000000000000000100000000000000003b6d0340f36284006d2f5b61cc3c495691917e99ed4e4779',
40+
],
41+
},
42+
destination: {
43+
chainId: '728126428',
44+
asset: {
45+
decimals: 6,
46+
symbol: 'USDT',
47+
name: 'USDT',
48+
chainId: '728126428',
49+
address: '0xa614f803B6FD780986A42c78Ec9c7f77e6DeD13C',
50+
resourceID: 'usdt',
51+
isMintable: false,
52+
isWrappedAsset: false,
53+
isReserveAsset: false,
54+
tokenInstance: {
55+
decimals: 6,
56+
symbol: 'USDT',
57+
name: 'USDT',
58+
chainId: 728126428,
59+
address: '0xa614f803B6FD780986A42c78Ec9c7f77e6DeD13C',
60+
},
61+
},
62+
tokenAmount: '2257',
63+
},
64+
partnerId: '1',
65+
sameChainSwapFee: '0',
66+
slippageTolerance: '2',
67+
estimatedTime: 40,
68+
senderAddress: '0x0259094fde1684b82d2c6b10b65d044c31c0693a',
69+
receiverAddress: '0x0259094fde1684b82d2c6b10b65d044c31c0693a',
70+
txn: {
71+
visible: true,
72+
txID: '5846745bd344a225b55172db92603af2e42c01a9600f2d315ad8676eaf684d91',
73+
raw_data: {
74+
contract: [
75+
{
76+
parameter: {
77+
value: {
78+
data: '183d9c950000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000271000000000000000000000000000000000000000000000000000000000000008a3000000000000000000000000000000000000000000000000000000000000016000000000000000000000000000000000000000000000000000000000000001a000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000259094fde1684b82d2c6b10b65d044c31c0693a00000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002000000000000000000000000eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee000000000000000000000000a614f803b6fd780986a42c78ec9c7f77e6ded13c00000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000fa50000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000c49871efa40000000000000000000199fa891cdb91d149f23b1a45d9c5ca78a88d0cb44c18000000000000000000000000000000000000000000000000000000000000271000000000000000000000000000000000000000000000000000000000000008a30000000000000000000000000000000000000000000000000000000000000080000000000000000000000000000000000000000000000000000000000000000100000000000000003b6d0340f36284006d2f5b61cc3c495691917e99ed4e477900000000000000000000000000000000000000000000000000000000',
79+
owner_address: 'TABd1oGDnR5SntsXxiKZYGfYQ1kNfyLUHx',
80+
contract_address: 'TQzRvYEjGCNjUc816x53MGbCw76uhZWLPs',
81+
call_value: 10000,
82+
},
83+
type_url: 'type.googleapis.com/protocol.TriggerSmartContract',
84+
},
85+
type: 'TriggerSmartContract',
86+
},
87+
],
88+
ref_block_bytes: '86b6',
89+
ref_block_hash: '1f63fc6fc47a0230',
90+
expiration: 1741716780000,
91+
fee_limit: 1000000000,
92+
timestamp: 1741716723257,
93+
},
94+
raw_data_hex:
95+
'0a0286b622081f63fc6fc47a023040e0e7a3b3d8325ad306081f12ce060a31747970652e676f6f676c65617069732e636f6d2f70726f746f636f6c2e54726967676572536d617274436f6e74726163741298060a15410259094fde1684b82d2c6b10b65d044c31c0693a121541a4c516eb09a847e05ed41b8a3f2340b9634bff0818904e22e405183d9c950000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000271000000000000000000000000000000000000000000000000000000000000008a3000000000000000000000000000000000000000000000000000000000000016000000000000000000000000000000000000000000000000000000000000001a000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000259094fde1684b82d2c6b10b65d044c31c0693a00000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002000000000000000000000000eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee000000000000000000000000a614f803b6fd780986a42c78ec9c7f77e6ded13c00000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000fa50000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000c49871efa40000000000000000000199fa891cdb91d149f23b1a45d9c5ca78a88d0cb44c18000000000000000000000000000000000000000000000000000000000000271000000000000000000000000000000000000000000000000000000000000008a30000000000000000000000000000000000000000000000000000000000000080000000000000000000000000000000000000000000000000000000000000000100000000000000003b6d0340f36284006d2f5b61cc3c495691917e99ed4e47790000000000000000000000000000000000000000000000000000000070b9aca0b3d83290018094ebdc03',
96+
},
97+
};

0 commit comments

Comments
 (0)