Skip to content

Commit 63e9005

Browse files
committed
refactor: remove cosmjs from hyperweb
1 parent 8ff409f commit 63e9005

File tree

6 files changed

+361
-182
lines changed

6 files changed

+361
-182
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,62 @@
11
// @ts-nocheck
2-
import { DirectSecp256k1HdWallet } from '@cosmjs/proto-signing';
3-
import { assertIsDeliverTxSuccess } from '@cosmjs/stargate';
2+
import { Secp256k1HDWallet } from "@interchainjs/cosmos/wallets/secp256k1hd";
3+
import { assertIsDeliverTxSuccess } from "@interchainjs/cosmos/utils";
44

55
import path from "path";
6-
import fs from 'fs';
7-
import { getSigningJsdClient, jsd } from 'hyperwebjs'
8-
import { useChain, generateMnemonic } from 'starshipjs';
9-
import { sleep } from '../test-utils/sleep';
10-
import './setup.test';
6+
import fs from "fs";
7+
import { getSigningJsdClient, jsd } from "hyperwebjs";
8+
import { useChain, generateMnemonic } from "starshipjs";
9+
import { sleep } from "../test-utils/sleep";
10+
import "./setup.test";
1111

12-
describe('Contract 2: AMM contract test', () => {
12+
describe("Contract 2: AMM contract test", () => {
1313
let wallet, denom, address, queryClient, signingClient;
1414
let chainInfo, getCoin, getRpcEndpoint, creditFromFaucet;
1515
let contractCode, contractIndex;
1616

1717
let wallet2, address2;
1818
let fee;
1919

20-
const denom2 = "uhypweb", uatom = "uatom", uusdc = "uusdc";
20+
const denom2 = "uhypweb",
21+
uatom = "uatom",
22+
uusdc = "uusdc";
2123

2224
beforeAll(async () => {
23-
({
24-
chainInfo,
25-
getCoin,
26-
getRpcEndpoint,
27-
creditFromFaucet
28-
} = useChain('hyperweb'));
25+
({ chainInfo, getCoin, getRpcEndpoint, creditFromFaucet } =
26+
useChain("hyperweb"));
2927
denom = (await getCoin()).base;
3028

29+
const commonPrefix = chainInfo.chain.bech32_prefix;
30+
const cosmosHdPath = "m/44'/118'/0'/0/0";
31+
3132
// Initialize wallet
32-
wallet = await DirectSecp256k1HdWallet.fromMnemonic(generateMnemonic(), {
33-
prefix: chainInfo.chain.bech32_prefix
34-
});
33+
wallet = Secp256k1HDWallet.fromMnemonic(generateMnemonic(), [
34+
{
35+
prefix: commonPrefix,
36+
hdPath: cosmosHdPath,
37+
},
38+
]);
3539
address = (await wallet.getAccounts())[0].address;
36-
console.log(`contract creator address for amm: ${address}`)
40+
console.log(`contract creator address for amm: ${address}`);
3741

3842
// Initialize wallet2
39-
wallet2 = await DirectSecp256k1HdWallet.fromMnemonic(generateMnemonic(), {
40-
prefix: chainInfo.chain.bech32_prefix
41-
});
43+
wallet2 = Secp256k1HDWallet.fromMnemonic(generateMnemonic(), [
44+
{
45+
prefix: commonPrefix,
46+
hdPath: cosmosHdPath,
47+
},
48+
]);
4249
address2 = (await wallet2.getAccounts())[0].address;
43-
console.log(`contract creator address2 for amm: ${address2}`)
50+
console.log(`contract creator address2 for amm: ${address2}`);
4451

4552
// Create custom cosmos interchain client
4653
queryClient = await jsd.ClientFactory.createRPCQueryClient({
47-
rpcEndpoint: await getRpcEndpoint()
54+
rpcEndpoint: await getRpcEndpoint(),
4855
});
4956

5057
signingClient = await getSigningJsdClient({
5158
rpcEndpoint: await getRpcEndpoint(),
52-
signer: wallet
59+
signer: wallet,
5360
});
5461

5562
await creditFromFaucet(address, denom);
@@ -60,21 +67,24 @@ describe('Contract 2: AMM contract test', () => {
6067
await creditFromFaucet(address2, denom);
6168
await creditFromFaucet(address2, denom2);
6269

63-
fee = {amount: [{denom, amount: '100000'}], gas: '550000'};
70+
fee = { amount: [{ denom, amount: "100000" }], gas: "550000" };
6471

6572
await sleep(2000); // sleep for 1 sec to get tokens transferred from faucet successfully
6673
});
6774

68-
it('check balance', async () => {
75+
it("check balance", async () => {
6976
const balance = await signingClient.getBalance(address, denom);
7077
expect(balance.amount).toEqual("10000000000");
7178
expect(balance.denom).toEqual(denom);
7279
});
7380

74-
it('instantiate contract', async () => {
81+
it("instantiate contract", async () => {
7582
// Read contract code from external file
76-
const contractPath = path.join(__dirname, '../dist/contracts/ammContract.js');
77-
contractCode = fs.readFileSync(contractPath, 'utf8');
83+
const contractPath = path.join(
84+
__dirname,
85+
"../dist/contracts/ammContract.js"
86+
);
87+
contractCode = fs.readFileSync(contractPath, "utf8");
7888

7989
const msg = jsd.jsd.MessageComposer.fromPartial.instantiate({
8090
creator: address,
@@ -85,20 +95,24 @@ describe('Contract 2: AMM contract test', () => {
8595
assertIsDeliverTxSuccess(result);
8696

8797
// Parse the response to get the contract index
88-
const response = jsd.jsd.MsgInstantiateResponse.fromProtoMsg(result.msgResponses[0]);
98+
const response = jsd.jsd.MsgInstantiateResponse.fromProtoMsg(
99+
result.msgResponses[0]
100+
);
89101
contractIndex = response.index;
90102
expect(contractIndex).toBeGreaterThan(0);
91103
console.log(`contract index: ${contractIndex}`);
92104
});
93105

94-
it('query for contract based on index', async () => {
95-
const response = await queryClient.jsd.jsd.contracts({index: contractIndex});
106+
it("query for contract based on index", async () => {
107+
const response = await queryClient.jsd.jsd.contracts({
108+
index: contractIndex,
109+
});
96110
expect(response.contracts.code).toEqual(contractCode);
97111
expect(response.contracts.index).toEqual(contractIndex);
98112
expect(response.contracts.creator).toEqual(address);
99113
});
100114

101-
it('perform getTotalSupply eval', async () => {
115+
it("perform getTotalSupply eval", async () => {
102116
const msg = jsd.jsd.MessageComposer.fromPartial.eval({
103117
creator: address,
104118
index: contractIndex,
@@ -109,11 +123,13 @@ describe('Contract 2: AMM contract test', () => {
109123
const result = await signingClient.signAndBroadcast(address, [msg], fee);
110124
assertIsDeliverTxSuccess(result);
111125

112-
const response = jsd.jsd.MsgEvalResponse.fromProtoMsg(result.msgResponses[0]);
126+
const response = jsd.jsd.MsgEvalResponse.fromProtoMsg(
127+
result.msgResponses[0]
128+
);
113129
expect(response.result).toEqual("0");
114130
});
115131

116-
it('perform addLiquidity eval', async () => {
132+
it("perform addLiquidity eval", async () => {
117133
const msg = jsd.jsd.MessageComposer.fromPartial.eval({
118134
creator: address,
119135
index: contractIndex,
@@ -124,19 +140,21 @@ describe('Contract 2: AMM contract test', () => {
124140
const result = await signingClient.signAndBroadcast(address, [msg], fee);
125141
assertIsDeliverTxSuccess(result);
126142

127-
const response = jsd.jsd.MsgEvalResponse.fromProtoMsg(result.msgResponses[0]);
143+
const response = jsd.jsd.MsgEvalResponse.fromProtoMsg(
144+
result.msgResponses[0]
145+
);
128146
expect(response.result).toEqual("null");
129147
});
130148

131-
it('check balance after addLiquidity', async () => {
149+
it("check balance after addLiquidity", async () => {
132150
const usdcBalance = await signingClient.getBalance(address, uusdc);
133151
expect(usdcBalance.amount).toEqual("9950000000");
134152

135153
const atomBalance = await signingClient.getBalance(address, uatom);
136154
expect(atomBalance.amount).toEqual("9950000000");
137155
});
138156

139-
it('perform swap eval', async () => {
157+
it("perform swap eval", async () => {
140158
const msg = jsd.jsd.MessageComposer.fromPartial.eval({
141159
creator: address,
142160
index: contractIndex,
@@ -147,7 +165,9 @@ describe('Contract 2: AMM contract test', () => {
147165
const result = await signingClient.signAndBroadcast(address, [msg], fee);
148166
assertIsDeliverTxSuccess(result);
149167

150-
const response = jsd.jsd.MsgEvalResponse.fromProtoMsg(result.msgResponses[0]);
168+
const response = jsd.jsd.MsgEvalResponse.fromProtoMsg(
169+
result.msgResponses[0]
170+
);
151171
expect(response.result).toEqual("9969998.011982398");
152172
});
153173
});
+38-13
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,52 @@
1-
// @ts-nocheck
2-
import path from 'path';
1+
import path from "path";
32

4-
import { StargateClient } from '@cosmjs/stargate';
5-
6-
import { ConfigContext, useChain, useRegistry } from 'starshipjs';
3+
import { SigningClient } from "@interchainjs/cosmos/signing-client";
4+
import { DirectGenericOfflineSigner } from "@interchainjs/cosmos/types/wallet";
5+
import { Secp256k1HDWallet } from "@interchainjs/cosmos/wallets/secp256k1hd";
6+
import {
7+
ConfigContext,
8+
generateMnemonic,
9+
useChain,
10+
useRegistry,
11+
} from "starshipjs";
712

813
beforeAll(async () => {
9-
const configFile = path.join(__dirname, '..', 'configs', 'local.yaml');
14+
const configFile = path.join(__dirname, "..", "configs", "local.yaml");
1015
ConfigContext.setConfigFile(configFile);
1116
ConfigContext.setRegistry(await useRegistry(configFile));
1217
});
1318

14-
describe('Test clients', () => {
15-
let client;
19+
describe("Test clients", () => {
20+
let client: SigningClient;
1621

1722
beforeAll(async () => {
18-
const { getRpcEndpoint } = useChain('hyperweb');
19-
client = await StargateClient.connect(await getRpcEndpoint());
23+
const { getRpcEndpoint, chainInfo } = useChain("hyperweb");
24+
25+
const commonPrefix = chainInfo?.chain?.bech32_prefix;
26+
const cosmosHdPath = "m/44'/118'/0'/0/0";
27+
28+
const directWallet = Secp256k1HDWallet.fromMnemonic(generateMnemonic(), [
29+
{
30+
prefix: commonPrefix,
31+
hdPath: cosmosHdPath,
32+
},
33+
]);
34+
const directSigner = directWallet.toOfflineDirectSigner();
35+
36+
client = await SigningClient.connectWithSigner(
37+
await getRpcEndpoint(),
38+
new DirectGenericOfflineSigner(directSigner),
39+
{
40+
signerOptions: {
41+
prefix: commonPrefix,
42+
},
43+
}
44+
);
2045
});
2146

22-
it('check chain height', async () => {
23-
const height = await client.getHeight();
47+
it("check chain height", async () => {
48+
const { header } = await client.getBlock(1);
2449

25-
expect(height).toBeGreaterThan(0);
50+
expect(header.height).toBeGreaterThan(0);
2651
});
2752
});

0 commit comments

Comments
 (0)