UNPKG

@mysten/sui

Version:

Sui TypeScript API(Work in Progress)

139 lines (138 loc) 5.12 kB
var __accessCheck = (obj, member, msg) => { if (!member.has(obj)) throw TypeError("Cannot " + msg); }; var __privateGet = (obj, member, getter) => { __accessCheck(obj, member, "read from private field"); return getter ? getter.call(obj) : member.get(obj); }; var __privateAdd = (obj, member, value) => { if (member.has(obj)) throw TypeError("Cannot add the same private member more than once"); member instanceof WeakSet ? member.add(obj) : member.set(obj, value); }; var __privateSet = (obj, member, value, setter) => { __accessCheck(obj, member, "write to private field"); setter ? setter.call(obj, value) : member.set(obj, value); return value; }; var __privateMethod = (obj, member, method) => { __accessCheck(obj, member, "access private method"); return method; }; var _queue, _signer, _cache, _client, _lastDigest, _cacheGasCoin, _buildTransaction, _waitForLastTransaction, waitForLastTransaction_fn; import { toB64 } from "@mysten/bcs"; import { bcs } from "../../bcs/index.js"; import { isTransaction, Transaction } from "../Transaction.js"; import { CachingTransactionExecutor } from "./caching.js"; import { SerialQueue } from "./queue.js"; class SerialTransactionExecutor { constructor({ signer, ...options }) { __privateAdd(this, _waitForLastTransaction); __privateAdd(this, _queue, new SerialQueue()); __privateAdd(this, _signer, void 0); __privateAdd(this, _cache, void 0); __privateAdd(this, _client, void 0); __privateAdd(this, _lastDigest, null); __privateAdd(this, _cacheGasCoin, async (effects) => { if (!effects.V2) { return; } const gasCoin = getGasCoinFromEffects(effects).ref; if (gasCoin) { __privateGet(this, _cache).cache.setCustom("gasCoin", gasCoin); } else { __privateGet(this, _cache).cache.deleteCustom("gasCoin"); } }); __privateAdd(this, _buildTransaction, async (transaction) => { const gasCoin = await __privateGet(this, _cache).cache.getCustom("gasCoin"); const copy = Transaction.from(transaction); if (gasCoin) { copy.setGasPayment([gasCoin]); } copy.setSenderIfNotSet(__privateGet(this, _signer).toSuiAddress()); return __privateGet(this, _cache).buildTransaction({ transaction: copy }); }); __privateSet(this, _signer, signer); __privateSet(this, _client, options.client); __privateSet(this, _cache, new CachingTransactionExecutor({ client: options.client, cache: options.cache })); } async applyEffects(effects) { return Promise.all([__privateGet(this, _cacheGasCoin).call(this, effects), __privateGet(this, _cache).cache.applyEffects(effects)]); } async buildTransaction(transaction) { return __privateGet(this, _queue).runTask(() => __privateGet(this, _buildTransaction).call(this, transaction)); } resetCache() { return Promise.all([__privateGet(this, _cache).reset(), __privateMethod(this, _waitForLastTransaction, waitForLastTransaction_fn).call(this)]); } executeTransaction(transaction) { return __privateGet(this, _queue).runTask(async () => { const bytes = isTransaction(transaction) ? await __privateGet(this, _buildTransaction).call(this, transaction) : transaction; const { signature } = await __privateGet(this, _signer).signTransaction(bytes); const results = await __privateGet(this, _cache).executeTransaction({ signature, transaction: bytes }).catch(async (error) => { await this.resetCache(); throw error; }); const effectsBytes = Uint8Array.from(results.rawEffects); const effects = bcs.TransactionEffects.parse(effectsBytes); await this.applyEffects(effects); __privateSet(this, _lastDigest, results.digest); return { digest: results.digest, effects: toB64(effectsBytes) }; }); } } _queue = new WeakMap(); _signer = new WeakMap(); _cache = new WeakMap(); _client = new WeakMap(); _lastDigest = new WeakMap(); _cacheGasCoin = new WeakMap(); _buildTransaction = new WeakMap(); _waitForLastTransaction = new WeakSet(); waitForLastTransaction_fn = async function() { if (__privateGet(this, _lastDigest)) { await __privateGet(this, _client).waitForTransaction({ digest: __privateGet(this, _lastDigest) }); __privateSet(this, _lastDigest, null); } }; function getGasCoinFromEffects(effects) { if (!effects.V2) { throw new Error("Unexpected effects version"); } const gasObjectChange = effects.V2.changedObjects[effects.V2.gasObjectIndex]; if (!gasObjectChange) { throw new Error("Gas object not found in effects"); } const [objectId, { outputState }] = gasObjectChange; if (!outputState.ObjectWrite) { throw new Error("Unexpected gas object state"); } const [digest, owner] = outputState.ObjectWrite; return { ref: { objectId, digest, version: effects.V2.lamportVersion }, owner: owner.AddressOwner || owner.ObjectOwner }; } export { SerialTransactionExecutor, getGasCoinFromEffects }; //# sourceMappingURL=serial.js.map