From b18434b3b681ca9e54a359178590cc3cebdea240 Mon Sep 17 00:00:00 2001 From: Kabie Date: Sun, 16 Jun 2024 00:01:40 +0800 Subject: [PATCH] update indexer --- indexer/dbschema/default.esdl | 2 +- .../dbschema/migrations/00003-m16hmfh.edgeql | 9 +++ indexer/src/indexer.ts | 79 ++++++++++++++++++- 3 files changed, 86 insertions(+), 4 deletions(-) create mode 100644 indexer/dbschema/migrations/00003-m16hmfh.edgeql diff --git a/indexer/dbschema/default.esdl b/indexer/dbschema/default.esdl index 3f3a842..7867ae0 100644 --- a/indexer/dbschema/default.esdl +++ b/indexer/dbschema/default.esdl @@ -74,7 +74,7 @@ module default { type Device extending SolanaAccount, SplAccount, WithIx { required product: Product; - required signing_alg: DeviceSigningAlgorithm; + signing_alg: DeviceSigningAlgorithm; overloaded required token_account: str { constraint exclusive; }; diff --git a/indexer/dbschema/migrations/00003-m16hmfh.edgeql b/indexer/dbschema/migrations/00003-m16hmfh.edgeql new file mode 100644 index 0000000..856a154 --- /dev/null +++ b/indexer/dbschema/migrations/00003-m16hmfh.edgeql @@ -0,0 +1,9 @@ +CREATE MIGRATION m16hmfhcgsxehqt6kfgbb45zwaz3grmiw542n2uml36aug4dzrezya + ONTO m1dpxkogxfvmuai6l4jp4mv727vip2ys7abenahh3r66bmsygjva4q +{ + ALTER TYPE default::Device { + ALTER PROPERTY signing_alg { + RESET OPTIONALITY; + }; + }; +}; diff --git a/indexer/src/indexer.ts b/indexer/src/indexer.ts index 084e185..17ebc86 100644 --- a/indexer/src/indexer.ts +++ b/indexer/src/indexer.ts @@ -28,7 +28,10 @@ import { identifyDephyIdInstruction, parseActivateDeviceInstruction, parseInitializeInstruction, parseCreateDeviceInstruction, parseCreateProductInstruction, -} from "@dephy-io/dephy-id-program-client"; + parseCreateActivatedDeviceInstruction, + ParsedCreateActivatedDeviceInstruction, +// } from "@dephy-io/dephy-id-program-client"; +} from "./generated"; interface Config { rpcUrl?: string @@ -337,7 +340,7 @@ export class Indexer { signing_alg: e.cast(e.DeviceSigningAlgorithm, e.str(DeviceSigningAlgorithm[createDevice.data.signingAlg])), }), metadata: e.insert(e.TokenMetadata, { - name: product?.metadata?.name + ' DID', + name: createDevice.data.name, symbol: product?.metadata?.symbol, uri: createDevice.data.uri, additional: createDevice.data.additionalMetadata as [string, string][], @@ -346,7 +349,7 @@ export class Indexer { } async handleActivateDevice(dbTx: Executor, activateDevice: ParsedActivateDeviceInstruction, meta: IxMeta) { - let owner = await e.select(e.User, () => ({ + const owner = await e.select(e.User, () => ({ filter_single: { pubkey: activateDevice.accounts.owner.address, } @@ -377,6 +380,71 @@ export class Indexer { })).run(dbTx) } + async handleCreateActivatedDevice(dbTx: Executor, createActivatedDevice: ParsedCreateActivatedDeviceInstruction, meta: IxMeta) { + const product = await e.select(e.Product, () => ({ + metadata: { + name: true, + symbol: true, + }, + filter_single: { + mint_account: createActivatedDevice.accounts.productMint.address, + } + })).run(dbTx) + + const owner = await e.select(e.User, () => ({ + filter_single: { + pubkey: createActivatedDevice.accounts.owner.address, + } + })).run(dbTx) + + let ownerQuery; + if (owner) { + ownerQuery = e.select(e.User, () => ({ + filter_single: { + pubkey: createActivatedDevice.accounts.owner.address, + } + })) + } else { + ownerQuery = e.insert(e.User, { + pubkey: createActivatedDevice.accounts.owner.address, + }) + } + + await e.insert(e.DID, { + owner: ownerQuery, + mint_account: createActivatedDevice.accounts.deviceMint.address, + mint_authority: null, + token_account: createActivatedDevice.accounts.deviceAssociatedToken.address, + tx: e.select(e.Transaction, () => ({ + filter_single: { + signature: meta.tx, + }, + "@ix_index": e.int16(meta.index), + })), + device: e.insert(e.Device, { + pubkey: createActivatedDevice.accounts.device.address, + token_account: createActivatedDevice.accounts.productAssociatedToken.address, + product: e.select(e.Product, () => ({ + filter_single: { + mint_account: createActivatedDevice.accounts.productMint.address, + } + })), + tx: e.select(e.Transaction, () => ({ + filter_single: { + signature: meta.tx, + }, + "@ix_index": e.int16(meta.index), + })), + }), + metadata: e.insert(e.TokenMetadata, { + name: createActivatedDevice.data.name, + symbol: product?.metadata?.symbol, + uri: createActivatedDevice.data.uri, + additional: createActivatedDevice.data.additionalMetadata as [string, string][], + }), + }).run(dbTx) + } + async processProgramIx(dbTx: Executor, ix: PartiallyDecodedTransactionInstruction, meta: IxMeta) { let programIx = { accounts: ix.accounts.map(address => ({ address, role: 0 })), @@ -405,6 +473,11 @@ export class Indexer { await this.handleActivateDevice(dbTx, activateDevice, meta) break + case DephyIdInstruction.CreateActivatedDevice: + let createActivatedDevice = parseCreateActivatedDeviceInstruction(programIx) + await this.handleCreateActivatedDevice(dbTx, createActivatedDevice, meta) + break + default: break }