UNPKG

@lucoadam/zebec-wormhole-sdk

Version:

This sdk can be use to transfer assets across chains and to interact with the Zebec's xchain bridge smart contracts for passing message from EVM chain to solana specially to utilize the features of Zebec Streaming and Zebec Multisig Streaming protocol.

83 lines (81 loc) 3.19 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.getDecimals = getDecimals; exports.logInstruction = logInstruction; exports.logTransaction = logTransaction; exports.parseSolPubKey = parseSolPubKey; exports.sendAndConfirmTransaction = sendAndConfirmTransaction; var _tweetnacl = _interopRequireDefault(require("tweetnacl")); var _splToken = require("@solana/spl-token"); var _web = require("@solana/web3.js"); var _constants = require("./constants"); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } function parseSolPubKey(address) { return new _web.PublicKey(address); } function logTransaction(transaction) { console.log("blockHash", transaction.recentBlockhash); console.log("feePayer", transaction.feePayer?.toString()); transaction.instructions.map(ixn => { logInstruction(ixn); }); console.log("signatures: ["); for (const signPair of transaction.signatures) { const verified = signPair.signature ? _tweetnacl.default.sign.detached.verify(transaction.serializeMessage(), Uint8Array.from(signPair.signature), signPair.publicKey.toBytes()) : null; console.log("{ " + "publicKey: " + signPair.publicKey.toString() + " signature: " + signPair.signature?.toString("hex") + " verified: " + verified + " }"); } console.log("]"); } function logInstruction(ixn) { console.log("data:", ixn.data.toString("hex")); console.log("program Id:", ixn.programId.toString()); console.log("keys: ["); for (const a of ixn.keys) { console.log("{ " + "pubkey: " + a.pubkey.toString() + ", isSigner: " + a.isSigner + ", isWriter: " + a.isWritable + " }"); } console.log("]"); } /** * Gets decimal value of given mint * @param mintAddress mint address of solana token * @returns */ async function getDecimals(mintAddress) { const connection = new _web.Connection(_constants.SOLANA_HOST, "confirmed"); const mint = await (0, _splToken.getMint)(connection, new _web.PublicKey(mintAddress), "confirmed"); return mint.decimals; } async function sendAndConfirmTransaction(connection, signTransaction, transaction, maxRetries = 0) { let currentRetries = 0; let success = false; let transactionReceipt = ""; while (!success && !(currentRetries > maxRetries)) { let signed; try { signed = await signTransaction(transaction); console.log("signed", signed); } catch (e) { //Eject here because this is most likely an intentional rejection from the user, or a genuine unrecoverable failure. return Promise.reject("Failed to sign transaction."); } try { logTransaction(signed); const txid = await connection.sendRawTransaction(signed.serialize()); await connection.confirmTransaction(txid); transactionReceipt = txid; success = true; } catch (e) { console.log("error occured"); console.log(e instanceof _web.SendTransactionError ? e.logs : e); currentRetries++; } } if (currentRetries > maxRetries) { return Promise.reject("Reached the maximum number of retries."); } else { return Promise.resolve(transactionReceipt); } } //# sourceMappingURL=solana.js.map