-
Notifications
You must be signed in to change notification settings - Fork 106
/
coin-inst.spec.ts
72 lines (60 loc) · 2.7 KB
/
coin-inst.spec.ts
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
import Long from "long";
import { Log } from "../../src";
import { CoinContract, CoinInst } from "../../src/v2/byzcoin/contracts";
import { BCTest } from "../helpers/bctest";
import { SIGNER } from "../support/conondes";
import { HistoryObs } from "../support/historyObs";
describe("CoinInst should", () => {
const name = Buffer.alloc(32);
beforeAll(async () => {
name.write("coinName");
});
it("retrieve an instance from byzcoin", async () => {
const {genesisInst, tx, rpc} = await BCTest.singleton();
const coinID = genesisInst.spawnCoin(tx, name);
await tx.send([[SIGNER]], 10);
const ci = await CoinInst.retrieve(rpc, coinID);
expect(ci.getValue().name.equals(name)).toBeTruthy();
});
it("mint some coins", async () => {
const {genesisInst, tx, rpc} = await BCTest.singleton();
const coinID = genesisInst.spawnCoin(tx, name);
await tx.send([[SIGNER]], 10);
const ci = await CoinInst.retrieve(rpc, coinID);
const h = new HistoryObs();
ci.subscribe((c) => h.push(c.value.toString()));
await h.resolve("0");
ci.mint(tx, Long.fromNumber(1e6));
await tx.send([[SIGNER]], 10);
await h.resolve(1e6.toString());
});
it("transfer coins", async () => {
const {genesisInst, tx, rpc} = await BCTest.singleton();
Log.lvl2("Spawning 2 coins");
const sourceID = genesisInst.spawnCoin(tx, name);
const targetID = genesisInst.spawnCoin(tx, name);
CoinContract.mint(tx, sourceID, Long.fromNumber(1e6));
CoinContract.transfer(tx, sourceID, targetID, Long.fromNumber(1e5));
await tx.send([[SIGNER]], 10);
Log.lvl2("Getting coins and values");
const target = await CoinInst.retrieve(rpc, targetID);
const hTarget = new HistoryObs();
target.subscribe((ci) => hTarget.push(ci.value.toString()));
await hTarget.resolve(1e5.toString());
Log.lvl2("Transferring some coins from source to target");
const source = await CoinInst.retrieve(rpc, sourceID);
const hSource = new HistoryObs();
source.subscribe((ci) => hSource.push(ci.value.toString()));
source.mint(tx, Long.fromNumber(1e6));
source.transfer(tx, targetID, Long.fromNumber(2e5));
await tx.send([[SIGNER]], 10);
await hSource.resolve(9e5.toString(), 17e5.toString());
await hTarget.resolve(3e5.toString());
Log.lvl2("Using fetch and store for transfer");
source.fetch(tx, Long.fromNumber(3e5));
target.store(tx);
await tx.send([[SIGNER]], 10);
await hSource.resolve(14e5.toString());
await hTarget.resolve(6e5.toString());
}, 600000);
});