Skip to content

Commit f2542ac

Browse files
authored
Merge branch 'main' into anmol/build-before-test
2 parents f52a2dc + d403d0e commit f2542ac

File tree

16 files changed

+568
-135
lines changed

16 files changed

+568
-135
lines changed

__tests__/contract1.test.ts

Lines changed: 5 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ describe('JSD tests', () => {
1414
let chainInfo, getCoin, getRpcEndpoint, creditFromFaucet;
1515
let contractCode, contractIndex;
1616

17+
let fee;
18+
1719
beforeAll(async () => {
1820
({
1921
chainInfo,
@@ -40,6 +42,9 @@ describe('JSD tests', () => {
4042
signer: wallet
4143
});
4244

45+
// set default fee
46+
fee = {amount: [{denom, amount: '100000'}], gas: '550000'};
47+
4348
await creditFromFaucet(address);
4449
await sleep(2000); // sleep for 1 sec to get tokens transferred from faucet successfully
4550
});
@@ -55,16 +60,6 @@ describe('JSD tests', () => {
5560
const contractPath = path.join(__dirname, '../dist/contracts/bundle1.js');
5661
contractCode = fs.readFileSync(contractPath, 'utf8');
5762

58-
const fee = {
59-
amount: [
60-
{
61-
denom,
62-
amount: '100000'
63-
}
64-
],
65-
gas: '550000'
66-
};
67-
6863
const msg = jsd.jsd.MessageComposer.fromPartial.instantiate({
6964
creator: address,
7065
code: contractCode,
@@ -93,16 +88,6 @@ describe('JSD tests', () => {
9388
});
9489

9590
it('perform inc eval', async () => {
96-
const fee = {
97-
amount: [
98-
{
99-
denom,
100-
amount: '100000'
101-
}
102-
],
103-
gas: '550000'
104-
};
105-
10691
const msg = jsd.jsd.MessageComposer.fromPartial.eval({
10792
creator: address,
10893
index: contractIndex,
@@ -118,16 +103,6 @@ describe('JSD tests', () => {
118103
});
119104

120105
it('eval read from eval', async () => {
121-
const fee = {
122-
amount: [
123-
{
124-
denom,
125-
amount: '100000'
126-
}
127-
],
128-
gas: '550000'
129-
};
130-
131106
const msg = jsd.jsd.MessageComposer.fromPartial.eval({
132107
creator: address,
133108
index: contractIndex,
@@ -147,16 +122,6 @@ describe('JSD tests', () => {
147122
});
148123

149124
it('perform dec eval', async () => {
150-
const fee = {
151-
amount: [
152-
{
153-
denom,
154-
amount: '100000'
155-
}
156-
],
157-
gas: '550000'
158-
};
159-
160125
const msg = jsd.jsd.MessageComposer.fromPartial.eval({
161126
creator: address,
162127
index: contractIndex,
@@ -172,16 +137,6 @@ describe('JSD tests', () => {
172137
});
173138

174139
it('eval read from eval', async () => {
175-
const fee = {
176-
amount: [
177-
{
178-
denom,
179-
amount: '100000'
180-
}
181-
],
182-
gas: '550000'
183-
};
184-
185140
const msg = jsd.jsd.MessageComposer.fromPartial.eval({
186141
creator: address,
187142
index: contractIndex,

__tests__/contract2.test.ts

Lines changed: 153 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,153 @@
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+
});

configs/ci.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ chains:
55
- id: hyperweb
66
name: custom
77
numValidators: 1
8-
image: ghcr.io/cosmology-tech/jsd:latest
8+
image: ghcr.io/cosmology-tech/jsd:0.1.1
99
home: /root/.jsd
1010
binary: jsdd
1111
prefix: hyper

configs/local.yaml

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,12 @@ chains:
55
- id: hyperweb
66
name: custom
77
numValidators: 1
8-
image: ghcr.io/cosmology-tech/jsd:latest
8+
image: ghcr.io/cosmology-tech/jsd:0.1.1
99
home: /root/.jsd
1010
binary: jsdd
1111
prefix: hyper
1212
denom: uhyper
13-
coins: 100000000000000uhyper,100000000000000uweb
13+
coins: 100000000000000uhyper,100000000000000uweb,100000000000000ATOM,100000000000000USDC
1414
hdPath: m/44'/118'/0'/0/0
1515
coinType: 118
1616
repo: https://github.com/cosmology-tech/jsd
@@ -21,12 +21,6 @@ chains:
2121
faucet:
2222
enabled: true
2323
type: starship
24-
## test accounts, add more accounts as needed
25-
balances:
26-
- address: hyper1e9ucjn5fjmetky5wezzcsccp7hqcwzrrhthpf5
27-
amount: "2000000000000uhyper"
28-
- address: hyper10eykchznjdn8jdlwaj5v9wvlmdsp6kxx7u0hv9
29-
amount: "2000000000000uhyper"
3024

3125
registry:
3226
enabled: true

dist/contracts/bundle1.js

Lines changed: 6 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/contracts/bundle1.js.map

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)