Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support draft nft #3

Merged
merged 1 commit into from
Oct 27, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
@@ -8,5 +8,7 @@ PROCESSOR_PROMETHEUS_PORT=3000
GQL_PORT=4350

NFT_CB=0x8d7ac116076a6031982ee65f323118b20b3d5a9ed3993a36a1da6e9faf8572c7
NFT_VIT=0x8d7ac116076a6031982ee65f323118b20b3d5a9ed3993a36a1da6e9faf8572c7
NFT_DRAFT=0x8d7ac116076a6031982ee65f323118b20b3d5a9ed3993a36a1da6e9faf8572c7
ARCHIVE_URI=https://squid-archive-testnet.gear-tech.io/graphql
NODE_ADDRESS=https://testnet.vara-network.io
1 change: 1 addition & 0 deletions assets/draft-nft.meta.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
0002000100000000000105000000010900000000000000010a000000010b0000000519580008306e66745f64726166745f696f304e46544472616674496e697400000c0128636f6c6c656374696f6e040128436f6c6c656374696f6e00012c6d617962655f6c696d69740c01344f7074696f6e3c4e667449643e000120696d675f6c696e6b080118537472696e6700000408306e66745f64726166745f696f28436f6c6c656374696f6e00000801106e616d65080118537472696e6700012c6465736372697074696f6e080118537472696e6700000800000502000c04184f7074696f6e04045401100108104e6f6e6500000010536f6d6504001000000100001000000507001408306e66745f64726166745f696f384e46544472616674416374696f6e0001202041646441646d696e0401246e65775f61646d696e18011c4163746f724964000000104d696e740801106e616d65080118537472696e6700012c6465736372697074696f6e080118537472696e67000100184d696e74546f0c0108746f18011c4163746f7249640001106e616d65080118537472696e6700012c6465736372697074696f6e080118537472696e67000200305472616e7366657246726f6d0c011066726f6d18011c4163746f724964000108746f18011c4163746f724964000120746f6b656e5f69641001144e66744964000300205472616e73666572080108746f18011c4163746f724964000120746f6b656e5f69641001144e667449640004001c417070726f7665080108746f18011c4163746f724964000120746f6b656e5f69641001144e66744964000500385265766f6b65417070726f76616c040120746f6b656e5f69641001144e66744964000600104275726e040120746f6b656e5f69641001144e66744964000700001810106773746418636f6d6d6f6e287072696d6974697665731c4163746f724964000004001c01205b75383b2033325d00001c0000032000000020002000000503002408306e66745f64726166745f696f344e465444726166744576656e7400011c2841646d696e4164646564000000184d696e7465641001146f776e657218011c4163746f724964000120746f6b656e5f69641001144e667449640001246d656469615f75726c080118537472696e670001286174747269625f75726c080118537472696e670001002c5472616e736665727265640c01146f776e657218011c4163746f724964000124726563697069656e7418011c4163746f724964000120746f6b656e5f69641001144e66744964000200144275726e74040120746f6b656e5f69641001144e6674496400030020417070726f76656408011c6163636f756e7418011c4163746f724964000120746f6b656e5f69641001144e667449640004003c417070726f76616c5265766f6b6564040120746f6b656e5f69641001144e66744964000500144572726f720400080118537472696e67000600002808306e66745f64726166745f696f285374617465517565727900010c104e616d650000002c4465736372697074696f6e0001000c416c6c000200002c08306e66745f64726166745f696f2853746174655265706c7900010c104e616d650400080118537472696e670000002c4465736372697074696f6e0400080118537472696e670001000c416c6c04003001344e465444726166745374617465000200003008306e66745f64726166745f696f344e46544472616674537461746500001c0118746f6b656e733401445665633c284e667449642c204e6674293e0001186f776e6572734001685665633c284163746f7249642c205665633c4e667449643e293e000124617070726f76616c734c01545665633c284e667449642c204163746f724964293e000128636f6c6c656374696f6e040128436f6c6c656374696f6e0001146e6f6e63651001144e66744964000120696d675f6c696e6b080118537472696e6700011861646d696e735401305665633c4163746f7249643e00003400000238003800000408103c003c08306e66745f64726166745f696f0c4e667400001401146f776e657218011c4163746f7249640001106e616d65080118537472696e6700012c6465736372697074696f6e080118537472696e670001246d656469615f75726c080118537472696e670001286174747269625f75726c080118537472696e67000040000002440044000004081848004800000210004c00000250005000000408101800540000021800
960 changes: 334 additions & 626 deletions package-lock.json

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -12,8 +12,8 @@
"start": "npm run migration:apply && node lib/main.js"
},
"dependencies": {
"@gear-js/api": "^0.34.0",
"@polkadot/api": "^10.9.1",
"@gear-js/api": "^0.35.1",
"@polkadot/api": "^10.10.1",
"@subsquid/archive-registry": "^3.1.0",
"@subsquid/graphql-server": "^3.3.2",
"@subsquid/ss58": "^0.1.4",
@@ -25,7 +25,7 @@
"typeorm": "^0.3.17"
},
"devDependencies": {
"@polkadot/types": "^10.9.1",
"@polkadot/types": "^10.10.1",
"@subsquid/substrate-metadata-explorer": "^1.1.2",
"@subsquid/substrate-typegen": "^2.2.1",
"@subsquid/typeorm-codegen": "^0.3.3",
2 changes: 2 additions & 0 deletions src/config.ts
Original file line number Diff line number Diff line change
@@ -13,12 +13,14 @@ export default {
nfts: {
cb: getEnv('NFT_CB'),
vit: getEnv('NFT_VIT'),
draft: getEnv('NFT_DRAFT'),
vitMigratedAtBlock: getEnv('VIT_MIGRATED_AT_BLOCK'),
vitMigratedTS: Number(getEnv('VIT_MIGRATED_TS')),
readMigrateNfts: Boolean(getEnv('READ_MIGRATED_NFTS', '')),
},
archive: {
uri: getEnv('ARCHIVE_URI'),
fromBlock: Number(getEnv('INDEX_FROM_BLOCK', '0')),
},
node: {
address: getEnv('NODE_ADDRESS'),
55 changes: 55 additions & 0 deletions src/handlers/draft-nft.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import { ProgramMetadata } from '@gear-js/api';
import { BatchState } from '../batchState';
import { readFileSync } from 'fs';
import { DraftNftEvent } from '../types';
import config from '../config';
import { getCollectionDescription, getCollectionName } from './helpers';

const meta = ProgramMetadata.from(readFileSync('./assets/draft-nft.meta.txt', 'utf8'));

export async function draftNftHandler(
state: BatchState,
payload: string,
source: string,
blockNumber: bigint,
ts: Date,
) {
const data = meta.createType<DraftNftEvent>(meta.types.handle.output, payload);
if (data.isMinted) {
const { owner, mediaUrl, tokenId, attribUrl } = data.asMinted;
let collection = await state.getCollection(source);
if (!collection) {
const [collectionName, collectionDesc] = await Promise.all([
getCollectionName(meta, config.nfts.draft, '0x00'),
getCollectionDescription(meta, config.nfts.draft, '0x01'),
]);
collection = state.newCollection(source, collectionName, collectionDesc);
}

await state.mintNft(
tokenId.toString(),
collection,
owner.toHex(),
[attribUrl.toString()],
collection.description,
collection.name + ' - ' + tokenId.toString(),
mediaUrl.toString(),
blockNumber,
ts,
data.isMinted,
);
} else if (data.isTransferred) {
const { tokenId, owner, recipient } = data.asTransferred;
const collection = await state.getCollection(source);
await state.transferNft(tokenId.toString(), collection, owner.toHex(), recipient.toHex(), ts, blockNumber);
} else if (data.isBurnt) {
const { tokenId } = data.asBurnt;
await state.burnNft(tokenId.toString(), source);
} else if (data.isApproved) {
const { account, tokenId } = data.asApproved;
await state.approveAccount(tokenId.toString(), source, account.toHex());
} else if (data.isApprovalRevoked) {
const { tokenId } = data.asApprovalRevoked;
await state.revokeApprove(tokenId.toString(), source);
}
}
6 changes: 2 additions & 4 deletions src/handlers/helpers.ts
Original file line number Diff line number Diff line change
@@ -3,8 +3,7 @@ import { StateReply } from '../types';
import { HumanTypesRepr } from '@gear-js/api';
import { gearReadStateReq } from '../utils';

export async function getCollectionName(meta: ProgramMetadata, programId: string) {
const payload = '0x07';
export async function getCollectionName(meta: ProgramMetadata, programId: string, payload = '0x07') {
const result = await gearReadStateReq(programId, payload);
const data = meta.createType<StateReply>((meta.types.state as HumanTypesRepr).output, result);
if (data.isName) {
@@ -13,8 +12,7 @@ export async function getCollectionName(meta: ProgramMetadata, programId: string
throw new Error('Invalid state');
}

export async function getCollectionDescription(meta: ProgramMetadata, programId: string) {
const payload = '0x08';
export async function getCollectionDescription(meta: ProgramMetadata, programId: string, payload = '0x08') {
const result = await gearReadStateReq(programId, payload);
const data = meta.createType<StateReply>((meta.types.state as HumanTypesRepr).output, result);
if (data.isDescription) {
3 changes: 3 additions & 0 deletions src/main.ts
Original file line number Diff line number Diff line change
@@ -5,6 +5,7 @@ import config from './config';
import { BatchState } from './batchState';
import { cbNftHandler } from './handlers/cb-nft';
import { readMigratedNfts } from './handlers/vit-nft';
import { draftNftHandler } from './handlers/draft-nft';

const programs = [config.nfts.cb, config.nfts.vit];

@@ -37,6 +38,8 @@ processor.run(new TypeormDatabase(), async (ctx) => {

if (programs.includes(source)) {
await cbNftHandler(state, payload, source, blockNumber, ts);
} else if (source === config.nfts.draft) {
await draftNftHandler(state, payload, source, blockNumber, ts);
}
}
}
2 changes: 1 addition & 1 deletion src/processor.ts
Original file line number Diff line number Diff line change
@@ -19,7 +19,7 @@ export const processor = new SubstrateBatchProcessor()
},
},
} as const)
.setBlockRange({ from: 513000 });
.setBlockRange({ from: config.archive.fromBlock });

export type Item = BatchProcessorItem<typeof processor>;
export type EventItem = BatchProcessorEventItem<typeof processor>;
38 changes: 38 additions & 0 deletions src/types/draft-nft.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { Enum, Text, u128 } from '@polkadot/types';
import { Hash } from '@polkadot/types/interfaces';

export interface DraftNftEvent extends Enum {
isMinted: boolean;
asMinted: {
owner: Hash;
tokenId: u128;
mediaUrl: Text;
attribUrl: Text;
};
isTransferred: boolean;
asTransferred: {
owner: Hash;
recipient: Hash;
tokenId: u128;
};
isBurnt: boolean;
asBurnt: {
tokenId: u128;
};
isApproved: boolean;
asApproved: {
account: Hash;
tokenId: u128;
};
isApprovalRevoked: boolean;
asApprovalRevoked: {
tokenId: u128;
};
}

export interface DragtNftStateReply extends Enum {
isName: boolean;
asName: Text;
isDescription: boolean;
asDescription: Text;
}
1 change: 1 addition & 0 deletions src/types/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export * from './events';
export * from './cb-nft';
export * from './vit.nft';
export * from './draft-nft';