-
Notifications
You must be signed in to change notification settings - Fork 9
/
get-asset-price-feeds.js
150 lines (124 loc) · 4.72 KB
/
get-asset-price-feeds.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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
const assert = require('assert');
const { TASK_NODE_CREATE_SERVER } = require('hardhat/builtin-tasks/task-names');
const hre = require('hardhat');
const ethers = require('ethers');
const networks = require('./addresses.json');
const net = hre.config.cometInstance;
const jsonRpcUrl = 'http://127.0.0.1:8545';
const providerUrl = hre.config.networks.hardhat.forking.url;
const blockNumber = hre.config.networks.hardhat.forking.blockNumber;
function forEachWithCallback(callback, after) {
const arrayCopy = JSON.parse(JSON.stringify(this));
let index = 0;
const next = () => {
index++;
if (arrayCopy.length > 0) {
callback(arrayCopy.shift(), index, next);
} else {
if (after) after();
}
}
next();
}
Array.prototype.forEachWithCallback = forEachWithCallback;
const myContractAbi = [
'function getAllAssetInfos() public',
`event AssetInfoLog(tuple(
uint8 offset,
address asset,
address priceFeed,
uint64 scale,
uint64 borrowCollateralFactor,
uint64 liquidateCollateralFactor,
uint64 liquidationFactor,
uint128 supplyCap
))`,
'event LogUint(string, uint)',
'event LogAddress(string, address)',
'function getPriceFeedAddress(address asset) public view returns (address)',
'function getBaseTokenPriceFeed() public view returns (address)'
];
const cometAbi = [
`function getAssetInfo(uint8 i) public view returns (
tuple(
uint8 offset,
address asset,
address priceFeed,
uint64 scale,
uint64 borrowCollateralFactor,
uint64 liquidateCollateralFactor,
uint64 liquidationFactor,
uint128 supplyCap
) memory
)`,
`function getAssetInfoByAddress(address) public view returns (
tuple(
uint8 offset,
address asset,
address priceFeed,
uint64 scale,
uint64 borrowCollateralFactor,
uint64 liquidateCollateralFactor,
uint64 liquidationFactor,
uint128 supplyCap
) memory
)`,
'function baseBorrowMin() returns (uint)',
'function baseMinForRewards() returns (uint)',
'function baseScale() returns (uint)',
'function baseToken() returns (address)',
'function baseTokenPriceFeed() returns (address)',
'function baseTrackingBorrowSpeed() returns (uint)',
'function baseTrackingSupplySpeed() returns (uint)',
'function numAssets() returns (uint)',
];
let jsonRpcServer, deployment, cometAddress, myContractFactory, baseAssetAddress;
const mnemonic = hre.network.config.accounts.mnemonic;
const addresses = [];
const privateKeys = [];
for (let i = 0; i < 20; i++) {
const wallet = new ethers.Wallet.fromMnemonic(mnemonic, `m/44'/60'/0'/0/${i}`);
addresses.push(wallet.address);
privateKeys.push(wallet._signingKey().privateKey);
}
describe("Finds asset price feeds for an instance of Compound III", function () {
before(async () => {
console.log('\n Running a hardhat local evm fork of a public net...\n');
jsonRpcServer = await hre.run(TASK_NODE_CREATE_SERVER, {
hostname: '127.0.0.1',
port: 8545,
provider: hre.network.provider
});
await jsonRpcServer.listen();
cometAddress = networks[net].comet;
myContractFactory = await hre.ethers.getContractFactory('MyContract');
});
beforeEach(async () => {
deployment = await myContractFactory.deploy(cometAddress);
});
after(async () => {
await jsonRpcServer.close();
});
it('Finds asset price feeds supported by a Comet instance using JS', async () => {
const provider = new ethers.providers.JsonRpcProvider(jsonRpcUrl);
const comet = new ethers.Contract(cometAddress, cometAbi, provider);
wbtcAddress = networks[net].WBTC;
// If you don't already have the asset address, use `getAssetInfo`
// see `get-supported-assets.js` example file
const priceFeed = (await comet.callStatic.getAssetInfoByAddress(wbtcAddress)).priceFeed;
console.log('\tJS - WBTC Price Feed', priceFeed);
console.log('\tJS - baseTokenPriceFeed', (await comet.callStatic.baseTokenPriceFeed()).toString());
});
it('Finds asset price feeds supported by a Comet instance using Solidity', async () => {
const provider = new ethers.providers.JsonRpcProvider(jsonRpcUrl);
const signer = provider.getSigner(addresses[0]);
const MyContract = new ethers.Contract(deployment.address, myContractAbi, signer);
wbtcAddress = networks[net].WBTC;
// If you don't already have the asset address, use `getAssetInfo`
// see `get-supported-assets.js` example file
const priceFeedAddress = await MyContract.callStatic.getPriceFeedAddress(wbtcAddress);
const baseTokenPriceFeed = await MyContract.callStatic.getBaseTokenPriceFeed();
console.log('\tSolidity - WBTC Price Feed', priceFeedAddress);
console.log('\tSolidity - baseTokenPriceFeed', baseTokenPriceFeed);
});
});