-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathbank.test.ts
More file actions
134 lines (110 loc) · 4.77 KB
/
bank.test.ts
File metadata and controls
134 lines (110 loc) · 4.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
// @ts-nocheck
import { DirectSecp256k1HdWallet } from '@cosmjs/proto-signing';
import { assertIsDeliverTxSuccess } from '@cosmjs/stargate';
import path from "path";
import fs from 'fs';
import { getSigningHyperwebClient, hyperweb, google } from 'hyperwebjs';
import { useChain, generateMnemonic } from 'starshipjs';
import { sleep } from '../test-utils/sleep';
import './setup.test';
describe('Bank Contract Tests', () => {
let wallet, denom, address, queryClient, signingClient;
let chainInfo, getCoin, getRpcEndpoint, creditFromFaucet;
let contractCode, contractIndex, contractAddress;
let fee;
let recipientWallet, recipientAddress;
beforeAll(async () => {
({
chainInfo,
getCoin,
getRpcEndpoint,
creditFromFaucet
} = useChain('hyperweb'));
denom = (await getCoin()).base;
wallet = await DirectSecp256k1HdWallet.fromMnemonic(generateMnemonic(), {
prefix: chainInfo.chain.bech32_prefix
});
address = (await wallet.getAccounts())[0].address;
console.log(`contract creator address: ${address}`);
recipientWallet = await DirectSecp256k1HdWallet.fromMnemonic(generateMnemonic(), {
prefix: chainInfo.chain.bech32_prefix
});
recipientAddress = (await recipientWallet.getAccounts())[0].address;
console.log(`recipient address: ${recipientAddress}`);
queryClient = await hyperweb.ClientFactory.createRPCQueryClient({
rpcEndpoint: await getRpcEndpoint()
});
signingClient = await getSigningHyperwebClient({
rpcEndpoint: await getRpcEndpoint(),
signer: wallet
});
fee = { amount: [{ denom, amount: '100000' }], gas: '550000' };
await creditFromFaucet(address);
await sleep(10000);
});
it('Check initial balance', async () => {
const balance = await signingClient.getBalance(address, denom);
expect(balance.amount).toEqual("10000000000");
expect(balance.denom).toEqual(denom);
});
it('Instantiate Bank contract', async () => {
const contractPath = path.join(
__dirname,
"../dist/contracts/bank.js"
);
contractCode = fs.readFileSync(contractPath, "utf8");
const msg = hyperweb.hvm.MessageComposer.fromPartial.instantiate({
creator: address,
code: contractCode,
source: "test_source",
});
const result = await signingClient.signAndBroadcast(address, [msg], fee);
assertIsDeliverTxSuccess(result);
const response = hyperweb.hvm.MsgInstantiateResponse.fromProtoMsg(result.msgResponses[0]);
contractIndex = response.index;
contractAddress = response.address;
expect(contractIndex).toBeGreaterThan(0);
console.log(`contract instantiated at index: ${contractIndex} and address ${contractAddress}`);
});
it('Call balance function', async () => {
const args = JSON.stringify({ address, denom });
const msg = hyperweb.hvm.MessageComposer.fromPartial.eval({
address: contractAddress,
creator: address,
callee: "balance",
args: [args]
});
const result = await signingClient.signAndBroadcast(address, [msg], fee);
assertIsDeliverTxSuccess(result);
const response = hyperweb.hvm.MsgEvalResponse.fromProtoMsg(result.msgResponses[0]);
expect(parseInt(response.result)).toBeGreaterThanOrEqual(0);
});
it('Transfer funds to another address and check balance', async () => {
const transferArgs = JSON.stringify({
from: address,
to: recipientAddress,
amount: 1000,
denom
});
const transferMsg = hyperweb.hvm.MessageComposer.fromPartial.eval({
address: contractAddress,
creator: address,
callee: "transfer",
args: [transferArgs]
});
const transferResult = await signingClient.signAndBroadcast(address, [transferMsg], fee);
assertIsDeliverTxSuccess(transferResult);
const checkArgs = JSON.stringify({ address: recipientAddress, denom });
const checkMsg = hyperweb.hvm.MessageComposer.fromPartial.eval({
address: contractAddress,
creator: address,
callee: "balance",
args: [checkArgs]
});
const checkResult = await signingClient.signAndBroadcast(address, [checkMsg], fee);
assertIsDeliverTxSuccess(checkResult);
const checkResponse = hyperweb.hvm.MsgEvalResponse.fromProtoMsg(checkResult.msgResponses[0]);
console.log(`recipient balance: ${checkResponse.result}`);
expect(parseInt(checkResponse.result)).toBeGreaterThanOrEqual(1000);
});
});