Skip to content

Commit a955e95

Browse files
OpenEden USYCtoken support to token balance (#3891)
* OpenEden Support USYC in Token_Balance * Changeset * Helper method for getWithdrawalQueueLength * Add Unit test for USYC * Correct unit test * update integration test * review rework * replace helper func * fix lint error * remove unused function * update README * default for param * token param related changes
1 parent 1a73657 commit a955e95

File tree

5 files changed

+98
-1
lines changed

5 files changed

+98
-1
lines changed

.changeset/silver-suits-vanish.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@chainlink/token-balance-adapter': minor
3+
---
4+
5+
Include USYC token for type tbill in token-balance endpoint

packages/sources/token-balance/README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,7 @@ Request:
165165
|| addresses | | List of addresses to read | object[] | | | | |
166166
| | addresses.chainId | | Chain ID of the network | string | | | | |
167167
|| addresses.contractAddress | | Address of token contract | string | | | | |
168+
|| addresses.token | | Token symbol | string | | | | |
168169
|| addresses.wallets | | Array of wallets to sum balances | string[] | | | | |
169170
|| addresses.priceOracleAddress | | Address of price oracle | string | | | | |
170171

@@ -180,6 +181,7 @@ Request:
180181
{
181182
"chainId": "1",
182183
"contractAddress": "0xdd50C053C096CB04A3e3362E2b622529EC5f2e8a",
184+
"token": "TBILL",
183185
"wallets": ["0x5EaFF7af80488033Bc845709806D5Fae5291eB88"],
184186
"priceOracleAddress": "0xCe9a6626Eb99eaeA829D7fA613d5D0A2eaE45F40"
185187
}

packages/sources/token-balance/src/endpoint/tbill.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,12 @@ export const inputParameters = new InputParameters(
1818
type: 'string',
1919
description: 'Address of token contract',
2020
},
21+
token: {
22+
required: false,
23+
type: 'string',
24+
default: 'TBILL',
25+
description: 'Token symbol',
26+
},
2127
wallets: {
2228
required: true,
2329
type: 'string',
@@ -40,6 +46,7 @@ export const inputParameters = new InputParameters(
4046
{
4147
chainId: '1',
4248
contractAddress: '0xdd50C053C096CB04A3e3362E2b622529EC5f2e8a',
49+
token: 'TBILL',
4350
wallets: ['0x5EaFF7af80488033Bc845709806D5Fae5291eB88'],
4451
priceOracleAddress: '0xCe9a6626Eb99eaeA829D7fA613d5D0A2eaE45F40',
4552
},

packages/sources/token-balance/src/transport/tbill.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ type AddressType = {
1515
network?: string
1616
chainId?: string
1717
contractAddress: string
18+
token: string
1819
wallets: Array<string>
1920
priceOracleAddress: string
2021
}
@@ -166,7 +167,7 @@ export class TbillTransport extends SubscriptionTransport<BaseEndpointTypes> {
166167
const [sharePriceUSD, sharesDecimals, queueLength, balanceResponse] = await Promise.all([
167168
priceOracleContract.getRateFromLatestRoundData(),
168169
contract.decimals(),
169-
contract.getWithdrawalQueueLength(),
170+
address.token === 'USYC' ? Promise.resolve(BigInt(0)) : contract.getWithdrawalQueueLength(),
170171
Promise.all(address.wallets.map((wallet) => contract.balanceOf(wallet))),
171172
])
172173

packages/sources/token-balance/test/unit/tbill.test.ts

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ type RequestParams = typeof inputParameters.validated
99
const ETHEREUM_RPC_CHAIN_ID = '1'
1010
const ETHEREUM_TBILL_CONTRACT_ADDRESS = '0xdd50C053C096CB04A3e3362E2b622529EC5f2e8a'
1111
const ETHEREUM_TBILL_PRICE_ORACLE_ADDRESS = '0xCe9a6626Eb99eaeA829D7fA613d5D0A2eaE45F40'
12+
const ETHEREUM_USYC_CONTRACT_ADDRESS = '0x136471a34f6ef19fE571EFFC1CA711fdb8E49f2b'
13+
const ETHEREUM_USYC_PRICE_ORACLE_ADDRESS = '0x4c48bcb2160F8e0aDbf9D4F3B034f1e36d1f8b3e'
1214

1315
const ARBITRUM_RPC_CHAIN_ID = '42161'
1416
const ARBITRUM_TBILL_CONTRACT_ADDRESS = '0xaf88d065e77c8cC2239327C5EDb3A432268e5831'
@@ -18,6 +20,7 @@ const BASE_RPC_CHAIN_ID = '8453'
1820
const BASE_TBILL_CONTRACT_ADDRESS = '0x833589fCD6eDb6E08f4c7C32D4f71b54bdA02913'
1921
const BASE_TBILL_PRICE_ORACLE_ADDRESS = 'unknown'
2022

23+
const TOKEN_SYMBOL = 'TBILL'
2124
const RESULT_DECIMALS = 18
2225

2326
const createRoundData = ({ price, priceDecimals }: { price: number; priceDecimals: number }) => {
@@ -45,12 +48,16 @@ const createMockPriceContract = () => ({
4548

4649
const ethTbillContract = createMockTokenContract()
4750
const ethTbillPriceContract = createMockPriceContract()
51+
const ethUsycContract = createMockTokenContract()
52+
const ethUsycPriceContract = createMockPriceContract()
4853
const arbTbillContract = createMockTokenContract()
4954
const arbTbillPriceContract = createMockPriceContract()
5055

5156
const contracts: Record<string, unknown> = {
5257
[ETHEREUM_TBILL_CONTRACT_ADDRESS]: ethTbillContract,
5358
[ETHEREUM_TBILL_PRICE_ORACLE_ADDRESS]: ethTbillPriceContract,
59+
[ETHEREUM_USYC_CONTRACT_ADDRESS]: ethUsycContract,
60+
[ETHEREUM_USYC_PRICE_ORACLE_ADDRESS]: ethUsycPriceContract,
5461
[ARBITRUM_TBILL_CONTRACT_ADDRESS]: arbTbillContract,
5562
[ARBITRUM_TBILL_PRICE_ORACLE_ADDRESS]: arbTbillPriceContract,
5663
}
@@ -131,6 +138,64 @@ describe('TbillTransport', () => {
131138
})
132139

133140
describe('handleRequest', () => {
141+
it('should cache ethereum usyc balance', async () => {
142+
const balance = 3
143+
const balanceDecimals = 6
144+
const price = 7
145+
const priceDecimals = 8
146+
const result = String(balance * price * 10 ** RESULT_DECIMALS)
147+
148+
const walletAddress = '0x602a1cb1f821a3e8f507a7637a4be7af19578f75'
149+
ethUsycContract.decimals.mockResolvedValue(balanceDecimals)
150+
ethUsycContract.balanceOf.mockResolvedValue(BigInt(balance * 10 ** balanceDecimals))
151+
ethUsycPriceContract.decimals.mockResolvedValue(priceDecimals)
152+
ethUsycPriceContract.latestRoundData.mockResolvedValue(
153+
createRoundData({ price, priceDecimals }),
154+
)
155+
156+
const param = {
157+
addresses: [
158+
{
159+
chainId: ETHEREUM_RPC_CHAIN_ID,
160+
contractAddress: ETHEREUM_USYC_CONTRACT_ADDRESS,
161+
token: 'USYC',
162+
priceOracleAddress: ETHEREUM_USYC_PRICE_ORACLE_ADDRESS,
163+
wallets: [walletAddress],
164+
},
165+
],
166+
} as RequestParams
167+
168+
const now = Date.now()
169+
await transport.handleRequest(context, param)
170+
171+
expect(ethUsycContract.balanceOf).toBeCalledWith(walletAddress)
172+
expect(ethUsycContract.balanceOf).toBeCalledTimes(1)
173+
expect(ethUsycPriceContract.latestRoundData).toBeCalledTimes(1)
174+
expect(ethUsycContract.getWithdrawalQueueLength).toBeCalledTimes(0)
175+
176+
expect(arbTbillContract.balanceOf).toBeCalledTimes(0)
177+
expect(arbTbillPriceContract.latestRoundData).toBeCalledTimes(0)
178+
179+
expect(responseCache.write).toBeCalledWith(transportName, [
180+
{
181+
params: param,
182+
response: {
183+
data: {
184+
decimals: RESULT_DECIMALS,
185+
result,
186+
},
187+
result,
188+
statusCode: 200,
189+
timestamps: {
190+
providerDataRequestedUnixMs: now,
191+
providerDataReceivedUnixMs: now,
192+
},
193+
},
194+
},
195+
])
196+
expect(responseCache.write).toBeCalledTimes(1)
197+
})
198+
134199
it('should cache ethereum tbill balance', async () => {
135200
const balance = 3
136201
const balanceDecimals = 6
@@ -151,6 +216,7 @@ describe('TbillTransport', () => {
151216
{
152217
chainId: ETHEREUM_RPC_CHAIN_ID,
153218
contractAddress: ETHEREUM_TBILL_CONTRACT_ADDRESS,
219+
token: TOKEN_SYMBOL,
154220
priceOracleAddress: ETHEREUM_TBILL_PRICE_ORACLE_ADDRESS,
155221
wallets: [walletAddress],
156222
},
@@ -207,6 +273,7 @@ describe('TbillTransport', () => {
207273
{
208274
chainId: ARBITRUM_RPC_CHAIN_ID,
209275
contractAddress: ARBITRUM_TBILL_CONTRACT_ADDRESS,
276+
token: TOKEN_SYMBOL,
210277
priceOracleAddress: ARBITRUM_TBILL_PRICE_ORACLE_ADDRESS,
211278
wallets: [walletAddress],
212279
},
@@ -307,18 +374,21 @@ describe('TbillTransport', () => {
307374
{
308375
chainId: ETHEREUM_RPC_CHAIN_ID,
309376
contractAddress: ETHEREUM_TBILL_CONTRACT_ADDRESS,
377+
token: TOKEN_SYMBOL,
310378
priceOracleAddress: ETHEREUM_TBILL_PRICE_ORACLE_ADDRESS,
311379
wallets: [ethWalletAddress2],
312380
},
313381
{
314382
chainId: ARBITRUM_RPC_CHAIN_ID,
315383
contractAddress: ARBITRUM_TBILL_CONTRACT_ADDRESS,
384+
token: TOKEN_SYMBOL,
316385
priceOracleAddress: ARBITRUM_TBILL_PRICE_ORACLE_ADDRESS,
317386
wallets: [arbWalletAddress1],
318387
},
319388
{
320389
chainId: ARBITRUM_RPC_CHAIN_ID,
321390
contractAddress: ARBITRUM_TBILL_CONTRACT_ADDRESS,
391+
token: TOKEN_SYMBOL,
322392
priceOracleAddress: ARBITRUM_TBILL_PRICE_ORACLE_ADDRESS,
323393
wallets: [arbWalletAddress2],
324394
},
@@ -382,6 +452,7 @@ describe('TbillTransport', () => {
382452
{
383453
chainId: ETHEREUM_RPC_CHAIN_ID,
384454
contractAddress: ETHEREUM_TBILL_CONTRACT_ADDRESS,
455+
token: TOKEN_SYMBOL,
385456
priceOracleAddress: ETHEREUM_TBILL_PRICE_ORACLE_ADDRESS,
386457
wallets: [walletAddress],
387458
},
@@ -437,6 +508,7 @@ describe('TbillTransport', () => {
437508
{
438509
chainId: ETHEREUM_RPC_CHAIN_ID,
439510
contractAddress: ETHEREUM_TBILL_CONTRACT_ADDRESS,
511+
token: TOKEN_SYMBOL,
440512
priceOracleAddress: ETHEREUM_TBILL_PRICE_ORACLE_ADDRESS,
441513
wallets: [walletAddress],
442514
},
@@ -485,12 +557,14 @@ describe('TbillTransport', () => {
485557
{
486558
chainId: ETHEREUM_RPC_CHAIN_ID,
487559
contractAddress: ETHEREUM_TBILL_CONTRACT_ADDRESS,
560+
token: TOKEN_SYMBOL,
488561
priceOracleAddress: ETHEREUM_TBILL_PRICE_ORACLE_ADDRESS,
489562
wallets: [walletAddress],
490563
},
491564
{
492565
chainId: BASE_RPC_CHAIN_ID,
493566
contractAddress: BASE_TBILL_CONTRACT_ADDRESS,
567+
token: TOKEN_SYMBOL,
494568
priceOracleAddress: BASE_TBILL_PRICE_ORACLE_ADDRESS,
495569
wallets: [walletAddress],
496570
},
@@ -564,6 +638,7 @@ describe('TbillTransport', () => {
564638
{
565639
chainId: ETHEREUM_RPC_CHAIN_ID,
566640
contractAddress: ETHEREUM_TBILL_CONTRACT_ADDRESS,
641+
token: TOKEN_SYMBOL,
567642
priceOracleAddress: ETHEREUM_TBILL_PRICE_ORACLE_ADDRESS,
568643
wallets: [walletAddress],
569644
},
@@ -639,6 +714,7 @@ describe('TbillTransport', () => {
639714
{
640715
chainId: ETHEREUM_RPC_CHAIN_ID,
641716
contractAddress: ETHEREUM_TBILL_CONTRACT_ADDRESS,
717+
token: TOKEN_SYMBOL,
642718
priceOracleAddress: ETHEREUM_TBILL_PRICE_ORACLE_ADDRESS,
643719
wallets: [walletAddress],
644720
},
@@ -720,6 +796,7 @@ describe('TbillTransport', () => {
720796
addresses: [walletAddress1, walletAddress2].map((walletAddress) => ({
721797
chainId: ETHEREUM_RPC_CHAIN_ID,
722798
contractAddress: ETHEREUM_TBILL_CONTRACT_ADDRESS,
799+
token: TOKEN_SYMBOL,
723800
priceOracleAddress: ETHEREUM_TBILL_PRICE_ORACLE_ADDRESS,
724801
wallets: [walletAddress],
725802
})),
@@ -804,6 +881,7 @@ describe('TbillTransport', () => {
804881
{
805882
chainId: ETHEREUM_RPC_CHAIN_ID,
806883
contractAddress: ETHEREUM_TBILL_CONTRACT_ADDRESS,
884+
token: TOKEN_SYMBOL,
807885
priceOracleAddress: ETHEREUM_TBILL_PRICE_ORACLE_ADDRESS,
808886
wallets: [walletAddress1, walletAddress2],
809887
},
@@ -872,6 +950,7 @@ describe('TbillTransport', () => {
872950
addresses: [walletAddress1, walletAddress2].map((walletAddress) => ({
873951
chainId: ETHEREUM_RPC_CHAIN_ID,
874952
contractAddress: ETHEREUM_TBILL_CONTRACT_ADDRESS,
953+
token: TOKEN_SYMBOL,
875954
priceOracleAddress: ETHEREUM_TBILL_PRICE_ORACLE_ADDRESS,
876955
wallets: [walletAddress],
877956
})),
@@ -953,12 +1032,14 @@ describe('TbillTransport', () => {
9531032
{
9541033
chainId: ETHEREUM_RPC_CHAIN_ID,
9551034
contractAddress: ETHEREUM_TBILL_CONTRACT_ADDRESS,
1035+
token: TOKEN_SYMBOL,
9561036
priceOracleAddress: ETHEREUM_TBILL_PRICE_ORACLE_ADDRESS,
9571037
wallets: [walletAddress],
9581038
},
9591039
{
9601040
chainId: ARBITRUM_RPC_CHAIN_ID,
9611041
contractAddress: ARBITRUM_TBILL_CONTRACT_ADDRESS,
1042+
token: TOKEN_SYMBOL,
9621043
priceOracleAddress: ARBITRUM_TBILL_PRICE_ORACLE_ADDRESS,
9631044
wallets: [walletAddress],
9641045
},
@@ -1037,6 +1118,7 @@ describe('TbillTransport', () => {
10371118
addresses: [walletAddress1, walletAddress2].map((walletAddress) => ({
10381119
chainId: ETHEREUM_RPC_CHAIN_ID,
10391120
contractAddress: ETHEREUM_TBILL_CONTRACT_ADDRESS,
1121+
token: TOKEN_SYMBOL,
10401122
priceOracleAddress: ETHEREUM_TBILL_PRICE_ORACLE_ADDRESS,
10411123
wallets: [walletAddress],
10421124
})),

0 commit comments

Comments
 (0)