Skip to content

Commit

Permalink
[Issue-3191] Demo inscription
Browse files Browse the repository at this point in the history
  • Loading branch information
bluezdot committed Jun 12, 2024
1 parent 1a17e91 commit 0fd60c8
Show file tree
Hide file tree
Showing 6 changed files with 182 additions and 5 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@
"@polkadot/types-support": "^11.2.1",
"@polkadot/util": "^12.6.2",
"@polkadot/util-crypto": "^12.6.2",
"@subwallet/chain-list": "0.2.67",
"@subwallet/chain-list": "/Users/bluedot/tets/SubWallet-ChainList/packages/chain-list/build",
"@subwallet/keyring": "^0.1.5",
"@subwallet/react-ui": "5.1.2-b77",
"@subwallet/ui-keyring": "^0.1.5",
Expand Down
171 changes: 171 additions & 0 deletions packages/extension-base/src/koni/api/nft/blobinscription/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,171 @@
// Copyright 2019-2022 @subwallet/extension-koni authors & contributors
// SPDX-License-Identifier: Apache-2.0

import { NftCollection, NftItem } from '@subwallet/extension-base/background/KoniTypes';
// import { HIRO_API } from '@subwallet/extension-base/koni/api/nft/inscription/constants/api';
// import { InscriptionResponseItem } from '@subwallet/extension-base/koni/api/nft/inscription/types/interface';
import { BaseNftApi, HandleNftParams } from '@subwallet/extension-base/koni/api/nft/nft';
import {AVAIL_LIGHT_CLIENT_NFT} from "@subwallet/extension-base/koni/api/nft/config";
import BigNumber from "bignumber.js";
// import { Inscription } from '@subwallet/extension-base/services/chain-service/handler/bitcoin/strategy/BlockStream/types';
// import { getAddressInscriptions, getInscriptionContent } from '@subwallet/extension-base/services/hiro-service/utils';
// import fetch from 'cross-fetch';

interface NftResponse {
data: {
dataAvailabilities: NftData[]
}
}

const COLLECT_ID = 'ALC_NFT';

interface NftData {
isJson: boolean,
id: string,
extrinsicHash: string,
dataValue: string,
dataRaw: string,
blockNumber: number,
action: string,
sender: {
address: string
}
}

interface ALC { // need confirm
p: string,
op: string,
tick: string,
amt: BigNumber,
val: BigNumber
}

export class BlobInscriptionApi extends BaseNftApi {
endpoint = AVAIL_LIGHT_CLIENT_NFT;

constructor (chain: string, addresses: string[]) {
super(chain, undefined, addresses);
}

private static parseNftRequest (address: string, isJson_eq = true) {
return {
query: `
query MyQuery {
dataAvailabilities(where: {isJson_eq: ${isJson_eq}, sender: {address_eq: ${address}}) {
isJson
id
extrinsicHash
dataValue
dataRaw
blockNumber
action
sender {
address
}
}
}
`
};
}

private async getBalances (address: string) {

const response = await fetch(this.endpoint, {
method: 'post',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify(BlobInscriptionApi.parseNftRequest(address))
});

// const result = await response.json() as NftResponse;
// return result?.data?.dataAvailabilities;
const result = [
{
"isJson": false,
"id": "0000116353-c5fc0-000001",
"extrinsicHash": "0x2b0a2945216f79032606a2cb10b7f846931f8016ff786e3eee3f0a15463e4548",
"dataValue": "example data",
"dataRaw": "0x6578616d706c652064617461",
"blockNumber": 116353,
"action": "DataAvailability.submit_data",
"sender": {
"address": "5GgRqSNN1zTsjA6N7cofcdP9yewA6JG83S649HbuBut8MG4o"
}
},
{
"isJson": true,
"id": "0000116822-9e7a8-000001",
"extrinsicHash": "0x45489cf02dd047f95242576829a5373231049b290c8354a4d8615bae02e5c05b",
"dataValue": '{"p":"pdc-20","op":"LIST","tick":"TEST","val":"100","amt":"100000"}',
"dataRaw": "0x6578616d706c652064617461",
"blockNumber": 116822,
"action": "DataAvailability.submit_data",
"sender": {
"address": "5GgRqSNN1zTsjA6N7cofcdP9yewA6JG83S649HbuBut8MG4o"
}
}
]

return result;
}

public async handleNfts (params: HandleNftParams) {
try {
await Promise.all(this.addresses.map(async (address) => {
// 1. Lấy balance
const balances = await this.getBalances(address);
console.log('balances', balances);

// 2. Handle và push inscription
if (balances.length > 0) {
const collectionMap: Record <string, NftCollection> = {};

for (const nft of balances) {
if (nft.isJson) {
const data = JSON.parse(nft.dataValue) as ALC;
console.log('data', data);

const parsedNft: NftItem = {
id: data.tick, // is distinct?
chain: this.chain,
owner: '5Hawkn8oUeSTB3LesTh5nGjfnpor2ZWBArdQ64d6BxgD5Pgm', // is submitter = owner? address, //
name: data.tick,
image: 'https://ipfs.uniquenetwork.dev/ipfs/Qmap7uz7JKZNovCdLfdDE3p4XA6shghdADS7EsHvLjL6jT/nft_image_43.png', // recheck
description: 'abc',
collectionId: COLLECT_ID
// properties: data
}

params.updateItem(this.chain, parsedNft, '5Hawkn8oUeSTB3LesTh5nGjfnpor2ZWBArdQ64d6BxgD5Pgm'); // '5Hawkn8oUeSTB3LesTh5nGjfnpor2ZWBArdQ64d6BxgD5Pgm'

if (!collectionMap[COLLECT_ID]) {
const parsedCollection: NftCollection = {
collectionId: COLLECT_ID,
chain: this.chain,
collectionName: COLLECT_ID
};

collectionMap[COLLECT_ID] = parsedCollection;
params.updateCollection(this.chain, parsedCollection);
console.log('parsedNft', parsedNft, parsedCollection);
}
}
}
}
}));
} catch (error) {
console.error(`Failed to fetch blob inscription`, error);
}
}

public async fetchNfts (params: HandleNftParams): Promise<number> {
try {
await this.handleNfts(params);
} catch (e) {
return 0;
}

return 1;
}
}
2 changes: 2 additions & 0 deletions packages/extension-base/src/koni/api/nft/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ export const PINATA_IPFS_GATEWAY = 'https://gateway.pinata.cloud/ipfs/';

export const VARA_SCAN_ENDPOINT = 'https://nft-explorer.vara-network.io/graphql';

export const AVAIL_LIGHT_CLIENT_NFT = 'https://indexer-nft.availspace.app/graphql';

export const UNIQUE_SCAN_ENDPOINT = 'https://api-unique.uniquescan.io/v1/graphql';

export const QUARTZ_SCAN_ENDPOINT = 'https://api-quartz.uniquescan.io/v1/graphql';
Expand Down
3 changes: 3 additions & 0 deletions packages/extension-base/src/koni/api/nft/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import { _isChainSupportEvmNft, _isChainSupportNativeNft, _isChainSupportWasmNft
import { categoryAddresses, targetIsWeb } from '@subwallet/extension-base/utils';

import AssetHubNftsPalletApi from './assethub_nft';
import { BlobInscriptionApi } from "@subwallet/extension-base/koni/api/nft/blobinscription";

function createSubstrateNftApi (chain: string, substrateApi: _SubstrateApi | null, addresses: string[]): BaseNftApi[] | null {
const [substrateAddresses] = categoryAddresses(addresses);
Expand All @@ -41,6 +42,8 @@ function createSubstrateNftApi (chain: string, substrateApi: _SubstrateApi | nul
return [new BitCountryNftApi(substrateApi, substrateAddresses, chain)];
} else if (_NFT_CHAIN_GROUP.vara.includes(chain)) {
return [new VaraNftApi(chain, substrateAddresses)];
} else if (_NFT_CHAIN_GROUP.avail.includes(chain)) {
return [new BlobInscriptionApi(chain, ['5GgRqSNN1zTsjA6N7cofcdP9yewA6JG83S649HbuBut8MG4o'])];
}

return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,8 @@ export const _NFT_CHAIN_GROUP = {
statemint: ['statemint'],
unique_network: ['unique_network', 'quartz', 'opal'],
bitcountry: ['bitcountry', 'pioneer', 'continuum_network'],
vara: ['vara_network']
vara: ['vara_network'],
avail: ['availTuringTest']
};

// Staking--------------------------------------------------------------------------------------------------------------
Expand Down
6 changes: 3 additions & 3 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -6000,14 +6000,14 @@ __metadata:
languageName: node
linkType: hard

"@subwallet/chain-list@npm:0.2.67":
"@subwallet/chain-list@file:/Users/bluedot/tets/SubWallet-ChainList/packages/chain-list/build::locator=root-workspace-0b6124%40workspace%3A.":
version: 0.2.67
resolution: "@subwallet/chain-list@npm:0.2.67"
resolution: "@subwallet/chain-list@file:/Users/bluedot/tets/SubWallet-ChainList/packages/chain-list/build#/Users/bluedot/tets/SubWallet-ChainList/packages/chain-list/build::hash=1d8d31&locator=root-workspace-0b6124%40workspace%3A."
dependencies:
"@polkadot/dev": 0.67.167
"@polkadot/util": ^12.5.1
eventemitter3: ^5.0.1
checksum: c86b9bb5d9bd0d5fe1fa8bfd00b2f6cd97ac34f1bb1056520840c85f084a30e4ad9a63708ceb10f26d6daedd648f47e74a8425df471e6feae9711f2b686c2d69
checksum: f09c35556a516a26055236771492218943c3982a3a6a8f9eb78e3e656882413de5a5d92a7fdede42d2b68c5a00d695578804f6d70bcea330f263a98b3ab64b0d
languageName: node
linkType: hard

Expand Down

0 comments on commit 0fd60c8

Please sign in to comment.