@saberhq/solana-contrib
Advanced tools
Comparing version 1.12.29 to 1.12.30
@@ -194,2 +194,10 @@ import type { Cluster, ConfirmOptions, PublicKey, RpcResponseAndContext, Signer, SimulatedTransactionResponse, TransactionInstruction } from "@solana/web3.js"; | ||
/** | ||
* Takes a list of {@link TransactionEnvelope}s and combines them if they | ||
* are able to be combined under the maximum TX size limit. | ||
* | ||
* @param txs | ||
* @returns | ||
*/ | ||
static pack(...txs: readonly TransactionEnvelope[]): TransactionEnvelope[]; | ||
/** | ||
* Combines multiple async TransactionEnvelopes into one, serially. | ||
@@ -196,0 +204,0 @@ */ |
@@ -6,2 +6,3 @@ "use strict"; | ||
const web3_js_1 = require("@solana/web3.js"); | ||
const tiny_invariant_1 = (0, tslib_1.__importDefault)(require("tiny-invariant")); | ||
const __1 = require(".."); | ||
@@ -11,2 +12,12 @@ const txSizer_1 = require("./txSizer"); | ||
/** | ||
* Filters the required signers for a list of instructions. | ||
* @param ixs | ||
* @returns | ||
*/ | ||
const filterRequiredSigners = (ixs, signers) => { | ||
// filter out the signers required for the transaction | ||
const requiredSigners = ixs.flatMap((ix) => ix.keys.filter((k) => k.isSigner).map((k) => k.pubkey)); | ||
return signers.filter((s) => requiredSigners.find((rs) => rs.equals(s.publicKey))); | ||
}; | ||
/** | ||
* Contains a Transaction that is being built. | ||
@@ -157,5 +168,3 @@ */ | ||
_filterRequiredSigners(ixs) { | ||
// filter out the signers required for the transaction | ||
const requiredSigners = ixs.flatMap((ix) => ix.keys.filter((k) => k.isSigner).map((k) => k.pubkey)); | ||
return this.signers.filter((s) => requiredSigners.find((rs) => rs.equals(s.publicKey))); | ||
return filterRequiredSigners(ixs, this.signers); | ||
} | ||
@@ -316,2 +325,44 @@ /** | ||
/** | ||
* Takes a list of {@link TransactionEnvelope}s and combines them if they | ||
* are able to be combined under the maximum TX size limit. | ||
* | ||
* @param txs | ||
* @returns | ||
*/ | ||
static pack(...txs) { | ||
if (txs.length === 0) { | ||
return []; | ||
} | ||
const [first, ...rest] = txs; | ||
(0, tiny_invariant_1.default)(first); | ||
const { provider } = first; | ||
let lastTXEnv = first; | ||
let lastEstimation = lastTXEnv.estimateSizeUnsafe(); | ||
const partition = []; | ||
rest.forEach((addedTX, i) => { | ||
if (lastEstimation > web3_js_1.PACKET_DATA_SIZE) { | ||
throw new Error(`cannot construct a valid partition: instruction ${i} is too large (${lastEstimation} > ${web3_js_1.PACKET_DATA_SIZE})`); | ||
} | ||
const nextIXs = [...lastTXEnv.instructions, ...addedTX.instructions]; | ||
const nextSigners = filterRequiredSigners(nextIXs, [ | ||
...lastTXEnv.signers, | ||
...addedTX.signers, | ||
]); | ||
const nextTXEnv = new TransactionEnvelope(provider, nextIXs, nextSigners); | ||
const nextEstimation = nextTXEnv.estimateSizeUnsafe(); | ||
// move to next tx envelope if too big | ||
if (nextEstimation > web3_js_1.PACKET_DATA_SIZE) { | ||
partition.push(lastTXEnv); | ||
lastTXEnv = addedTX; | ||
lastEstimation = lastTXEnv.estimateSizeUnsafe(); | ||
} | ||
else { | ||
lastTXEnv = nextTXEnv; | ||
lastEstimation = nextEstimation; | ||
} | ||
}); | ||
partition.push(lastTXEnv); | ||
return partition; | ||
} | ||
/** | ||
* Combines multiple async TransactionEnvelopes into one, serially. | ||
@@ -318,0 +369,0 @@ */ |
@@ -194,2 +194,10 @@ import type { Cluster, ConfirmOptions, PublicKey, RpcResponseAndContext, Signer, SimulatedTransactionResponse, TransactionInstruction } from "@solana/web3.js"; | ||
/** | ||
* Takes a list of {@link TransactionEnvelope}s and combines them if they | ||
* are able to be combined under the maximum TX size limit. | ||
* | ||
* @param txs | ||
* @returns | ||
*/ | ||
static pack(...txs: readonly TransactionEnvelope[]): TransactionEnvelope[]; | ||
/** | ||
* Combines multiple async TransactionEnvelopes into one, serially. | ||
@@ -196,0 +204,0 @@ */ |
import { __awaiter } from "tslib"; | ||
import { PACKET_DATA_SIZE, Transaction } from "@solana/web3.js"; | ||
import invariant from "tiny-invariant"; | ||
import { EstimatedTXTooBigError, printTXTable, suppressConsoleError, TXSizeEstimationError, } from ".."; | ||
@@ -7,2 +8,12 @@ import { calculateTxSizeUnsafe } from "./txSizer"; | ||
/** | ||
* Filters the required signers for a list of instructions. | ||
* @param ixs | ||
* @returns | ||
*/ | ||
const filterRequiredSigners = (ixs, signers) => { | ||
// filter out the signers required for the transaction | ||
const requiredSigners = ixs.flatMap((ix) => ix.keys.filter((k) => k.isSigner).map((k) => k.pubkey)); | ||
return signers.filter((s) => requiredSigners.find((rs) => rs.equals(s.publicKey))); | ||
}; | ||
/** | ||
* Contains a Transaction that is being built. | ||
@@ -153,5 +164,3 @@ */ | ||
_filterRequiredSigners(ixs) { | ||
// filter out the signers required for the transaction | ||
const requiredSigners = ixs.flatMap((ix) => ix.keys.filter((k) => k.isSigner).map((k) => k.pubkey)); | ||
return this.signers.filter((s) => requiredSigners.find((rs) => rs.equals(s.publicKey))); | ||
return filterRequiredSigners(ixs, this.signers); | ||
} | ||
@@ -312,2 +321,44 @@ /** | ||
/** | ||
* Takes a list of {@link TransactionEnvelope}s and combines them if they | ||
* are able to be combined under the maximum TX size limit. | ||
* | ||
* @param txs | ||
* @returns | ||
*/ | ||
static pack(...txs) { | ||
if (txs.length === 0) { | ||
return []; | ||
} | ||
const [first, ...rest] = txs; | ||
invariant(first); | ||
const { provider } = first; | ||
let lastTXEnv = first; | ||
let lastEstimation = lastTXEnv.estimateSizeUnsafe(); | ||
const partition = []; | ||
rest.forEach((addedTX, i) => { | ||
if (lastEstimation > PACKET_DATA_SIZE) { | ||
throw new Error(`cannot construct a valid partition: instruction ${i} is too large (${lastEstimation} > ${PACKET_DATA_SIZE})`); | ||
} | ||
const nextIXs = [...lastTXEnv.instructions, ...addedTX.instructions]; | ||
const nextSigners = filterRequiredSigners(nextIXs, [ | ||
...lastTXEnv.signers, | ||
...addedTX.signers, | ||
]); | ||
const nextTXEnv = new TransactionEnvelope(provider, nextIXs, nextSigners); | ||
const nextEstimation = nextTXEnv.estimateSizeUnsafe(); | ||
// move to next tx envelope if too big | ||
if (nextEstimation > PACKET_DATA_SIZE) { | ||
partition.push(lastTXEnv); | ||
lastTXEnv = addedTX; | ||
lastEstimation = lastTXEnv.estimateSizeUnsafe(); | ||
} | ||
else { | ||
lastTXEnv = nextTXEnv; | ||
lastEstimation = nextEstimation; | ||
} | ||
}); | ||
partition.push(lastTXEnv); | ||
return partition; | ||
} | ||
/** | ||
* Combines multiple async TransactionEnvelopes into one, serially. | ||
@@ -314,0 +365,0 @@ */ |
{ | ||
"name": "@saberhq/solana-contrib", | ||
"version": "1.12.29", | ||
"version": "1.12.30", | ||
"description": "Common types and libraries for Solana", | ||
@@ -45,3 +45,3 @@ "author": "Ian Macalinao <ian@saber.so>", | ||
}, | ||
"gitHead": "d95f6521e078e7b54247daab4f5366c14bc4fedd", | ||
"gitHead": "486233753e8e1550bff03d0d713a2711ff1a0742", | ||
"publishConfig": { | ||
@@ -48,0 +48,0 @@ "access": "public" |
@@ -11,2 +11,3 @@ import type { | ||
import { PACKET_DATA_SIZE, Transaction } from "@solana/web3.js"; | ||
import invariant from "tiny-invariant"; | ||
@@ -28,2 +29,20 @@ import type { BroadcastOptions } from ".."; | ||
/** | ||
* Filters the required signers for a list of instructions. | ||
* @param ixs | ||
* @returns | ||
*/ | ||
const filterRequiredSigners = ( | ||
ixs: TransactionInstruction[], | ||
signers: Signer[] | ||
): Signer[] => { | ||
// filter out the signers required for the transaction | ||
const requiredSigners = ixs.flatMap((ix) => | ||
ix.keys.filter((k) => k.isSigner).map((k) => k.pubkey) | ||
); | ||
return signers.filter((s) => | ||
requiredSigners.find((rs) => rs.equals(s.publicKey)) | ||
); | ||
}; | ||
/** | ||
* Options for simulating a transaction. | ||
@@ -217,9 +236,3 @@ */ | ||
private _filterRequiredSigners(ixs: TransactionInstruction[]): Signer[] { | ||
// filter out the signers required for the transaction | ||
const requiredSigners = ixs.flatMap((ix) => | ||
ix.keys.filter((k) => k.isSigner).map((k) => k.pubkey) | ||
); | ||
return this.signers.filter((s) => | ||
requiredSigners.find((rs) => rs.equals(s.publicKey)) | ||
); | ||
return filterRequiredSigners(ixs, this.signers); | ||
} | ||
@@ -417,2 +430,51 @@ | ||
/** | ||
* Takes a list of {@link TransactionEnvelope}s and combines them if they | ||
* are able to be combined under the maximum TX size limit. | ||
* | ||
* @param txs | ||
* @returns | ||
*/ | ||
static pack(...txs: readonly TransactionEnvelope[]): TransactionEnvelope[] { | ||
if (txs.length === 0) { | ||
return []; | ||
} | ||
const [first, ...rest] = txs; | ||
invariant(first); | ||
const { provider } = first; | ||
let lastTXEnv: TransactionEnvelope = first; | ||
let lastEstimation: number = lastTXEnv.estimateSizeUnsafe(); | ||
const partition: TransactionEnvelope[] = []; | ||
rest.forEach((addedTX, i) => { | ||
if (lastEstimation > PACKET_DATA_SIZE) { | ||
throw new Error( | ||
`cannot construct a valid partition: instruction ${i} is too large (${lastEstimation} > ${PACKET_DATA_SIZE})` | ||
); | ||
} | ||
const nextIXs = [...lastTXEnv.instructions, ...addedTX.instructions]; | ||
const nextSigners = filterRequiredSigners(nextIXs, [ | ||
...lastTXEnv.signers, | ||
...addedTX.signers, | ||
]); | ||
const nextTXEnv = new TransactionEnvelope(provider, nextIXs, nextSigners); | ||
const nextEstimation = nextTXEnv.estimateSizeUnsafe(); | ||
// move to next tx envelope if too big | ||
if (nextEstimation > PACKET_DATA_SIZE) { | ||
partition.push(lastTXEnv); | ||
lastTXEnv = addedTX; | ||
lastEstimation = lastTXEnv.estimateSizeUnsafe(); | ||
} else { | ||
lastTXEnv = nextTXEnv; | ||
lastEstimation = nextEstimation; | ||
} | ||
}); | ||
partition.push(lastTXEnv); | ||
return partition; | ||
} | ||
/** | ||
* Combines multiple async TransactionEnvelopes into one, serially. | ||
@@ -419,0 +481,0 @@ */ |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
607951
8441