-
Notifications
You must be signed in to change notification settings - Fork 9
/
claim-reward-example.js
99 lines (77 loc) · 3.76 KB
/
claim-reward-example.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
const assert = require('assert');
const { TASK_NODE_CREATE_SERVER } = require('hardhat/builtin-tasks/task-names');
const hre = require('hardhat');
const ethers = require('ethers');
const { resetForkedChain } = require('./common.js');
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;
const myContractAbi = [
'function getRewardsOwed(address rewardsContract) public view returns (uint)',
'function claimCometRewards(address rewardsContract) public',
];
const rewardAbi = [
'function getRewardOwed(address comet, address account) external returns (address token, uint owed)',
'function claim(address comet, address src, bool shouldAccrue) external',
];
let jsonRpcServer, deployment, cometAddress, rewardsAddress, compAddress, myContractFactory;
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("Reward token operations for 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;
rewardsAddress = networks[net].rewards;
compAddress = networks[net].COMP;
myContractFactory = await hre.ethers.getContractFactory('MyContract');
});
beforeEach(async () => {
await resetForkedChain(hre, providerUrl, blockNumber);
deployment = await myContractFactory.deploy(cometAddress);
});
after(async () => {
await jsonRpcServer.close();
});
it('Gets the current amount of reward token due to an account using JS', async () => {
const provider = new ethers.providers.JsonRpcProvider(jsonRpcUrl);
const rewards = new ethers.Contract(rewardsAddress, rewardAbi, provider);
const [ tokenAddress, amount ] = await rewards.callStatic.getRewardOwed(cometAddress, addresses[0]);
const compTokenMantissa = 1e18;
console.log('\tReward token is COMP', tokenAddress === compAddress);
console.log('\tJS - COMP token rewards due to the account', +amount.toString() / compTokenMantissa);
});
it('Claims and transfers the current amount of reward token due to an account using JS', async () => {
const provider = new ethers.providers.JsonRpcProvider(jsonRpcUrl);
const signer = provider.getSigner(addresses[0]);
const rewards = new ethers.Contract(rewardsAddress, rewardAbi, signer);
let tx = await rewards.claim(cometAddress, addresses[0], true);
await tx.wait(1);
// If due rewards, check to see if the account's COMP balance increased
console.log('\tJS - COMP tokens claimed and transferred to the account');
});
it('Runs rewards examples 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);
const amount = await MyContract.callStatic.getRewardsOwed(rewardsAddress);
const compTokenMantissa = 1e18;
console.log('\tSolidity - COMP token rewards due to the account', +amount.toString() / compTokenMantissa);
let tx = await MyContract.claimCometRewards(rewardsAddress);
await tx.wait(1);
console.log('\tSolidity - Reward tokens successfully claimed for contract.');
});
});