修改erc721相关方法

This commit is contained in:
cebgcontract 2022-06-17 21:38:43 +08:00
parent 43ace4dae1
commit 5a4d7298f9
4 changed files with 697 additions and 322 deletions

View File

@ -6,6 +6,7 @@ import { ZError } from "./common/ZError";
import { AllChains } from "./data/allchain"; import { AllChains } from "./data/allchain";
import { createWalletEvents, WALLET_CHAIN_CHANGE } from "./common/WalletEvent"; import { createWalletEvents, WALLET_CHAIN_CHANGE } from "./common/WalletEvent";
import { ERC20Standard } from "./standards/ERC20Standard"; import { ERC20Standard } from "./standards/ERC20Standard";
import { ERC721Standard } from "./standards/ERC721Standard";
var global = var global =
(typeof globalThis !== 'undefined' && globalThis) || (typeof globalThis !== 'undefined' && globalThis) ||
@ -39,11 +40,13 @@ export default class JCWallet {
chainMap: Map<number, IChainData> = new Map() chainMap: Map<number, IChainData> = new Map()
private _currentChain: IChainData private _currentChain: IChainData
public erc20Standard: ERC20Standard public erc20Standard: ERC20Standard
public erc721Standard: ERC721Standard
public mainHandlers = createWalletEvents() public mainHandlers = createWalletEvents()
constructor() { constructor() {
this.web3 = new Web3('https://rpc-testnet.kcc.network') this.web3 = new Web3('https://rpc-testnet.kcc.network')
this.erc20Standard = new ERC20Standard(this.web3); this.erc20Standard = new ERC20Standard(this.web3);
this.erc721Standard = new ERC721Standard(this.web3);
this.wallet = this.web3.eth.accounts.wallet.load(this.password) this.wallet = this.web3.eth.accounts.wallet.load(this.password)
if (!this.wallet) { if (!this.wallet) {
this.wallet = this.web3.eth.accounts.wallet; this.wallet = this.web3.eth.accounts.wallet;

View File

@ -70,12 +70,11 @@ export class ERC721Standard {
selectedAddress: string, selectedAddress: string,
index: number, index: number,
): Promise<string> => { ): Promise<string> => {
const contract = this.web3.eth.contract(abiERC721).at(address); const contract = new this.web3.eth.Contract(abiERC721, address);
return new Promise<string>((resolve, reject) => { return new Promise<string>((resolve, reject) => {
contract.tokenOfOwnerByIndex( contract.methods.tokenOfOwnerByIndex(
selectedAddress, selectedAddress,
index, index).call((error: Error, result: string) => {
(error: Error, result: string) => {
/* istanbul ignore if */ /* istanbul ignore if */
if (error) { if (error) {
reject(error); reject(error);
@ -95,7 +94,7 @@ export class ERC721Standard {
* @returns Promise resolving to the 'tokenURI'. * @returns Promise resolving to the 'tokenURI'.
*/ */
getTokenURI = async (address: string, tokenId: string): Promise<string> => { getTokenURI = async (address: string, tokenId: string): Promise<string> => {
const contract = this.web3.eth.contract(abiERC721).at(address); const contract = new this.web3.eth.Contract(abiERC721, address);
const supportsMetadata = await this.contractSupportsMetadataInterface( const supportsMetadata = await this.contractSupportsMetadataInterface(
address, address,
); );
@ -103,7 +102,7 @@ export class ERC721Standard {
throw new Error('Contract does not support ERC721 metadata interface.'); throw new Error('Contract does not support ERC721 metadata interface.');
} }
return new Promise<string>((resolve, reject) => { return new Promise<string>((resolve, reject) => {
contract.tokenURI(tokenId, (error: Error, result: string) => { contract.methods.tokenURI(tokenId).call( (error: Error, result: string) => {
/* istanbul ignore if */ /* istanbul ignore if */
if (error) { if (error) {
reject(error); reject(error);
@ -121,9 +120,9 @@ export class ERC721Standard {
* @returns Promise resolving to the 'name'. * @returns Promise resolving to the 'name'.
*/ */
getAssetName = async (address: string): Promise<string> => { getAssetName = async (address: string): Promise<string> => {
const contract = this.web3.eth.contract(abiERC721).at(address); const contract = new this.web3.eth.Contract(abiERC721, address);
return new Promise<string>((resolve, reject) => { return new Promise<string>((resolve, reject) => {
contract.name((error: Error, result: string) => { contract.methods.name().call((error: Error, result: string) => {
/* istanbul ignore if */ /* istanbul ignore if */
if (error) { if (error) {
reject(error); reject(error);
@ -141,9 +140,9 @@ export class ERC721Standard {
* @returns Promise resolving to the 'symbol'. * @returns Promise resolving to the 'symbol'.
*/ */
getAssetSymbol = async (address: string): Promise<string> => { getAssetSymbol = async (address: string): Promise<string> => {
const contract = this.web3.eth.contract(abiERC721).at(address); const contract = new this.web3.eth.Contract(abiERC721, address);
return new Promise<string>((resolve, reject) => { return new Promise<string>((resolve, reject) => {
contract.symbol((error: Error, result: string) => { contract.methods.symbol().call((error: Error, result: string) => {
/* istanbul ignore if */ /* istanbul ignore if */
if (error) { if (error) {
reject(error); reject(error);
@ -162,9 +161,9 @@ export class ERC721Standard {
* @returns Promise resolving to the owner address. * @returns Promise resolving to the owner address.
*/ */
async getOwnerOf(address: string, tokenId: string): Promise<string> { async getOwnerOf(address: string, tokenId: string): Promise<string> {
const contract = this.web3.eth.contract(abiERC721).at(address); const contract = new this.web3.eth.Contract(abiERC721, address);
return new Promise<string>((resolve, reject) => { return new Promise<string>((resolve, reject) => {
contract.ownerOf(tokenId, (error: Error, result: string) => { contract.methods.ownerOf(tokenId).call( (error: Error, result: string) => {
/* istanbul ignore if */ /* istanbul ignore if */
if (error) { if (error) {
reject(error); reject(error);
@ -186,11 +185,10 @@ export class ERC721Standard {
address: string, address: string,
interfaceId: string, interfaceId: string,
): Promise<boolean> => { ): Promise<boolean> => {
const contract = this.web3.eth.contract(abiERC721).at(address); const contract = new this.web3.eth.Contract(abiERC721, address);
return new Promise<boolean>((resolve, reject) => { return new Promise<boolean>((resolve, reject) => {
contract.supportsInterface( contract.methods.supportsInterface(
interfaceId, interfaceId).call((error: Error, result: boolean) => {
(error: Error, result: boolean) => {
/* istanbul ignore if */ /* istanbul ignore if */
if (error) { if (error) {
reject(error); reject(error);

File diff suppressed because it is too large Load Diff

View File

@ -119,4 +119,10 @@ export default class WalletController extends cc.Component {
let result = await this.wallet.erc20Standard.transfer({address, from, to, amount}) let result = await this.wallet.erc20Standard.transfer({address, from, to, amount})
console.log(result) console.log(result)
} }
async testGetHero() {
let address = '0x52917087cd4E48bDb5f336012E677f471f9E1C2D'
let info = await this.wallet.erc721Standard.getDetails(address, '', '101')
console.log(info)
}
} }