UNPKG

@reservoir0x/relay-sdk

Version:

Relay is the Fastest and Cheapest Way to Bridge and Transact Across Chains.

186 lines 7.78 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.adaptViemWallet = exports.isViemWalletClient = void 0; const logger_js_1 = require("./logger.js"); const client_js_1 = require("../client.js"); const viem_1 = require("viem"); const experimental_1 = require("viem/experimental"); function isViemWalletClient(wallet) { return (wallet.extend !== undefined && wallet.getPermissions !== undefined); } exports.isViemWalletClient = isViemWalletClient; const adaptViemWallet = (wallet) => { return { vmType: 'evm', getChainId: async () => { return wallet.getChainId(); }, transport: (0, viem_1.custom)(wallet.transport), address: async () => { let address = wallet.account?.address; if (!address) { ; [address] = await wallet.getAddresses(); } return address; }, handleSignMessageStep: async (stepItem) => { const client = (0, client_js_1.getClient)(); const signData = stepItem.data?.sign; let signature; if (signData) { if (signData.signatureKind === 'eip191') { client.log(['Execute Steps: Signing with eip191'], logger_js_1.LogLevel.Verbose); if (signData.message.match(/0x[0-9a-fA-F]{64}/)) { signature = await wallet.signMessage({ account: wallet.account, message: { raw: signData.message } }); } else { signature = await wallet.signMessage({ account: wallet.account, message: signData.message }); } } else if (signData.signatureKind === 'eip712') { client.log(['Execute Steps: Signing with eip712'], logger_js_1.LogLevel.Verbose); signature = await wallet.signTypedData({ account: wallet.account, domain: signData.domain, types: signData.types, primaryType: signData.primaryType, message: signData.value }); } } return signature; }, handleSendTransactionStep: async (chainId, stepItem) => { const stepData = stepItem.data; const client = (0, client_js_1.getClient)(); const chain = (0, client_js_1.getClient)().chains.find((chain) => chain.id === chainId)?.viemChain; if (!chain) { throw 'Chain not found when sending transaction'; } return await wallet.sendTransaction({ chain, data: stepData.data, account: wallet.account ?? stepData.from, to: stepData.to, value: (0, viem_1.hexToBigInt)(stepData.value || 0), ...(stepData.maxFeePerGas && client.useGasFeeEstimations && { maxFeePerGas: (0, viem_1.hexToBigInt)(stepData.maxFeePerGas) }), ...(stepData.maxPriorityFeePerGas && client.useGasFeeEstimations && { maxPriorityFeePerGas: (0, viem_1.hexToBigInt)(stepData.maxPriorityFeePerGas) }), ...(stepData.gas && client.useGasFeeEstimations && { gas: (0, viem_1.hexToBigInt)(stepData.gas) }) }); }, handleConfirmTransactionStep: async (txHash, chainId, onReplaced, onCancelled) => { const client = (0, client_js_1.getClient)(); const chain = client.chains.find((chain) => chain.id === chainId); const viemClient = (0, viem_1.createPublicClient)({ chain: chain?.viemChain, transport: wallet.transport ? (0, viem_1.fallback)([(0, viem_1.custom)(wallet.transport), (0, viem_1.http)()]) : (0, viem_1.http)() }); const receipt = await viemClient.waitForTransactionReceipt({ hash: txHash, onReplaced: (replacement) => { if (replacement.reason === 'cancelled') { onCancelled(); throw Error('Transaction cancelled'); } onReplaced(replacement.transaction.hash); } }); return receipt; }, switchChain: async (chainId) => { try { await wallet.switchChain({ id: chainId }); return; } catch (e) { if (e && e?.message) { if (e.message.includes('does not support the requested chain')) { throw new Error('Wallet does not support chain'); } else if (e.message.includes('rejected')) { throw e; } else if (e.message.includes('already pending')) { return; } } const client = (0, client_js_1.getClient)(); const chain = client.chains.find((chain) => chain.id === chainId); if (!chain) { throw 'Chain missing from Relay Client'; } await wallet.addChain({ chain: chain?.viemChain }); return; } }, supportsAtomicBatch: async (chainId) => { if (!wallet.account) return false; try { const eip5792Wallet = wallet.extend((0, experimental_1.eip5792Actions)()); const capabilities = await eip5792Wallet.getCapabilities({ account: eip5792Wallet.account }); return capabilities[chainId]?.atomicBatch?.supported; } catch { return false; } }, handleBatchTransactionStep: async (chainId, items) => { const calls = items.map((item) => ({ to: item.data.to, data: item.data.data, value: (0, viem_1.hexToBigInt)(item.data.value || 0), ...(item.data.maxFeePerGas && { maxFeePerGas: (0, viem_1.hexToBigInt)(item.data.maxFeePerGas) }), ...(item.data.maxPriorityFeePerGas && { maxPriorityFeePerGas: (0, viem_1.hexToBigInt)(item.data.maxPriorityFeePerGas) }), ...(item.data.gas && { gas: (0, viem_1.hexToBigInt)(item.data.gas) }) })); const eip5792Wallet = wallet.extend((0, experimental_1.eip5792Actions)()); const client = (0, client_js_1.getClient)(); const chain = client.chains.find((chain) => chain.id === chainId)?.viemChain; if (!chain) { throw 'Chain not found when sending transaction'; } const id = await eip5792Wallet.sendCalls({ chain, account: wallet.account, calls }); return id; } }; }; exports.adaptViemWallet = adaptViemWallet; //# sourceMappingURL=viemWallet.js.map