|
| 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 { getSigningJsdClient, jsd } from 'jsdjs' |
| 8 | +import { useChain, generateMnemonic } from 'starshipjs'; |
| 9 | +import { sleep } from '../src/utils'; |
| 10 | +import './setup.test'; |
| 11 | + |
| 12 | +describe('JSD tests', () => { |
| 13 | + let wallet, denom, address, queryClient, signingClient; |
| 14 | + let chainInfo, getCoin, getRpcEndpoint, creditFromFaucet; |
| 15 | + let contractCode, contractIndex; |
| 16 | + |
| 17 | + beforeAll(async () => { |
| 18 | + ({ |
| 19 | + chainInfo, |
| 20 | + getCoin, |
| 21 | + getRpcEndpoint, |
| 22 | + creditFromFaucet |
| 23 | + } = useChain('jsd')); |
| 24 | + denom = (await getCoin()).base; |
| 25 | + |
| 26 | + // Initialize wallet |
| 27 | + wallet = await DirectSecp256k1HdWallet.fromMnemonic(generateMnemonic(), { |
| 28 | + prefix: chainInfo.chain.bech32_prefix |
| 29 | + }); |
| 30 | + address = (await wallet.getAccounts())[0].address; |
| 31 | + console.log(`contract creator address: ${address}`) |
| 32 | + |
| 33 | + // Create custom cosmos interchain client |
| 34 | + queryClient = await jsd.ClientFactory.createRPCQueryClient({ |
| 35 | + rpcEndpoint: await getRpcEndpoint() |
| 36 | + }); |
| 37 | + |
| 38 | + signingClient = await getSigningJsdClient({ |
| 39 | + rpcEndpoint: await getRpcEndpoint(), |
| 40 | + signer: wallet |
| 41 | + }); |
| 42 | + |
| 43 | + await creditFromFaucet(address); |
| 44 | + await sleep(2000); // sleep for 1 sec to get tokens transferred from faucet successfully |
| 45 | + }); |
| 46 | + |
| 47 | + it('check balance', async () => { |
| 48 | + const balance = await signingClient.getBalance(address, denom); |
| 49 | + expect(balance.amount).toEqual("10000000000"); |
| 50 | + expect(balance.denom).toEqual(denom); |
| 51 | + }); |
| 52 | + |
| 53 | + it('instantiate contract', async () => { |
| 54 | + // Read contract code from external file |
| 55 | + const contractPath = path.join(__dirname, '../contracts/simple-state.js'); |
| 56 | + contractCode = fs.readFileSync(contractPath, 'utf8'); |
| 57 | + |
| 58 | + const fee = { |
| 59 | + amount: [ |
| 60 | + { |
| 61 | + denom, |
| 62 | + amount: '100000' |
| 63 | + } |
| 64 | + ], |
| 65 | + gas: '550000' |
| 66 | + }; |
| 67 | + |
| 68 | + const msg = jsd.jsd.MessageComposer.fromPartial.instantiate({ |
| 69 | + creator: address, |
| 70 | + code: contractCode, |
| 71 | + }); |
| 72 | + |
| 73 | + const result = await signingClient.signAndBroadcast(address, [msg], fee); |
| 74 | + assertIsDeliverTxSuccess(result); |
| 75 | + |
| 76 | + // Parse the response to get the contract index |
| 77 | + const response = jsd.jsd.MsgInstantiateResponse.fromProtoMsg(result.msgResponses[0]); |
| 78 | + contractIndex = response.index; |
| 79 | + 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({index: contractIndex}); |
| 85 | + expect(response.contracts.code).toEqual(contractCode); |
| 86 | + expect(response.contracts.index).toEqual(contractIndex); |
| 87 | + expect(response.contracts.creator).toEqual(address); |
| 88 | + }); |
| 89 | + |
| 90 | + it('query state before eval', async () => { |
| 91 | + const state = await queryClient.jsd.jsd.localState({index: contractIndex, key: "value"}); |
| 92 | + expect(state).toEqual({value: ""}); |
| 93 | + }); |
| 94 | + |
| 95 | + it('perform inc eval', async () => { |
| 96 | + const fee = { |
| 97 | + amount: [ |
| 98 | + { |
| 99 | + denom, |
| 100 | + amount: '100000' |
| 101 | + } |
| 102 | + ], |
| 103 | + gas: '550000' |
| 104 | + }; |
| 105 | + |
| 106 | + const msg = jsd.jsd.MessageComposer.fromPartial.eval({ |
| 107 | + creator: address, |
| 108 | + index: contractIndex, |
| 109 | + fnName: "inc", |
| 110 | + arg: `{"x": 10}`, |
| 111 | + }); |
| 112 | + |
| 113 | + const result = await signingClient.signAndBroadcast(address, [msg], fee); |
| 114 | + assertIsDeliverTxSuccess(result); |
| 115 | + |
| 116 | + const response = jsd.jsd.MsgEvalResponse.fromProtoMsg(result.msgResponses[0]); |
| 117 | + expect(response.result).toEqual("10"); |
| 118 | + }); |
| 119 | + |
| 120 | + it('eval read from eval', async () => { |
| 121 | + const fee = { |
| 122 | + amount: [ |
| 123 | + { |
| 124 | + denom, |
| 125 | + amount: '100000' |
| 126 | + } |
| 127 | + ], |
| 128 | + gas: '550000' |
| 129 | + }; |
| 130 | + |
| 131 | + const msg = jsd.jsd.MessageComposer.fromPartial.eval({ |
| 132 | + creator: address, |
| 133 | + index: contractIndex, |
| 134 | + fnName: "read", |
| 135 | + }); |
| 136 | + |
| 137 | + const result = await signingClient.signAndBroadcast(address, [msg], fee); |
| 138 | + assertIsDeliverTxSuccess(result); |
| 139 | + |
| 140 | + const response = jsd.jsd.MsgEvalResponse.fromProtoMsg(result.msgResponses[0]); |
| 141 | + expect(response.result).toEqual("10"); |
| 142 | + }); |
| 143 | + |
| 144 | + it('query state after eval', async () => { |
| 145 | + const state = await queryClient.jsd.jsd.localState({index: contractIndex, key: "value"}); |
| 146 | + expect(state).toEqual({value: "10"}); |
| 147 | + }); |
| 148 | + |
| 149 | + it('perform dec eval', async () => { |
| 150 | + const fee = { |
| 151 | + amount: [ |
| 152 | + { |
| 153 | + denom, |
| 154 | + amount: '100000' |
| 155 | + } |
| 156 | + ], |
| 157 | + gas: '550000' |
| 158 | + }; |
| 159 | + |
| 160 | + const msg = jsd.jsd.MessageComposer.fromPartial.eval({ |
| 161 | + creator: address, |
| 162 | + index: contractIndex, |
| 163 | + fnName: "dec", |
| 164 | + arg: `{"x": 5}`, |
| 165 | + }); |
| 166 | + |
| 167 | + const result = await signingClient.signAndBroadcast(address, [msg], fee); |
| 168 | + assertIsDeliverTxSuccess(result); |
| 169 | + |
| 170 | + const response = jsd.jsd.MsgEvalResponse.fromProtoMsg(result.msgResponses[0]); |
| 171 | + expect(response.result).toEqual("5"); |
| 172 | + }); |
| 173 | + |
| 174 | + it('eval read from eval', async () => { |
| 175 | + const fee = { |
| 176 | + amount: [ |
| 177 | + { |
| 178 | + denom, |
| 179 | + amount: '100000' |
| 180 | + } |
| 181 | + ], |
| 182 | + gas: '550000' |
| 183 | + }; |
| 184 | + |
| 185 | + const msg = jsd.jsd.MessageComposer.fromPartial.eval({ |
| 186 | + creator: address, |
| 187 | + index: contractIndex, |
| 188 | + fnName: "read", |
| 189 | + }); |
| 190 | + |
| 191 | + const result = await signingClient.signAndBroadcast(address, [msg], fee); |
| 192 | + assertIsDeliverTxSuccess(result); |
| 193 | + |
| 194 | + const response = jsd.jsd.MsgEvalResponse.fromProtoMsg(result.msgResponses[0]); |
| 195 | + expect(response.result).toEqual("5"); |
| 196 | + }); |
| 197 | +}); |
0 commit comments