forked from MystenLabs/sui
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[NFT Mirroring] validate ownership (MystenLabs#874)
- Loading branch information
Showing
5 changed files
with
192 additions
and
92 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
[{"constant":true,"inputs":[],"name":"name","outputs":[{"name":"_name","type":"string"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[{"name":"_tokenId","type":"uint256"}],"name":"getApproved","outputs":[{"name":"_approved","type":"address"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[{"name":"_to","type":"address"},{"name":"_tokenId","type":"uint256"}],"name":"approve","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[],"name":"implementsERC721","outputs":[{"name":"_implementsERC721","type":"bool"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[],"name":"totalSupply","outputs":[{"name":"_totalSupply","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[{"name":"_from","type":"address"},{"name":"_to","type":"address"},{"name":"_tokenId","type":"uint256"}],"name":"transferFrom","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[{"name":"_owner","type":"address"},{"name":"_index","type":"uint256"}],"name":"tokenOfOwnerByIndex","outputs":[{"name":"_tokenId","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[{"name":"_tokenId","type":"uint256"}],"name":"ownerOf","outputs":[{"name":"_owner","type":"address"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[{"name":"_tokenId","type":"uint256"}],"name":"tokenMetadata","outputs":[{"name":"_infoUrl","type":"string"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[{"name":"_owner","type":"address"}],"name":"balanceOf","outputs":[{"name":"_balance","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[{"name":"_owner","type":"address"},{"name":"_tokenId","type":"uint256"},{"name":"_approvedAddress","type":"address"},{"name":"_metadata","type":"string"}],"name":"mint","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[],"name":"symbol","outputs":[{"name":"_symbol","type":"string"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[{"name":"_to","type":"address"},{"name":"_tokenId","type":"uint256"}],"name":"transfer","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[],"name":"numTokensTotal","outputs":[{"name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[{"name":"_owner","type":"address"}],"name":"getOwnerTokens","outputs":[{"name":"_tokenIds","type":"uint256[]"}],"payable":false,"stateMutability":"view","type":"function"},{"anonymous":false,"inputs":[{"indexed":true,"name":"_to","type":"address"},{"indexed":true,"name":"_tokenId","type":"uint256"}],"name":"Mint","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"name":"_from","type":"address"},{"indexed":true,"name":"_to","type":"address"},{"indexed":false,"name":"_tokenId","type":"uint256"}],"name":"Transfer","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"name":"_owner","type":"address"},{"indexed":true,"name":"_approved","type":"address"},{"indexed":false,"name":"_tokenId","type":"uint256"}],"name":"Approval","type":"event"}] |
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,117 @@ | ||
// Copyright (c) 2022, Mysten Labs, Inc. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
import { | ||
AlchemyWeb3, | ||
createAlchemyWeb3, | ||
GetNftsParams, | ||
Nft as AlchemyNft, | ||
} from '@alch/alchemy-web3'; | ||
|
||
export interface NFT { | ||
/** | ||
* A descriptive name for the NFT | ||
*/ | ||
name: string; | ||
|
||
/** | ||
* The address of the collection contract | ||
*/ | ||
contract_address: string; | ||
|
||
/** | ||
* The token id associated with the source contract address | ||
*/ | ||
token_id: string; | ||
|
||
/** | ||
* Uri representing the location of the NFT media asset. The uri often | ||
* links to an image. The uri is parsed from the metadata and can be | ||
* standard URLs pointing to images on conventional servers, IPFS, or | ||
* Arweave. The image format can be SVGs, PNGs, JPEGs, etc. | ||
*/ | ||
media_uri?: string; | ||
} | ||
|
||
export interface NFTInfo { | ||
token: NFT; | ||
claim_status: 'none' | 'claimed'; | ||
destination_sui_address?: string; | ||
sui_explorer_link?: string; | ||
} | ||
|
||
/** | ||
* Utility class for fetching NFT info | ||
*/ | ||
export class NFTFetcher { | ||
/** @internal */ _alchemy: AlchemyWeb3; | ||
|
||
constructor() { | ||
this._alchemy = this.getAlchemyAPI(); | ||
} | ||
|
||
public async getNFTInfoByAddress(address: string): Promise<NFTInfo[]> { | ||
return await this.getNFTInfo({ owner: address }); | ||
} | ||
|
||
public async getNFTInfo(params: GetNftsParams): Promise<NFTInfo[]> { | ||
const nfts = await this.getNFTsByAddress(params); | ||
return nfts.map((token) => ({ | ||
token, | ||
// TODO: check db to see if airdrop has been claimed or not | ||
claim_status: 'none', | ||
})); | ||
} | ||
|
||
private async getNFTsByAddress(params: GetNftsParams): Promise<NFT[]> { | ||
const nfts = await this._alchemy.alchemy.getNfts(params); | ||
return await Promise.all( | ||
nfts.ownedNfts.map((a) => | ||
this.extractFieldsFromAlchemyNFT(a as AlchemyNft) | ||
) | ||
); | ||
} | ||
|
||
private async extractFieldsFromAlchemyNFT( | ||
alchemyNft: AlchemyNft | ||
): Promise<NFT> { | ||
// TODO: look into using gateway uri https://docs.alchemy.com/alchemy/guides/nft-api-faq#understanding-nft-metadata | ||
const { | ||
title, | ||
metadata, | ||
id: { tokenId: token_id }, | ||
contract: { address: contract_address }, | ||
} = alchemyNft; | ||
const name = (await this.getTokenName(contract_address)) ?? title; | ||
return { | ||
contract_address, | ||
name, | ||
token_id, | ||
media_uri: metadata?.image, | ||
}; | ||
} | ||
|
||
private getAlchemyAPI(): AlchemyWeb3 { | ||
// TODO: implement pagination | ||
const api_key = process.env.ALCHEMY_API_KEY || 'demo'; | ||
return createAlchemyWeb3( | ||
`https://eth-mainnet.alchemyapi.io/v2/${api_key}` | ||
); | ||
} | ||
|
||
private async getTokenName(tokenContract: string) { | ||
try { | ||
const contractABI = require('./erc721.abi.json'); | ||
const contractObject = new (this.getAlchemyAPI().eth.Contract)( | ||
contractABI, | ||
tokenContract | ||
); | ||
return await contractObject.methods.name().call(); | ||
} catch (err) { | ||
console.error( | ||
`Encountered error fetching name for contract ${tokenContract}: ${err}` | ||
); | ||
return ''; | ||
} | ||
} | ||
} |
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