forked from hanchon-live/ethermint-faucet-backend
-
Notifications
You must be signed in to change notification settings - Fork 0
/
faucet.ts
78 lines (69 loc) · 2.21 KB
/
faucet.ts
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
import { DirectSecp256k1HdWallet } from "@cosmjs/proto-signing";
import { SigningStargateClient, coins } from "@cosmjs/stargate";
import { stringToPath } from "@cosmjs/crypto";
import parse from "parse-duration";
const NETWORK_RPC_NODE = process.env.NETWORK_RPC_NODE;
const FAUCET_MNEMONIC = process.env.FAUCET_MNEMONIC;
const FAUCET_WAIT_PERIOD = process.env.FAUCET_WAIT_PERIOD || "24h";
const FAUCET_DISTRIBUTION_AMOUNT =
process.env.FAUCET_DISTRIBUTION_AMOUNT || 1000000000;
const FAUCET_DENOM = process.env.FAUCET_DENOM || "uakt";
const FAUCET_FEES = process.env.FAUCET_FEES || 5000;
const FAUCET_GAS = process.env.FAUCET_GAS || 180000;
const FAUCET_MEMO = process.env.FAUCET_MEMO;
const ADDRESS_PREFIX = process.env.ADDRESS_PREFIX || "ethm";
export const getWallet = () => {
return DirectSecp256k1HdWallet.fromMnemonic(FAUCET_MNEMONIC as any, {
prefix: ADDRESS_PREFIX,
hdPaths: [stringToPath("m/44'/60'/0'/0/0")],
});
};
export const getDenom = () => {
return FAUCET_DENOM;
};
export const getWaitPeriod = () => {
return parse(FAUCET_WAIT_PERIOD);
};
export const getDistributionAmount = () => {
return FAUCET_DISTRIBUTION_AMOUNT;
};
export const getChainId = async () => {
const wallet = await getWallet();
const client = await SigningStargateClient.connectWithSigner(
NETWORK_RPC_NODE as any,
wallet
);
return await client.getChainId();
};
export const sendTokens = async (recipient: any, amount: any) => {
const wallet = await getWallet();
console.log('sender ' + wallet)
console.log(JSON.stringify(wallet))
const [account] = await wallet.getAccounts();
console.log(account)
const client = await SigningStargateClient.connectWithSigner(
NETWORK_RPC_NODE as any,
wallet
);
if (!amount) amount = getDistributionAmount();
const sendMsg = {
typeUrl: "/cosmos.bank.v1beta1.MsgSend",
value: {
fromAddress: account.address,
toAddress: recipient,
amount: coins(parseInt(amount), FAUCET_DENOM),
},
};
console.log("1")
const fee = {
amount: coins(parseInt(FAUCET_FEES as any), FAUCET_DENOM),
gas: FAUCET_GAS,
};
console.log("2")
return await client.signAndBroadcast(
account.address,
[sendMsg],
fee as any,
FAUCET_MEMO
);
};