Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/real-apples-approve.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@chainlink/proof-of-reserves-adapter': minor
---

Support porBalance endpoint of ethereum-cl-indexer
Original file line number Diff line number Diff line change
Expand Up @@ -157,20 +157,22 @@ export const execute: ExecuteWithConfig<Config> = async (input, context, config)

const validatedAddresses = getValidAddresses(protocolOutput, validator)

const indexerEndpoint = validator.validated.data.indexerEndpoint
const balanceOutput = await runBalanceAdapter(
indexer,
context,
confirmations,
config,
validatedAddresses,
validator.validated.data.indexerEndpoint,
indexerEndpoint,
validator.validated.data.indexerParams,
)

const reduceOutput = await runReduceAdapter(
indexer,
context,
balanceOutput,
indexerEndpoint,
validator.validated.data.viewFunctionIndexerResultDecimals,
)
reduceOutput.data.description = validator.validated.data.description
Expand Down
37 changes: 29 additions & 8 deletions packages/composites/proof-of-reserves/src/utils/reduce.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ export const runReduceAdapter = async (
indexer: string,
context: AdapterContext,
input: AdapterResponse,
indexerEndpoint?: string,
viewFunctionIndexerResultDecimals?: number,
): Promise<AdapterResponse> => {
// Some adapters' balances come already reduced
Expand All @@ -71,22 +72,42 @@ export const runReduceAdapter = async (
// TODO: type makeExecute response
return returnParsedUnits(input.jobRunID, input.data.result as string, 0)
case ETHEREUM_CL_INDEXER:
if (input.data.isValid) {
return {
jobRunID: input.jobRunID,
result: input.data.totalBalance as string,
statusCode: 200,
data: {
if (indexerEndpoint === 'porBalance') {
const hasInvalidResults = (input.data.result as unknown as Record<string, unknown>[])?.some(
(result) => result.isValid === false,
)
if (hasInvalidResults) {
throw new AdapterError({
statusCode: 400,
message: 'ETHEREUM_CL_INDEXER endpoint porBalance ripcord is true',
})
}
// If all results are valid, use default processing below the
// switch block.
} else if (indexerEndpoint === 'etherFiBalance') {
if (input.data.isValid) {
return {
jobRunID: input.jobRunID,
result: input.data.totalBalance as string,
statusCode: 200,
},
data: {
result: input.data.totalBalance as string,
statusCode: 200,
},
}
} else {
throw new AdapterError({
statusCode: 400,
message: `ETHEREUM_CL_INDEXER ripcord is true: ${JSON.stringify(input.data)}`,
})
}
} else {
throw new AdapterError({
statusCode: 400,
message: `ETHEREUM_CL_INDEXER ripcord is true: ${JSON.stringify(input.data)}`,
message: `ETHEREUM_CL_INDEXER indexerEndpoint is not supported: ${indexerEndpoint}`,
})
}
break
case viewFunctionMultiChain.name:
if (!viewFunctionIndexerResultDecimals) {
throw new Error(
Expand Down
86 changes: 83 additions & 3 deletions packages/composites/proof-of-reserves/test/unit/reduce.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import { runReduceAdapter } from '../../src/utils/reduce'
describe('reduce', () => {
describe('ethereum-cl-indexer', () => {
describe('etherFiBalance endpoint', () => {
const indexerEndpoint = 'etherFiBalance'

it('should get totalBalance', async () => {
const jobRunID = '45'
const totalBalance = '123000000000000000000'
Expand All @@ -17,7 +19,12 @@ describe('reduce', () => {
},
} as unknown as AdapterResponse)

const response = await runReduceAdapter('ETHEREUM_CL_INDEXER', context, input)
const response = await runReduceAdapter(
'ETHEREUM_CL_INDEXER',
context,
input,
indexerEndpoint,
)

expect(response).toEqual({
jobRunID,
Expand All @@ -42,9 +49,82 @@ describe('reduce', () => {
},
} as unknown as AdapterResponse)

await expect(() => runReduceAdapter('ETHEREUM_CL_INDEXER', context, input)).rejects.toThrow(
`ETHEREUM_CL_INDEXER ripcord is true: ${JSON.stringify(input.data)}`,
await expect(() =>
runReduceAdapter('ETHEREUM_CL_INDEXER', context, input, indexerEndpoint),
).rejects.toThrow(`ETHEREUM_CL_INDEXER ripcord is true: ${JSON.stringify(input.data)}`)
})
})

describe('porBalance endpoint', () => {
const indexerEndpoint = 'porBalance'

it('should add balances', async () => {
const jobRunID = '45'
const balance1 = '1000000000000000000'
const balance2 = '2000000000000000000'
const totalBalance = '3000000000000000000'
const context = makeStub('context', {} as AdapterContext)
const input = makeStub('input', {
jobRunID,
data: {
result: [
{
isValid: true,
balance: balance1,
length: undefined,
},
{
isValid: true,
balance: balance2,
length: undefined,
},
],
},
} as unknown as AdapterResponse)

const response = await runReduceAdapter(
'ETHEREUM_CL_INDEXER',
context,
input,
indexerEndpoint,
)

expect(response).toEqual({
jobRunID,
result: totalBalance,
statusCode: 200,
providerStatusCode: 200,
data: {
result: totalBalance,
},
})
})

it('should throw if any result is not valid', async () => {
const jobRunID = '45'
const balance = '1000000000000000000'
const context = makeStub('context', {} as AdapterContext)
const input = makeStub('input', {
jobRunID,
data: {
result: [
{
isValid: true,
balance: balance,
length: undefined,
},
{
isValid: false,
balance: '0',
length: undefined,
},
],
},
} as unknown as AdapterResponse)

await expect(() =>
runReduceAdapter('ETHEREUM_CL_INDEXER', context, input, indexerEndpoint),
).rejects.toThrow('ETHEREUM_CL_INDEXER endpoint porBalance ripcord is true')
})
})
})
Expand Down
Loading