This repository has been archived by the owner on Oct 10, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
mirror.js
118 lines (105 loc) · 3.34 KB
/
mirror.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
108
109
110
111
112
113
114
115
116
117
118
import { LCDClient } from "@terra-money/terra.js";
import { Mirror } from "@mirror-protocol/mirror.js";
import BigNumber from "bignumber.js";
import { request, gql } from "graphql-request";
const mirror = new Mirror();
const terra = new LCDClient({
URL: "https://lcd.terra.dev",
chainID: "localterra"
});
const mAssetsReverseIndex = {
terra15gwkyepfc6xgca5t5zefzwy42uts8l2m4g40k6: "MIR",
terra1vxtwu4ehgzz77mnfwrntyrmgl64qjs75mpwqaz: "mAAPL",
terra1h8arz2k547uvmpxctuwush3jzc8fun4s96qgwt: "mGOOGL",
terra14y5affaarufk3uscy2vr6pe6w6zqf2wpjzn5sh: "mTSLA",
terra1jsxngqasf2zynj5kyh0tgq9mj3zksa5gk35j4k: "mNFLX",
terra1csk6tc7pdmpr782w527hwhez6gfv632tyf72cp: "mQQQ",
terra1cc3enj9qgchlrj34cnzhwuclc4vl2z3jl7tkqg: "mTWTR",
terra1227ppwxxj3jxz8cfgq00jgnxqcny7ryenvkwj6: "mMSFT",
terra165nd2qmrtszehcfrntlplzern7zl4ahtlhd5t2: "mAMZN",
terra1w7zgkcyt7y4zpct9dw8mw362ywvdlydnum2awa: "mBABA",
terra15hp9pr8y4qsvqvxf3m4xeptlk7l8h60634gqec: "mIAU",
terra1kscs6uhrqwy6rx5kuw5lwpuqvm3t6j2d6uf2lp: "mSLV",
terra1lvmx8fsagy70tv0fhmfzdw9h6s3sy4prz38ugf: "mUSO",
terra1zp3a6q6q4953cz376906g5qfmxnlg77hx3te45: "mVIXY",
terra1g4x2pzmkc9z3mseewxf758rllg08z3797xly0n: "mABNB",
terra137drsu8gce5thf6jr5mxlfghw36rpljt3zj73v: "mGS",
terra1dk3g53js3034x4v5c3vavhj2738une880yu6kx: "mETH",
terra1rhhvx8nzfrx5fufkuft06q5marfkucdqwq5sjw: "mBTC",
terra1mqsjugsugfprn3cvgxsrr8akkvdxv2pzc74us7: "mFB"
};
export async function getReward(address) {
console.log("getting reward");
const { reward_infos: rewardInfos } = await mirror.staking.getRewardInfo(
address
);
const mapRewardIndex = rewardInfos.map(rewardObj => {
return mirror.staking.getPoolInfo(rewardObj.asset_token);
});
console.log("getting global index");
const mapRewardIndexResult = await Promise.all(mapRewardIndex);
const calculateReward = rewardInfos.map((rewardObj, i) => {
const _reward = rewardCalc(mapRewardIndexResult[i].reward_index, rewardObj);
return {
name: mAssetsReverseIndex[rewardObj.asset_token] || "unknown",
asset_token: rewardObj.asset_token,
reward: new BigNumber(_reward)
.integerValue(BigNumber.ROUND_FLOOR)
.dividedBy(1000000)
.toString()
};
});
return calculateReward;
}
export async function getPrices() {
//Oracle
mirror.oracle.getPrices().then(res => {
let prices = res.Ok.prices;
prices.map(price => {
return {
...price,
name: mAssetsReverseIndex[price.asset_token]
};
});
});
}
export async function getMirPrice() {
const query = gql`
query {
asset(token: "terra15gwkyepfc6xgca5t5zefzwy42uts8l2m4g40k6") {
token
prices {
price
}
}
}
`;
const price = await request("https://graph.mirror.finance/graphql", query);
return price;
}
export async function getMarketPrices() {
const query = gql`
query {
assets {
symbol
prices {
price
oraclePrice
}
}
}
`;
const prices = await request("https://graph.mirror.finance/graphql", query);
return prices;
}
function rewardCalc(globalIndex, info) {
if (globalIndex && info) {
const { index, bond_amount, pending_reward } = info;
const reward = new BigNumber(globalIndex)
.minus(index)
.times(bond_amount)
.plus(pending_reward);
return reward.toString();
}
return "0";
}