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