Skip to content

Commit f9d8a59

Browse files
authored
Add optional address filter parameter to virtune endpoints of por-address-list (#4045)
1 parent 8a8cba0 commit f9d8a59

File tree

7 files changed

+156
-3
lines changed

7 files changed

+156
-3
lines changed

.changeset/few-maps-cover.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@chainlink/por-address-list-adapter': minor
3+
---
4+
5+
Add optional address filter parameter to virtune endpoints

packages/sources/por-address-list/src/endpoint/virtune.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,11 @@ export const sharedVirtuneInputParameters = {
2121
type: 'string',
2222
required: true,
2323
},
24+
addressPattern: {
25+
description: 'Return only addresses matching the given regular expression pattern.',
26+
type: 'string',
27+
required: false,
28+
},
2429
} as const
2530

2631
export const inputParameters = new InputParameters(
@@ -32,6 +37,7 @@ export const inputParameters = new InputParameters(
3237
accountId: 'VIRBTC',
3338
network: 'bitcoin',
3439
chainId: 'mainnet',
40+
addressPattern: '^(?!P-)', // Only addresses that don't start with 'P-'.
3541
},
3642
],
3743
)

packages/sources/por-address-list/src/transport/virtune-token.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import {
66
import { BaseEndpointTypes } from '../endpoint/virtune-token'
77
import {
88
createVirtuneTransportConfig,
9+
filterAddresses,
910
getUrl,
1011
ResponseSchema,
1112
VirtuneParams,
@@ -42,7 +43,7 @@ const getResultFromAddresses = ({
4243
}
4344

4445
const transportConfig: HttpTransportConfig<HttpTransportTypes> =
45-
createVirtuneTransportConfig<HttpTransportTypes>(getUrl, getResultFromAddresses)
46+
createVirtuneTransportConfig<HttpTransportTypes>(getUrl, filterAddresses, getResultFromAddresses)
4647

4748
// Exported for testing
4849
export class VirtuneTokenTransport extends HttpTransport<HttpTransportTypes> {

packages/sources/por-address-list/src/transport/virtune-utils.ts

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,19 @@ export const getUrl = (params: { accountId: string }): string => {
77
return params.accountId
88
}
99

10+
export const filterAddresses = ({
11+
addresses,
12+
params,
13+
}: {
14+
addresses: string[]
15+
params: { addressPattern?: string }
16+
}): string[] => {
17+
const { addressPattern } = params
18+
if (!addressPattern) return addresses
19+
const re = new RegExp(addressPattern)
20+
return addresses.filter((address) => re.test(address))
21+
}
22+
1023
export interface ResponseSchema {
1124
accountName: string
1225
result: {
@@ -43,6 +56,16 @@ export const createVirtuneTransportConfig = <T extends VirtuneTransportGenerics>
4356
// because the compiler doesn't know getUrl satisfies the type until T
4457
// is instantiated.
4558
getUrlFromParams: (params: VirtuneParams<T>) => string,
59+
// filterAddresses is always assigned to the exported filterAddresses.
60+
// It's only a parameter because the compiler doesn't know
61+
// filterAddresses satisfies the type until T is instantiated.
62+
filterAddresses: ({
63+
addresses,
64+
params,
65+
}: {
66+
addresses: string[]
67+
params: VirtuneParams<T>
68+
}) => string[],
4669
getResultFromAddresses: (_: {
4770
params: VirtuneParams<T>
4871
addresses: string[]
@@ -88,8 +111,10 @@ export const createVirtuneTransportConfig = <T extends VirtuneTransportGenerics>
88111
]
89112
}
90113

114+
const filteredAddresses = filterAddresses({ addresses, params: param })
115+
91116
const result = getResultFromAddresses({
92-
addresses,
117+
addresses: filteredAddresses,
93118
params: param,
94119
})
95120

packages/sources/por-address-list/src/transport/virtune.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import {
66
import { BaseEndpointTypes } from '../endpoint/virtune'
77
import {
88
createVirtuneTransportConfig,
9+
filterAddresses,
910
getUrl,
1011
ResponseSchema,
1112
VirtuneParams,
@@ -36,7 +37,7 @@ const getResultFromAddresses = ({
3637
}
3738

3839
const transportConfig: HttpTransportConfig<HttpTransportTypes> =
39-
createVirtuneTransportConfig<HttpTransportTypes>(getUrl, getResultFromAddresses)
40+
createVirtuneTransportConfig<HttpTransportTypes>(getUrl, filterAddresses, getResultFromAddresses)
4041

4142
// Exported for testing
4243
export class VirtuneTransport extends HttpTransport<HttpTransportTypes> {

packages/sources/por-address-list/test/unit/virtune-token.test.ts

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,67 @@ describe('VirtuneTokenTransport', () => {
136136
expect(responseCache.write).toHaveBeenCalledTimes(1)
137137
}
138138

139+
it('should filter based on address pattern', async () => {
140+
const accountId = 'VIRBTC'
141+
const network = 'bitcoin'
142+
const chainId = 'mainnet'
143+
const contractAddress = '0x514910771af9ca656af840dff83e8264ecf986ca'
144+
const address1 = 'x-addr1'
145+
const address2 = 'y-addr2'
146+
147+
const params = makeStub('params', {
148+
accountId,
149+
network,
150+
chainId,
151+
contractAddress,
152+
addressPattern: '^y-',
153+
})
154+
155+
const expectedRequestConfig = {
156+
baseURL: adapterSettings.VIRTUNE_API_URL,
157+
params: {
158+
key: virtuneApiKey,
159+
},
160+
url: accountId,
161+
}
162+
163+
const response = makeStub('response', {
164+
response: {
165+
data: {
166+
result: [
167+
{
168+
wallets: [{ address: address1 }, { address: address2 }],
169+
},
170+
],
171+
cost: undefined,
172+
},
173+
},
174+
timestamps: {},
175+
})
176+
177+
const expectedResponse = {
178+
data: {
179+
result: [
180+
{
181+
network,
182+
chainId,
183+
contractAddress,
184+
wallets: [address2],
185+
},
186+
],
187+
},
188+
result: null,
189+
timestamps: {},
190+
}
191+
192+
await testTransport({
193+
params,
194+
expectedRequestConfig,
195+
response,
196+
expectedResponse,
197+
})
198+
})
199+
139200
it('should cache a response for a successful request', async () => {
140201
const accountId = 'VIRBTC'
141202
const network = 'bitcoin'
@@ -149,6 +210,7 @@ describe('VirtuneTokenTransport', () => {
149210
network,
150211
chainId,
151212
contractAddress,
213+
addressPattern: undefined,
152214
})
153215

154216
const expectedRequestConfig = {

packages/sources/por-address-list/test/unit/virtune.test.ts

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,7 @@ describe('VirtuneTransport', () => {
147147
accountId,
148148
network,
149149
chainId,
150+
addressPattern: undefined,
150151
})
151152

152153
const expectedRequestConfig = {
@@ -190,6 +191,58 @@ describe('VirtuneTransport', () => {
190191
})
191192
})
192193

194+
it('should filter based on address pattern', async () => {
195+
const accountId = 'VIRBTC'
196+
const network = 'bitcoin'
197+
const chainId = 'mainnet'
198+
const address1 = 'x-addr1'
199+
const address2 = 'y-addr2'
200+
201+
const params = makeStub('params', {
202+
accountId,
203+
network,
204+
chainId,
205+
addressPattern: '^y-',
206+
})
207+
208+
const expectedRequestConfig = {
209+
baseURL: adapterSettings.VIRTUNE_API_URL,
210+
params: {
211+
key: virtuneApiKey,
212+
},
213+
url: accountId,
214+
}
215+
216+
const response = makeStub('response', {
217+
response: {
218+
data: {
219+
result: [
220+
{
221+
wallets: [{ address: address1 }, { address: address2 }],
222+
},
223+
],
224+
cost: undefined,
225+
},
226+
},
227+
timestamps: {},
228+
})
229+
230+
const expectedResponse = {
231+
data: {
232+
result: [{ address: address2, network, chainId }],
233+
},
234+
result: null,
235+
timestamps: {},
236+
}
237+
238+
await testTransport({
239+
params,
240+
expectedRequestConfig,
241+
response,
242+
expectedResponse,
243+
})
244+
})
245+
193246
it('should cache an error if the response has no data', async () => {
194247
const accountId = 'VIRBTC'
195248
const network = 'bitcoin'

0 commit comments

Comments
 (0)