|
| 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('Bank Contract Tests', () => { |
| 13 | + let wallet, denom, address, queryClient, signingClient; |
| 14 | + let chainInfo, getCoin, getRpcEndpoint, creditFromFaucet; |
| 15 | + let contractCode, contractIndex, contractAddress; |
| 16 | + let fee; |
| 17 | + let recipientWallet, recipientAddress; |
| 18 | + |
| 19 | + beforeAll(async () => { |
| 20 | + ({ |
| 21 | + chainInfo, |
| 22 | + getCoin, |
| 23 | + getRpcEndpoint, |
| 24 | + creditFromFaucet |
| 25 | + } = useChain('hyperweb')); |
| 26 | + |
| 27 | + denom = (await getCoin()).base; |
| 28 | + |
| 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 | + recipientWallet = await DirectSecp256k1HdWallet.fromMnemonic(generateMnemonic(), { |
| 36 | + prefix: chainInfo.chain.bech32_prefix |
| 37 | + }); |
| 38 | + recipientAddress = (await recipientWallet.getAccounts())[0].address; |
| 39 | + console.log(`recipient address: ${recipientAddress}`); |
| 40 | + |
| 41 | + queryClient = await hyperweb.ClientFactory.createRPCQueryClient({ |
| 42 | + rpcEndpoint: await getRpcEndpoint() |
| 43 | + }); |
| 44 | + |
| 45 | + signingClient = await getSigningHyperwebClient({ |
| 46 | + rpcEndpoint: await getRpcEndpoint(), |
| 47 | + signer: wallet |
| 48 | + }); |
| 49 | + |
| 50 | + fee = { amount: [{ denom, amount: '100000' }], gas: '550000' }; |
| 51 | + |
| 52 | + await creditFromFaucet(address); |
| 53 | + await sleep(10000); |
| 54 | + }); |
| 55 | + |
| 56 | + it('Check initial balance', async () => { |
| 57 | + const balance = await signingClient.getBalance(address, denom); |
| 58 | + expect(balance.amount).toEqual("10000000000"); |
| 59 | + expect(balance.denom).toEqual(denom); |
| 60 | + }); |
| 61 | + |
| 62 | + it('Instantiate Bank contract', async () => { |
| 63 | + const contractPath = path.join( |
| 64 | + __dirname, |
| 65 | + "../dist/contracts/bank.js" |
| 66 | + ); |
| 67 | + contractCode = fs.readFileSync(contractPath, "utf8"); |
| 68 | + |
| 69 | + const msg = hyperweb.hvm.MessageComposer.fromPartial.instantiate({ |
| 70 | + creator: address, |
| 71 | + code: contractCode, |
| 72 | + source: "test_source", |
| 73 | + }); |
| 74 | + |
| 75 | + const result = await signingClient.signAndBroadcast(address, [msg], fee); |
| 76 | + assertIsDeliverTxSuccess(result); |
| 77 | + |
| 78 | + const response = hyperweb.hvm.MsgInstantiateResponse.fromProtoMsg(result.msgResponses[0]); |
| 79 | + contractIndex = response.index; |
| 80 | + contractAddress = response.address; |
| 81 | + expect(contractIndex).toBeGreaterThan(0); |
| 82 | + console.log(`contract instantiated at index: ${contractIndex} and address ${contractAddress}`); |
| 83 | + }); |
| 84 | + |
| 85 | + it('Call balance function', async () => { |
| 86 | + const args = JSON.stringify({ address, denom }); |
| 87 | + const msg = hyperweb.hvm.MessageComposer.fromPartial.eval({ |
| 88 | + address: contractAddress, |
| 89 | + creator: address, |
| 90 | + callee: "balance", |
| 91 | + args: [args] |
| 92 | + }); |
| 93 | + |
| 94 | + const result = await signingClient.signAndBroadcast(address, [msg], fee); |
| 95 | + assertIsDeliverTxSuccess(result); |
| 96 | + |
| 97 | + const response = hyperweb.hvm.MsgEvalResponse.fromProtoMsg(result.msgResponses[0]); |
| 98 | + expect(parseInt(response.result)).toBeGreaterThanOrEqual(0); |
| 99 | + }); |
| 100 | + |
| 101 | + it('Transfer funds to another address and check balance', async () => { |
| 102 | + const transferArgs = JSON.stringify({ |
| 103 | + from: address, |
| 104 | + to: recipientAddress, |
| 105 | + amount: 1000, |
| 106 | + denom |
| 107 | + }); |
| 108 | + const transferMsg = hyperweb.hvm.MessageComposer.fromPartial.eval({ |
| 109 | + address: contractAddress, |
| 110 | + creator: address, |
| 111 | + callee: "transfer", |
| 112 | + args: [transferArgs] |
| 113 | + }); |
| 114 | + |
| 115 | + const transferResult = await signingClient.signAndBroadcast(address, [transferMsg], fee); |
| 116 | + assertIsDeliverTxSuccess(transferResult); |
| 117 | + |
| 118 | + const checkArgs = JSON.stringify({ address: recipientAddress, denom }); |
| 119 | + const checkMsg = hyperweb.hvm.MessageComposer.fromPartial.eval({ |
| 120 | + address: contractAddress, |
| 121 | + creator: address, |
| 122 | + callee: "balance", |
| 123 | + args: [checkArgs] |
| 124 | + }); |
| 125 | + |
| 126 | + const checkResult = await signingClient.signAndBroadcast(address, [checkMsg], fee); |
| 127 | + assertIsDeliverTxSuccess(checkResult); |
| 128 | + |
| 129 | + const checkResponse = hyperweb.hvm.MsgEvalResponse.fromProtoMsg(checkResult.msgResponses[0]); |
| 130 | + console.log(`recipient balance: ${checkResponse.result}`); |
| 131 | + expect(parseInt(checkResponse.result)).toBeGreaterThanOrEqual(1000); |
| 132 | + }); |
| 133 | + |
| 134 | +}); |
0 commit comments