forked from smartcontractkit/external-adapters-js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrewards.test.ts
130 lines (113 loc) · 3.79 KB
/
rewards.test.ts
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
import * as IPFS_Adapter from '@chainlink/ipfs-adapter'
import {
calcMarketMakerRewards,
calcTraderRewards,
constructJsonTree,
constructMerkleTree,
} from '../../src/method/poke'
import * as bn from 'bignumber.js'
import rewardsTestData1 from '../mock-data/rewards-test-data-1.json'
import rewardsTestData2 from '../mock-data/rewards-test-data-2.json'
import { AddressRewards, storeJsonTree } from '../../src/ipfs-data'
import nock from 'nock'
import { mockIpfsRetroactiveRewardsData, mockEthNode, mockIpfsResponseSuccess } from './fixtures'
import { makeExecute } from '../../src'
import { calcRetroactiveRewards } from '../../src/method/formulas/initial'
let oldEnv: NodeJS.ProcessEnv
beforeAll(() => {
oldEnv = JSON.parse(JSON.stringify(process.env))
process.env.CACHE_ENABLED = 'false'
process.env.API_ENDPOINT = process.env.API_ENDPOINT || 'http://127.0.0.1:5001'
process.env.ETHEREUM_RPC_URL = process.env.ETHEREUM_RPC_URL || 'http://127.0.0.1:8545'
process.env.PRIVATE_KEY =
process.env.PRIVATE_KEY || '8da4ef21b864d2cc526dbdb2a120bd2874c36c9d0a1fb7f8c63d7f7a8b41de8f'
process.env.API_VERBOSE = 'true'
if (process.env.RECORD) {
nock.recorder.rec()
}
})
afterAll(() => {
process.env = oldEnv
if (process.env.RECORD) {
nock.recorder.play()
}
nock.restore()
nock.cleanAll()
nock.enableNetConnect()
})
describe('calculating rewards', () => {
const jobRunID = '1'
const ipfs = IPFS_Adapter.makeExecute()
const treasuryClaimAddress = '0x95EaBB0248D013b9F59c5D5256CE11b0a8140B54'
it('should calculate the correct rewards for epoch 0', async () => {
mockIpfsRetroactiveRewardsData()
const addressRewards: AddressRewards = {}
calcRetroactiveRewards(rewardsTestData1, addressRewards, treasuryClaimAddress)
calcTraderRewards(
rewardsTestData1,
addressRewards,
new bn.BigNumber(3_835_616).shiftedBy(18),
0.7,
)
calcMarketMakerRewards(
rewardsTestData1,
addressRewards,
new bn.BigNumber(1_150_685).shiftedBy(18),
)
const merkleTree = constructMerkleTree(addressRewards)
const jsonTree = constructJsonTree(addressRewards)
const newIpfsCid = await storeJsonTree(jobRunID, ipfs, jsonTree, {})
expect({
jsonTree,
cid: newIpfsCid,
root: merkleTree.getRoot().toString('hex'),
}).toMatchSnapshot()
})
it('should calculate the correct rewards for epoch 5', async () => {
mockIpfsResponseSuccess()
const addressRewards: AddressRewards = {}
calcTraderRewards(
rewardsTestData2,
addressRewards,
new bn.BigNumber(3_835_616).shiftedBy(18),
0.67,
0.28,
0.05,
)
calcMarketMakerRewards(
rewardsTestData2,
addressRewards,
new bn.BigNumber(1_150_685).shiftedBy(18),
)
const merkleTree = constructMerkleTree(addressRewards)
const jsonTree = constructJsonTree(addressRewards)
const newIpfsCid = await storeJsonTree(jobRunID, ipfs, jsonTree, {})
expect({
jsonTree,
cid: newIpfsCid,
root: merkleTree.getRoot().toString('hex'),
}).toMatchSnapshot()
})
})
describe('full request', () => {
const jobRunID = '1'
const dydxRewards = makeExecute()
it('services request for epoch 0 correctly', async () => {
mockIpfsRetroactiveRewardsData()
const mockEth = mockEthNode()
const req = {
id: jobRunID,
data: {
ipnsName: 'k51qzi5uqu5dlmlt9vu0tp1o4hkwr9hrhl5oia4gf4qgpolsjkj7erk3hy2cvv',
traderScoreAlpha: '700000000000000000',
callbackAddress: '0xaffdA0625B24a28EBA18eb733c41C8481EC0D6D0',
newEpoch: '0',
activeRootIpfsCid: 'test-cid',
},
}
const response = await dydxRewards(req, {})
expect(response).toMatchSnapshot()
// Assert that the correct data was written on-chain
expect(mockEth.isDone()).toBe(true)
})
})