@saberhq/solana
Advanced tools
Comparing version 0.1.0-beta.11 to 0.1.1-beta.1
/** | ||
* Default configuration for all networks. | ||
*/ | ||
export const DEFAULT_NETWORK_CONFIG_MAP = { | ||
export var DEFAULT_NETWORK_CONFIG_MAP = { | ||
"mainnet-beta": { | ||
@@ -6,0 +6,0 @@ name: "Mainnet Beta", |
@@ -1,2 +0,2 @@ | ||
import { __awaiter } from "tslib"; | ||
import { __awaiter, __generator } from "tslib"; | ||
import { sendAndConfirmRawTransaction, } from "@solana/web3.js"; | ||
@@ -10,3 +10,3 @@ import invariant from "tiny-invariant"; | ||
*/ | ||
export default class SolanaProvider { | ||
var SolanaProvider = /** @class */ (function () { | ||
/** | ||
@@ -17,3 +17,3 @@ * @param connection The cluster connection where the program is deployed. | ||
*/ | ||
constructor(connection, wallet, opts) { | ||
function SolanaProvider(connection, wallet, opts) { | ||
this.connection = connection; | ||
@@ -23,3 +23,3 @@ this.wallet = wallet; | ||
} | ||
static defaultOptions() { | ||
SolanaProvider.defaultOptions = function () { | ||
return { | ||
@@ -29,3 +29,3 @@ preflightCommitment: "recent", | ||
}; | ||
} | ||
}; | ||
/** | ||
@@ -39,58 +39,91 @@ * Sends the given transaction, paid for and signed by the provider's wallet. | ||
*/ | ||
send(tx, signers, opts) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
if (signers === undefined) { | ||
signers = []; | ||
} | ||
if (opts === undefined) { | ||
opts = this.opts; | ||
} | ||
tx.feePayer = this.wallet.publicKey; | ||
tx.recentBlockhash = (yield this.connection.getRecentBlockhash(opts.preflightCommitment)).blockhash; | ||
yield this.wallet.signTransaction(tx); | ||
signers | ||
.filter((s) => s !== undefined) | ||
.forEach((kp) => { | ||
tx.partialSign(kp); | ||
SolanaProvider.prototype.send = function (tx, signers, opts) { | ||
return __awaiter(this, void 0, void 0, function () { | ||
var _a, rawTx, txId; | ||
return __generator(this, function (_b) { | ||
switch (_b.label) { | ||
case 0: | ||
if (signers === undefined) { | ||
signers = []; | ||
} | ||
if (opts === undefined) { | ||
opts = this.opts; | ||
} | ||
tx.feePayer = this.wallet.publicKey; | ||
_a = tx; | ||
return [4 /*yield*/, this.connection.getRecentBlockhash(opts.preflightCommitment)]; | ||
case 1: | ||
_a.recentBlockhash = (_b.sent()).blockhash; | ||
return [4 /*yield*/, this.wallet.signTransaction(tx)]; | ||
case 2: | ||
_b.sent(); | ||
signers | ||
.filter(function (s) { return s !== undefined; }) | ||
.forEach(function (kp) { | ||
tx.partialSign(kp); | ||
}); | ||
rawTx = tx.serialize(); | ||
return [4 /*yield*/, sendAndConfirmRawTransaction(this.connection, rawTx, opts)]; | ||
case 3: | ||
txId = _b.sent(); | ||
return [2 /*return*/, txId]; | ||
} | ||
}); | ||
const rawTx = tx.serialize(); | ||
const txId = yield sendAndConfirmRawTransaction(this.connection, rawTx, opts); | ||
return txId; | ||
}); | ||
} | ||
}; | ||
/** | ||
* Similar to `send`, but for an array of transactions and signers. | ||
*/ | ||
sendAll(reqs, opts) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
if (opts === undefined) { | ||
opts = this.opts; | ||
} | ||
const blockhash = yield this.connection.getRecentBlockhash(opts.preflightCommitment); | ||
const txs = reqs.map((r) => { | ||
const tx = r.tx; | ||
let signers = r.signers; | ||
if (signers === undefined) { | ||
signers = []; | ||
SolanaProvider.prototype.sendAll = function (reqs, opts) { | ||
return __awaiter(this, void 0, void 0, function () { | ||
var blockhash, txs, signedTxs, sigs, k, tx, rawTx, _a, _b; | ||
var _this = this; | ||
return __generator(this, function (_c) { | ||
switch (_c.label) { | ||
case 0: | ||
if (opts === undefined) { | ||
opts = this.opts; | ||
} | ||
return [4 /*yield*/, this.connection.getRecentBlockhash(opts.preflightCommitment)]; | ||
case 1: | ||
blockhash = _c.sent(); | ||
txs = reqs.map(function (r) { | ||
var tx = r.tx; | ||
var signers = r.signers; | ||
if (signers === undefined) { | ||
signers = []; | ||
} | ||
tx.feePayer = _this.wallet.publicKey; | ||
tx.recentBlockhash = blockhash.blockhash; | ||
signers | ||
.filter(function (s) { return s !== undefined; }) | ||
.forEach(function (kp) { | ||
tx.partialSign(kp); | ||
}); | ||
return tx; | ||
}); | ||
return [4 /*yield*/, this.wallet.signAllTransactions(txs)]; | ||
case 2: | ||
signedTxs = _c.sent(); | ||
sigs = []; | ||
k = 0; | ||
_c.label = 3; | ||
case 3: | ||
if (!(k < txs.length)) return [3 /*break*/, 6]; | ||
tx = signedTxs[k]; | ||
invariant(tx, "tx missing"); | ||
rawTx = tx.serialize(); | ||
_b = (_a = sigs).push; | ||
return [4 /*yield*/, sendAndConfirmRawTransaction(this.connection, rawTx, opts)]; | ||
case 4: | ||
_b.apply(_a, [_c.sent()]); | ||
_c.label = 5; | ||
case 5: | ||
k += 1; | ||
return [3 /*break*/, 3]; | ||
case 6: return [2 /*return*/, sigs]; | ||
} | ||
tx.feePayer = this.wallet.publicKey; | ||
tx.recentBlockhash = blockhash.blockhash; | ||
signers | ||
.filter((s) => s !== undefined) | ||
.forEach((kp) => { | ||
tx.partialSign(kp); | ||
}); | ||
return tx; | ||
}); | ||
const signedTxs = yield this.wallet.signAllTransactions(txs); | ||
const sigs = []; | ||
for (let k = 0; k < txs.length; k += 1) { | ||
const tx = signedTxs[k]; | ||
invariant(tx, "tx missing"); | ||
const rawTx = tx.serialize(); | ||
sigs.push(yield sendAndConfirmRawTransaction(this.connection, rawTx, opts)); | ||
} | ||
return sigs; | ||
}); | ||
} | ||
}; | ||
/** | ||
@@ -104,43 +137,68 @@ * Simulates the given transaction, returning emitted logs from execution. | ||
*/ | ||
simulate(tx, signers, opts) { | ||
SolanaProvider.prototype.simulate = function (tx, signers, opts) { | ||
var _a, _b, _c; | ||
return __awaiter(this, void 0, void 0, function* () { | ||
if (signers === undefined) { | ||
signers = []; | ||
} | ||
if (opts === undefined) { | ||
opts = this.opts; | ||
} | ||
tx.feePayer = this.wallet.publicKey; | ||
tx.recentBlockhash = (yield this.connection.getRecentBlockhash((_a = opts.preflightCommitment) !== null && _a !== void 0 ? _a : this.opts.preflightCommitment)).blockhash; | ||
yield this.wallet.signTransaction(tx); | ||
signers | ||
.filter((s) => s !== undefined) | ||
.forEach((kp) => { | ||
tx.partialSign(kp); | ||
return __awaiter(this, void 0, void 0, function () { | ||
var _d; | ||
return __generator(this, function (_e) { | ||
switch (_e.label) { | ||
case 0: | ||
if (signers === undefined) { | ||
signers = []; | ||
} | ||
if (opts === undefined) { | ||
opts = this.opts; | ||
} | ||
tx.feePayer = this.wallet.publicKey; | ||
_d = tx; | ||
return [4 /*yield*/, this.connection.getRecentBlockhash((_a = opts.preflightCommitment) !== null && _a !== void 0 ? _a : this.opts.preflightCommitment)]; | ||
case 1: | ||
_d.recentBlockhash = (_e.sent()).blockhash; | ||
return [4 /*yield*/, this.wallet.signTransaction(tx)]; | ||
case 2: | ||
_e.sent(); | ||
signers | ||
.filter(function (s) { return s !== undefined; }) | ||
.forEach(function (kp) { | ||
tx.partialSign(kp); | ||
}); | ||
return [4 /*yield*/, simulateTransaction(this.connection, tx, (_c = (_b = opts.commitment) !== null && _b !== void 0 ? _b : this.opts.commitment) !== null && _c !== void 0 ? _c : "recent")]; | ||
case 3: return [2 /*return*/, _e.sent()]; | ||
} | ||
}); | ||
return yield simulateTransaction(this.connection, tx, (_c = (_b = opts.commitment) !== null && _b !== void 0 ? _b : this.opts.commitment) !== null && _c !== void 0 ? _c : "recent"); | ||
}); | ||
} | ||
} | ||
}; | ||
return SolanaProvider; | ||
}()); | ||
export default SolanaProvider; | ||
// Copy of Connection.simulateTransaction that takes a commitment parameter. | ||
function simulateTransaction(connection, transaction, commitment) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
const connectionInner = connection; | ||
const transactionTyped = transaction; | ||
transaction.recentBlockhash = yield connectionInner._recentBlockhash(connectionInner._disableBlockhashCaching); | ||
const signData = transaction.serializeMessage(); | ||
const wireTransaction = transactionTyped._serialize(signData); | ||
const encodedTransaction = wireTransaction.toString("base64"); | ||
const config = { encoding: "base64", commitment }; | ||
const res = yield connectionInner._rpcRequest("simulateTransaction", [ | ||
encodedTransaction, | ||
config, | ||
]); | ||
if (res.error) { | ||
throw new Error("failed to simulate transaction: " + res.error.message); | ||
} | ||
return res.result; | ||
return __awaiter(this, void 0, void 0, function () { | ||
var connectionInner, transactionTyped, _a, signData, wireTransaction, encodedTransaction, config, res; | ||
return __generator(this, function (_b) { | ||
switch (_b.label) { | ||
case 0: | ||
connectionInner = connection; | ||
transactionTyped = transaction; | ||
_a = transaction; | ||
return [4 /*yield*/, connectionInner._recentBlockhash(connectionInner._disableBlockhashCaching)]; | ||
case 1: | ||
_a.recentBlockhash = _b.sent(); | ||
signData = transaction.serializeMessage(); | ||
wireTransaction = transactionTyped._serialize(signData); | ||
encodedTransaction = wireTransaction.toString("base64"); | ||
config = { encoding: "base64", commitment: commitment }; | ||
return [4 /*yield*/, connectionInner._rpcRequest("simulateTransaction", [ | ||
encodedTransaction, | ||
config, | ||
])]; | ||
case 2: | ||
res = _b.sent(); | ||
if (res.error) { | ||
throw new Error("failed to simulate transaction: " + res.error.message); | ||
} | ||
return [2 /*return*/, res.result]; | ||
} | ||
}); | ||
}); | ||
} | ||
//# sourceMappingURL=provider.js.map |
@@ -1,2 +0,2 @@ | ||
import { __awaiter } from "tslib"; | ||
import { __awaiter, __generator } from "tslib"; | ||
import { Transaction, } from "@solana/web3.js"; | ||
@@ -8,4 +8,5 @@ import promiseRetry from "promise-retry"; | ||
*/ | ||
export class TransactionEnvelope { | ||
constructor(provider, instructions, signers = []) { | ||
var TransactionEnvelope = /** @class */ (function () { | ||
function TransactionEnvelope(provider, instructions, signers) { | ||
if (signers === void 0) { signers = []; } | ||
this.provider = provider; | ||
@@ -15,15 +16,21 @@ this.instructions = instructions; | ||
} | ||
addSigners(...signers) { | ||
this.signers.push(...signers); | ||
TransactionEnvelope.prototype.addSigners = function () { | ||
var _a; | ||
var signers = []; | ||
for (var _i = 0; _i < arguments.length; _i++) { | ||
signers[_i] = arguments[_i]; | ||
} | ||
(_a = this.signers).push.apply(_a, signers); | ||
return this; | ||
} | ||
}; | ||
/** | ||
* Builds a transaction from this envelope. | ||
*/ | ||
build() { | ||
return new Transaction().add(...this.instructions); | ||
} | ||
simulate(opts) { | ||
TransactionEnvelope.prototype.build = function () { | ||
var _a; | ||
return (_a = new Transaction()).add.apply(_a, this.instructions); | ||
}; | ||
TransactionEnvelope.prototype.simulate = function (opts) { | ||
return this.provider.simulate(this.build(), this.signers, opts); | ||
} | ||
}; | ||
/** | ||
@@ -34,14 +41,23 @@ * Sends the transaction. | ||
*/ | ||
send(opts) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
const sig = yield this.provider.send(this.build(), this.signers, opts); | ||
return new PendingTransaction(this.provider, sig); | ||
TransactionEnvelope.prototype.send = function (opts) { | ||
return __awaiter(this, void 0, void 0, function () { | ||
var sig; | ||
return __generator(this, function (_a) { | ||
switch (_a.label) { | ||
case 0: return [4 /*yield*/, this.provider.send(this.build(), this.signers, opts)]; | ||
case 1: | ||
sig = _a.sent(); | ||
return [2 /*return*/, new PendingTransaction(this.provider, sig)]; | ||
} | ||
}); | ||
}); | ||
} | ||
} | ||
}; | ||
return TransactionEnvelope; | ||
}()); | ||
export { TransactionEnvelope }; | ||
/** | ||
* Transaction which may or may not be confirmed. | ||
*/ | ||
export class PendingTransaction { | ||
constructor(provider, signature) { | ||
var PendingTransaction = /** @class */ (function () { | ||
function PendingTransaction(provider, signature) { | ||
this.provider = provider; | ||
@@ -55,27 +71,45 @@ this.signature = signature; | ||
*/ | ||
wait() { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
if (this.receipt) { | ||
return this.receipt; | ||
} | ||
const receipt = yield promiseRetry((retry) => __awaiter(this, void 0, void 0, function* () { | ||
const result = yield this.provider.connection.getTransaction(this.signature, { | ||
commitment: "confirmed", | ||
}); | ||
if (!result) { | ||
retry(new Error("error")); | ||
return; | ||
PendingTransaction.prototype.wait = function () { | ||
return __awaiter(this, void 0, void 0, function () { | ||
var receipt; | ||
var _this = this; | ||
return __generator(this, function (_a) { | ||
switch (_a.label) { | ||
case 0: | ||
if (this.receipt) { | ||
return [2 /*return*/, this.receipt]; | ||
} | ||
return [4 /*yield*/, promiseRetry(function (retry) { return __awaiter(_this, void 0, void 0, function () { | ||
var result; | ||
return __generator(this, function (_a) { | ||
switch (_a.label) { | ||
case 0: return [4 /*yield*/, this.provider.connection.getTransaction(this.signature, { | ||
commitment: "confirmed", | ||
})]; | ||
case 1: | ||
result = _a.sent(); | ||
if (!result) { | ||
retry(new Error("error")); | ||
return [2 /*return*/]; | ||
} | ||
return [2 /*return*/, new TransactionReceipt(this.provider, this.signature, result)]; | ||
} | ||
}); | ||
}); }, { retries: 5 })]; | ||
case 1: | ||
receipt = _a.sent(); | ||
invariant(receipt, "transaction could not be confirmed"); | ||
return [2 /*return*/, receipt]; | ||
} | ||
return new TransactionReceipt(this.provider, this.signature, result); | ||
}), { retries: 5 }); | ||
invariant(receipt, "transaction could not be confirmed"); | ||
return receipt; | ||
}); | ||
}); | ||
} | ||
} | ||
}; | ||
return PendingTransaction; | ||
}()); | ||
export { PendingTransaction }; | ||
/** | ||
* A transaction that has been processed by the cluster. | ||
*/ | ||
export class TransactionReceipt { | ||
constructor( | ||
var TransactionReceipt = /** @class */ (function () { | ||
function TransactionReceipt( | ||
/** | ||
@@ -100,5 +134,5 @@ * Current provider. | ||
*/ | ||
getEvents(eventParser) { | ||
TransactionReceipt.prototype.getEvents = function (eventParser) { | ||
var _a; | ||
const logs = (_a = this.response.meta) === null || _a === void 0 ? void 0 : _a.logMessages; | ||
var logs = (_a = this.response.meta) === null || _a === void 0 ? void 0 : _a.logMessages; | ||
if (logs && logs.length > 0) { | ||
@@ -108,25 +142,31 @@ return eventParser(logs); | ||
return []; | ||
} | ||
}; | ||
/** | ||
* Prints the logs associated with this transaction. | ||
*/ | ||
printLogs() { | ||
TransactionReceipt.prototype.printLogs = function () { | ||
var _a, _b; | ||
console.log((_b = (_a = this.response.meta) === null || _a === void 0 ? void 0 : _a.logMessages) === null || _b === void 0 ? void 0 : _b.join("\n")); | ||
} | ||
/** | ||
* Gets the compute units used by the transaction. | ||
* @returns | ||
*/ | ||
get computeUnits() { | ||
var _a; | ||
const logs = (_a = this.response.meta) === null || _a === void 0 ? void 0 : _a.logMessages; | ||
invariant(logs, "no logs"); | ||
const consumeLog = logs[logs.length - 2]; | ||
invariant(consumeLog, "no consume log"); | ||
const amtStr = consumeLog.split(" ")[3]; | ||
invariant(amtStr, "no amount"); | ||
return parseInt(amtStr); | ||
} | ||
} | ||
}; | ||
Object.defineProperty(TransactionReceipt.prototype, "computeUnits", { | ||
/** | ||
* Gets the compute units used by the transaction. | ||
* @returns | ||
*/ | ||
get: function () { | ||
var _a; | ||
var logs = (_a = this.response.meta) === null || _a === void 0 ? void 0 : _a.logMessages; | ||
invariant(logs, "no logs"); | ||
var consumeLog = logs[logs.length - 2]; | ||
invariant(consumeLog, "no consume log"); | ||
var amtStr = consumeLog.split(" ")[3]; | ||
invariant(amtStr, "no amount"); | ||
return parseInt(amtStr); | ||
}, | ||
enumerable: false, | ||
configurable: true | ||
}); | ||
return TransactionReceipt; | ||
}()); | ||
export { TransactionReceipt }; | ||
//# sourceMappingURL=transaction.js.map |
{ | ||
"name": "@saberhq/solana", | ||
"version": "0.1.0-beta.11", | ||
"version": "0.1.1-beta.1", | ||
"description": "Common types and libraries for Solana", | ||
@@ -33,3 +33,3 @@ "author": "Ian Macalinao <ian@saber.so>", | ||
}, | ||
"gitHead": "aa5e1b13ee151070bed4a86d69cab6df920da87b", | ||
"gitHead": "7341e6079710ca881cef8225173920223ab0c265", | ||
"publishConfig": { | ||
@@ -36,0 +36,0 @@ "access": "public" |
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
55842
1041