Skip to content

Commit 36fce41

Browse files
authored
Add unit test for ` transport of por-address-list (#3890)
* Add unit test for openEdenUSDOAddress transport of por-address-list * lint+compile
1 parent 63a5a5f commit 36fce41

File tree

1 file changed

+189
-0
lines changed

1 file changed

+189
-0
lines changed
Lines changed: 189 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,189 @@
1+
import { TransportDependencies } from '@chainlink/external-adapter-framework/transports'
2+
import { LoggerFactoryProvider } from '@chainlink/external-adapter-framework/util'
3+
import { makeStub } from '@chainlink/external-adapter-framework/util/testing-utils'
4+
import { BaseEndpointTypes, inputParameters } from '../../src/endpoint/openEdenUSDOAddress'
5+
import { AddressTransport } from '../../src/transport/openEdenUSDOAddress'
6+
7+
type RequestParams = typeof inputParameters.validated
8+
9+
const originalEnv = { ...process.env }
10+
11+
const restoreEnv = () => {
12+
for (const key of Object.keys(process.env)) {
13+
if (key in originalEnv) {
14+
process.env[key] = originalEnv[key]
15+
} else {
16+
delete process.env[key]
17+
}
18+
}
19+
}
20+
21+
const ADDRESS_LIST_CONTRACT_ADDRESS = '0x440139321A15d14ce0729E004e91D66BaF1A08B0'
22+
23+
const addressListContract = {
24+
getPoRAddressListLength: jest.fn().mockResolvedValue(0),
25+
getPoRAddressList: jest.fn().mockResolvedValue([]),
26+
}
27+
28+
const contracts: Record<string, unknown> = makeStub('contracts', {
29+
[ADDRESS_LIST_CONTRACT_ADDRESS]: addressListContract,
30+
})
31+
32+
const makeEthers = () => {
33+
return {
34+
providers: {
35+
JsonRpcProvider: jest.fn(),
36+
},
37+
Contract: function (address: string) {
38+
return contracts[address]
39+
},
40+
}
41+
}
42+
43+
jest.mock('ethers', () => ({
44+
ethers: makeEthers(),
45+
}))
46+
47+
const walletAddress = '0x5EaFF7af80488033Bc845709806D5Fae5291eB88'
48+
const tbillContractAddress = '0xdd50C053C096CB04A3e3362E2b622529EC5f2e8a'
49+
const tbillPriceOracleAddress = '0xCe9a6626Eb99eaeA829D7fA613d5D0A2eaE45F40'
50+
const usdcContractAddress = '0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48'
51+
const tbillAddress = makeStub('tbillAddress', {
52+
chain: 'Ethereem Mainnet',
53+
chainId: 1,
54+
tokenSymbol: 'TBILL',
55+
tokenAddress: tbillContractAddress,
56+
tokenPriceOracle: tbillPriceOracleAddress,
57+
yourVaultAddress: walletAddress,
58+
})
59+
const usdcAddress = makeStub('otherAddress', {
60+
chain: 'Ethereem Mainnet',
61+
chainId: 1,
62+
tokenSymbol: 'USDC',
63+
tokenAddress: usdcContractAddress,
64+
yourVaultAddress: walletAddress,
65+
})
66+
67+
LoggerFactoryProvider.set()
68+
69+
describe('AddressTransport', () => {
70+
const transportName = 'default_single_transport'
71+
const endpointName = 'openedenAddress'
72+
73+
const adapterSettings = makeStub('adapterSettings', {
74+
WARMUP_SUBSCRIPTION_TTL: 10_000,
75+
} as unknown as BaseEndpointTypes['Settings'])
76+
77+
const responseCache = {
78+
write: jest.fn(),
79+
}
80+
81+
const dependencies = makeStub('dependencies', {
82+
responseCache,
83+
subscriptionSetFactory: {
84+
buildSet: jest.fn(),
85+
},
86+
} as unknown as TransportDependencies<BaseEndpointTypes>)
87+
88+
let transport: AddressTransport
89+
90+
beforeEach(async () => {
91+
restoreEnv()
92+
jest.restoreAllMocks()
93+
jest.useFakeTimers()
94+
95+
process.env.BASE_RPC_URL = 'https://base-rpc.example.com'
96+
process.env.BASE_RPC_CHAIN_ID = '8453'
97+
98+
transport = new AddressTransport()
99+
100+
await transport.initialize(dependencies, adapterSettings, endpointName, transportName)
101+
})
102+
103+
describe('_handleRequest', () => {
104+
it('should return an empty list', async () => {
105+
const params = makeStub('params', {
106+
contractAddress: ADDRESS_LIST_CONTRACT_ADDRESS,
107+
contractAddressNetwork: 'BASE',
108+
type: 'tbill',
109+
} as RequestParams)
110+
const response = await transport._handleRequest(params)
111+
expect(response).toEqual({
112+
statusCode: 200,
113+
data: {
114+
result: [],
115+
},
116+
result: null,
117+
timestamps: {
118+
providerDataRequestedUnixMs: Date.now(),
119+
providerDataReceivedUnixMs: Date.now(),
120+
},
121+
})
122+
})
123+
124+
it('should return tbill address', async () => {
125+
const addresses = [tbillAddress, usdcAddress]
126+
addressListContract.getPoRAddressList.mockResolvedValue(addresses)
127+
addressListContract.getPoRAddressListLength.mockResolvedValue(addresses.length)
128+
129+
const params = makeStub('params', {
130+
contractAddress: ADDRESS_LIST_CONTRACT_ADDRESS,
131+
contractAddressNetwork: 'BASE',
132+
type: 'tbill',
133+
} as RequestParams)
134+
const response = await transport._handleRequest(params)
135+
expect(response).toEqual({
136+
statusCode: 200,
137+
data: {
138+
result: [
139+
{
140+
contractAddress: tbillContractAddress,
141+
network: 'Ethereem Mainnet',
142+
chainId: '1',
143+
token: 'TBILL',
144+
wallets: [walletAddress],
145+
priceOracleAddress: tbillPriceOracleAddress,
146+
},
147+
],
148+
},
149+
result: null,
150+
timestamps: {
151+
providerDataRequestedUnixMs: Date.now(),
152+
providerDataReceivedUnixMs: Date.now(),
153+
},
154+
})
155+
})
156+
157+
it('should return other address', async () => {
158+
const addresses = [tbillAddress, usdcAddress]
159+
addressListContract.getPoRAddressList.mockResolvedValue(addresses)
160+
addressListContract.getPoRAddressListLength.mockResolvedValue(addresses.length)
161+
162+
const params = makeStub('params', {
163+
contractAddress: ADDRESS_LIST_CONTRACT_ADDRESS,
164+
contractAddressNetwork: 'BASE',
165+
type: 'other',
166+
} as RequestParams)
167+
const response = await transport._handleRequest(params)
168+
expect(response).toEqual({
169+
statusCode: 200,
170+
data: {
171+
result: [
172+
{
173+
contractAddress: usdcContractAddress,
174+
network: 'Ethereem Mainnet',
175+
chainId: '1',
176+
token: 'USDC',
177+
wallets: [walletAddress],
178+
},
179+
],
180+
},
181+
result: null,
182+
timestamps: {
183+
providerDataRequestedUnixMs: Date.now(),
184+
providerDataReceivedUnixMs: Date.now(),
185+
},
186+
})
187+
})
188+
})
189+
})

0 commit comments

Comments
 (0)