@stabbleorg/anchor-contrib
Advanced tools
Comparing version 0.2.4 to 0.3.0
export * from "./wallet"; | ||
export * from "./helius"; | ||
export * from "./listener"; |
@@ -18,3 +18,4 @@ "use strict"; | ||
__exportStar(require("./wallet"), exports); | ||
__exportStar(require("./helius"), exports); | ||
__exportStar(require("./listener"), exports); | ||
//# sourceMappingURL=index.js.map |
import { Provider } from "@coral-xyz/anchor"; | ||
import { AddressLookupTableAccount, BlockhashWithExpiryBlockHeight, PublicKey, TransactionInstruction, VersionedTransaction } from "@solana/web3.js"; | ||
import { PriorityLevel } from "./helius"; | ||
export type TransactionWithRecentBlockAndSlot = { | ||
@@ -21,4 +22,4 @@ transaction: VersionedTransaction; | ||
}>; | ||
newTX(instructions: TransactionInstruction[], altAccounts?: AddressLookupTableAccount[], payerAddress?: PublicKey): Promise<TransactionWithRecentBlockAndSlot>; | ||
newPrioritizedTX(instructions: TransactionInstruction[], priorityFee?: number, altAccounts?: AddressLookupTableAccount[], payerAddress?: PublicKey): Promise<TransactionWithRecentBlockAndSlot>; | ||
getPriorityFeeEstimate(transaction: VersionedTransaction, priorityLevel?: PriorityLevel): Promise<number>; | ||
createTransaction(instructions: TransactionInstruction[], altAccounts?: AddressLookupTableAccount[], priorityLevel?: PriorityLevel): Promise<TransactionWithRecentBlockAndSlot>; | ||
} |
@@ -11,4 +11,8 @@ "use strict"; | ||
}; | ||
var __importDefault = (this && this.__importDefault) || function (mod) { | ||
return (mod && mod.__esModule) ? mod : { "default": mod }; | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.WalletContext = void 0; | ||
const bs58_1 = __importDefault(require("bs58")); | ||
const spl_token_1 = require("@solana/spl-token"); | ||
@@ -46,17 +50,37 @@ const web3_js_1 = require("@solana/web3.js"); | ||
} | ||
newTX(instructions, altAccounts = [], payerAddress) { | ||
return this.newPrioritizedTX(instructions, 0, altAccounts, payerAddress); | ||
getPriorityFeeEstimate(transaction_1) { | ||
return __awaiter(this, arguments, void 0, function* (transaction, priorityLevel = "Medium") { | ||
if (priorityLevel === "None") | ||
return 0; | ||
try { | ||
const response = yield fetch(this.provider.connection.rpcEndpoint, { | ||
method: "POST", | ||
headers: { "Content-Type": "application/json" }, | ||
body: JSON.stringify({ | ||
jsonrpc: "2.0", | ||
id: this.walletAddress.toBase58(), | ||
method: "getPriorityFeeEstimate", | ||
params: [ | ||
{ | ||
transaction: bs58_1.default.encode(transaction.serialize()), | ||
options: { priorityLevel: priorityLevel }, | ||
}, | ||
], | ||
}), | ||
}); | ||
const data = (yield response.json()); | ||
return data.result.priorityFeeEstimate; | ||
} | ||
catch (err) { | ||
return 0; | ||
} | ||
}); | ||
} | ||
newPrioritizedTX(instructions_1) { | ||
return __awaiter(this, arguments, void 0, function* (instructions, priorityFee = 0, altAccounts = [], payerAddress) { | ||
createTransaction(instructions_1) { | ||
return __awaiter(this, arguments, void 0, function* (instructions, altAccounts = [], priorityLevel = "High") { | ||
const { value: recentBlock, context: { slot }, } = yield this.provider.connection.getLatestBlockhashAndContext(); | ||
const payerKey = payerAddress || this.walletAddress; | ||
if (priorityFee) { | ||
instructions.unshift(web3_js_1.ComputeBudgetProgram.setComputeUnitPrice({ | ||
microLamports: priorityFee, | ||
})); | ||
} | ||
try { | ||
const sim = new web3_js_1.VersionedTransaction(new web3_js_1.TransactionMessage({ | ||
payerKey, | ||
// transaction to simulate | ||
const transaction = new web3_js_1.VersionedTransaction(new web3_js_1.TransactionMessage({ | ||
payerKey: this.walletAddress, | ||
recentBlockhash: recentBlock.blockhash, | ||
@@ -70,6 +94,14 @@ instructions: [ | ||
}).compileToV0Message(altAccounts)); | ||
const { value } = yield this.provider.connection.simulateTransaction(sim); | ||
if (value.unitsConsumed) { | ||
// calculate priority fee | ||
const priorityFee = yield this.getPriorityFeeEstimate(transaction, priorityLevel); | ||
if (priorityFee) { | ||
instructions.unshift(web3_js_1.ComputeBudgetProgram.setComputeUnitPrice({ | ||
microLamports: priorityFee, | ||
})); | ||
} | ||
// calculate exact compute units needed | ||
const { value: sim } = yield this.provider.connection.simulateTransaction(transaction); | ||
if (sim.unitsConsumed) { | ||
instructions.unshift(web3_js_1.ComputeBudgetProgram.setComputeUnitLimit({ | ||
units: value.unitsConsumed, | ||
units: sim.unitsConsumed, | ||
})); | ||
@@ -79,4 +111,5 @@ } | ||
catch (err) { } | ||
// transaction to send | ||
const transaction = new web3_js_1.VersionedTransaction(new web3_js_1.TransactionMessage({ | ||
payerKey, | ||
payerKey: this.walletAddress, | ||
recentBlockhash: recentBlock.blockhash, | ||
@@ -83,0 +116,0 @@ instructions, |
{ | ||
"name": "@stabbleorg/anchor-contrib", | ||
"version": "0.2.4", | ||
"version": "0.3.0", | ||
"main": "dist/index.js", | ||
@@ -19,3 +19,4 @@ "types": "dist/index.d.ts", | ||
"@solana/spl-token": "^0.4.0", | ||
"@solana/web3.js": "^1.91.0" | ||
"@solana/web3.js": "^1.91.0", | ||
"bs58": "^5.0.0" | ||
}, | ||
@@ -22,0 +23,0 @@ "devDependencies": { |
export * from "./wallet"; | ||
export * from "./helius"; | ||
export * from "./listener"; |
@@ -0,1 +1,2 @@ | ||
import bs58 from "bs58"; | ||
import { Provider } from "@coral-xyz/anchor"; | ||
@@ -17,2 +18,3 @@ import { | ||
} from "@solana/web3.js"; | ||
import { HeliusResponse, PriorityFeeEstimate, PriorityLevel } from "./helius"; | ||
@@ -62,15 +64,36 @@ export type TransactionWithRecentBlockAndSlot = { | ||
newTX( | ||
instructions: TransactionInstruction[], | ||
altAccounts: AddressLookupTableAccount[] = [], | ||
payerAddress?: PublicKey, | ||
): Promise<TransactionWithRecentBlockAndSlot> { | ||
return this.newPrioritizedTX(instructions, 0, altAccounts, payerAddress); | ||
async getPriorityFeeEstimate( | ||
transaction: VersionedTransaction, | ||
priorityLevel: PriorityLevel = "Medium", | ||
): Promise<number> { | ||
if (priorityLevel === "None") return 0; | ||
try { | ||
const response = await fetch(this.provider.connection.rpcEndpoint, { | ||
method: "POST", | ||
headers: { "Content-Type": "application/json" }, | ||
body: JSON.stringify({ | ||
jsonrpc: "2.0", | ||
id: this.walletAddress.toBase58(), | ||
method: "getPriorityFeeEstimate", | ||
params: [ | ||
{ | ||
transaction: bs58.encode(transaction.serialize()), | ||
options: { priorityLevel: priorityLevel }, | ||
}, | ||
], | ||
}), | ||
}); | ||
const data = (await response.json()) as HeliusResponse<PriorityFeeEstimate>; | ||
return data.result.priorityFeeEstimate; | ||
} catch (err) { | ||
return 0; | ||
} | ||
} | ||
async newPrioritizedTX( | ||
async createTransaction( | ||
instructions: TransactionInstruction[], | ||
priorityFee: number = 0, | ||
altAccounts: AddressLookupTableAccount[] = [], | ||
payerAddress?: PublicKey, | ||
priorityLevel: PriorityLevel = "High", | ||
): Promise<TransactionWithRecentBlockAndSlot> { | ||
@@ -81,16 +104,8 @@ const { | ||
} = await this.provider.connection.getLatestBlockhashAndContext(); | ||
const payerKey = payerAddress || this.walletAddress; | ||
if (priorityFee) { | ||
instructions.unshift( | ||
ComputeBudgetProgram.setComputeUnitPrice({ | ||
microLamports: priorityFee, | ||
}), | ||
); | ||
} | ||
try { | ||
const sim = new VersionedTransaction( | ||
// transaction to simulate | ||
const transaction = new VersionedTransaction( | ||
new TransactionMessage({ | ||
payerKey, | ||
payerKey: this.walletAddress, | ||
recentBlockhash: recentBlock.blockhash, | ||
@@ -106,7 +121,18 @@ instructions: [ | ||
const { value } = await this.provider.connection.simulateTransaction(sim); | ||
if (value.unitsConsumed) { | ||
// calculate priority fee | ||
const priorityFee = await this.getPriorityFeeEstimate(transaction, priorityLevel); | ||
if (priorityFee) { | ||
instructions.unshift( | ||
ComputeBudgetProgram.setComputeUnitPrice({ | ||
microLamports: priorityFee, | ||
}), | ||
); | ||
} | ||
// calculate exact compute units needed | ||
const { value: sim } = await this.provider.connection.simulateTransaction(transaction); | ||
if (sim.unitsConsumed) { | ||
instructions.unshift( | ||
ComputeBudgetProgram.setComputeUnitLimit({ | ||
units: value.unitsConsumed, | ||
units: sim.unitsConsumed, | ||
}), | ||
@@ -117,5 +143,6 @@ ); | ||
// transaction to send | ||
const transaction = new VersionedTransaction( | ||
new TransactionMessage({ | ||
payerKey, | ||
payerKey: this.walletAddress, | ||
recentBlockhash: recentBlock.blockhash, | ||
@@ -122,0 +149,0 @@ instructions, |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Network access
Supply chain riskThis module accesses the network.
Found 1 instance in 1 package
17756
17
332
4
1
+ Addedbs58@^5.0.0
+ Addedbase-x@4.0.0(transitive)
+ Addedbs58@5.0.0(transitive)