|
| 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 | +}; |
0 commit comments