forked from near/multisig-tool
-
Notifications
You must be signed in to change notification settings - Fork 0
/
staking.js
108 lines (96 loc) · 4.07 KB
/
staking.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
107
import * as nearAPI from 'near-api-js';
import Mustache from 'mustache';
import * as utils from './utils.js';
import { funcCall, setAccountSigner } from './actions';
async function fetchPools(masterAccount) {
const result = await masterAccount.connection.provider.sendJsonRpc('validators', [null]);
const pools = new Set();
const stakes = new Map();
result.current_validators.forEach((validator) => {
pools.add(validator.account_id);
stakes.set(validator.account_id, validator.stake);
});
result.next_validators.forEach((validator) => pools.add(validator.account_id));
result.current_proposals.forEach((validator) => pools.add(validator.account_id));
let poolsWithFee = [];
let promises = []
pools.forEach((accountId) => {
promises.push((async () => {
let stake = nearAPI.utils.format.formatNearAmount(stakes.get(accountId), 2);
let fee = await masterAccount.viewFunction(accountId, 'get_reward_fee_fraction', {});
poolsWithFee.push({ poolId: accountId, stake, fee: `${(fee.numerator * 100 / fee.denominator)}%` });
})());
});
await Promise.all(promises);
return poolsWithFee;
}
async function loadAccountStaking(accountId) {
let masterAccount = await window.near.account(accountId);
let pools = await fetchPools(masterAccount);
const template = document.getElementById('template-staking').innerHTML;
let staking = [];
await Promise.all(pools.map(async ({ poolId }) => {
let totalStaked = await masterAccount.viewFunction(poolId, "get_account_staked_balance", { account_id: accountId });
let totalUnstaked = await masterAccount.viewFunction(poolId, "get_account_unstaked_balance", { account_id: accountId });
if (totalStaked != "0" || totalUnstaked != "0") {
staking.push({
poolId,
totalStaked: nearAPI.utils.format.formatNearAmount(totalStaked, 2),
totalUnstaked: nearAPI.utils.format.formatNearAmount(totalUnstaked, 2),
});
}
}));
document.getElementById('requests').innerHTML = Mustache.render(template, {
accountId,
pools,
staking
});
window.initInputs();
}
async function poolRequest(accountId, poolId, action, args, amount) {
console.log(`from ${accountId} to ${poolId}.${action}(${JSON.stringify(args)}) with ${amount}N`);
amount = amount ? utils.parseAmount(amount) : null;
let masterAccount = await window.near.account(accountId);
try {
await setAccountSigner(masterAccount);
await masterAccount.functionCall(accountId, "add_request", {
request: {
receiver_id: poolId,
actions: [funcCall(action, args, amount)],
}
});
} catch (error) {
console.error(error);
alert(error);
}
loadAccountStaking(accountId);
}
async function withdrawAll(accountId, poolId) {
await poolRequest(accountId, poolId, "withdraw_all", {});
}
async function unstakeAll(accountId, poolId) {
await poolRequest(accountId, poolId, "unstake_all", {});
}
async function depositAndStake(accountId) {
const poolId = document.querySelector('#select-pool-id').value;
const amount = document.querySelector('#stake-amount').value;
await poolRequest(accountId, poolId, "deposit_and_stake", {}, amount);
}
async function unstake(accountId) {
const poolId = document.querySelector('#select-pool-id').value;
const amount = document.querySelector('#stake-amount').value;
await poolRequest(accountId, poolId, "unstake", { amount: utils.parseAmount(amount) });
}
async function stakeWithdraw(accountId) {
const poolId = document.querySelector('#select-pool-id').value;
const amount = document.querySelector('#stake-amount').value;
await poolRequest(accountId, poolId, "withdraw", { amount: utils.parseAmount(amount) });
}
module.exports = {
loadAccountStaking,
}
window.withdrawAll = withdrawAll;
window.unstakeAll = unstakeAll;
window.depositAndStake = depositAndStake;
window.unstake = unstake;
window.stakeWithdraw = stakeWithdraw;