-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.test.ts
172 lines (155 loc) · 5.95 KB
/
index.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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
import { unstable_dev } from 'wrangler';
import type { UnstableDevWorker } from 'wrangler';
import { describe, expect, it, beforeAll, afterAll } from 'vitest';
import { AptosAccount, AptosClient, FaucetClient, HexString, TransactionBuilderEd25519 } from 'aptos';
import { find } from 'lodash';
describe('Worker', () => {
let worker: UnstableDevWorker;
beforeAll(async () => {
worker = await unstable_dev('src/index.ts', {
experimental: { disableExperimentalWarning: true },
});
});
afterAll(async () => {
await worker.stop();
});
it('should return error', async () => {
let resp = await worker.fetch();
expect(resp.status).toBe(404);
resp = await worker.fetch('/foo', {
method: 'POST',
});
expect(resp.status).toBe(404);
resp = await worker.fetch('', {
method: 'POST',
});
let res = await resp.json();
expect(res).toMatchObject({ jsonrpc: '2.0', error: { code: -32700, message: 'Parse error' } });
});
it('should return data', async () => {
// general
let resp = await worker.fetch('', {
method: 'POST',
body: JSON.stringify({
method: 'get_ledger_info',
params: [],
}),
});
let res: any = await resp.json();
expect(!!res.result).toBeTruthy();
resp = await worker.fetch('', {
method: 'POST',
body: JSON.stringify({
method: 'healthy',
params: [],
}),
});
res = await resp.json();
expect(!!res.result).toBeTruthy();
// accounts
const address = '0x000000616a48469384a03540e9b391d9753c1e2cde382d058a58975585fca596';
resp = await worker.fetch('', {
method: 'POST',
body: JSON.stringify({
method: 'getAccount',
params: [address],
}),
});
res = await resp.json();
expect(!!res.result).toBeTruthy();
resp = await worker.fetch('', {
method: 'POST',
body: JSON.stringify({
method: 'apt_getAccountResources',
params: [address, {}, '764000'],
}),
});
res = await resp.json();
expect(!!res.error).toBeTruthy();
resp = await worker.fetch('', {
method: 'POST',
body: JSON.stringify({
method: 'apt_getAccountResources',
params: [address, { limit: 1 }],
}),
});
res = await resp.json();
expect(!!res.result).toBeTruthy();
// transactions
const nodeUrl = 'https://fullnode.devnet.aptoslabs.com';
const faucetUrl = 'https://faucet.devnet.aptoslabs.com';
const client = new AptosClient(nodeUrl);
const faucetClient = new FaucetClient(nodeUrl, faucetUrl);
const account = new AptosAccount();
await faucetClient.fundAccount(account.address().hex(), 100_000_000);
resp = await worker.fetch('', {
method: 'POST',
body: JSON.stringify({
method: 'apt_getAccountResources',
params: [account.address().hex()],
}),
});
res = await resp.json();
expect(!!res.result).toBeTruthy();
expect(find(res.result, { type: '0x1::coin::CoinStore<0x1::aptos_coin::AptosCoin>' }).data.coin.value).toBe('100000000');
const payload = {
function: '0x1::coin::transfer',
type_arguments: ['0x1::aptos_coin::AptosCoin'],
arguments: [address, '88888888'],
};
let txn = await client.generateTransaction(account.address().hex(), payload);
const signature = account.signBuffer(TransactionBuilderEd25519.getSigningMessage(txn));
// submit json format transaction
resp = await worker.fetch('', {
method: 'POST',
body: JSON.stringify({
method: 'submit_transaction',
params: [
{
sender: account.address().hex(),
sequence_number: txn.sequence_number.toString(),
max_gas_amount: txn.max_gas_amount.toString(),
gas_unit_price: txn.gas_unit_price.toString(),
expiration_timestamp_secs: txn.expiration_timestamp_secs.toString(),
payload: {
type: 'entry_function_payload',
...payload,
},
signature: {
type: 'ed25519_signature',
public_key: account.pubKey().hex(),
signature: signature.hex(),
},
},
],
}),
});
res = await resp.json();
expect(!!res.result).toBeTruthy();
// submit bcs format transaction
await faucetClient.fundAccount(account.address().hex(), 100_000_000);
txn = await client.generateTransaction(account.address().hex(), payload);
const signedTxn = await AptosClient.generateBCSTransaction(account, txn);
resp = await worker.fetch('', {
method: 'POST',
body: JSON.stringify({
method: 'submit_transaction',
params: [HexString.fromBuffer(signedTxn).hex()],
}),
});
res = await resp.json();
expect(!!res.result).toBeTruthy();
const hash = res.result.hash;
await new Promise((r) => setTimeout(r, 3000));
resp = await worker.fetch('', {
method: 'POST',
body: JSON.stringify({
method: 'get_transaction_by_hash',
params: [hash],
}),
});
res = await resp.json();
expect(!!res.result).toBeTruthy();
console.log(res);
}, 60000);
});