Skip to content

Commit 8cc1cc6

Browse files
hieronxfilo87
andauthored
feat: set up remark handler (#273)
* feat: set up remark handler * chore: improved logging * chore: improved ethHandlers error handling and logging * fix: ns2 config (#274) * fix: ff1 config (#275) * fix: ns2 config * fix: ff1 config * chore: improved logging * chore: export automation * fix: fix last contract selection for FF1 * fix: improve pool initialisation * feat: add remark handlers * feat: add signer address to attestations --------- Co-authored-by: Filippo Fontana <filippo@embrio.tech>
1 parent b6e95b1 commit 8cc1cc6

File tree

10 files changed

+691
-412
lines changed

10 files changed

+691
-412
lines changed

chains-cfg/_root.yaml

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -179,4 +179,9 @@ dataSources:
179179
kind: substrate/EventHandler
180180
filter:
181181
module: uniques
182-
method: MetadataSet
182+
method: MetadataSet
183+
- handler: handleRemark
184+
kind: substrate/EventHandler
185+
filter:
186+
module: remarks
187+
method: Remark

schema.graphql

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -691,4 +691,15 @@ type PoolFeeTransaction @entity {
691691
epoch: Epoch!
692692

693693
amount: BigInt
694+
}
695+
696+
type Attestation @entity {
697+
id: ID! # extrinsicHash
698+
699+
pool: Pool!
700+
timestamp: Date!
701+
702+
account: Account! @index
703+
704+
data: String
694705
}

src/helpers/types.ts

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,20 @@
11
//find out types: const a = createType(api.registry, '[u8;32]', 18)
22
import { AugmentedCall, AugmentedRpc, PromiseRpcResult } from '@polkadot/api/types'
3-
import { Enum, Null, Struct, u128, u32, u64, U8aFixed, Option, Vec, Bytes, Result, bool } from '@polkadot/types'
3+
import {
4+
Enum,
5+
Null,
6+
Struct,
7+
u128,
8+
u32,
9+
u64,
10+
U8aFixed,
11+
Option,
12+
Vec,
13+
Bytes,
14+
Result,
15+
bool,
16+
GenericCall,
17+
} from '@polkadot/types'
418
import { AccountId32, Perquintill, Balance } from '@polkadot/types/interfaces'
519
import { ITuple, Observable } from '@polkadot/types/types'
620

@@ -469,6 +483,16 @@ export type InvestOrdersCollectedEvent = ITuple<
469483
]
470484
>
471485

486+
export interface RemarksRemark extends Enum {
487+
readonly isIpfsHash: boolean
488+
readonly asIpfsHash: Bytes
489+
readonly isNamed: boolean
490+
readonly asNamed: Bytes
491+
readonly isLoan: boolean
492+
readonly asLoan: ITuple<[u64, u64]>
493+
readonly type: 'IpfsHash' | 'Named' | 'Loan'
494+
}
495+
472496
export type RedeemOrdersCollectedEvent = ITuple<
473497
[
474498
investmentId: TrancheCurrency | TrancheCurrencyBefore1400,
@@ -503,6 +527,8 @@ export type PoolFeesList = Vec<PoolFeesOfBucket>
503527

504528
export type OracleFedEvent = ITuple<[feeder: DevelopmentRuntimeOriginCaller, key: OracleKey, value: u128]>
505529

530+
export type RemarkEvent = ITuple<[remarks: Vec<RemarksRemark>, call: GenericCall]>
531+
506532
export type ExtendedRpc = {
507533
pools: {
508534
trancheTokenPrice: PromiseRpcResult<

src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,3 +27,4 @@ export * from './mappings/handlers/ethHandlers'
2727
export * from './mappings/handlers/poolFeesHandlers'
2828
export * from './mappings/handlers/oracleHandlers'
2929
export * from './mappings/handlers/uniquesHandlers'
30+
export * from './mappings/handlers/remarkHandlers'

src/mappings/handlers/blockHandlers.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,13 @@ async function _handleBlock(block: SubstrateBlock): Promise<void> {
5454
// Update Pool States
5555
const pools = await PoolService.getCfgActivePools()
5656
for (const pool of pools) {
57-
const currency = (await CurrencyService.getByPoolId(pool.id, { limit: 1 })).pop()
5857
logger.info(` ## Updating pool ${pool.id} states...`)
58+
if (!pool.currencyId) {
59+
logger.error(`Pool currencyId not set for ${pool.id}, skipping...`)
60+
continue
61+
}
62+
const [chainId, currencyType, currencyValue] = pool.currencyId.split('-')
63+
const currency = await CurrencyService.getOrInit(chainId, currencyType, currencyValue)
5964

6065
if (!pool.currentEpoch) {
6166
logger.error(`Pool currentEpoch not set for ${pool.id}, skipping...`)
@@ -185,7 +190,7 @@ async function _handleBlock(block: SubstrateBlock): Promise<void> {
185190
const accruedFees = await pool.getAccruedFees()
186191
for (const accruals of accruedFees) {
187192
const [feeId, pending, disbursement] = accruals
188-
const poolFee = poolFees.find( fee => fee.id === `${pool.id}-${feeId}`)
193+
const poolFee = poolFees.find((fee) => fee.id === `${pool.id}-${feeId}`)
189194
if (!poolFee) {
190195
logger.error(`Unable to retrieve PoolFee ${pool.id}-${feeId}, skipping accruals!`)
191196
continue

src/mappings/handlers/poolsHandlers.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,11 @@ async function _handlePoolCreated(event: SubstrateEvent<PoolCreatedEvent>): Prom
2222
const [, , poolId, essence] = event.event.data
2323
const timestamp = event.block.timestamp
2424
if (!timestamp) throw new Error(`Block ${event.block.block.header.number.toString()} has no timestamp`)
25+
2526
const formattedCurrency =
2627
`${LOCAL_CHAIN_ID}-${essence.currency.type}-` +
2728
`${currencyFormatters[essence.currency.type](essence.currency.value).join('-')}`
29+
2830
logger.info(
2931
`Creating Pool ${poolId.toString()} with currency: ${formattedCurrency} ` +
3032
`in block ${event.block.block.header.number}`
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import { SubstrateEvent } from '@subql/types'
2+
import { errorHandler } from '../../helpers/errorHandler'
3+
import { RemarkEvent } from '../../helpers/types'
4+
import { AttestationService } from '../services/attestationService'
5+
import { AccountService } from '../services/accountService'
6+
7+
export const handleRemark = errorHandler(_handleRemark)
8+
async function _handleRemark(event: SubstrateEvent<RemarkEvent>) {
9+
const [remarks, _call] = event.event.data
10+
if (!event.extrinsic) throw new Error('Missing event extrinsic!')
11+
const account = await AccountService.getOrInit(event.extrinsic.extrinsic.signer.toHex())
12+
logger.info(`Remark event fired for ${event.hash.toString()} at block ${event.block.block.header.number.toNumber()}`)
13+
const namedRemarks = remarks.filter((remark) => remark.isNamed)
14+
15+
for (const namedRemark of namedRemarks) {
16+
const namedRemarkData = namedRemark.asNamed.toUtf8()
17+
const [type, poolId, attestationData] = namedRemarkData.split(':')
18+
logger.info(`Named remark with data: ${namedRemarkData}`)
19+
if (type === 'attestation') {
20+
const attestation = await AttestationService.init(
21+
poolId,
22+
event.hash.toString(),
23+
event.block.timestamp!,
24+
account.id,
25+
attestationData
26+
)
27+
await attestation.save()
28+
}
29+
}
30+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import { Attestation } from '../../types/models/Attestation'
2+
3+
export class AttestationService extends Attestation {
4+
static init(poolId: string, hash: string, timestamp: Date, accountId: string, data: string) {
5+
const id = `${poolId}-${hash}`
6+
logger.info(`Initialising new attestation ${id} with data: ${data}`)
7+
const attestation = new this(id, poolId, timestamp, accountId)
8+
attestation.data = data
9+
return attestation
10+
}
11+
}

src/mappings/services/epochService.ts

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,9 @@ export class EpochService extends Epoch {
2929
epoch.sumPoolFeesPaidAmount = BigInt(0)
3030

3131
epoch.states = trancheIds.map((trancheId) => {
32-
const epochState = new EpochState(`${poolId}-${epochNr}-${trancheId}`, epoch.id, trancheId)
32+
const epochStateId = `${poolId}-${epochNr.toString(10)}-${trancheId}`
33+
logger.info(`Initialising epoch state ${epochStateId}`)
34+
const epochState = new EpochState(epochStateId, epoch.id, trancheId)
3335
epochState.sumOutstandingInvestOrders = BigInt(0)
3436
epochState.sumOutstandingRedeemOrders = BigInt(0)
3537
epochState.sumOutstandingRedeemOrdersCurrency = BigInt(0)
@@ -50,9 +52,14 @@ export class EpochService extends Epoch {
5052
return epoch
5153
}
5254

53-
async saveWithStates() {
54-
await this.save()
55-
return Promise.all(this.states.map((epochState) => epochState.save()))
55+
public async saveWithStates() {
56+
const epochStatesSaves = this.states.map((epochState) => epochState.save())
57+
await Promise.all(epochStatesSaves)
58+
59+
const epochSave = this.save()
60+
await epochSave
61+
62+
return this
5663
}
5764

5865
public getStates() {

0 commit comments

Comments
 (0)