-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.js
86 lines (80 loc) · 2.04 KB
/
utils.js
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
/**
* TAKEN FROM https://github.com/axelarnetwork/axelar-local-gmp-examples/blob/main/scripts/utils.js
*/
const {
Contract,
ContractFactory,
constants: { AddressZero },
utils: { keccak256, defaultAbiCoder },
} = require("ethers");
const axios = require("axios");
const axelarLocal = require("@axelar-network/axelar-local-dev");
const { AxelarAssetTransfer } = require("@axelar-network/axelarjs-sdk");
function getDepositAddress(
env,
source,
destination,
destinationAddress,
symbol
) {
if (env == "testnet") {
const listing = {
aUSDC: "uusdc",
};
const sdk = new AxelarAssetTransfer({
environment: "testnet",
auth: "local",
});
return sdk.getDepositAddress(
source,
destination,
destinationAddress,
listing[symbol]
);
} else {
return axelarLocal.getDepositAddress(
source,
destination,
destinationAddress,
symbol,
8500
);
}
}
async function getGasPrice(env, source, destination, tokenAddress) {
if (env == "local") return 1;
if (env != "testnet") throw Error('env needs to be "local" or "testnet".');
const api_url = "https://devnet.api.gmp.axelarscan.io";
const requester = axios.create({ baseURL: api_url });
const params = {
method: "getGasPrice",
destinationChain: destination.name,
sourceChain: source.name,
};
// set gas token address to params
if (tokenAddress != AddressZero) {
params.sourceTokenAddress = tokenAddress;
} else {
params.sourceTokenSymbol = source.tokenSymbol;
}
// send request
const response = await requester.get("/", { params }).catch((error) => {
return { data: { error } };
});
const result = response.data.result;
const dest = result.destination_native_token;
const destPrice = 1e18 * dest.gas_price * dest.token_price.usd;
return destPrice / result.source_token.token_price.usd;
}
function sleep(ms) {
return new Promise((resolve) => {
setTimeout(() => {
resolve();
}, ms);
});
}
module.exports = {
getGasPrice,
getDepositAddress,
sleep,
};