-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
122 lines (105 loc) · 3.69 KB
/
Copy pathindex.js
File metadata and controls
122 lines (105 loc) · 3.69 KB
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
119
120
121
122
import { DAI_ADDRESS, RDAI_ADDRESS } from "../config";
import Web3 from "web3";
import axios from "axios";
import RDAI_ABI from "../abi/rdai";
import DAI_ABI from "../abi/dai";
import BigNumber from "bignumber.js";
const cTokenApi = "https://api.compound.finance/api/v2/ctoken";
const ethGasStationApi = "https://ethgasstation.info/json/ethgasAPI.json";
class RedeemDai {
web3;
rdaiContract;
daiContract;
sendOptions;
DAI_ADDRESS;
RDAI_ADDRESS;
constructor(provider, options = {}) {
if (typeof provider === "undefined") throw new Error("Provider required");
if (typeof provider.selectedAddress === "undefined")
throw new Error("Please call ethereum.enable() first if using Metamask");
this.web3 = new Web3(provider);
this.from = provider.selectedAddress;
this.DAI_ADDRESS = options.DAI_ADDRESS || DAI_ADDRESS;
this.RDAI_ADDRESS = options.RDAI_ADDRESS || RDAI_ADDRESS;
this.daiContract = new this.web3.eth.Contract(DAI_ABI, this.DAI_ADDRESS);
this.rdaiContract = new this.web3.eth.Contract(RDAI_ABI, this.RDAI_ADDRESS);
}
getFastGasPrice = async () => {
const res = await axios.get(ethGasStationApi);
const fast = res.data.fast / 10;
return this.web3.utils.toWei(String(fast), "gwei").toString();
};
getSendOptions = async () => {
return {
from: this.from,
gasPrice: await this.getFastGasPrice()
};
};
approve = async (hashCallback = null) => {
const MAX_UINT = new BigNumber(2)
.pow(256)
.minus(1)
.toFixed();
return await this.daiContract.methods
.approve(this.RDAI_ADDRESS, MAX_UINT)
.send(await this.getSendOptions())
.on("transactionHash", hashCallback);
};
isApproved = async () => {
const allowance = await this.daiContract.methods
.allowance(this.from, this.RDAI_ADDRESS)
.call();
return allowance !== "0";
};
mintWithHat = async (amount, hat, hashCallback = null) => {
return await this.rdaiContract.methods
.mintWithNewHat(amount, hat.recipients, hat.proportions)
.send(await this.getSendOptions())
.on("transactionHash", hashCallback);
};
mint = async (amount, hashCallback = null) => {
return await this.rdaiContract.methods
.mint(amount)
.send(await this.getSendOptions())
.on("transactionHash", hashCallback);
};
createHat = async (hat, useHat = true, hashCallback = null) => {
return await this.rdaiContract.methods
.createHat(hat.recipients, hat.proportions, useHat)
.send(await this.getSendOptions())
.on("transactionHash", hashCallback);
};
getHat = address => {
return this.rdaiContract.methods.getHatByAddress(address).call();
};
redeem = async (amount, hashCallback = null) => {
if (typeof amount != "undefined") {
return await this.rdaiContract.methods
.redeem(amount)
.send(await this.getSendOptions())
.on("transactionHash", hashCallback);
} else {
return await this.rdaiContract.methods
.redeemAll()
.send(await this.getSendOptions())
.on("transactionHash", hashCallback);
}
};
getDaiBalance = address => {
return this.daiContract.methods.balanceOf(address).call();
};
getRdaiBalance = address => {
return this.rdaiContract.methods.balanceOf(address).call();
};
getInterestEarned = address => {
return this.rdaiContract.methods.interestPayableOf(address).call();
};
getInterestRate = async () => {
const cTokens = (await axios.get(cTokenApi)).data.cToken;
const cDai = cTokens.filter(cToken => cToken.symbol === "cDAI")[0];
const rate = cDai.supply_rate.value;
const percentage = (rate * 100).toFixed(2);
return Number(percentage);
};
}
export default RedeemDai;