|
| 1 | +import { sendTransactions } from "@thirdweb-dev/api"; |
| 2 | +import type { UserWallet } from "../wallets/types.js"; |
| 3 | +import type { TransactionRequest } from "./types.js"; |
| 4 | +import { getThirdwebBaseUrl } from "../../utils/domains.js"; |
| 5 | +import { getClientFetch } from "../../utils/fetch.js"; |
| 6 | + |
| 7 | +/** |
| 8 | + * Sends a series of transactions for execution. Each transaction can be a human-readable contract call, native transfer, or encoded transaction. |
| 9 | + * |
| 10 | + * @param options - Options including the wallet, chain id, and transactions |
| 11 | + * @param options.wallet - The wallet to use for signing transactions |
| 12 | + * @param options.chainId - The chain ID to use for the transactions |
| 13 | + * @param options.transactions - An array of transactions to send |
| 14 | + * @returns The sent transaction IDs |
| 15 | + * @example |
| 16 | + * |
| 17 | + * ## Call a contract |
| 18 | + * ```typescript |
| 19 | + * import { Client, Transactions, Wallets } from "thirdweb/v2"; |
| 20 | + * |
| 21 | + * const userWallet = await Wallets.loginWithOauth({ |
| 22 | + * client: thirdwebClient, |
| 23 | + * provider: "google", |
| 24 | + * }); |
| 25 | + * |
| 26 | + * const transactionIds = await Transactions.send({ |
| 27 | + * wallet: userWallet, |
| 28 | + * chainId: 1, |
| 29 | + * transactions: [ |
| 30 | + * { |
| 31 | + * contractAddress: "0x...", |
| 32 | + * method: "function transfer(address,uint256)", |
| 33 | + * params: ["0x...", 100n], |
| 34 | + * } |
| 35 | + * ], |
| 36 | + * }); |
| 37 | + * ``` |
| 38 | + * |
| 39 | + * ## Send native currency |
| 40 | + * ```typescript |
| 41 | + * import { Client, Transactions, Wallets } from "thirdweb/v2"; |
| 42 | + * |
| 43 | + * const userWallet = await Wallets.loginAsGuest({ |
| 44 | + * client: thirdwebClient, |
| 45 | + * }); |
| 46 | + * |
| 47 | + * const transactionIds = await Transactions.send({ |
| 48 | + * wallet: userWallet, |
| 49 | + * chainId: 1, |
| 50 | + * transactions: [ |
| 51 | + * { |
| 52 | + * to: "0x...", |
| 53 | + * value: 100n, |
| 54 | + * } |
| 55 | + * ], |
| 56 | + * }); |
| 57 | + * ``` |
| 58 | + * |
| 59 | + * ## Send an encoded transaction |
| 60 | + * ```typescript |
| 61 | + * import { Client, Transactions, Wallets } from "thirdweb/v2"; |
| 62 | + * |
| 63 | + * const thirdwebClient = Client.init({ |
| 64 | + * clientId: "YOUR_CLIENT_ID", |
| 65 | + * }); |
| 66 | + * |
| 67 | + * const userWallet = await Wallets.loginWithOauth({ |
| 68 | + * client: thirdwebClient, |
| 69 | + * provider: "github", |
| 70 | + * }); |
| 71 | + * |
| 72 | + * const transactionIds = await Transactions.send({ |
| 73 | + * wallet: userWallet, |
| 74 | + * chainId: 1, |
| 75 | + * transactions: [ |
| 76 | + * { |
| 77 | + * to: "0x...", |
| 78 | + * data: "0x...", |
| 79 | + * } |
| 80 | + * ], |
| 81 | + * }); |
| 82 | + */ |
| 83 | +export async function send(options: send.Options): Promise<Array<string>> { |
| 84 | + const transactions = options.transactions.map((transaction) => { |
| 85 | + if ("contractAddress" in transaction) { |
| 86 | + return { |
| 87 | + contractAddress: transaction.contractAddress, |
| 88 | + method: transaction.method, |
| 89 | + params: transaction.params, |
| 90 | + value: transaction.value?.toString() ?? "0", |
| 91 | + type: "contract-call" as const, |
| 92 | + } as const; |
| 93 | + } else if ("data" in transaction) { |
| 94 | + return { |
| 95 | + to: transaction.to, |
| 96 | + data: transaction.data, |
| 97 | + value: transaction.value?.toString() ?? "0", |
| 98 | + type: "encoded" as const, |
| 99 | + } as const; |
| 100 | + } else { |
| 101 | + return { |
| 102 | + to: transaction.to, |
| 103 | + value: transaction.value?.toString() ?? "0", |
| 104 | + type: "native-transfer" as const, |
| 105 | + } as const; |
| 106 | + } |
| 107 | + }); |
| 108 | + |
| 109 | + const result = await sendTransactions({ |
| 110 | + baseUrl: getThirdwebBaseUrl("api"), |
| 111 | + fetch: getClientFetch(options.wallet.client), |
| 112 | + headers: { |
| 113 | + Authorization: `Bearer ${options.wallet.authToken}`, |
| 114 | + }, |
| 115 | + body: { |
| 116 | + chainId: options.chainId, |
| 117 | + from: options.wallet.address, |
| 118 | + transactions |
| 119 | + } |
| 120 | + }); |
| 121 | + |
| 122 | + if (result.error) { |
| 123 | + throw new Error( |
| 124 | + `Failed to send transactions: ${result.response.status} - ${result.error}`, |
| 125 | + ); |
| 126 | + } |
| 127 | + const transactionIds = result.data?.result?.transactionIds; |
| 128 | + if (!transactionIds) { |
| 129 | + throw new Error("Failed to send transactions: no transaction ids"); |
| 130 | + } |
| 131 | + return transactionIds; |
| 132 | +}; |
| 133 | + |
| 134 | +export namespace send { |
| 135 | + export type Options = { |
| 136 | + wallet: UserWallet; |
| 137 | + chainId: number; |
| 138 | + transactions: Array<TransactionRequest>; |
| 139 | + }; |
| 140 | +} |
| 141 | + |
0 commit comments