Repository navigation
Expand file tree
/
Copy pathhelper.js
More file actions
124 lines (115 loc) · 4.18 KB
/
Copy pathhelper.js
File metadata and controls
124 lines (115 loc) · 4.18 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
import {DirectSecp256k1HdWallet, Registry} from "@cosmjs/proto-signing";
import {
SigningStargateClient,
defaultRegistryTypes as defaultStargateTypes,
QueryClient,
createProtobufRpcClient
} from "@cosmjs/stargate";
import {registry as ibcTransferRegistry} from "persistenceonejs/ibc/applications/transfer/v1/tx.registry.js";
import {registry as wasmRegistry} from "persistenceonejs/cosmwasm/wasm/v1/tx.registry.js";
import {COMET_BFT_VERSIONS, MNEMONIC} from "./constants.js";
import {Tendermint34Client, Tendermint37Client} from "@cosmjs/tendermint-rpc";
import {MAX_SPREAD, USDC_DYDX_POOL_ID} from "./swapper.js";
import {SKIP_API_URL, SkipRouter} from "@skip-router/core";
export const CustomRegistry = new Registry([...defaultStargateTypes, ...ibcTransferRegistry, ...wasmRegistry]);
export function SkipClient() {
return new SkipRouter({
apiURL: SKIP_API_URL,
getCosmosSigner: async (chainID) => {
switch (chainID) {
case "dydx-mainnet-1":
return DirectSecp256k1HdWallet.fromMnemonic(MNEMONIC, {prefix: "dydx"});
case "noble-1":
return DirectSecp256k1HdWallet.fromMnemonic(MNEMONIC, {prefix: "noble"});
case "osmosis-1":
return DirectSecp256k1HdWallet.fromMnemonic(MNEMONIC, {prefix: "osmo"});
}
return DirectSecp256k1HdWallet.fromMnemonic(MNEMONIC)
},
});
}
export async function RpcClient(chainInfo) {
let tendermintClient = {}
switch (chainInfo.tmVersion) {
case COMET_BFT_VERSIONS.comet34:
tendermintClient = await Tendermint34Client.connect(chainInfo.rpc);
break
case COMET_BFT_VERSIONS.comet37:
tendermintClient = await Tendermint37Client.connect(chainInfo.rpc);
break
case COMET_BFT_VERSIONS.comet38:
tendermintClient = await Tendermint37Client.connect(chainInfo.rpc);
break
}
const queryClient = new QueryClient(tendermintClient);
return [tendermintClient, createProtobufRpcClient(queryClient)];
}
export async function CreateWallet(address) {
const wallet = await DirectSecp256k1HdWallet.fromMnemonic(MNEMONIC, {
prefix: address.prefix,
hdPaths: [address.hdPath]
});
const [firstAccount] = await wallet.getAccounts();
if (firstAccount.address !== address.address) {
throw new Error("Incorrect address generated, expected: " + address.address + ", got: " + firstAccount.address);
}
return [wallet, firstAccount]
}
export async function CreateSigningClient(chainInfo, wallet) {
return await SigningStargateClient.connectWithSigner(chainInfo.rpc, wallet, {
prefix: chainInfo.prefix,
registry: CustomRegistry,
gasPrice: chainInfo.gasPrice,
})
}
export async function CreateSigningClientFromAddress(address) {
const [wallet, _] = await CreateWallet(address)
return CreateSigningClient(address.chainInfo, wallet)
}
export function GenerateOnSwapRequest(offerAsset, askAsset, amount) {
return {
on_swap: {
offer_asset: {
native_token: {
denom: offerAsset
}
},
ask_asset: {
native_token: {
denom: askAsset
}
},
amount: amount,
max_spread: MAX_SPREAD,
swap_type: {
give_in: {}
}
}
}
}
export function GenerateSwapMsg(minReceive, amount, assetIn, assetOut, poolID) {
return {
swap: {
min_receive: minReceive,
swap_request: {
amount: amount,
asset_in: {
native_token: {
denom: assetIn
}
},
asset_out: {
native_token: {
denom: assetOut
}
},
max_spread: MAX_SPREAD,
pool_id: poolID,
swap_type: {
give_in: {}
}
}
}
}
}
export const sleep = ms => new Promise(r => setTimeout(r, ms));