|
1 | 1 | // @ts-nocheck
|
2 |
| -import { Secp256k1HDWallet } from "@interchainjs/cosmos/wallets/secp256k1hd"; |
3 |
| -import { assertIsDeliverTxSuccess } from "@interchainjs/cosmos/utils"; |
| 2 | +import { DirectSecp256k1HdWallet } from '@cosmjs/proto-signing'; |
| 3 | +import { assertIsDeliverTxSuccess } from '@cosmjs/stargate'; |
4 | 4 |
|
5 | 5 | import path from "path";
|
6 |
| -import fs from "fs"; |
7 |
| -import { getSigningJsdClient, jsd } from "hyperwebjs"; |
8 |
| -import { useChain, generateMnemonic } from "starshipjs"; |
9 |
| -import { sleep } from "../test-utils/sleep"; |
10 |
| -import "./setup.test"; |
11 |
| -import { DirectGenericOfflineSigner } from "@interchainjs/cosmos/types/wallet"; |
12 |
| - |
13 |
| -describe("Simple state tests", () => { |
| 6 | +import fs from 'fs'; |
| 7 | +import { getSigningHyperwebClient, hyperweb } from 'hyperwebjs'; |
| 8 | +import { useChain, generateMnemonic } from 'starshipjs'; |
| 9 | +import { sleep } from '../test-utils/sleep'; |
| 10 | +import './setup.test'; |
| 11 | + |
| 12 | +describe('State Contract Tests', () => { |
14 | 13 | let wallet, denom, address, queryClient, signingClient;
|
15 | 14 | let chainInfo, getCoin, getRpcEndpoint, creditFromFaucet;
|
16 | 15 | let contractCode, contractIndex;
|
17 |
| - |
18 | 16 | let fee;
|
19 | 17 |
|
20 | 18 | beforeAll(async () => {
|
21 |
| - ({ chainInfo, getCoin, getRpcEndpoint, creditFromFaucet } = |
22 |
| - useChain("hyperweb")); |
| 19 | + ({ |
| 20 | + chainInfo, |
| 21 | + getCoin, |
| 22 | + getRpcEndpoint, |
| 23 | + creditFromFaucet |
| 24 | + } = useChain('hyperweb')); |
| 25 | + |
23 | 26 | denom = (await getCoin()).base;
|
24 | 27 |
|
25 | 28 | // Initialize wallet
|
26 |
| - wallet = Secp256k1HDWallet.fromMnemonic(generateMnemonic(), [ |
27 |
| - { |
28 |
| - prefix: chainInfo.chain.bech32_prefix, |
29 |
| - hdPath: "m/44'/118'/0'/0/0", |
30 |
| - }, |
31 |
| - ]); |
| 29 | + wallet = await DirectSecp256k1HdWallet.fromMnemonic(generateMnemonic(), { |
| 30 | + prefix: chainInfo.chain.bech32_prefix |
| 31 | + }); |
32 | 32 | address = (await wallet.getAccounts())[0].address;
|
33 |
| - console.log(`contract creator address: ${address}`); |
| 33 | + console.log(`Contract creator address: ${address}`); |
34 | 34 |
|
35 | 35 | // Create custom cosmos interchain client
|
36 |
| - queryClient = await jsd.ClientFactory.createRPCQueryClient({ |
37 |
| - rpcEndpoint: await getRpcEndpoint(), |
| 36 | + queryClient = await hyperweb.ClientFactory.createRPCQueryClient({ |
| 37 | + rpcEndpoint: await getRpcEndpoint() |
38 | 38 | });
|
39 | 39 |
|
40 |
| - signingClient = await getSigningJsdClient({ |
| 40 | + signingClient = await getSigningHyperwebClient({ |
41 | 41 | rpcEndpoint: await getRpcEndpoint(),
|
42 |
| - signer: wallet, |
| 42 | + signer: wallet |
43 | 43 | });
|
44 | 44 |
|
45 |
| - // set default fee |
46 |
| - fee = { amount: [{ denom, amount: "100000" }], gas: "550000" }; |
| 45 | + // Set default transaction fee |
| 46 | + fee = { amount: [{ denom, amount: '100000' }], gas: '550000' }; |
47 | 47 |
|
48 | 48 | await creditFromFaucet(address);
|
49 |
| - await sleep(2000); // sleep for 1 sec to get tokens transferred from faucet successfully |
| 49 | + await sleep(2000); // Sleep for 2 sec to allow faucet tokens to arrive |
50 | 50 | });
|
51 | 51 |
|
52 |
| - it("check balance", async () => { |
| 52 | + it('Check initial balance', async () => { |
53 | 53 | const balance = await signingClient.getBalance(address, denom);
|
54 | 54 | expect(balance.amount).toEqual("10000000000");
|
55 | 55 | expect(balance.denom).toEqual(denom);
|
56 | 56 | });
|
57 | 57 |
|
58 |
| - it("instantiate contract", async () => { |
| 58 | + it('Instantiate state contract', async () => { |
59 | 59 | // Read contract code from external file
|
60 | 60 | const contractPath = path.join(
|
61 | 61 | __dirname,
|
62 | 62 | "../dist/contracts/simpleState.js"
|
63 | 63 | );
|
64 | 64 | contractCode = fs.readFileSync(contractPath, "utf8");
|
65 | 65 |
|
66 |
| - const msg = jsd.jsd.MessageComposer.fromPartial.instantiate({ |
| 66 | + const msg = hyperweb.hvm.MessageComposer.fromPartial.instantiate({ |
67 | 67 | creator: address,
|
68 | 68 | code: contractCode,
|
| 69 | + source: "test_source", |
69 | 70 | });
|
70 | 71 |
|
71 | 72 | const result = await signingClient.signAndBroadcast(address, [msg], fee);
|
72 | 73 | assertIsDeliverTxSuccess(result);
|
73 | 74 |
|
74 | 75 | // Parse the response to get the contract index
|
75 |
| - const response = jsd.jsd.MsgInstantiateResponse.fromProtoMsg( |
76 |
| - result.msgResponses[0] |
77 |
| - ); |
| 76 | + const response = hyperweb.hvm.MsgInstantiateResponse.fromProtoMsg(result.msgResponses[0]); |
78 | 77 | contractIndex = response.index;
|
79 | 78 | expect(contractIndex).toBeGreaterThan(0);
|
80 |
| - console.log(`contract index: ${contractIndex}`); |
81 |
| - }); |
82 |
| - |
83 |
| - it("query for contract based on index", async () => { |
84 |
| - const response = await queryClient.jsd.jsd.contracts({ |
85 |
| - index: contractIndex, |
86 |
| - }); |
87 |
| - expect(response.contracts.code).toEqual(contractCode); |
88 |
| - expect(response.contracts.index).toEqual(contractIndex); |
89 |
| - expect(response.contracts.creator).toEqual(address); |
90 |
| - }); |
91 |
| - |
92 |
| - it("query state before eval", async () => { |
93 |
| - const state = await queryClient.jsd.jsd.localState({ |
94 |
| - index: contractIndex, |
95 |
| - key: "value", |
96 |
| - }); |
97 |
| - expect(state).toEqual({ value: "" }); |
98 |
| - }); |
99 |
| - |
100 |
| - it("perform inc eval", async () => { |
101 |
| - const msg = jsd.jsd.MessageComposer.fromPartial.eval({ |
102 |
| - creator: address, |
103 |
| - index: contractIndex, |
104 |
| - fnName: "inc", |
105 |
| - arg: `{"x": 10}`, |
106 |
| - }); |
107 |
| - |
108 |
| - const result = await signingClient.signAndBroadcast(address, [msg], fee); |
109 |
| - assertIsDeliverTxSuccess(result); |
110 |
| - |
111 |
| - const response = jsd.jsd.MsgEvalResponse.fromProtoMsg( |
112 |
| - result.msgResponses[0] |
113 |
| - ); |
114 |
| - expect(response.result).toEqual("10"); |
| 79 | + console.log(`Contract instantiated at index: ${contractIndex}`); |
115 | 80 | });
|
116 | 81 |
|
117 |
| - it("eval read from eval", async () => { |
118 |
| - const msg = jsd.jsd.MessageComposer.fromPartial.eval({ |
| 82 | + it('Perform increment evaluation', async () => { |
| 83 | + const msg = hyperweb.hvm.MessageComposer.fromPartial.eval({ |
119 | 84 | creator: address,
|
| 85 | + callee: "inc", |
120 | 86 | index: contractIndex,
|
121 |
| - fnName: "read", |
122 |
| - arg: "", |
| 87 | + args: [10] as any[], |
123 | 88 | });
|
124 | 89 |
|
125 | 90 | const result = await signingClient.signAndBroadcast(address, [msg], fee);
|
126 | 91 | assertIsDeliverTxSuccess(result);
|
127 | 92 |
|
128 |
| - const response = jsd.jsd.MsgEvalResponse.fromProtoMsg( |
129 |
| - result.msgResponses[0] |
130 |
| - ); |
| 93 | + const response = hyperweb.hvm.MsgEvalResponse.fromProtoMsg(result.msgResponses[0]); |
131 | 94 | expect(response.result).toEqual("10");
|
132 | 95 | });
|
133 |
| - |
134 |
| - it("query state after eval", async () => { |
135 |
| - const state = await queryClient.jsd.jsd.localState({ |
136 |
| - index: contractIndex, |
137 |
| - key: "value", |
138 |
| - }); |
139 |
| - expect(state).toEqual({ value: "10" }); |
140 |
| - }); |
141 |
| - |
142 |
| - it("perform dec eval", async () => { |
143 |
| - const msg = jsd.jsd.MessageComposer.fromPartial.eval({ |
144 |
| - creator: address, |
145 |
| - index: contractIndex, |
146 |
| - fnName: "dec", |
147 |
| - arg: `{"x": 5}`, |
148 |
| - }); |
149 |
| - |
150 |
| - const result = await signingClient.signAndBroadcast(address, [msg], fee); |
151 |
| - assertIsDeliverTxSuccess(result); |
152 |
| - |
153 |
| - const response = jsd.jsd.MsgEvalResponse.fromProtoMsg( |
154 |
| - result.msgResponses[0] |
155 |
| - ); |
156 |
| - expect(response.result).toEqual("5"); |
157 |
| - }); |
158 |
| - |
159 |
| - it("eval read from eval", async () => { |
160 |
| - const msg = jsd.jsd.MessageComposer.fromPartial.eval({ |
161 |
| - creator: address, |
162 |
| - index: contractIndex, |
163 |
| - fnName: "read", |
164 |
| - arg: "", |
165 |
| - }); |
166 |
| - |
167 |
| - const result = await signingClient.signAndBroadcast(address, [msg], fee); |
168 |
| - assertIsDeliverTxSuccess(result); |
169 |
| - |
170 |
| - const response = jsd.jsd.MsgEvalResponse.fromProtoMsg( |
171 |
| - result.msgResponses[0] |
172 |
| - ); |
173 |
| - expect(response.result).toEqual("5"); |
174 |
| - }); |
| 96 | + // |
| 97 | + // it('Perform decrement evaluation', async () => { |
| 98 | + // const msg = hyperweb.hvm.MessageComposer.fromPartial.eval({ |
| 99 | + // creator: address, |
| 100 | + // callee: "dec", |
| 101 | + // index: contractIndex, |
| 102 | + // args: [] |
| 103 | + // }); |
| 104 | + // |
| 105 | + // const result = await signingClient.signAndBroadcast(address, [msg], fee); |
| 106 | + // assertIsDeliverTxSuccess(result); |
| 107 | + // |
| 108 | + // const response = hyperweb.hvm.MsgEvalResponse.fromProtoMsg(result.msgResponses[0]); |
| 109 | + // expect(response.result).toEqual("0"); |
| 110 | + // }); |
| 111 | + // |
| 112 | + // it('Perform multiple increments and verify state', async () => { |
| 113 | + // for (let i = 0; i < 3; i++) { |
| 114 | + // const msg = hyperweb.hvm.MessageComposer.fromPartial.eval({ |
| 115 | + // creator: address, |
| 116 | + // callee: "inc", |
| 117 | + // index: contractIndex, |
| 118 | + // args: [] |
| 119 | + // }); |
| 120 | + // |
| 121 | + // const result = await signingClient.signAndBroadcast(address, [msg], fee); |
| 122 | + // assertIsDeliverTxSuccess(result); |
| 123 | + // } |
| 124 | + // |
| 125 | + // const msgRead = hyperweb.hvm.MessageComposer.fromPartial.eval({ |
| 126 | + // creator: address, |
| 127 | + // callee: "read", |
| 128 | + // index: contractIndex, |
| 129 | + // args: [] |
| 130 | + // }); |
| 131 | + // |
| 132 | + // const result = await signingClient.signAndBroadcast(address, [msgRead], fee); |
| 133 | + // assertIsDeliverTxSuccess(result); |
| 134 | + // |
| 135 | + // const response = hyperweb.hvm.MsgEvalResponse.fromProtoMsg(result.msgResponses[0]); |
| 136 | + // expect(response.result).toEqual("3"); |
| 137 | + // }); |
| 138 | + // |
| 139 | + // it('Perform reset and verify state', async () => { |
| 140 | + // const msg = hyperweb.hvm.MessageComposer.fromPartial.eval({ |
| 141 | + // creator: address, |
| 142 | + // callee: "reset", |
| 143 | + // index: contractIndex, |
| 144 | + // args: [] |
| 145 | + // }); |
| 146 | + // |
| 147 | + // const result = await signingClient.signAndBroadcast(address, [msg], fee); |
| 148 | + // assertIsDeliverTxSuccess(result); |
| 149 | + // |
| 150 | + // const response = hyperweb.hvm.MsgEvalResponse.fromProtoMsg(result.msgResponses[0]); |
| 151 | + // expect(response.result).toEqual("0"); |
| 152 | + // }); |
| 153 | + // |
| 154 | + // it('Verify read function returns the correct state value', async () => { |
| 155 | + // const msg = hyperweb.hvm.MessageComposer.fromPartial.eval({ |
| 156 | + // creator: address, |
| 157 | + // callee: "read", |
| 158 | + // index: contractIndex, |
| 159 | + // args: [] |
| 160 | + // }); |
| 161 | + // |
| 162 | + // const result = await signingClient.signAndBroadcast(address, [msg], fee); |
| 163 | + // assertIsDeliverTxSuccess(result); |
| 164 | + // |
| 165 | + // const response = hyperweb.hvm.MsgEvalResponse.fromProtoMsg(result.msgResponses[0]); |
| 166 | + // expect(response.result).toEqual("0"); |
| 167 | + // }); |
| 168 | + // |
| 169 | + // it('Retrieve contract source and validate', async () => { |
| 170 | + // const contractSource = await queryClient.hyperweb.hvm.getContractSource({ index: contractIndex }); |
| 171 | + // expect(contractSource.source).toEqual("test_source"); |
| 172 | + // }); |
175 | 173 | });
|
0 commit comments