Skip to content

Commit

Permalink
update indexer
Browse files Browse the repository at this point in the history
  • Loading branch information
Kabie authored and jasl committed Jun 24, 2024
1 parent b2b59ed commit b18434b
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 4 deletions.
2 changes: 1 addition & 1 deletion indexer/dbschema/default.esdl
Original file line number Diff line number Diff line change
Expand Up @@ -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;
};
Expand Down
9 changes: 9 additions & 0 deletions indexer/dbschema/migrations/00003-m16hmfh.edgeql
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
CREATE MIGRATION m16hmfhcgsxehqt6kfgbb45zwaz3grmiw542n2uml36aug4dzrezya
ONTO m1dpxkogxfvmuai6l4jp4mv727vip2ys7abenahh3r66bmsygjva4q
{
ALTER TYPE default::Device {
ALTER PROPERTY signing_alg {
RESET OPTIONALITY;
};
};
};
79 changes: 76 additions & 3 deletions indexer/src/indexer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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][],
Expand All @@ -346,7 +349,7 @@ export class Indexer {
}

async handleActivateDevice(dbTx: Executor, activateDevice: ParsedActivateDeviceInstruction<string, readonly IAccountMeta[]>, meta: IxMeta) {
let owner = await e.select(e.User, () => ({
const owner = await e.select(e.User, () => ({
filter_single: {
pubkey: activateDevice.accounts.owner.address,
}
Expand Down Expand Up @@ -377,6 +380,71 @@ export class Indexer {
})).run(dbTx)
}

async handleCreateActivatedDevice(dbTx: Executor, createActivatedDevice: ParsedCreateActivatedDeviceInstruction<string, IAccountMeta[]>, 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 })),
Expand Down Expand Up @@ -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
}
Expand Down

0 comments on commit b18434b

Please sign in to comment.