Skip to content

Commit 9260a3c

Browse files
Jorge-Lopesanilhelvaci
authored andcommitted
chore: add helper functions to dedicated file
1 parent ac3b7e8 commit 9260a3c

File tree

2 files changed

+256
-238
lines changed

2 files changed

+256
-238
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,237 @@
1+
import {
2+
createFundedWalletAndClient,
3+
makeIBCTransferMsg,
4+
} from '../../tools/ibc-transfer.js';
5+
import { execa } from 'execa';
6+
import path from 'path';
7+
import { fileURLToPath } from 'url';
8+
9+
export const fundRemote = async (
10+
t,
11+
destinationChain,
12+
denomToTransfer = 'ubld',
13+
amount = 100000000n,
14+
) => {
15+
const { retryUntilCondition, useChain } = t.context;
16+
17+
const { client, address, wallet } = await createFundedWalletAndClient(
18+
t.log,
19+
destinationChain,
20+
useChain,
21+
);
22+
const balancesResult = await retryUntilCondition(
23+
() => client.getAllBalances(address),
24+
coins => !!coins?.length,
25+
`Faucet balances found for ${address}`,
26+
);
27+
console.log('Balances:', balancesResult);
28+
29+
const { client: agoricClient, address: agoricAddress } =
30+
await createFundedWalletAndClient(t.log, 'agoric', useChain);
31+
32+
const balancesResultAg = await retryUntilCondition(
33+
() => agoricClient.getAllBalances(agoricAddress),
34+
coins => !!coins?.length,
35+
`Faucet balances found for ${agoricAddress}`,
36+
);
37+
console.log('Balances AGORIC:', balancesResultAg);
38+
39+
const transferArgs = makeIBCTransferMsg(
40+
{ denom: denomToTransfer, value: amount },
41+
{ address, chainName: destinationChain },
42+
{ address: agoricAddress, chainName: 'agoric' },
43+
Date.now(),
44+
useChain,
45+
);
46+
console.log('Transfer Args:', transferArgs);
47+
// TODO #9200 `sendIbcTokens` does not support `memo`
48+
// @ts-expect-error spread argument for concise code
49+
const txRes = await agoricClient.sendIbcTokens(...transferArgs);
50+
if (txRes && txRes.code !== 0) {
51+
console.error(txRes);
52+
throw Error(`failed to ibc transfer funds to ${denomToTransfer}`);
53+
}
54+
const { events: _events, ...txRest } = txRes;
55+
console.log(txRest);
56+
t.is(txRes.code, 0, `Transaction succeeded`);
57+
t.log(`Funds transferred to ${agoricAddress}`);
58+
59+
await retryUntilCondition(
60+
() => client.getAllBalances(address),
61+
coins => !!coins?.length,
62+
`${denomToTransfer} transferred to ${address}`,
63+
);
64+
65+
return {
66+
client,
67+
address,
68+
wallet,
69+
};
70+
};
71+
72+
const filename = fileURLToPath(import.meta.url);
73+
const dirname = path.dirname(filename);
74+
75+
export const setupXcsContracts = async t => {
76+
console.log('Setting XCS Contracts ...');
77+
const osmosisBranch = 'main';
78+
try {
79+
const scriptPath = path.resolve(dirname, '../../scripts/setup-xcs.sh');
80+
const { stdout } = await execa(scriptPath, [osmosisBranch]);
81+
console.log('setup-xcs script output:', stdout);
82+
} catch (error) {
83+
t.fail(`setup-xcs script failed with error: ${error}`);
84+
}
85+
};
86+
87+
export const createOsmosisPool = async t => {
88+
console.log('Creating Osmosis Pool ...');
89+
const tokenInDenom = 'ubld';
90+
const tokenInAmount = '250000';
91+
const tokenInWeight = '1';
92+
const tokenOutDenom = 'uosmo';
93+
const tokenOutAmount = '250000';
94+
const tokenOutWeight = '1';
95+
try {
96+
const scriptPath = path.resolve(
97+
dirname,
98+
'../../scripts/create-osmosis-pool.sh',
99+
);
100+
const { stdout } = await execa(scriptPath, [
101+
tokenInDenom,
102+
tokenInAmount,
103+
tokenInWeight,
104+
tokenOutDenom,
105+
tokenOutAmount,
106+
tokenOutWeight,
107+
]);
108+
console.log('create-osmosis-pool script output:', stdout);
109+
} catch (error) {
110+
t.fail(`create-osmosis-pool failed with error: ${error}`);
111+
}
112+
};
113+
114+
export const setupXcsChannelLink = async (t, chainA, chainB) => {
115+
console.log('Setting XCS Channel Links ...');
116+
try {
117+
const scriptPath = path.resolve(
118+
dirname,
119+
'../../scripts/setup-xcs-channel-link.sh',
120+
);
121+
const { stdout } = await execa(scriptPath, [chainA, chainB]);
122+
console.log('channel link setup output:', stdout);
123+
} catch (error) {
124+
t.fail(`channel link setup failed with error: ${error}`);
125+
}
126+
};
127+
128+
export const setupXcsPrefix = async t => {
129+
console.log('Setting XCS Prefixes ...');
130+
try {
131+
const scriptPath = path.resolve(
132+
dirname,
133+
'../../scripts/setup-xcs-prefix.sh',
134+
);
135+
const { stdout } = await execa(scriptPath);
136+
console.log('prefix setup output:', stdout);
137+
} catch (error) {
138+
t.fail(`prefix setup failed with error: ${error}`);
139+
}
140+
};
141+
142+
export const getXcsContractsAddress = async () => {
143+
const osmosisCLI =
144+
'kubectl exec -i osmosislocal-genesis-0 -c validator -- /bin/bash -c';
145+
146+
const registryQuery = `${osmosisCLI} "jq -r '.crosschain_registry.address' /contract-info.json"`;
147+
const swaprouterQuery = `${osmosisCLI} "jq -r '.swaprouter.address' /contract-info.json"`;
148+
const swapQuery = `${osmosisCLI} "jq -r '.crosschain_swaps.address' /contract-info.json"`;
149+
150+
const { stdout: registryAddress } = await execa(registryQuery, {
151+
shell: true,
152+
});
153+
const { stdout: swaprouterAddress } = await execa(swaprouterQuery, {
154+
shell: true,
155+
});
156+
const { stdout: swapAddress } = await execa(swapQuery, { shell: true });
157+
158+
return { registryAddress, swaprouterAddress, swapAddress };
159+
};
160+
161+
export const getXcsState = async () => {
162+
const { registryAddress } = await getXcsContractsAddress();
163+
164+
const osmosisExecQuery =
165+
'kubectl exec -i osmosislocal-genesis-0 -c validator -- osmosisd query wasm contract-state smart';
166+
167+
const channelObj = {
168+
get_channel_from_chain_pair: {
169+
source_chain: 'osmosis',
170+
destination_chain: 'agoric',
171+
},
172+
};
173+
const channelJson = `'${JSON.stringify(channelObj)}'`;
174+
const channelQuery = `${osmosisExecQuery} ${registryAddress} ${channelJson}`;
175+
176+
const { stdout: channel } = await execa(channelQuery, {
177+
shell: true,
178+
});
179+
180+
const prefixObj = {
181+
get_bech32_prefix_from_chain_name: {
182+
chain_name: 'osmosis',
183+
},
184+
};
185+
const prefixJson = `'${JSON.stringify(prefixObj)}'`;
186+
const prefixQuery = `${osmosisExecQuery} ${registryAddress} ${prefixJson}`;
187+
188+
const { stdout: prefix } = await execa(prefixQuery, {
189+
shell: true,
190+
});
191+
192+
const channelData = JSON.parse(channel).data;
193+
const prefixData = JSON.parse(prefix).data;
194+
195+
return { channelData, prefixData };
196+
};
197+
198+
export const getPoolRoute = async () => {
199+
const { swaprouterAddress } = await getXcsContractsAddress();
200+
201+
const osmosisExecQuery =
202+
'kubectl exec -i osmosislocal-genesis-0 -c validator -- osmosisd query wasm contract-state smart';
203+
204+
const routeObj = {
205+
get_route: {
206+
input_denom:
207+
'ibc/E7827844CB818EE9C4DB2C159F1543FF62B26213B44CE8029D5CEFE52F0EE596',
208+
output_denom: 'uosmo',
209+
},
210+
};
211+
const routeJson = `'${JSON.stringify(routeObj)}'`;
212+
const routeQuery = `${osmosisExecQuery} ${swaprouterAddress} ${routeJson}`;
213+
214+
const { stdout } = await execa(routeQuery, {
215+
shell: true,
216+
});
217+
218+
const routeData = JSON.parse(stdout).data;
219+
const route = routeData.pool_route[routeData.pool_route.length - 1];
220+
221+
return route;
222+
};
223+
224+
export const getPool = async poolId => {
225+
const osmosisExec =
226+
'kubectl exec -i osmosislocal-genesis-0 -c validator -- osmosisd';
227+
228+
const poolQuery = `${osmosisExec} query gamm pool ${poolId}`;
229+
230+
const { stdout } = await execa(poolQuery, {
231+
shell: true,
232+
});
233+
234+
const pool = JSON.parse(stdout).pool;
235+
236+
return pool;
237+
};

0 commit comments

Comments
 (0)