|
| 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 '../test-utils/sleep'; |
| 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 | + let wallet2, address2; |
| 18 | + let fee; |
| 19 | + |
| 20 | + const denom2 = "uweb", denomATOM = "ATOM", denomUSDC = "USDC"; |
| 21 | + |
| 22 | + beforeAll(async () => { |
| 23 | + ({ |
| 24 | + chainInfo, |
| 25 | + getCoin, |
| 26 | + getRpcEndpoint, |
| 27 | + creditFromFaucet |
| 28 | + } = useChain('hyperweb')); |
| 29 | + denom = (await getCoin()).base; |
| 30 | + |
| 31 | + // Initialize wallet |
| 32 | + wallet = await DirectSecp256k1HdWallet.fromMnemonic(generateMnemonic(), { |
| 33 | + prefix: chainInfo.chain.bech32_prefix |
| 34 | + }); |
| 35 | + address = (await wallet.getAccounts())[0].address; |
| 36 | + console.log(`contract creator address: ${address}`) |
| 37 | + |
| 38 | + // Initialize wallet2 |
| 39 | + wallet2 = await DirectSecp256k1HdWallet.fromMnemonic(generateMnemonic(), { |
| 40 | + prefix: chainInfo.chain.bech32_prefix |
| 41 | + }); |
| 42 | + address2 = (await wallet2.getAccounts())[0].address; |
| 43 | + console.log(`contract creator address: ${address2}`) |
| 44 | + |
| 45 | + // Create custom cosmos interchain client |
| 46 | + queryClient = await jsd.ClientFactory.createRPCQueryClient({ |
| 47 | + rpcEndpoint: await getRpcEndpoint() |
| 48 | + }); |
| 49 | + |
| 50 | + signingClient = await getSigningJsdClient({ |
| 51 | + rpcEndpoint: await getRpcEndpoint(), |
| 52 | + signer: wallet |
| 53 | + }); |
| 54 | + |
| 55 | + await creditFromFaucet(address, denom); |
| 56 | + await creditFromFaucet(address, denom2); |
| 57 | + await creditFromFaucet(address, denomATOM); |
| 58 | + await creditFromFaucet(address, denomUSDC); |
| 59 | + |
| 60 | + await creditFromFaucet(address2, denom); |
| 61 | + await creditFromFaucet(address2, denom2); |
| 62 | + |
| 63 | + fee = {amount: [{denom, amount: '100000'}], gas: '550000'}; |
| 64 | + |
| 65 | + await sleep(2000); // sleep for 1 sec to get tokens transferred from faucet successfully |
| 66 | + }); |
| 67 | + |
| 68 | + it('check balance', async () => { |
| 69 | + const balance = await signingClient.getBalance(address, denom); |
| 70 | + expect(balance.amount).toEqual("10000000000"); |
| 71 | + expect(balance.denom).toEqual(denom); |
| 72 | + }); |
| 73 | + |
| 74 | + it('instantiate contract', async () => { |
| 75 | + // Read contract code from external file |
| 76 | + const contractPath = path.join(__dirname, '../dist/contracts/bundle2.js'); |
| 77 | + contractCode = fs.readFileSync(contractPath, 'utf8'); |
| 78 | + |
| 79 | + const msg = jsd.jsd.MessageComposer.fromPartial.instantiate({ |
| 80 | + creator: address, |
| 81 | + code: contractCode, |
| 82 | + }); |
| 83 | + |
| 84 | + const result = await signingClient.signAndBroadcast(address, [msg], fee); |
| 85 | + assertIsDeliverTxSuccess(result); |
| 86 | + |
| 87 | + // Parse the response to get the contract index |
| 88 | + const response = jsd.jsd.MsgInstantiateResponse.fromProtoMsg(result.msgResponses[0]); |
| 89 | + contractIndex = response.index; |
| 90 | + expect(contractIndex).toBeGreaterThan(0); |
| 91 | + console.log(`contract index: ${contractIndex}`); |
| 92 | + }); |
| 93 | + |
| 94 | + it('query for contract based on index', async () => { |
| 95 | + const response = await queryClient.jsd.jsd.contracts({index: contractIndex}); |
| 96 | + expect(response.contracts.code).toEqual(contractCode); |
| 97 | + expect(response.contracts.index).toEqual(contractIndex); |
| 98 | + expect(response.contracts.creator).toEqual(address); |
| 99 | + }); |
| 100 | + |
| 101 | + it('perform getTotalSupply eval', async () => { |
| 102 | + const msg = jsd.jsd.MessageComposer.fromPartial.eval({ |
| 103 | + creator: address, |
| 104 | + index: contractIndex, |
| 105 | + fnName: "getTotalSupply", |
| 106 | + arg: `{}`, |
| 107 | + }); |
| 108 | + |
| 109 | + const result = await signingClient.signAndBroadcast(address, [msg], fee); |
| 110 | + assertIsDeliverTxSuccess(result); |
| 111 | + |
| 112 | + const response = jsd.jsd.MsgEvalResponse.fromProtoMsg(result.msgResponses[0]); |
| 113 | + expect(response.result).toEqual("0"); |
| 114 | + }); |
| 115 | + |
| 116 | + it('perform addLiquidity eval', async () => { |
| 117 | + const msg = jsd.jsd.MessageComposer.fromPartial.eval({ |
| 118 | + creator: address, |
| 119 | + index: contractIndex, |
| 120 | + fnName: "addLiquidity", |
| 121 | + arg: `{"amount0":50, "amount1":50}`, |
| 122 | + }); |
| 123 | + |
| 124 | + const result = await signingClient.signAndBroadcast(address, [msg], fee); |
| 125 | + assertIsDeliverTxSuccess(result); |
| 126 | + |
| 127 | + const response = jsd.jsd.MsgEvalResponse.fromProtoMsg(result.msgResponses[0]); |
| 128 | + expect(response.result).toEqual("null"); |
| 129 | + }); |
| 130 | + |
| 131 | + it('check balance after addLiquidity', async () => { |
| 132 | + const usdcBalance = await signingClient.getBalance(address, "USDC"); |
| 133 | + expect(usdcBalance.amount).toEqual("9999999950"); |
| 134 | + |
| 135 | + const atomBalance = await signingClient.getBalance(address, "ATOM"); |
| 136 | + expect(atomBalance.amount).toEqual("9999999950"); |
| 137 | + }); |
| 138 | + |
| 139 | + it('perform swap eval', async () => { |
| 140 | + const msg = jsd.jsd.MessageComposer.fromPartial.eval({ |
| 141 | + creator: address, |
| 142 | + index: contractIndex, |
| 143 | + fnName: "swap", |
| 144 | + arg: `{"tokenIn":"USDC","amountIn":10}`, |
| 145 | + }); |
| 146 | + |
| 147 | + const result = await signingClient.signAndBroadcast(address, [msg], fee); |
| 148 | + assertIsDeliverTxSuccess(result); |
| 149 | + |
| 150 | + const response = jsd.jsd.MsgEvalResponse.fromProtoMsg(result.msgResponses[0]); |
| 151 | + expect(response.result).toEqual("8.312489578122396"); |
| 152 | + }); |
| 153 | +}); |
0 commit comments