Skip to content

Commit d685818

Browse files
committed
test(hello): added hello contract tests
1 parent 0a05752 commit d685818

File tree

1 file changed

+93
-0
lines changed

1 file changed

+93
-0
lines changed
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
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('Hello 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+
wallet = await DirectSecp256k1HdWallet.fromMnemonic(generateMnemonic(), {
29+
prefix: chainInfo.chain.bech32_prefix
30+
});
31+
address = (await wallet.getAccounts())[0].address;
32+
console.log(`Contract creator address: ${address}`);
33+
34+
queryClient = await hyperweb.ClientFactory.createRPCQueryClient({
35+
rpcEndpoint: await getRpcEndpoint()
36+
});
37+
38+
signingClient = await getSigningHyperwebClient({
39+
rpcEndpoint: await getRpcEndpoint(),
40+
signer: wallet
41+
});
42+
43+
fee = { amount: [{ denom, amount: '100000' }], gas: '550000' };
44+
45+
await creditFromFaucet(address);
46+
await sleep(10000);
47+
});
48+
49+
it('Check initial balance', async () => {
50+
const balance = await signingClient.getBalance(address, denom);
51+
expect(balance.amount).toEqual("10000000000");
52+
expect(balance.denom).toEqual(denom);
53+
});
54+
55+
it('Instantiate hello contract', async () => {
56+
const contractPath = path.join(
57+
__dirname,
58+
"../dist/contracts/hello.js"
59+
);
60+
contractCode = fs.readFileSync(contractPath, "utf8");
61+
62+
const msg = hyperweb.hvm.MessageComposer.fromPartial.instantiate({
63+
creator: address,
64+
code: contractCode,
65+
source: "test_source",
66+
});
67+
68+
const result = await signingClient.signAndBroadcast(address, [msg], fee);
69+
assertIsDeliverTxSuccess(result);
70+
71+
const response = hyperweb.hvm.MsgInstantiateResponse.fromProtoMsg(result.msgResponses[0]);
72+
contractIndex = response.index;
73+
contractAddress = response.address;
74+
expect(contractIndex).toBeGreaterThan(0);
75+
console.log(`contract instantiated at index: ${contractIndex} and address ${contractAddress}`);
76+
});
77+
78+
it('Invoke hello function', async () => {
79+
const msg = hyperweb.hvm.MessageComposer.fromPartial.eval({
80+
address: contractAddress,
81+
creator: address,
82+
callee: "hello",
83+
args: [JSON.stringify("World")]
84+
});
85+
86+
const result = await signingClient.signAndBroadcast(address, [msg], fee);
87+
assertIsDeliverTxSuccess(result);
88+
89+
const response = hyperweb.hvm.MsgEvalResponse.fromProtoMsg(result.msgResponses[0]);
90+
expect(response.result).toEqual("Hello, World");
91+
});
92+
93+
});

0 commit comments

Comments
 (0)