-
Notifications
You must be signed in to change notification settings - Fork 308
/
price.test.ts
132 lines (114 loc) · 4.01 KB
/
price.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
131
132
import { AdapterError, Requester } from '@chainlink/ea-bootstrap'
import { assertError } from '@chainlink/ea-test-helpers'
import { AdapterRequest } from '@chainlink/ea-bootstrap'
import { makeExecute } from '../../src/adapter'
import { execute as bethExecute } from '../../src/endpoint/price/beth'
import { execute as blunaExecute } from '../../src/endpoint/price/bluna'
import { ethers } from 'ethers'
import { callViewFunctionEA } from '../../src/utils'
import { Config } from '../../src/config'
import { TInputParameters } from '../../src/endpoint'
jest.mock('ethers', () => {
const actualModule = jest.requireActual('ethers')
return {
...actualModule,
ethers: {
...actualModule.ethers,
providers: {
JsonRpcProvider: function () {
return {}
},
},
Contract: function () {
return {
get_rate: jest.fn(),
get_dy: jest.fn(),
}
},
},
}
})
jest.mock('../../src/utils', () => {
const actualModule = jest.requireActual('../../src/utils')
return {
...actualModule,
callViewFunctionEA: jest.fn(),
}
})
describe('execute', () => {
beforeAll(() => {
process.env.ETHEREUM_RPC_URL = 'fake-url'
process.env.ANCHOR_VAULT_CONTRACT_ADDRESS = 'fake-address'
process.env.API_KEY = 'fake-key'
process.env.LOCALTERRA_LCD_URL = 'fake-lcd-url'
})
beforeEach(() => {
jest.clearAllMocks()
})
describe('decimal precision', () => {
const input: AdapterRequest = { id: '1', data: { from: 'BETH', to: 'USD' } }
const config = {
rpcUrl: '',
stEthPoolContractAddress: '',
anchorVaultContractAddress: '',
terraBLunaHubContractAddress: '',
feedAddresses: {},
} as Config
it('beth execute responds with correct precision', async () => {
const bEthStEthString = '1' + '0'.repeat(150)
const ethStETHString = '1' + '0'.repeat(50)
const usdEthString = '1' + '0'.repeat(300)
const bEthStEth = ethers.BigNumber.from(bEthStEthString)
const ethStETH = ethers.BigNumber.from(ethStETHString)
const usdEth = ethers.BigNumber.from(usdEthString)
// (USD / ETH) * (ETH / stETH) / (bETH / stETH) = USD / bETH
const expected = '1' + '0'.repeat(200) // usdEthDecimals + ethStEthDecimals - bEthStEth decimals
//@ts-expect-error ethers.Contract must be mocked this way
ethers.Contract = jest.fn(function () {
return {
get_rate: () => bEthStEth,
get_dy: () => ethStETH,
}
})
const result = await bethExecute(input, {}, config, usdEth)
expect(result.toString()).toEqual(expected)
})
it('bluna execute responds with correct precision', async () => {
const lunaBLunaString = '1' + '0'.repeat(100)
const usdLunaString = '1' + '0'.repeat(100)
const usdLuna = ethers.BigNumber.from(usdLunaString)
// (LUNA / bLUNA) * (USD / LUNA) = USD / bLUNA
const expected = '1' + '0'.repeat(200)
//@ts-expect-error callViewFunctionEA must be mocked this way
callViewFunctionEA.mockReturnValue({ data: { result: { exchange_rate: lunaBLunaString } } })
const result = await blunaExecute(input, {}, config, usdLuna)
expect(result.toString()).toEqual(expected)
})
})
describe('validation error', () => {
const jobID = '1'
const execute = makeExecute()
const requests = [
{ name: 'empty body', testData: {} },
{ name: 'empty data', testData: { data: {} } },
{
name: 'empty from',
testData: { id: jobID, data: { to: 'ETH', from: '' } },
},
{
name: 'empty to',
testData: { id: jobID, data: { from: 'ETH' } },
},
]
requests.forEach((req) => {
it(`${req.name}`, async () => {
try {
await execute(req.testData as AdapterRequest<TInputParameters>, {})
} catch (error) {
const errorResp = Requester.errored(jobID, error as AdapterError)
assertError({ expected: 400, actual: errorResp.statusCode }, errorResp, jobID)
}
})
})
})
})