1
1
// @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" ;
4
4
5
5
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" ;
11
11
12
- describe ( ' Contract 2: AMM contract test' , ( ) => {
12
+ describe ( " Contract 2: AMM contract test" , ( ) => {
13
13
let wallet , denom , address , queryClient , signingClient ;
14
14
let chainInfo , getCoin , getRpcEndpoint , creditFromFaucet ;
15
15
let contractCode , contractIndex ;
16
16
17
17
let wallet2 , address2 ;
18
18
let fee ;
19
19
20
- const denom2 = "uhypweb" , uatom = "uatom" , uusdc = "uusdc" ;
20
+ const denom2 = "uhypweb" ,
21
+ uatom = "uatom" ,
22
+ uusdc = "uusdc" ;
21
23
22
24
beforeAll ( async ( ) => {
23
- ( {
24
- chainInfo,
25
- getCoin,
26
- getRpcEndpoint,
27
- creditFromFaucet
28
- } = useChain ( 'hyperweb' ) ) ;
25
+ ( { chainInfo, getCoin, getRpcEndpoint, creditFromFaucet } =
26
+ useChain ( "hyperweb" ) ) ;
29
27
denom = ( await getCoin ( ) ) . base ;
30
28
29
+ const commonPrefix = chainInfo . chain . bech32_prefix ;
30
+ const cosmosHdPath = "m/44'/118'/0'/0/0" ;
31
+
31
32
// 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
+ ] ) ;
35
39
address = ( await wallet . getAccounts ( ) ) [ 0 ] . address ;
36
- console . log ( `contract creator address for amm: ${ address } ` )
40
+ console . log ( `contract creator address for amm: ${ address } ` ) ;
37
41
38
42
// 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
+ ] ) ;
42
49
address2 = ( await wallet2 . getAccounts ( ) ) [ 0 ] . address ;
43
- console . log ( `contract creator address2 for amm: ${ address2 } ` )
50
+ console . log ( `contract creator address2 for amm: ${ address2 } ` ) ;
44
51
45
52
// Create custom cosmos interchain client
46
53
queryClient = await jsd . ClientFactory . createRPCQueryClient ( {
47
- rpcEndpoint : await getRpcEndpoint ( )
54
+ rpcEndpoint : await getRpcEndpoint ( ) ,
48
55
} ) ;
49
56
50
57
signingClient = await getSigningJsdClient ( {
51
58
rpcEndpoint : await getRpcEndpoint ( ) ,
52
- signer : wallet
59
+ signer : wallet ,
53
60
} ) ;
54
61
55
62
await creditFromFaucet ( address , denom ) ;
@@ -60,21 +67,24 @@ describe('Contract 2: AMM contract test', () => {
60
67
await creditFromFaucet ( address2 , denom ) ;
61
68
await creditFromFaucet ( address2 , denom2 ) ;
62
69
63
- fee = { amount : [ { denom, amount : ' 100000' } ] , gas : ' 550000' } ;
70
+ fee = { amount : [ { denom, amount : " 100000" } ] , gas : " 550000" } ;
64
71
65
72
await sleep ( 2000 ) ; // sleep for 1 sec to get tokens transferred from faucet successfully
66
73
} ) ;
67
74
68
- it ( ' check balance' , async ( ) => {
75
+ it ( " check balance" , async ( ) => {
69
76
const balance = await signingClient . getBalance ( address , denom ) ;
70
77
expect ( balance . amount ) . toEqual ( "10000000000" ) ;
71
78
expect ( balance . denom ) . toEqual ( denom ) ;
72
79
} ) ;
73
80
74
- it ( ' instantiate contract' , async ( ) => {
81
+ it ( " instantiate contract" , async ( ) => {
75
82
// 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" ) ;
78
88
79
89
const msg = jsd . jsd . MessageComposer . fromPartial . instantiate ( {
80
90
creator : address ,
@@ -85,20 +95,24 @@ describe('Contract 2: AMM contract test', () => {
85
95
assertIsDeliverTxSuccess ( result ) ;
86
96
87
97
// 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
+ ) ;
89
101
contractIndex = response . index ;
90
102
expect ( contractIndex ) . toBeGreaterThan ( 0 ) ;
91
103
console . log ( `contract index: ${ contractIndex } ` ) ;
92
104
} ) ;
93
105
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
+ } ) ;
96
110
expect ( response . contracts . code ) . toEqual ( contractCode ) ;
97
111
expect ( response . contracts . index ) . toEqual ( contractIndex ) ;
98
112
expect ( response . contracts . creator ) . toEqual ( address ) ;
99
113
} ) ;
100
114
101
- it ( ' perform getTotalSupply eval' , async ( ) => {
115
+ it ( " perform getTotalSupply eval" , async ( ) => {
102
116
const msg = jsd . jsd . MessageComposer . fromPartial . eval ( {
103
117
creator : address ,
104
118
index : contractIndex ,
@@ -109,11 +123,13 @@ describe('Contract 2: AMM contract test', () => {
109
123
const result = await signingClient . signAndBroadcast ( address , [ msg ] , fee ) ;
110
124
assertIsDeliverTxSuccess ( result ) ;
111
125
112
- const response = jsd . jsd . MsgEvalResponse . fromProtoMsg ( result . msgResponses [ 0 ] ) ;
126
+ const response = jsd . jsd . MsgEvalResponse . fromProtoMsg (
127
+ result . msgResponses [ 0 ]
128
+ ) ;
113
129
expect ( response . result ) . toEqual ( "0" ) ;
114
130
} ) ;
115
131
116
- it ( ' perform addLiquidity eval' , async ( ) => {
132
+ it ( " perform addLiquidity eval" , async ( ) => {
117
133
const msg = jsd . jsd . MessageComposer . fromPartial . eval ( {
118
134
creator : address ,
119
135
index : contractIndex ,
@@ -124,19 +140,21 @@ describe('Contract 2: AMM contract test', () => {
124
140
const result = await signingClient . signAndBroadcast ( address , [ msg ] , fee ) ;
125
141
assertIsDeliverTxSuccess ( result ) ;
126
142
127
- const response = jsd . jsd . MsgEvalResponse . fromProtoMsg ( result . msgResponses [ 0 ] ) ;
143
+ const response = jsd . jsd . MsgEvalResponse . fromProtoMsg (
144
+ result . msgResponses [ 0 ]
145
+ ) ;
128
146
expect ( response . result ) . toEqual ( "null" ) ;
129
147
} ) ;
130
148
131
- it ( ' check balance after addLiquidity' , async ( ) => {
149
+ it ( " check balance after addLiquidity" , async ( ) => {
132
150
const usdcBalance = await signingClient . getBalance ( address , uusdc ) ;
133
151
expect ( usdcBalance . amount ) . toEqual ( "9950000000" ) ;
134
152
135
153
const atomBalance = await signingClient . getBalance ( address , uatom ) ;
136
154
expect ( atomBalance . amount ) . toEqual ( "9950000000" ) ;
137
155
} ) ;
138
156
139
- it ( ' perform swap eval' , async ( ) => {
157
+ it ( " perform swap eval" , async ( ) => {
140
158
const msg = jsd . jsd . MessageComposer . fromPartial . eval ( {
141
159
creator : address ,
142
160
index : contractIndex ,
@@ -147,7 +165,9 @@ describe('Contract 2: AMM contract test', () => {
147
165
const result = await signingClient . signAndBroadcast ( address , [ msg ] , fee ) ;
148
166
assertIsDeliverTxSuccess ( result ) ;
149
167
150
- const response = jsd . jsd . MsgEvalResponse . fromProtoMsg ( result . msgResponses [ 0 ] ) ;
168
+ const response = jsd . jsd . MsgEvalResponse . fromProtoMsg (
169
+ result . msgResponses [ 0 ]
170
+ ) ;
151
171
expect ( response . result ) . toEqual ( "9969998.011982398" ) ;
152
172
} ) ;
153
173
} ) ;
0 commit comments