-
Notifications
You must be signed in to change notification settings - Fork 45
/
Copy pathrust-deploy-write-read.test.ts
270 lines (221 loc) · 9.05 KB
/
rust-deploy-write-read.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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
import fs from 'fs';
import ArLocal from 'arlocal';
import Arweave from 'arweave';
import { JWKInterface } from 'arweave/node/lib/wallet';
import path from 'path';
import { mineBlock } from '../_helpers';
import { WasmSrc } from '../../../core/modules/impl/wasm/WasmSrc';
import { PstState, PstContract } from '../../../contract/PstContract';
import { Warp } from '../../../core/Warp';
import { WarpFactory } from '../../../core/WarpFactory';
import { LoggerFactory } from '../../../logging/LoggerFactory';
import { ArweaveWrapper } from '../../../utils/ArweaveWrapper';
import { TagsParser } from '../../../core/modules/impl/TagsParser';
import { DeployPlugin } from 'warp-contracts-plugin-deploy';
import { SMART_WEAVE_TAGS, WARP_TAGS } from '../../../core/KnownTags';
import { InteractionResult } from '../../../core/modules/impl/HandlerExecutorFactory';
describe('Testing the Rust WASM Profit Sharing Token', () => {
let wallet: JWKInterface;
let walletAddress: string;
let initialState: PstState;
let arweave: Arweave;
let arlocal: ArLocal;
let warp: Warp;
let pst: PstContract;
let contractTxId: string;
let properForeignContractTxId: string;
let wrongForeignContractTxId: string;
let arweaveWrapper;
let tagsParser: TagsParser;
beforeAll(async () => {
// note: each tests suit (i.e. file with tests that Jest is running concurrently
// with another files has to have ArLocal set to a different port!)
arlocal = new ArLocal(1201, false);
await arlocal.start();
LoggerFactory.INST.logLevel('error');
tagsParser = new TagsParser();
warp = WarpFactory.forLocal(1201).use(new DeployPlugin());
({ arweave } = warp);
arweaveWrapper = new ArweaveWrapper(warp);
({ jwk: wallet } = await warp.generateWallet());
walletAddress = await arweave.wallets.jwkToAddress(wallet);
const contractSrc = fs.readFileSync(path.join(__dirname, '../data/wasm/rust/rust-pst_bg.wasm'));
const stateFromFile: PstState = JSON.parse(fs.readFileSync(path.join(__dirname, '../data/token-pst.json'), 'utf8'));
initialState = {
...stateFromFile,
...{
owner: walletAddress,
balances: {
...stateFromFile.balances,
[walletAddress]: 555669
}
}
};
// deploying contract using the new SDK.
({ contractTxId } = await warp.deploy({
wallet,
initState: JSON.stringify(initialState),
src: contractSrc,
wasmSrcCodeDir: path.join(__dirname, '../data/wasm/rust/src'),
wasmGlueCode: path.join(__dirname, '../data/wasm/rust/rust-pst.js')
}));
({ contractTxId: properForeignContractTxId } = await warp.deploy({
wallet,
initState: JSON.stringify({
...initialState,
...{
ticker: 'FOREIGN_PST',
name: 'foreign contract'
}
}),
src: contractSrc,
wasmSrcCodeDir: path.join(__dirname, '../data/wasm/rust/src'),
wasmGlueCode: path.join(__dirname, '../data/wasm/rust/rust-pst.js')
}));
({ contractTxId: wrongForeignContractTxId } = await warp.deploy({
wallet,
initState: JSON.stringify({
...initialState,
...{
ticker: 'FOREIGN_PST_2',
name: 'foreign contract 2'
}
}),
src: contractSrc,
wasmSrcCodeDir: path.join(__dirname, '../data/wasm/rust/src'),
wasmGlueCode: path.join(__dirname, '../data/wasm/rust/rust-pst.js')
}));
// connecting to the PST contract
pst = warp.pst(contractTxId);
// connecting wallet to the PST contract
pst.connect(wallet);
await mineBlock(warp);
}, 50000);
afterAll(async () => {
await arlocal.stop();
});
it('should properly deploy contract', async () => {
const contractTx = await arweave.transactions.get(contractTxId);
expect(contractTx).not.toBeNull();
const contractSrcTxId = tagsParser.getTag(contractTx, SMART_WEAVE_TAGS.CONTRACT_SRC_TX_ID);
const contractSrcTx = await arweave.transactions.get(contractSrcTxId);
expect(tagsParser.getTag(contractSrcTx, SMART_WEAVE_TAGS.CONTENT_TYPE)).toEqual('application/wasm');
expect(tagsParser.getTag(contractSrcTx, WARP_TAGS.WASM_LANG)).toEqual('rust');
expect(tagsParser.getTag(contractSrcTx, WARP_TAGS.WASM_META)).toEqual(JSON.stringify({ dtor: 74 }));
const srcTxData = await arweaveWrapper.txData(contractSrcTxId);
const wasmSrc = new WasmSrc(srcTxData);
expect(wasmSrc.wasmBinary()).not.toBeNull();
expect(wasmSrc.additionalCode()).toEqual(
fs.readFileSync(path.join(__dirname, '../data/wasm/rust/rust-pst.js'), 'utf-8')
);
expect((await wasmSrc.sourceCode()).size).toEqual(11);
});
it('should read pst state and balance data', async () => {
expect(await pst.currentState()).toEqual(initialState);
expect(await pst.currentBalance('uhE-QeYS8i4pmUtnxQyHD7dzXFNaJ9oMK-IM-QPNY6M')).toEqual(10000000);
expect(await pst.currentBalance('33F0QHcb22W7LwWR1iRC8Az1ntZG09XQ03YWuw2ABqA')).toEqual(23111222);
expect(await pst.currentBalance(walletAddress)).toEqual(555669);
});
it('should properly transfer tokens', async () => {
await pst.transfer({
target: 'uhE-QeYS8i4pmUtnxQyHD7dzXFNaJ9oMK-IM-QPNY6M',
qty: 555
});
await mineBlock(warp);
expect((await pst.currentState()).balances[walletAddress]).toEqual(555669 - 555);
expect((await pst.currentState()).balances['uhE-QeYS8i4pmUtnxQyHD7dzXFNaJ9oMK-IM-QPNY6M']).toEqual(10000555);
});
it('should properly view contract state', async () => {
const result = await pst.currentBalance('uhE-QeYS8i4pmUtnxQyHD7dzXFNaJ9oMK-IM-QPNY6M');
expect(result).toEqual(10000000 + 555);
});
// note: the dummy logic on the test contract should add 1000 tokens
// to each address, if the foreign contract state 'ticker' field = 'FOREIGN_PST'
it('should properly read foreign contract state', async () => {
await pst.writeInteraction({
function: 'foreignCall',
contract_tx_id: wrongForeignContractTxId
});
await mineBlock(warp);
expect((await pst.currentState()).balances[walletAddress]).toEqual(555669 - 555);
expect((await pst.currentState()).balances['uhE-QeYS8i4pmUtnxQyHD7dzXFNaJ9oMK-IM-QPNY6M']).toEqual(10000000 + 555);
await pst.writeInteraction({
function: 'foreignCall',
contract_tx_id: properForeignContractTxId
});
await mineBlock(warp);
expect((await pst.currentState()).balances[walletAddress]).toEqual(555669 - 555 + 1000);
expect((await pst.currentState()).balances['uhE-QeYS8i4pmUtnxQyHD7dzXFNaJ9oMK-IM-QPNY6M']).toEqual(
10000000 + 555 + 1000
);
});
xit('should return stable gas results', async () => {
const results: InteractionResult<PstState, unknown>[] = [];
for (let i = 0; i < 10; i++) {
const result = await pst.dryWrite({
function: 'transfer',
target: 'uhE-QeYS8i4pmUtnxQyHD7dzXFNaJ9oMK-IM-QPNY6M',
qty: 555
});
results.push(result);
}
results.forEach((result) => {
expect(result.gasUsed).toEqual(9388933);
});
}, 10000);
it('should properly handle runtime errors', async () => {
const result = await pst.dryWrite({
target: 'uhE-QeYS8i4pmUtnxQyHD7dzXFNaJ9oMK-IM-QPNY6M',
qty: 555
});
expect(result.type).toEqual('exception');
expect(result.errorMessage).toEqual('[RE:RE] Error while parsing input');
});
it('should properly handle contract errors', async () => {
const result = await pst.dryWrite({
function: 'transfer',
target: 'uhE-QeYS8i4pmUtnxQyHD7dzXFNaJ9oMK-IM-QPNY6M',
qty: 0
});
expect(result.type).toEqual('error');
expect(result.errorMessage).toEqual('[CE:TransferAmountMustBeHigherThanZero]');
});
it("should properly evolve contract's source code", async () => {
const result = await pst.currentBalance('uhE-QeYS8i4pmUtnxQyHD7dzXFNaJ9oMK-IM-QPNY6M');
expect(result).toEqual(10000000 + 1000 + 555);
const newContractSrc = fs.readFileSync(path.join(__dirname, '../data/wasm/rust/rust-pst-evolve_bg.wasm'));
const srcTx = await warp.createSource(
{
src: newContractSrc,
wasmSrcCodeDir: path.join(__dirname, '../data/wasm/rust/src-evolve'),
wasmGlueCode: path.join(__dirname, '../data/wasm/rust/rust-pst-evolve.js')
},
wallet
);
const newSrcTxId = await warp.saveSource(srcTx);
await mineBlock(warp);
await pst.evolve(newSrcTxId);
await mineBlock(warp);
// note: evolve should add to the transfer additional 200
await pst.transfer({
target: 'uhE-QeYS8i4pmUtnxQyHD7dzXFNaJ9oMK-IM-QPNY6M',
qty: 555
});
await mineBlock(warp);
expect((await pst.currentState()).balances['uhE-QeYS8i4pmUtnxQyHD7dzXFNaJ9oMK-IM-QPNY6M']).toEqual(
10000000 + 555 + 1000 + 555 + 200
);
});
xit('should honor gas limits', async () => {
pst.setEvaluationOptions({
gasLimit: 5000000
});
const result = await pst.dryWrite({
function: 'transfer',
target: 'uhE-QeYS8i4pmUtnxQyHD7dzXFNaJ9oMK-IM-QPNY6M',
qty: 555
});
expect(result.type).toEqual('exception');
expect(result.errorMessage?.startsWith('[RE:OOG] Out of gas!')).toBeTruthy();
});
});