forked from mechanismHQ/bns-x
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathchainhooks-test.ts
145 lines (127 loc) · 3.14 KB
/
chainhooks-test.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
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
import { assertArrayIncludes } from "https://deno.land/[email protected]/testing/asserts.ts";
import { registerNameV1 } from "../tests/bns-helpers.ts";
import {
deployWithNamespace,
describe,
it,
contracts,
accounts,
testUtils,
alice,
utilsRegisterBtc,
assertEquals,
deployer,
signatureVrsToRsv,
registry,
bob,
} from "../tests/helpers.ts";
import { alicePubkeyHash, btcBytes, signedContracts } from "../tests/mocks.ts";
import { Account, Name, Wrapper } from "../tests/prisma-types.ts";
const server = "http://localhost:3003";
async function apiFetch(path: string) {
const res = await fetch(`${server}${path}`);
return res.json();
}
async function fetchAll() {
const data = await apiFetch(`/all`);
console.log(data);
return data as {
users: (Account & {
names: Name[];
primaryName: Name;
})[];
};
}
async function fetchName(name: string, namespace: string) {
const data = await apiFetch(`/names/${name}/${namespace}`);
return data as {
name: Name & {
owner: Account;
primaryOwner?: Account;
wrapper: Wrapper;
};
};
}
async function fetchAccount(account: string) {
const data = await apiFetch(`/accounts/${account}`);
return data as {
account: Account & {
names: Name[];
primaryName: Name;
};
};
}
function sleep(secs: number) {
return new Promise((resolve, reject) => {
setTimeout(() => {
resolve(true);
}, secs * 1000);
});
}
describe("chainhook tests", () => {
const { chain } = deployWithNamespace();
const migrator = contracts.wrapperMigrator;
it("registers a name", () => {
utilsRegisterBtc({
name: "alice",
owner: alice,
chain,
});
});
it("is saved in db", async () => {
const { users } = await fetchAll();
const [user] = users;
assertEquals(user.principal, alice);
assertEquals(user.names[0].nftId, 0);
console.log(users);
});
it("works with wrapper migrations", async () => {
chain.txOk(
migrator.setSigners([
{
signer: alicePubkeyHash,
enabled: true,
},
]),
deployer
);
const wrapper = signedContracts[0];
registerNameV1({
chain,
owner: alice,
name: "alice2",
});
chain.txOk(
migrator.migrate({
recipient: alice,
wrapper: wrapper.id,
signature: signatureVrsToRsv(wrapper.signature),
}),
alice
);
const { account } = await fetchAccount(alice);
assertEquals(account.names.length, 2);
const ids = account.names.map((n) => n.nftId);
assertArrayIncludes(ids, [1, 0]);
assertEquals(account.primaryNameId, 0);
await sleep(5);
const { name } = await fetchName("alice2", "btc");
console.log("name", name);
assertEquals(name.nftId, 1);
assertEquals(name.primaryOwner, null);
assertEquals(name.wrapper.principal, wrapper.id);
});
it("works with transfers", async () => {
chain.txOk(
registry.transfer({
sender: alice,
recipient: bob,
id: 1n,
}),
alice
);
await sleep(3);
const { account } = await fetchAccount(bob);
assertEquals(account.names, [1]);
});
});