UNPKG

@relayprotocol/relay-svm-wallet-adapter

Version:

An SVM (Solana Virtual Machine) adapter for use in @relayprotocol/relay-sdk

68 lines 3.1 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.adaptSolanaWallet = void 0; const web3_js_1 = require("@solana/web3.js"); const relay_sdk_1 = require("@relayprotocol/relay-sdk"); const adaptSolanaWallet = (walletAddress, chainId, connection, signAndSendTransaction, payerKey) => { let _chainId = chainId; const getChainId = async () => { return _chainId; }; return { vmType: 'svm', getChainId, address: async () => { return walletAddress; }, handleSignMessageStep: async () => { throw new Error('Message signing not implemented for Solana'); }, handleSendTransactionStep: async (_chainId, stepItem) => { const client = (0, relay_sdk_1.getClient)(); const instructions = stepItem?.data?.instructions?.map((i) => new web3_js_1.TransactionInstruction({ keys: i.keys.map((k) => ({ isSigner: k.isSigner, isWritable: k.isWritable, pubkey: new web3_js_1.PublicKey(k.pubkey) })), programId: new web3_js_1.PublicKey(i.programId), data: Buffer.from(i.data, 'hex') })) ?? []; const messageV0 = new web3_js_1.TransactionMessage({ payerKey: new web3_js_1.PublicKey(payerKey ?? walletAddress), instructions, recentBlockhash: await connection .getLatestBlockhash() .then((b) => b.blockhash) }).compileToV0Message(await Promise.all(stepItem?.data?.addressLookupTableAddresses?.map(async (address) => await connection .getAddressLookupTable(new web3_js_1.PublicKey(address)) .then((res) => res.value)) ?? [])); const transaction = new web3_js_1.VersionedTransaction(messageV0); const signature = await signAndSendTransaction(transaction, undefined, instructions, stepItem.data.instructions); client.log(['Transaction Signature obtained', signature], relay_sdk_1.LogLevel.Verbose); return signature.signature; }, handleConfirmTransactionStep: async (txHash) => { const { blockhash, lastValidBlockHeight } = await connection.getLatestBlockhash('confirmed'); const result = await connection.confirmTransaction({ blockhash: blockhash, lastValidBlockHeight: lastValidBlockHeight, signature: txHash }); if (result.value.err) { throw new Error(`Transaction failed: ${result.value.err}`); } return { blockHash: result.context.slot.toString(), blockNumber: result.context.slot, txHash }; }, switchChain: (chainId) => { _chainId = chainId; return new Promise((res) => res()); } }; }; exports.adaptSolanaWallet = adaptSolanaWallet; //# sourceMappingURL=adapter.js.map