@drift-labs/sdk
Version:
SDK for Drift Protocol
77 lines (76 loc) • 3.37 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
exports.createNativeInstructionDiscriminatorBuffer = exports.getSizeOfTransaction = exports.isVersionedTransaction = exports.NATIVE_INSTRUCTION_MAGIC_BYTES = exports.MAX_TX_BYTE_SIZE = void 0;
const web3_js_1 = require("@solana/web3.js");
exports.MAX_TX_BYTE_SIZE = 1232;
exports.NATIVE_INSTRUCTION_MAGIC_BYTES = [0xff, 0xff, 0xff, 0xff];
const isVersionedTransaction = (tx) => {
const version = tx === null || tx === void 0 ? void 0 : tx.version;
const isVersionedTx = tx instanceof web3_js_1.VersionedTransaction || version !== undefined;
return isVersionedTx;
};
exports.isVersionedTransaction = isVersionedTransaction;
const getSizeOfTransaction = (instructions, versionedTransaction = true, addressLookupTables = []) => {
const programs = new Set();
const signers = new Set();
let accounts = new Set();
instructions.forEach((ix) => {
try {
if (ix.programId) {
programs.add(ix.programId.toBase58());
accounts.add(ix.programId.toBase58());
}
if (ix.keys) {
ix.keys.forEach((key) => {
if (key.isSigner) {
signers.add(key.pubkey.toBase58());
}
accounts.add(key.pubkey.toBase58());
});
}
}
catch (e) {
console.log(e);
}
});
const instructionSizes = instructions
.map((ix) => 1 +
getSizeOfCompressedU16(ix.keys.length) +
ix.keys.length +
getSizeOfCompressedU16(ix.data.length) +
ix.data.length)
.reduce((a, b) => a + b, 0);
let numberOfAddressLookups = 0;
if (addressLookupTables.length > 0) {
const lookupTableAddresses = addressLookupTables
.map((addressLookupTable) => addressLookupTable.state.addresses.map((address) => address.toBase58()))
.flat();
const totalNumberOfAccounts = accounts.size;
accounts = new Set([...accounts].filter((account) => !lookupTableAddresses.includes(account)));
accounts = new Set([...accounts, ...programs, ...signers]);
numberOfAddressLookups = totalNumberOfAccounts - accounts.size;
}
return (getSizeOfCompressedU16(signers.size) +
signers.size * 64 + // array of signatures
3 +
getSizeOfCompressedU16(accounts.size) +
32 * accounts.size + // array of account addresses
32 + // recent blockhash
getSizeOfCompressedU16(instructions.length) +
instructionSizes + // array of instructions
(versionedTransaction ? 1 + getSizeOfCompressedU16(0) : 0) +
(versionedTransaction ? 32 * addressLookupTables.length : 0) +
(versionedTransaction && addressLookupTables.length > 0 ? 2 : 0) +
numberOfAddressLookups);
};
exports.getSizeOfTransaction = getSizeOfTransaction;
function getSizeOfCompressedU16(n) {
return 1 + Number(n >= 128) + Number(n >= 16384);
}
function createNativeInstructionDiscriminatorBuffer(discriminator) {
const buffer = new Uint8Array(5);
buffer.set(exports.NATIVE_INSTRUCTION_MAGIC_BYTES, 0);
buffer.set([discriminator], 4);
return buffer;
}
exports.createNativeInstructionDiscriminatorBuffer = createNativeInstructionDiscriminatorBuffer;
;