Skip to content

Commit 295416c

Browse files
authored
Support porBalance endpoint of ethereum-cl-indexer in proof-of-reserves (#3968)
* Support porBalance endpoint of ethereum-cl-indexer in proof-of-reserves * Re-arrange endpoint logic * remove break * Revert "remove break" This reverts commit 2e013c8. * Move break
1 parent b04c514 commit 295416c

File tree

4 files changed

+120
-12
lines changed

4 files changed

+120
-12
lines changed

.changeset/real-apples-approve.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@chainlink/proof-of-reserves-adapter': minor
3+
---
4+
5+
Support porBalance endpoint of ethereum-cl-indexer

packages/composites/proof-of-reserves/src/endpoint/reserves.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,20 +157,22 @@ export const execute: ExecuteWithConfig<Config> = async (input, context, config)
157157

158158
const validatedAddresses = getValidAddresses(protocolOutput, validator)
159159

160+
const indexerEndpoint = validator.validated.data.indexerEndpoint
160161
const balanceOutput = await runBalanceAdapter(
161162
indexer,
162163
context,
163164
confirmations,
164165
config,
165166
validatedAddresses,
166-
validator.validated.data.indexerEndpoint,
167+
indexerEndpoint,
167168
validator.validated.data.indexerParams,
168169
)
169170

170171
const reduceOutput = await runReduceAdapter(
171172
indexer,
172173
context,
173174
balanceOutput,
175+
indexerEndpoint,
174176
validator.validated.data.viewFunctionIndexerResultDecimals,
175177
)
176178
reduceOutput.data.description = validator.validated.data.description

packages/composites/proof-of-reserves/src/utils/reduce.ts

Lines changed: 29 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ export const runReduceAdapter = async (
4848
indexer: string,
4949
context: AdapterContext,
5050
input: AdapterResponse,
51+
indexerEndpoint?: string,
5152
viewFunctionIndexerResultDecimals?: number,
5253
): Promise<AdapterResponse> => {
5354
// Some adapters' balances come already reduced
@@ -71,22 +72,42 @@ export const runReduceAdapter = async (
7172
// TODO: type makeExecute response
7273
return returnParsedUnits(input.jobRunID, input.data.result as string, 0)
7374
case ETHEREUM_CL_INDEXER:
74-
if (input.data.isValid) {
75-
return {
76-
jobRunID: input.jobRunID,
77-
result: input.data.totalBalance as string,
78-
statusCode: 200,
79-
data: {
75+
if (indexerEndpoint === 'porBalance') {
76+
const hasInvalidResults = (input.data.result as unknown as Record<string, unknown>[])?.some(
77+
(result) => result.isValid === false,
78+
)
79+
if (hasInvalidResults) {
80+
throw new AdapterError({
81+
statusCode: 400,
82+
message: 'ETHEREUM_CL_INDEXER endpoint porBalance ripcord is true',
83+
})
84+
}
85+
// If all results are valid, use default processing below the
86+
// switch block.
87+
} else if (indexerEndpoint === 'etherFiBalance') {
88+
if (input.data.isValid) {
89+
return {
90+
jobRunID: input.jobRunID,
8091
result: input.data.totalBalance as string,
8192
statusCode: 200,
82-
},
93+
data: {
94+
result: input.data.totalBalance as string,
95+
statusCode: 200,
96+
},
97+
}
98+
} else {
99+
throw new AdapterError({
100+
statusCode: 400,
101+
message: `ETHEREUM_CL_INDEXER ripcord is true: ${JSON.stringify(input.data)}`,
102+
})
83103
}
84104
} else {
85105
throw new AdapterError({
86106
statusCode: 400,
87-
message: `ETHEREUM_CL_INDEXER ripcord is true: ${JSON.stringify(input.data)}`,
107+
message: `ETHEREUM_CL_INDEXER indexerEndpoint is not supported: ${indexerEndpoint}`,
88108
})
89109
}
110+
break
90111
case viewFunctionMultiChain.name:
91112
if (!viewFunctionIndexerResultDecimals) {
92113
throw new Error(

packages/composites/proof-of-reserves/test/unit/reduce.test.ts

Lines changed: 83 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ import { runReduceAdapter } from '../../src/utils/reduce'
55
describe('reduce', () => {
66
describe('ethereum-cl-indexer', () => {
77
describe('etherFiBalance endpoint', () => {
8+
const indexerEndpoint = 'etherFiBalance'
9+
810
it('should get totalBalance', async () => {
911
const jobRunID = '45'
1012
const totalBalance = '123000000000000000000'
@@ -17,7 +19,12 @@ describe('reduce', () => {
1719
},
1820
} as unknown as AdapterResponse)
1921

20-
const response = await runReduceAdapter('ETHEREUM_CL_INDEXER', context, input)
22+
const response = await runReduceAdapter(
23+
'ETHEREUM_CL_INDEXER',
24+
context,
25+
input,
26+
indexerEndpoint,
27+
)
2128

2229
expect(response).toEqual({
2330
jobRunID,
@@ -42,9 +49,82 @@ describe('reduce', () => {
4249
},
4350
} as unknown as AdapterResponse)
4451

45-
await expect(() => runReduceAdapter('ETHEREUM_CL_INDEXER', context, input)).rejects.toThrow(
46-
`ETHEREUM_CL_INDEXER ripcord is true: ${JSON.stringify(input.data)}`,
52+
await expect(() =>
53+
runReduceAdapter('ETHEREUM_CL_INDEXER', context, input, indexerEndpoint),
54+
).rejects.toThrow(`ETHEREUM_CL_INDEXER ripcord is true: ${JSON.stringify(input.data)}`)
55+
})
56+
})
57+
58+
describe('porBalance endpoint', () => {
59+
const indexerEndpoint = 'porBalance'
60+
61+
it('should add balances', async () => {
62+
const jobRunID = '45'
63+
const balance1 = '1000000000000000000'
64+
const balance2 = '2000000000000000000'
65+
const totalBalance = '3000000000000000000'
66+
const context = makeStub('context', {} as AdapterContext)
67+
const input = makeStub('input', {
68+
jobRunID,
69+
data: {
70+
result: [
71+
{
72+
isValid: true,
73+
balance: balance1,
74+
length: undefined,
75+
},
76+
{
77+
isValid: true,
78+
balance: balance2,
79+
length: undefined,
80+
},
81+
],
82+
},
83+
} as unknown as AdapterResponse)
84+
85+
const response = await runReduceAdapter(
86+
'ETHEREUM_CL_INDEXER',
87+
context,
88+
input,
89+
indexerEndpoint,
4790
)
91+
92+
expect(response).toEqual({
93+
jobRunID,
94+
result: totalBalance,
95+
statusCode: 200,
96+
providerStatusCode: 200,
97+
data: {
98+
result: totalBalance,
99+
},
100+
})
101+
})
102+
103+
it('should throw if any result is not valid', async () => {
104+
const jobRunID = '45'
105+
const balance = '1000000000000000000'
106+
const context = makeStub('context', {} as AdapterContext)
107+
const input = makeStub('input', {
108+
jobRunID,
109+
data: {
110+
result: [
111+
{
112+
isValid: true,
113+
balance: balance,
114+
length: undefined,
115+
},
116+
{
117+
isValid: false,
118+
balance: '0',
119+
length: undefined,
120+
},
121+
],
122+
},
123+
} as unknown as AdapterResponse)
124+
125+
await expect(() =>
126+
runReduceAdapter('ETHEREUM_CL_INDEXER', context, input, indexerEndpoint),
127+
).rejects.toThrow('ETHEREUM_CL_INDEXER endpoint porBalance ripcord is true')
48128
})
49129
})
50130
})

0 commit comments

Comments
 (0)