Skip to content

Commit 580626b

Browse files
committed
fix: class
1 parent 987a75c commit 580626b

File tree

3 files changed

+102
-102
lines changed

3 files changed

+102
-102
lines changed

packages/indexer-common/src/indexing-fees/__tests__/dips.test.ts

Lines changed: 19 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
jest.mock('../gateway-dips-service-client')
21
jest.mock('../../allocations/escrow-accounts')
32
import {
43
DipsManager,
@@ -30,8 +29,8 @@ import { Sequelize } from 'sequelize'
3029
import { testNetworkSpecification } from '../../indexer-management/__tests__/util'
3130
import { BigNumber } from 'ethers'
3231
import { CollectPaymentStatus } from '@graphprotocol/dips-proto/generated/gateway'
33-
import { decodeTapReceipt } from '../gateway-dips-service-client'
3432
import { getEscrowSenderForSigner } from '../../allocations/escrow-accounts'
33+
import { GatewayDipsServiceMessages } from '../gateway-dips-service-client'
3534

3635
// Make global Jest variables available
3736
// eslint-disable-next-line @typescript-eslint/no-explicit-any
@@ -110,23 +109,6 @@ const setup = async () => {
110109

111110
const setupEach = async () => {
112111
sequelize = await sequelize.sync({ force: true })
113-
const indexerManagementClient = await createIndexerManagementClient({
114-
models: managementModels,
115-
graphNode,
116-
logger,
117-
defaults: {
118-
globalIndexingRule: {
119-
allocationAmount: parseGRT('1000'),
120-
parallelAllocations: 1,
121-
},
122-
},
123-
network,
124-
})
125-
126-
const operator = new Operator(logger, indexerManagementClient, networkSpecWithDips)
127-
await operator.ensureGlobalIndexingRule()
128-
logger.debug('Ensured global indexing rule')
129-
logger.debug(JSON.stringify(network.specification, null, 2))
130112
}
131113

132114
const teardownEach = async () => {
@@ -459,6 +441,23 @@ describe('DipsCollector', () => {
459441
graphNode.entityCount = jest.fn().mockResolvedValue([250000])
460442
})
461443
test('collects payment for a specific agreement', async () => {
444+
const indexerManagementClient = await createIndexerManagementClient({
445+
models: managementModels,
446+
graphNode,
447+
logger,
448+
defaults: {
449+
globalIndexingRule: {
450+
allocationAmount: parseGRT('1000'),
451+
parallelAllocations: 1,
452+
},
453+
},
454+
network,
455+
})
456+
457+
const operator = new Operator(logger, indexerManagementClient, networkSpecWithDips)
458+
await operator.ensureGlobalIndexingRule()
459+
logger.debug('Ensured global indexing rule')
460+
462461
const agreement = await managementModels.IndexingAgreement.findOne({
463462
where: { id: testAgreementId },
464463
})
@@ -473,7 +472,7 @@ describe('DipsCollector', () => {
473472
status: CollectPaymentStatus.ACCEPT,
474473
tapReceipt: Buffer.from('1234', 'hex'),
475474
})
476-
;(decodeTapReceipt as jest.Mock).mockImplementation(() => {
475+
GatewayDipsServiceMessages.decodeTapReceipt = jest.fn().mockImplementation(() => {
477476
logger.info('MOCK Decoding TAP receipt')
478477
return {
479478
allocation_id: toAddress(testAllocationId),

packages/indexer-common/src/indexing-fees/dips.ts

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,7 @@ import { Op } from 'sequelize'
2323

2424
import {
2525
createGatewayDipsServiceClient,
26-
createSignedCancellationRequest,
27-
createSignedCollectionRequest,
28-
decodeTapReceipt,
26+
GatewayDipsServiceMessages,
2927
} from './gateway-dips-service-client'
3028
import {
3129
CollectPaymentStatus,
@@ -72,10 +70,11 @@ export class DipsManager {
7270
})
7371
if (agreement) {
7472
try {
75-
const cancellation = await createSignedCancellationRequest(
76-
uuidToHex(agreement.id),
77-
this.network.wallet,
78-
)
73+
const cancellation =
74+
await GatewayDipsServiceMessages.createSignedCancellationRequest(
75+
uuidToHex(agreement.id),
76+
this.network.wallet,
77+
)
7978
await this.gatewayDipsServiceClient.CancelAgreement({
8079
version: 1,
8180
signedCancellation: cancellation,
@@ -250,7 +249,7 @@ export class DipsCollector {
250249
return
251250
}
252251
const entityCount = entityCounts[0]
253-
const collection = await createSignedCollectionRequest(
252+
const collection = await GatewayDipsServiceMessages.createSignedCollectionRequest(
254253
uuidToHex(agreement.id),
255254
agreement.last_allocation_id,
256255
entityCount,
@@ -268,7 +267,7 @@ export class DipsCollector {
268267
}
269268
// Store the tap receipt in the database
270269
this.logger.info(`Decoding TAP receipt for agreement`)
271-
const tapReceipt = decodeTapReceipt(
270+
const tapReceipt = GatewayDipsServiceMessages.decodeTapReceipt(
272271
response.tapReceipt,
273272
this.tapCollector?.tapContracts.tapVerifier.address,
274273
)

packages/indexer-common/src/indexing-fees/gateway-dips-service-client.ts

Lines changed: 75 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -43,83 +43,85 @@ export const collectPaymentsTypes = {
4343
],
4444
}
4545

46-
export const createSignedCancellationRequest = async (
47-
agreementId: string,
48-
wallet: Wallet,
49-
): Promise<Uint8Array> => {
50-
const signature = await wallet._signTypedData(
51-
cancelAgreementDomain,
52-
cancelAgreementTypes,
53-
{ agreement_id: agreementId },
54-
)
55-
return arrayify(
56-
defaultAbiCoder.encode(['tuple(bytes16)', 'bytes'], [[agreementId], signature]),
57-
)
58-
}
59-
60-
export const createSignedCollectionRequest = async (
61-
agreementId: string,
62-
allocationId: string,
63-
entityCount: number,
64-
wallet: Wallet,
65-
): Promise<Uint8Array> => {
66-
const signature = await wallet._signTypedData(
67-
collectPaymentsDomain,
68-
collectPaymentsTypes,
69-
{
70-
agreement_id: agreementId,
71-
allocation_id: toAddress(allocationId),
72-
entity_count: entityCount,
73-
},
74-
)
75-
return arrayify(
76-
defaultAbiCoder.encode(
77-
['tuple(bytes16, address, uint64)', 'bytes'],
78-
[[agreementId, toAddress(allocationId), entityCount], signature],
79-
),
80-
)
81-
}
82-
83-
export const decodeTapReceipt = (receipt: Uint8Array, verifyingContract: string) => {
84-
const [message, signature] = defaultAbiCoder.decode(
85-
['tuple(address,uint64,uint64,uint128)', 'bytes'],
86-
receipt,
87-
)
88-
89-
const [allocationId, timestampNs, nonce, value] = message
90-
91-
// Recover the signer address from the signature
92-
// compute the EIP-712 digest of the message
93-
const domain = {
94-
name: 'TAP',
95-
version: '1',
96-
chainId: chainId,
97-
verifyingContract,
46+
export class GatewayDipsServiceMessages {
47+
static createSignedCancellationRequest = async (
48+
agreementId: string,
49+
wallet: Wallet,
50+
): Promise<Uint8Array> => {
51+
const signature = await wallet._signTypedData(
52+
cancelAgreementDomain,
53+
cancelAgreementTypes,
54+
{ agreement_id: agreementId },
55+
)
56+
return arrayify(
57+
defaultAbiCoder.encode(['tuple(bytes16)', 'bytes'], [[agreementId], signature]),
58+
)
9859
}
9960

100-
const types = {
101-
Receipt: [
102-
{ name: 'allocation_id', type: 'address' },
103-
{ name: 'timestamp_ns', type: 'uint64' },
104-
{ name: 'nonce', type: 'uint64' },
105-
{ name: 'value', type: 'uint128' },
106-
],
61+
static createSignedCollectionRequest = async (
62+
agreementId: string,
63+
allocationId: string,
64+
entityCount: number,
65+
wallet: Wallet,
66+
): Promise<Uint8Array> => {
67+
const signature = await wallet._signTypedData(
68+
collectPaymentsDomain,
69+
collectPaymentsTypes,
70+
{
71+
agreement_id: agreementId,
72+
allocation_id: toAddress(allocationId),
73+
entity_count: entityCount,
74+
},
75+
)
76+
return arrayify(
77+
defaultAbiCoder.encode(
78+
['tuple(bytes16, address, uint64)', 'bytes'],
79+
[[agreementId, toAddress(allocationId), entityCount], signature],
80+
),
81+
)
10782
}
10883

109-
const digest = _TypedDataEncoder.hash(domain, types, {
110-
allocation_id: allocationId,
111-
timestamp_ns: timestampNs,
112-
nonce: nonce,
113-
value: value,
114-
})
115-
const signerAddress = recoverAddress(digest, signature)
116-
return {
117-
allocation_id: toAddress(allocationId),
118-
signer_address: toAddress(signerAddress),
119-
signature: signature,
120-
timestamp_ns: timestampNs,
121-
nonce: nonce,
122-
value: value,
84+
static decodeTapReceipt = (receipt: Uint8Array, verifyingContract: string) => {
85+
const [message, signature] = defaultAbiCoder.decode(
86+
['tuple(address,uint64,uint64,uint128)', 'bytes'],
87+
receipt,
88+
)
89+
90+
const [allocationId, timestampNs, nonce, value] = message
91+
92+
// Recover the signer address from the signature
93+
// compute the EIP-712 digest of the message
94+
const domain = {
95+
name: 'TAP',
96+
version: '1',
97+
chainId: chainId,
98+
verifyingContract,
99+
}
100+
101+
const types = {
102+
Receipt: [
103+
{ name: 'allocation_id', type: 'address' },
104+
{ name: 'timestamp_ns', type: 'uint64' },
105+
{ name: 'nonce', type: 'uint64' },
106+
{ name: 'value', type: 'uint128' },
107+
],
108+
}
109+
110+
const digest = _TypedDataEncoder.hash(domain, types, {
111+
allocation_id: allocationId,
112+
timestamp_ns: timestampNs,
113+
nonce: nonce,
114+
value: value,
115+
})
116+
const signerAddress = recoverAddress(digest, signature)
117+
return {
118+
allocation_id: toAddress(allocationId),
119+
signer_address: toAddress(signerAddress),
120+
signature: signature,
121+
timestamp_ns: timestampNs,
122+
nonce: nonce,
123+
value: value,
124+
}
123125
}
124126
}
125127

0 commit comments

Comments
 (0)