Skip to content

Commit 0e448be

Browse files
committed
wip: graph tally support
Signed-off-by: Tomás Migone <[email protected]>
1 parent 557f002 commit 0e448be

File tree

1 file changed

+170
-0
lines changed
  • packages/indexer-common/src/query-fees

1 file changed

+170
-0
lines changed

packages/indexer-common/src/query-fees/models.ts

Lines changed: 170 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ import { DataTypes, Sequelize, Model, Association, CreationOptional } from 'sequ
22
import { Address, toAddress } from '@graphprotocol/common-ts'
33
import { caip2IdRegex } from '../parsers'
44
import { TAPVerifier } from '@semiotic-labs/tap-contracts-bindings'
5+
import { RAV as RAVv2 } from '@graphprotocol/toolshed'
6+
import { BytesLike } from 'ethers'
57

68
export interface ScalarTapReceiptsAttributes {
79
id: number
@@ -100,6 +102,21 @@ export interface ReceiptAggregateVoucherAttributes {
100102
redeemedAt: Date | null
101103
final: boolean
102104
}
105+
106+
export interface ReceiptAggregateVoucherV2Attributes {
107+
collectionId: string
108+
payer: string
109+
serviceProvider: string
110+
dataService: string
111+
metadata: string
112+
signature: Uint8Array
113+
timestampNs: bigint
114+
valueAggregate: bigint
115+
last: boolean
116+
redeemedAt: Date | null
117+
final: boolean
118+
}
119+
103120
export interface FailedReceiptAggregateVoucherAttributes {
104121
allocationId: string
105122
senderAddress: string
@@ -142,6 +159,53 @@ export class ReceiptAggregateVoucher
142159
}
143160
}
144161

162+
// TODO HORIZON: move this to the toolshed package
163+
export type SignedRAVv2 = {
164+
rav: RAVv2
165+
signature: BytesLike
166+
}
167+
168+
export class ReceiptAggregateVoucherV2
169+
extends Model<ReceiptAggregateVoucherV2Attributes>
170+
implements ReceiptAggregateVoucherV2Attributes
171+
{
172+
public collectionId!: Address
173+
public payer!: Address
174+
public serviceProvider!: Address
175+
public dataService!: Address
176+
public metadata!: string
177+
public signature!: Uint8Array
178+
public timestampNs!: bigint
179+
public valueAggregate!: bigint
180+
public final!: boolean
181+
public last!: boolean
182+
public redeemedAt!: Date | null
183+
184+
declare createdAt: CreationOptional<Date>
185+
declare updatedAt: CreationOptional<Date>
186+
187+
public readonly allocationSummary?: AllocationSummary
188+
189+
public static associations: {
190+
allocationSummary: Association<ReceiptAggregateVoucherV2, AllocationSummary>
191+
}
192+
193+
getSignedRAV(): SignedRAVv2 {
194+
return {
195+
rav: {
196+
collectionId: this.collectionId,
197+
payer: this.payer,
198+
serviceProvider: this.serviceProvider,
199+
dataService: this.dataService,
200+
timestampNs: Number(this.timestampNs),
201+
valueAggregate: this.valueAggregate,
202+
metadata: this.metadata,
203+
},
204+
signature: this.signature,
205+
}
206+
}
207+
}
208+
145209
export type SignedRAV = TAPVerifier.SignedRAVStruct
146210

147211
export class FailedReceiptAggregateVoucher
@@ -265,6 +329,7 @@ export interface QueryFeeModels {
265329
allocationReceipts: typeof AllocationReceipt
266330
vouchers: typeof Voucher
267331
receiptAggregateVouchers: typeof ReceiptAggregateVoucher
332+
receiptAggregateVouchersV2: typeof ReceiptAggregateVoucherV2
268333
transferReceipts: typeof TransferReceipt
269334
transfers: typeof Transfer
270335
allocationSummaries: typeof AllocationSummary
@@ -416,6 +481,110 @@ export function defineQueryFeeModels(sequelize: Sequelize): QueryFeeModels {
416481
},
417482
)
418483

484+
485+
ReceiptAggregateVoucherV2.init(
486+
{
487+
collectionId: {
488+
type: DataTypes.CHAR(64), // 64 because prefix '0x' gets removed by GraphTally agent
489+
allowNull: false,
490+
primaryKey: true,
491+
get() {
492+
const rawValue = this.getDataValue('collectionId')
493+
return toAddress(rawValue)
494+
},
495+
set(value: Address) {
496+
const addressWithoutPrefix = value.toLowerCase().replace('0x', '')
497+
this.setDataValue('collectionId', addressWithoutPrefix)
498+
},
499+
},
500+
payer: {
501+
type: DataTypes.CHAR(40), // 40 because prefix '0x' gets removed by TAP agent
502+
allowNull: false,
503+
primaryKey: true,
504+
get() {
505+
const rawValue = this.getDataValue('payer')
506+
return toAddress(rawValue)
507+
},
508+
set(value: string) {
509+
const addressWithoutPrefix = value.toLowerCase().replace('0x', '')
510+
this.setDataValue('payer', addressWithoutPrefix)
511+
},
512+
},
513+
serviceProvider: {
514+
type: DataTypes.CHAR(40), // 40 because prefix '0x' gets removed by TAP agent
515+
allowNull: false,
516+
primaryKey: true,
517+
get() {
518+
const rawValue = this.getDataValue('serviceProvider')
519+
return toAddress(rawValue)
520+
},
521+
set(value: string) {
522+
const addressWithoutPrefix = value.toLowerCase().replace('0x', '')
523+
this.setDataValue('serviceProvider', addressWithoutPrefix)
524+
},
525+
},
526+
dataService: {
527+
type: DataTypes.CHAR(40), // 40 because prefix '0x' gets removed by TAP agent
528+
allowNull: false,
529+
primaryKey: true,
530+
get() {
531+
const rawValue = this.getDataValue('dataService')
532+
return toAddress(rawValue)
533+
},
534+
set(value: string) {
535+
const addressWithoutPrefix = value.toLowerCase().replace('0x', '')
536+
this.setDataValue('dataService', addressWithoutPrefix)
537+
},
538+
},
539+
metadata: {
540+
type: DataTypes.STRING,
541+
allowNull: false,
542+
},
543+
signature: {
544+
type: DataTypes.BLOB,
545+
allowNull: false,
546+
},
547+
// ternary operator added to timestampNs and valueAggregate
548+
// due to sequelize UPDATE
549+
// calls the getters with undefined data
550+
// 0 is returned since no real data is being requested
551+
timestampNs: {
552+
type: DataTypes.DECIMAL,
553+
allowNull: false,
554+
get() {
555+
return BigInt(this.getDataValue('timestampNs'))
556+
},
557+
},
558+
valueAggregate: {
559+
type: DataTypes.DECIMAL,
560+
allowNull: false,
561+
get() {
562+
return BigInt(this.getDataValue('valueAggregate'))
563+
},
564+
},
565+
last: {
566+
type: DataTypes.BOOLEAN,
567+
allowNull: false,
568+
defaultValue: false,
569+
},
570+
final: {
571+
type: DataTypes.BOOLEAN,
572+
allowNull: false,
573+
defaultValue: false,
574+
},
575+
redeemedAt: {
576+
type: DataTypes.DATE,
577+
allowNull: true,
578+
defaultValue: null,
579+
},
580+
},
581+
{
582+
underscored: true,
583+
sequelize,
584+
tableName: 'scalar_tap_ravs',
585+
},
586+
)
587+
419588
TransferReceipt.init(
420589
{
421590
id: {
@@ -718,6 +887,7 @@ export function defineQueryFeeModels(sequelize: Sequelize): QueryFeeModels {
718887
allocationReceipts: AllocationReceipt,
719888
vouchers: Voucher,
720889
receiptAggregateVouchers: ReceiptAggregateVoucher,
890+
receiptAggregateVouchersV2: ReceiptAggregateVoucherV2,
721891
transferReceipts: TransferReceipt,
722892
transfers: Transfer,
723893
allocationSummaries: AllocationSummary,

0 commit comments

Comments
 (0)