Repository navigation
Expand file tree
/
Copy pathagent.js
More file actions
100 lines (78 loc) · 2.21 KB
/
Copy pathagent.js
File metadata and controls
100 lines (78 loc) · 2.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
import { BalanceClient } from '@galacticcouncil/sdk';
import { Keyring } from '@polkadot/keyring';
import assert from 'node:assert';
import fs from 'fs';
import Big from 'big.js';
import { toDecimal } from './utils.js';
const ZERO = new Big("0.0");
export class Agent {
#api
#signer
#assets
#registry
constructor(api, assets, assetRegistry, signer) {
this.#api = new BalanceClient(api);
this.#registry = assetRegistry;
this.#signer = signer;
this.#assets = {};
assets.forEach(a => {
this.#assets[a] = new Big(0);
});
}
get address() {
return this.#signer.address;
}
get signer() {
return this.#signer;
}
sub(assetId, amount) {
let balanceNow = this.balanceInt(assetId);
assert.ok(balanceNow.gte(amount), `balance too low, balance_now=${balanceNow}, amount=${amount}`);
this.#assets[assetId] = balanceNow.minus(amount);
}
async updateBalances() {
const rawBalances = [];
Object.keys(this.#assets).forEach(key => {
rawBalances.push(this.#api.getBalance(this.address, key));
});
const res = await Promise.allSettled(rawBalances);
let i = 0;
Object.keys(this.#assets).forEach(key => {
if (res[i].status == "fulfilled") {
this.#assets[key] = res[i].value;
} else {
console.log(`ERROR: to pull balance, assetId=${key}, reason=${res[i].reason}`);
this.#assets[key] = new Big(0);
}
i++
})
}
balanceInt(assetId) {
const b = this.#assets[assetId];
assert.ok(b, `no balance found for asset=${assetId}`);
return b;
}
balanceDec(assetId) {
const b = this.balanceInt(assetId);
const dec = this.#registry.decimals(assetId);
return toDecimal(b, dec);
}
log() {
let balances = "";
for (const [k, v] of Object.entries(this.#assets)) {
balances += `, ${this.#registry.symbol(k)}: assetId=${k}, balance=${toDecimal(v, this.#registry.decimals(k)).toString()}`;
}
console.log(`AGENT: address=${this.address}${balances}`);
}
}
export function loadSigner(path, password) {
try {
const keyring = new Keyring( { type: 'sr25519'});
const p = JSON.parse(fs.readFileSync(path));
const pair = keyring.addFromJson(p);
pair.decodePkcs8(password);
return pair;
} catch (err) {
throw new Error(`Failed to load signer from ${path}: ${err.message}`);
}
}