swapnet-sdk-test-4
Version:
TypeScript SDK for SwapNet API.
42 lines (41 loc) • 1.24 kB
JavaScript
import { mergeDeep } from "./utils.js";
export class LedgerState {
static from(blockTag) {
return new LedgerState(blockTag);
}
static original = new LedgerState(undefined);
_blockTag;
_asIfs = [];
constructor(blockTag){
this._blockTag = blockTag;
}
asif(asIf) {
this._asIfs.push(asIf);
return this;
}
async getStateAsync() {
if (this._blockTag === undefined) {
throw new Error(`Failed to call LedgerState.state() as blockTag is not set!`);
}
const override = await this.getDiffAsync();
return [
this._blockTag,
override
];
}
blockTag() {
if (this._blockTag === undefined) {
throw new Error(`Failed to call LedgerState.blockTag() as blockTag is not set!`);
}
return this._blockTag;
}
async getDiffAsync() {
const asIfValues = await Promise.all(this._asIfs.map((asIf)=>asIf.valueAsync()));
let override = {};
asIfValues.forEach((asIfValue)=>{
// console.log(`asIfValue: ${JSON.stringify(asIfValue)}`);
override = mergeDeep(override, asIfValue);
});
return override;
}
}