forked from polkadot-js/extension
-
Notifications
You must be signed in to change notification settings - Fork 62
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
182 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
171 changes: 171 additions & 0 deletions
171
packages/extension-base/src/koni/api/nft/blobinscription/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters