-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathsimple-state.test.ts
More file actions
189 lines (169 loc) · 6.21 KB
/
simple-state.test.ts
File metadata and controls
189 lines (169 loc) · 6.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
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
// @ts-nocheck
import { DirectSecp256k1HdWallet } from '@cosmjs/proto-signing';
import { assertIsDeliverTxSuccess } from '@cosmjs/stargate';
import path from "path";
import fs from 'fs';
import { getSigningHyperwebClient, hyperweb, google } from 'hyperwebjs';
import { useChain, generateMnemonic } from 'starshipjs';
import { sleep } from '../test-utils/sleep';
import './setup.test';
describe('State Contract Tests', () => {
let wallet, denom, address, queryClient, signingClient;
let chainInfo, getCoin, getRpcEndpoint, creditFromFaucet;
let contractCode, contractIndex, contractAddress;
let fee;
beforeAll(async () => {
({
chainInfo,
getCoin,
getRpcEndpoint,
creditFromFaucet
} = useChain('hyperweb'));
denom = (await getCoin()).base;
// Initialize wallet
wallet = await DirectSecp256k1HdWallet.fromMnemonic(generateMnemonic(), {
prefix: chainInfo.chain.bech32_prefix
});
address = (await wallet.getAccounts())[0].address;
console.log(`Contract creator address: ${address}`);
// Create custom cosmos interchain client
queryClient = await hyperweb.ClientFactory.createRPCQueryClient({
rpcEndpoint: await getRpcEndpoint()
});
signingClient = await getSigningHyperwebClient({
rpcEndpoint: await getRpcEndpoint(),
signer: wallet
});
// Set default transaction fee
fee = { amount: [{ denom, amount: '100000' }], gas: '550000' };
await creditFromFaucet(address);
await sleep(10000); // Sleep for 2 sec to allow faucet tokens to arrive
});
it('Check initial balance', async () => {
const balance = await signingClient.getBalance(address, denom);
expect(balance.amount).toEqual("10000000000");
expect(balance.denom).toEqual(denom);
});
it('Instantiate state contract', async () => {
// Read contract code from external file
const contractPath = path.join(
__dirname,
"../dist/contracts/simple-state.js"
);
contractCode = fs.readFileSync(contractPath, "utf8");
const msg = hyperweb.hvm.MessageComposer.fromPartial.instantiate({
creator: address,
code: contractCode,
source: "test_source",
});
const result = await signingClient.signAndBroadcast(address, [msg], fee);
assertIsDeliverTxSuccess(result);
// Parse the response to get the contract index
const response = hyperweb.hvm.MsgInstantiateResponse.fromProtoMsg(result.msgResponses[0]);
contractIndex = response.index;
contractAddress = response.address;
expect(contractIndex).toBeGreaterThan(0);
console.log(`Contract instantiated at index: ${contractIndex} and address ${contractAddress}`);
});
it('Perform init function', async () => {
const msg = hyperweb.hvm.MessageComposer.fromPartial.eval({
address: contractAddress,
creator: address,
callee: "init",
args: []
});
const result = await signingClient.signAndBroadcast(address, [msg], fee);
assertIsDeliverTxSuccess(result);
const response = hyperweb.hvm.MsgEvalResponse.fromProtoMsg(result.msgResponses[0]);
expect(response.result).toEqual("0");
});
it('Perform increment evaluation', async () => {
const msg = hyperweb.hvm.MessageComposer.fromPartial.eval({
address: contractAddress,
creator: address,
callee: "inc",
args: ["10"]
});
const result = await signingClient.signAndBroadcast(address, [msg], fee);
assertIsDeliverTxSuccess(result);
const response = hyperweb.hvm.MsgEvalResponse.fromProtoMsg(result.msgResponses[0]);
expect(response.result).toEqual("10");
});
//
// it('Perform decrement evaluation', async () => {
// const msg = hyperweb.hvm.MessageComposer.fromPartial.eval({
// creator: address,
// callee: "dec",
// index: contractIndex,
// args: []
// });
//
// const result = await signingClient.signAndBroadcast(address, [msg], fee);
// assertIsDeliverTxSuccess(result);
//
// const response = hyperweb.hvm.MsgEvalResponse.fromProtoMsg(result.msgResponses[0]);
// expect(response.result).toEqual("0");
// });
//
// it('Perform multiple increments and verify state', async () => {
// for (let i = 0; i < 3; i++) {
// const msg = hyperweb.hvm.MessageComposer.fromPartial.eval({
// creator: address,
// callee: "inc",
// index: contractIndex,
// args: []
// });
//
// const result = await signingClient.signAndBroadcast(address, [msg], fee);
// assertIsDeliverTxSuccess(result);
// }
//
// const msgRead = hyperweb.hvm.MessageComposer.fromPartial.eval({
// creator: address,
// callee: "read",
// index: contractIndex,
// args: []
// });
//
// const result = await signingClient.signAndBroadcast(address, [msgRead], fee);
// assertIsDeliverTxSuccess(result);
//
// const response = hyperweb.hvm.MsgEvalResponse.fromProtoMsg(result.msgResponses[0]);
// expect(response.result).toEqual("3");
// });
//
// it('Perform reset and verify state', async () => {
// const msg = hyperweb.hvm.MessageComposer.fromPartial.eval({
// creator: address,
// callee: "reset",
// index: contractIndex,
// args: []
// });
//
// const result = await signingClient.signAndBroadcast(address, [msg], fee);
// assertIsDeliverTxSuccess(result);
//
// const response = hyperweb.hvm.MsgEvalResponse.fromProtoMsg(result.msgResponses[0]);
// expect(response.result).toEqual("0");
// });
//
// it('Verify read function returns the correct state value', async () => {
// const msg = hyperweb.hvm.MessageComposer.fromPartial.eval({
// creator: address,
// callee: "read",
// index: contractIndex,
// args: []
// });
//
// const result = await signingClient.signAndBroadcast(address, [msg], fee);
// assertIsDeliverTxSuccess(result);
//
// const response = hyperweb.hvm.MsgEvalResponse.fromProtoMsg(result.msgResponses[0]);
// expect(response.result).toEqual("0");
// });
//
// it('Retrieve contract source and validate', async () => {
// const contractSource = await queryClient.hyperweb.hvm.getContractSource({ index: contractIndex });
// expect(contractSource.source).toEqual("test_source");
// });
});