@reservoir0x/relay-sdk
Version:
Relay is the Fastest and Cheapest Way to Bridge and Transact Across Chains.
211 lines • 8.89 kB
JavaScript
;
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");
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') {
const signatureData = {
account: wallet.account,
domain: signData.domain,
types: signData.types,
primaryType: signData.primaryType,
message: signData.value
};
client.log(['Execute Steps: Signing with eip712', signatureData], logger_js_1.LogLevel.Verbose);
signature = await wallet.signTypedData(signatureData);
}
}
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';
}
const viemClient = (0, viem_1.createWalletClient)({
account: wallet.account ?? stepData.from,
chain,
transport: (0, viem_1.custom)(wallet.transport, { retryCount: 10, retryDelay: 200 })
});
return await viemClient.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 rpcUrl = chain?.httpRpcUrl;
const viemClient = (0, viem_1.createPublicClient)({
chain: chain?.viemChain,
transport: wallet.transport
? (0, viem_1.fallback)(rpcUrl
? [(0, viem_1.http)(rpcUrl), (0, viem_1.custom)(wallet.transport), (0, viem_1.http)()]
: [(0, viem_1.custom)(wallet.transport), (0, viem_1.http)()])
: (0, viem_1.fallback)([(0, viem_1.http)(rpcUrl), (0, viem_1.http)()]),
pollingInterval: client.confirmationPollingInterval
});
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';
}
try {
await wallet.addChain({
chain: chain?.viemChain
});
}
catch (e) {
if (e instanceof Error &&
e.name &&
e.name === 'InternalRpcError' &&
e.message.includes('is not a function')) {
(0, client_js_1.getClient)()?.log([
'Execute Steps: Detected internal RPC Error when adding a chain to the wallet',
e
], logger_js_1.LogLevel.Verbose);
return;
}
else {
throw e;
}
}
return;
}
},
supportsAtomicBatch: async (chainId) => {
if (!wallet.account)
return false;
try {
const capabilities = await wallet.getCapabilities({
account: wallet.account,
chainId
});
return capabilities?.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 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 wallet.sendCalls({
chain,
account: wallet.account,
calls
});
return id;
}
};
};
exports.adaptViemWallet = adaptViemWallet;
//# sourceMappingURL=viemWallet.js.map