-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathfeedPriceToConstOracleMock.js
59 lines (45 loc) · 2.28 KB
/
feedPriceToConstOracleMock.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
const NEST3PriceOracleMock = artifacts.require("NEST3PriceOracleAutoUpdateConstMock");
const ERC20 = artifacts.require("TestERC20");
const { BN } = require('@openzeppelin/test-helpers');
const { web3 } = require('@openzeppelin/test-environment');
const Decimal = require('decimal.js');
const argv = require('yargs').argv;
module.exports = async function (callback) {
try {
var PriceOracle;
var Token;
var ethAmount;
var tokenAmount;
console.log(`argv> oracle=${argv.oracle}, token=${argv.token}, ethAmount=${argv.ethAmount}, tokenAmount=${argv.tokenAmount}`);
if (argv.oracle === "" || argv.oracle === undefined) {
PriceOracle = await NEST3PriceOracleMock.deployed();
} else {
PriceOracle = await NEST3PriceOracleMock.at(argv.oracle);
}
Token = await ERC20.at(argv.token);
if (argv.ethAmount === "" || argv.ethAmount === undefined) {
ethAmount = new BN("10000000000000000000");
} else {
ethAmount = new BN(argv.ethAmount);
}
if (argv.ethAmount === "" || argv.ethAmount === undefined) {
tokenAmount = new BN("3255000000");
} else {
tokenAmount = new BN(argv.tokenAmount);
}
let symbol = await Token.symbol();
console.log(`token symbol=${symbol}, address=${Token.address}, ethAmount=${ethAmount}, tokenAmount=${tokenAmount}`);
await PriceOracle.feedPrice(Token.address, ethAmount, tokenAmount);
// get price now from NEST3PriceOracleMock Contract
let p = await PriceOracle.checkPriceNow(Token.address);
let decimal = await Token.decimals();
// p.erc20Amount.mul(new BN(web3.utils.toWei('1', 'ether'))).div(p.ethAmount).div((new BN('10')).pow(decimal)).toString()
const ethBase = Decimal((new BN(web3.utils.toWei('1', 'ether'))).toString());
const tokenDecimal = Decimal(((new BN('10')).pow(decimal)).toString());
let price = Decimal(p.erc20Amount.toString()).mul(ethBase).div(Decimal(p.ethAmount.toString())).div(tokenDecimal);
console.log(`price now> ethAmount=${p.ethAmount.toString()}, erc20Amount=${p.erc20Amount.toString()}, price=${price} ${symbol}/ETH`);
callback();
} catch (e) {
callback(e);
}
}