|
| 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('Token Factory 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 fullDenom: string; |
| 18 | + |
| 19 | + |
| 20 | + beforeAll(async () => { |
| 21 | + ({ |
| 22 | + chainInfo, |
| 23 | + getCoin, |
| 24 | + getRpcEndpoint, |
| 25 | + creditFromFaucet |
| 26 | + } = useChain('hyperweb')); |
| 27 | + |
| 28 | + denom = (await getCoin()).base; |
| 29 | + |
| 30 | + wallet = await DirectSecp256k1HdWallet.fromMnemonic(generateMnemonic(), { |
| 31 | + prefix: chainInfo.chain.bech32_prefix |
| 32 | + }); |
| 33 | + address = (await wallet.getAccounts())[0].address; |
| 34 | + console.log(`contract creator address: ${address}`); |
| 35 | + |
| 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 | + fee = { amount: [{ denom, amount: '100000' }], gas: '550000' }; |
| 46 | + |
| 47 | + await creditFromFaucet(address); |
| 48 | + await sleep(10000); |
| 49 | + }); |
| 50 | + |
| 51 | + it('Check initial balance', async () => { |
| 52 | + const balance = await signingClient.getBalance(address, denom); |
| 53 | + expect(balance.amount).toEqual("10000000000"); |
| 54 | + expect(balance.denom).toEqual(denom); |
| 55 | + }); |
| 56 | + |
| 57 | + it('Instantiate Token Factory contract', async () => { |
| 58 | + const contractPath = path.join( |
| 59 | + __dirname, |
| 60 | + "../dist/contracts/token-factory.js" |
| 61 | + ); |
| 62 | + contractCode = fs.readFileSync(contractPath, "utf8"); |
| 63 | + |
| 64 | + const msg = hyperweb.hvm.MessageComposer.fromPartial.instantiate({ |
| 65 | + creator: address, |
| 66 | + code: contractCode, |
| 67 | + source: "test_source", |
| 68 | + }); |
| 69 | + |
| 70 | + const result = await signingClient.signAndBroadcast(address, [msg], fee); |
| 71 | + assertIsDeliverTxSuccess(result); |
| 72 | + |
| 73 | + const response = hyperweb.hvm.MsgInstantiateResponse.fromProtoMsg(result.msgResponses[0]); |
| 74 | + contractIndex = response.index; |
| 75 | + contractAddress = response.address; |
| 76 | + expect(contractIndex).toBeGreaterThan(0); |
| 77 | + console.log(`contract instantiated at index: ${contractIndex} and address ${contractAddress}`); |
| 78 | + }); |
| 79 | + |
| 80 | + it('Create new denom', async () => { |
| 81 | + const args = JSON.stringify({ denom: "testdenom" }); |
| 82 | + const msg = hyperweb.hvm.MessageComposer.fromPartial.eval({ |
| 83 | + address: contractAddress, |
| 84 | + creator: address, |
| 85 | + callee: "createDenom", |
| 86 | + args: [args] |
| 87 | + }); |
| 88 | + |
| 89 | + const result = await signingClient.signAndBroadcast(address, [msg], fee); |
| 90 | + assertIsDeliverTxSuccess(result); |
| 91 | + |
| 92 | + const response = hyperweb.hvm.MsgEvalResponse.fromProtoMsg(result.msgResponses[0]); |
| 93 | + fullDenom = response.result; |
| 94 | + console.log("created fullDenom:", fullDenom); |
| 95 | + expect(fullDenom).toContain("factory"); |
| 96 | + }); |
| 97 | + |
| 98 | + it('Mint tokens using fullDenom', async () => { |
| 99 | + const args = JSON.stringify({ denom: fullDenom, amount: 1000 }); |
| 100 | + const msg = hyperweb.hvm.MessageComposer.fromPartial.eval({ |
| 101 | + address: contractAddress, |
| 102 | + creator: address, |
| 103 | + callee: "mintTokens", |
| 104 | + args: [args] |
| 105 | + }); |
| 106 | + |
| 107 | + const result = await signingClient.signAndBroadcast(address, [msg], fee); |
| 108 | + assertIsDeliverTxSuccess(result); |
| 109 | + }); |
| 110 | + |
| 111 | + it('Check balance of minted tokens', async () => { |
| 112 | + const args = JSON.stringify({ address, denom: fullDenom }); |
| 113 | + const msg = hyperweb.hvm.MessageComposer.fromPartial.eval({ |
| 114 | + address: contractAddress, |
| 115 | + creator: address, |
| 116 | + callee: "getBalance", |
| 117 | + args: [args] |
| 118 | + }); |
| 119 | + |
| 120 | + const result = await signingClient.signAndBroadcast(address, [msg], fee); |
| 121 | + assertIsDeliverTxSuccess(result); |
| 122 | + |
| 123 | + const response = hyperweb.hvm.MsgEvalResponse.fromProtoMsg(result.msgResponses[0]); |
| 124 | + expect(parseInt(response.result)).toBeGreaterThanOrEqual(1000); |
| 125 | + }); |
| 126 | + |
| 127 | + |
| 128 | +}); |
0 commit comments