-
Notifications
You must be signed in to change notification settings - Fork 32
/
fundRescue-osmo.js
106 lines (98 loc) · 3.6 KB
/
fundRescue-osmo.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
import pkg from '@cosmjs/launchpad';
import {
QueryClient, setupBankExtension, SigningStargateClient
} from "@cosmjs/stargate";
import { Tendermint34Client } from '@cosmjs/tendermint-rpc';
import axios from 'axios';
import { createInterface } from "readline";
async function getUnbondingDelegations(address) {
return new Promise((resolve) => {
axios.get(`https://lcd-osmosis.blockapsis.com/cosmos/staking/v1beta1/delegators/${address}/unbonding_delegations`).then(res => {
if (res.data.unbonding_responses.length > 0) {
let unbondingResponse = res.data.unbonding_responses[0];
let completion = unbondingResponse.entries[0].completion_time;
resolve(new Date(completion).getTime());
} else {
resolve(0)
}
}).catch(err => {
resolve(0)
})
})
}
async function getQueryClient(rpcEndpoint) {
const tendermint34Client = await Tendermint34Client.connect(rpcEndpoint);
const queryClient = QueryClient.withExtensions(
tendermint34Client,
setupBankExtension
);
return queryClient;
}
async function transfer(client, from, recipient, amount) {
let ops = [];
let msg = {
typeUrl: "/cosmos.bank.v1beta1.MsgSend",
value: {
fromAddress: from,
toAddress: recipient,
amount: pkg.coins(amount, "uosmo")
},
};
ops.push(msg);
const fee = {
amount: pkg.coins(6000, "uosmo"),
gas: "80000",
};
let result = await client.signAndBroadcast(from, ops, fee, '');
if (result.code > 0) {
console.log("Failed. Please try again. " + result.rawLog);
} else {
console.log("Your fund is in safe place now. Tx Hash: " + result.transactionHash);
}
process.exit(0);
}
const sleep = ms => new Promise(resolve => setTimeout(resolve, ms));
function timeLeft(timeLeft) {
let days = Math.floor(timeLeft / (1000 * 60 * 60 * 24));
let hours = Math.floor((timeLeft % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
let minutes = Math.floor((timeLeft % (1000 * 60 * 60)) / (1000 * 60));
let seconds = Math.floor((timeLeft % (1000 * 60)) / 1000);
return `${days} Days, ${hours} Hours, ${minutes} Mins, ${seconds} secs`
}
async function start(mnemonic, recipient) {
const rpcEndpoint = "https://rpc-osmosis.blockapsis.com/"
const queryClient = await getQueryClient(rpcEndpoint);
const wallet = await pkg.Secp256k1HdWallet.fromMnemonic(
mnemonic, { prefix: 'osmo' }
);
const [account] = await wallet.getAccounts();
let completion = await getUnbondingDelegations(account.address);
console.log('completion', completion);
let current = new Date().getTime();
let diff = completion - current;
//if completion time is less than 5 minutes
while (diff > 5 * 60 * 1000) {
current = new Date().getTime();
diff = completion - current;
console.log(timeLeft(diff) + " until unbonding completion");
await sleep(10 * 1000);
}
let balance = await queryClient.bank.balance(account.address, "uosmo");
while (Number(balance.amount) / 1e6 < 0.1) {
console.log(`Your account has ${balance.amount / 1e6} OSMO`);
balance = await queryClient.bank.balance(account.address, "uosmo");
await sleep(1000);
}
const client = await SigningStargateClient.connectWithSigner(rpcEndpoint, wallet);
console.log(`Ready to transfer ${balance.amount / 1e6} OSMO to ${recipient}`);
transfer(client, account.address, recipient, Number(balance.amount) - 10000);
}
const readline = createInterface({
input: process.stdin,
output: process.stdout
});
readline.question("Please enter your mnemonic:\n", async (mnemonic) => {
readline.question("Please enter the recipient:\n", async (recipient) => {
start(mnemonic, recipient);
});
});