@xcapit/shelter-sdk
Version:
SDK for Shelter smart contracts on Stellar
35 lines (34 loc) • 1.1 kB
JavaScript
import { rpc } from "@stellar/stellar-sdk";
import { TxResult } from "../tx-result/tx-result";
export class Transaction {
_rawTx;
_signer;
_rpc;
_errorMsg;
_simulate;
constructor(_rawTx, _signer, _rpc, _errorMsg = 'Shelter SDK Transaction Error', _simulate = false) {
this._rawTx = _rawTx;
this._signer = _signer;
this._rpc = _rpc;
this._errorMsg = _errorMsg;
this._simulate = _simulate;
}
async result() {
const tx = await this._txBuilt();
tx.sign(this._signer);
return new TxResult(await this._txData(tx), this._errorMsg).validatedData();
}
async _txBuilt() {
return this._simulate ?
await this._simulatedTx(this._rawTx.built) :
this._rawTx.built;
}
async _simulatedTx(tx) {
return this._rpc.assembleTransaction(tx, await this._rpc.server().simulateTransaction(tx)).build();
}
async _txData(tx) {
return await this._rpc
.server()
.pollTransaction((await this._rpc.server().sendTransaction(tx)).hash);
}
}