UNPKG

@symmetry-hq/agents-sdk

Version:

Symmetry Agents SDK

172 lines (171 loc) 7.91 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; Object.defineProperty(exports, "__esModule", { value: true }); exports.delay = delay; exports.getAddressLookupTableAccounts = getAddressLookupTableAccounts; exports.getMultipleAddressLookupTableAccounts = getMultipleAddressLookupTableAccounts; exports.wrapV0Transaction = wrapV0Transaction; exports.sendV0Transaction = sendV0Transaction; exports.prepareV0Transactions = prepareV0Transactions; exports.sendV0Transactions = sendV0Transactions; const web3_js_1 = require("@solana/web3.js"); const constants_1 = require("../constants"); function delay(ms) { return __awaiter(this, void 0, void 0, function* () { return new Promise((resolve) => setTimeout(resolve, ms)); }); } function getAddressLookupTableAccounts(connection, keys) { return __awaiter(this, void 0, void 0, function* () { const addressLookupTableAccountInfos = yield connection.getMultipleAccountsInfo(keys, "confirmed"); return addressLookupTableAccountInfos.reduce((acc, accountInfo, index) => { const addressLookupTableAddress = keys[index]; if (accountInfo) { const addressLookupTableAccount = new web3_js_1.AddressLookupTableAccount({ key: new web3_js_1.PublicKey(addressLookupTableAddress), state: web3_js_1.AddressLookupTableAccount.deserialize(accountInfo.data), }); acc.push(addressLookupTableAccount); } return acc; }, new Array()); }); } ; function getMultipleAddressLookupTableAccounts(connection, keys) { return __awaiter(this, void 0, void 0, function* () { let allLuts = []; keys.forEach(luts => luts.forEach(lut => allLuts.push(lut.toBase58()))); allLuts = [...new Set(allLuts)]; const addressLookupTableAccounts = yield getAddressLookupTableAccounts(connection, allLuts.map(lut => new web3_js_1.PublicKey(lut))); const map = {}; allLuts.forEach((pubkey, id) => map[pubkey] = addressLookupTableAccounts[id]); return keys.map(luts => luts.map(lut => map[lut.toBase58()])); }); } function wrapV0Transaction(blockhash, addressLookupTableAccounts, payerPubkey, priorityFee, ixs) { ixs = [ ...ixs, web3_js_1.ComputeBudgetProgram.setComputeUnitLimit({ units: constants_1.COMPUTE_UNITS }), web3_js_1.ComputeBudgetProgram.setComputeUnitPrice({ microLamports: priorityFee }), ]; const txMessage = new web3_js_1.TransactionMessage({ payerKey: payerPubkey, recentBlockhash: blockhash, instructions: ixs, }); let versionedTx = new web3_js_1.VersionedTransaction(txMessage.compileToV0Message(addressLookupTableAccounts)); try { let tt = versionedTx.serialize().length; if (tt > 1232) { throw new Error("Transaction too large"); } } catch (e) { throw new Error(e.message); } return versionedTx; } function sendV0Transaction(connection, tx, blockhash, lastValidBlockHeight, simulateTransactions) { return __awaiter(this, void 0, void 0, function* () { var _a; const serializedTx = tx.serialize(); let txId = "Error"; if (simulateTransactions) { txId = yield connection.sendRawTransaction(serializedTx, { preflightCommitment: "confirmed" }).catch(e => { console.log(e.message); return "Error"; }); console.log("Simulation txId:", txId); for (let i = 0; i < 4; i++) { yield delay(1000).then(() => connection.sendRawTransaction(serializedTx, { preflightCommitment: "confirmed" }).catch(() => { })); } if (txId === "Error") throw new Error("Simulation failed"); } else { connection.sendRawTransaction(serializedTx, { preflightCommitment: "confirmed" }) .catch(e => { console.log(e.message); }); txId = yield connection.sendRawTransaction(serializedTx, { skipPreflight: true }); console.log("Sending tx:", txId); for (let i = 0; i < 4; i++) { yield delay(1000).then(() => connection.sendRawTransaction(serializedTx, { skipPreflight: true }).catch(() => { })); } } let confirmation = null; let result = null; connection.confirmTransaction({ blockhash, lastValidBlockHeight, signature: txId, }, "confirmed").then((res) => confirmation = res); let iterations = 20; while (confirmation === null && result === null && iterations > 0) { yield delay(2500); result = yield connection.getTransaction(txId, { commitment: "confirmed", maxSupportedTransactionVersion: 0, }); if (result && result.meta && ((_a = result.meta) === null || _a === void 0 ? void 0 : _a.err)) { throw new Error(txId); } iterations--; } if (result) return txId; //@ts-ignore if (!confirmation || confirmation.value.err) { //@ts-ignore console.log(confirmation === null || confirmation === void 0 ? void 0 : confirmation.value.err); throw new Error(txId); } return txId; }); } function prepareV0Transactions(params) { return __awaiter(this, void 0, void 0, function* () { const { connection, payer, priorityFee, multipleIxs, multipleLookupTableAddresses, signers, batches } = params; const { blockhash, lastValidBlockHeight } = yield connection.getLatestBlockhash(); const multipleAddressLookupTableAccounts = yield getMultipleAddressLookupTableAccounts(connection, multipleLookupTableAddresses); const txs = multipleIxs.map((ixs, index) => { let tx = null; try { tx = wrapV0Transaction(blockhash, multipleAddressLookupTableAccounts[index], payer, priorityFee, ixs); if (signers[index].length > 0) tx.sign(signers[index]); } catch (e) { console.log("Error signing tx:", e.message); } return tx; }).filter(tx => tx !== null); return { blockhash, lastValidBlockHeight, versionedTxs: txs, batches, }; }); } function sendV0Transactions(connection, txParams, simulateTransactions) { return __awaiter(this, void 0, void 0, function* () { const { versionedTxs, blockhash, lastValidBlockHeight, batches } = txParams; const signedTxs = versionedTxs; let lastIndex = 0; let txIds = []; for (let i = 0; i < batches.length; i++) { const ids = yield Promise.all(signedTxs.slice(lastIndex, lastIndex + batches[i]).map(signedTx => sendV0Transaction(connection, signedTx, blockhash, lastValidBlockHeight, simulateTransactions).catch(e => { console.log("Transaction failed:", e.message); return "Error"; }))); txIds = [...txIds, ...ids]; lastIndex += batches[i]; } return txIds; }); }