修改链交互操作的gas和gasPrice
This commit is contained in:
parent
e0591c1be5
commit
3edab20653
@ -1,13 +1,13 @@
|
|||||||
import Web3 from "web3";
|
import Web3 from 'web3';
|
||||||
import { abiERC1155 } from "../abis/abiERC1155";
|
import { abiERC1155 } from '../abis/abiERC1155';
|
||||||
import { universalChainCb } from "../util/chain.util";
|
import { universalChainCb } from '../util/chain.util';
|
||||||
import { timeoutFetch } from "../util/net.util";
|
import { timeoutFetch } from '../util/net.util';
|
||||||
import { getFormattedIpfsUrl } from "../util/wallet.util";
|
import { getFormattedIpfsUrl } from '../util/wallet.util';
|
||||||
|
|
||||||
export const ERC1155 = "ERC1155";
|
export const ERC1155 = 'ERC1155';
|
||||||
export const ERC1155_INTERFACE_ID = "0xd9b67a26";
|
export const ERC1155_INTERFACE_ID = '0xd9b67a26';
|
||||||
export const ERC1155_METADATA_URI_INTERFACE_ID = "0x0e89341c";
|
export const ERC1155_METADATA_URI_INTERFACE_ID = '0x0e89341c';
|
||||||
export const ERC1155_TOKEN_RECEIVER_INTERFACE_ID = "0x4e2312e0";
|
export const ERC1155_TOKEN_RECEIVER_INTERFACE_ID = '0x4e2312e0';
|
||||||
|
|
||||||
export class ERC1155Standard {
|
export class ERC1155Standard {
|
||||||
private web3: Web3;
|
private web3: Web3;
|
||||||
@ -22,13 +22,8 @@ export class ERC1155Standard {
|
|||||||
* @param address - ERC1155 asset contract address.
|
* @param address - ERC1155 asset contract address.
|
||||||
* @returns Promise resolving to whether the contract implements ERC1155 URI Metadata interface.
|
* @returns Promise resolving to whether the contract implements ERC1155 URI Metadata interface.
|
||||||
*/
|
*/
|
||||||
contractSupportsURIMetadataInterface = async (
|
contractSupportsURIMetadataInterface = async (address: string): Promise<boolean> => {
|
||||||
address: string
|
return this.contractSupportsInterface(address, ERC1155_METADATA_URI_INTERFACE_ID);
|
||||||
): Promise<boolean> => {
|
|
||||||
return this.contractSupportsInterface(
|
|
||||||
address,
|
|
||||||
ERC1155_METADATA_URI_INTERFACE_ID
|
|
||||||
);
|
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -37,13 +32,8 @@ export class ERC1155Standard {
|
|||||||
* @param address - ERC1155 asset contract address.
|
* @param address - ERC1155 asset contract address.
|
||||||
* @returns Promise resolving to whether the contract implements ERC1155 Token Receiver interface.
|
* @returns Promise resolving to whether the contract implements ERC1155 Token Receiver interface.
|
||||||
*/
|
*/
|
||||||
contractSupportsTokenReceiverInterface = async (
|
contractSupportsTokenReceiverInterface = async (address: string): Promise<boolean> => {
|
||||||
address: string
|
return this.contractSupportsInterface(address, ERC1155_TOKEN_RECEIVER_INTERFACE_ID);
|
||||||
): Promise<boolean> => {
|
|
||||||
return this.contractSupportsInterface(
|
|
||||||
address,
|
|
||||||
ERC1155_TOKEN_RECEIVER_INTERFACE_ID
|
|
||||||
);
|
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -52,9 +42,7 @@ export class ERC1155Standard {
|
|||||||
* @param address - ERC1155 asset contract address.
|
* @param address - ERC1155 asset contract address.
|
||||||
* @returns Promise resolving to whether the contract implements the base ERC1155 interface.
|
* @returns Promise resolving to whether the contract implements the base ERC1155 interface.
|
||||||
*/
|
*/
|
||||||
contractSupportsBase1155Interface = async (
|
contractSupportsBase1155Interface = async (address: string): Promise<boolean> => {
|
||||||
address: string
|
|
||||||
): Promise<boolean> => {
|
|
||||||
return this.contractSupportsInterface(address, ERC1155_INTERFACE_ID);
|
return this.contractSupportsInterface(address, ERC1155_INTERFACE_ID);
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -68,16 +56,14 @@ export class ERC1155Standard {
|
|||||||
getTokenURI = async (address: string, tokenId: string): Promise<string> => {
|
getTokenURI = async (address: string, tokenId: string): Promise<string> => {
|
||||||
const contract = new this.web3.eth.Contract(abiERC1155, address);
|
const contract = new this.web3.eth.Contract(abiERC1155, address);
|
||||||
return new Promise<string>((resolve, reject) => {
|
return new Promise<string>((resolve, reject) => {
|
||||||
contract.methods
|
contract.methods.tokenURI(tokenId).call((error: Error, result: string) => {
|
||||||
.tokenURI(tokenId)
|
/* istanbul ignore if */
|
||||||
.call((error: Error, result: string) => {
|
if (error) {
|
||||||
/* istanbul ignore if */
|
reject(error);
|
||||||
if (error) {
|
return;
|
||||||
reject(error);
|
}
|
||||||
return;
|
resolve(result);
|
||||||
}
|
});
|
||||||
resolve(result);
|
|
||||||
});
|
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -89,25 +75,17 @@ export class ERC1155Standard {
|
|||||||
* @param tokenId - ERC1155 asset identifier.
|
* @param tokenId - ERC1155 asset identifier.
|
||||||
* @returns Promise resolving to the 'balanceOf'.
|
* @returns Promise resolving to the 'balanceOf'.
|
||||||
*/
|
*/
|
||||||
getBalanceOf = async (
|
getBalanceOf = async (contractAddress: string, address: string, tokenId: string): Promise<number> => {
|
||||||
contractAddress: string,
|
|
||||||
address: string,
|
|
||||||
tokenId: string
|
|
||||||
): Promise<number> => {
|
|
||||||
const contract = new this.web3.eth.Contract(abiERC1155, contractAddress);
|
const contract = new this.web3.eth.Contract(abiERC1155, contractAddress);
|
||||||
return new Promise<number>((resolve, reject) => {
|
return new Promise<number>((resolve, reject) => {
|
||||||
contract.methods.balanceOf(
|
contract.methods.balanceOf(address, tokenId, (error: Error, result: number) => {
|
||||||
address,
|
/* istanbul ignore if */
|
||||||
tokenId,
|
if (error) {
|
||||||
(error: Error, result: number) => {
|
reject(error);
|
||||||
/* istanbul ignore if */
|
return;
|
||||||
if (error) {
|
|
||||||
reject(error);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
resolve(result);
|
|
||||||
}
|
}
|
||||||
);
|
resolve(result);
|
||||||
|
});
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -118,23 +96,17 @@ export class ERC1155Standard {
|
|||||||
* @param interfaceId - Interface identifier.
|
* @param interfaceId - Interface identifier.
|
||||||
* @returns Promise resolving to whether the contract implements `interfaceID`.
|
* @returns Promise resolving to whether the contract implements `interfaceID`.
|
||||||
*/
|
*/
|
||||||
private contractSupportsInterface = async (
|
private contractSupportsInterface = async (address: string, interfaceId: string): Promise<boolean> => {
|
||||||
address: string,
|
|
||||||
interfaceId: string
|
|
||||||
): Promise<boolean> => {
|
|
||||||
const contract = new this.web3.eth.Contract(abiERC1155, address);
|
const contract = new this.web3.eth.Contract(abiERC1155, address);
|
||||||
return new Promise<boolean>((resolve, reject) => {
|
return new Promise<boolean>((resolve, reject) => {
|
||||||
contract.methods.supportsInterface(
|
contract.methods.supportsInterface(interfaceId, (error: Error, result: boolean) => {
|
||||||
interfaceId,
|
/* istanbul ignore if */
|
||||||
(error: Error, result: boolean) => {
|
if (error) {
|
||||||
/* istanbul ignore if */
|
reject(error);
|
||||||
if (error) {
|
return;
|
||||||
reject(error);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
resolve(result);
|
|
||||||
}
|
}
|
||||||
);
|
resolve(result);
|
||||||
|
});
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -164,7 +136,7 @@ export class ERC1155Standard {
|
|||||||
|
|
||||||
if (tokenId) {
|
if (tokenId) {
|
||||||
tokenURI = await this.getTokenURI(address, tokenId);
|
tokenURI = await this.getTokenURI(address, tokenId);
|
||||||
if (tokenURI.startsWith("ipfs://")) {
|
if (tokenURI.startsWith('ipfs://')) {
|
||||||
tokenURI = getFormattedIpfsUrl(ipfsGateway, tokenURI, true);
|
tokenURI = getFormattedIpfsUrl(ipfsGateway, tokenURI, true);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -172,7 +144,7 @@ export class ERC1155Standard {
|
|||||||
const response = await timeoutFetch(tokenURI);
|
const response = await timeoutFetch(tokenURI);
|
||||||
const object = await response.json();
|
const object = await response.json();
|
||||||
image = object?.image;
|
image = object?.image;
|
||||||
if (image?.startsWith("ipfs://")) {
|
if (image?.startsWith('ipfs://')) {
|
||||||
image = getFormattedIpfsUrl(ipfsGateway, image, true);
|
image = getFormattedIpfsUrl(ipfsGateway, image, true);
|
||||||
}
|
}
|
||||||
} catch {
|
} catch {
|
||||||
@ -207,9 +179,7 @@ export class ERC1155Standard {
|
|||||||
}) {
|
}) {
|
||||||
const contract = new this.web3.eth.Contract(abiERC1155, address);
|
const contract = new this.web3.eth.Contract(abiERC1155, address);
|
||||||
if (!gas) {
|
if (!gas) {
|
||||||
gas = await contract.methods
|
gas = await contract.methods.safeBatchTransferFrom(from, to, tokenIds, amounts, []).estimateGas();
|
||||||
.safeBatchTransferFrom(from, to, tokenIds, amounts, [])
|
|
||||||
.estimateGas();
|
|
||||||
}
|
}
|
||||||
gas = (gas * 1.1) | 1;
|
gas = (gas * 1.1) | 1;
|
||||||
if (estimate) {
|
if (estimate) {
|
||||||
@ -227,17 +197,17 @@ export class ERC1155Standard {
|
|||||||
}
|
}
|
||||||
const logData = {
|
const logData = {
|
||||||
gas,
|
gas,
|
||||||
title: "transfer",
|
title: 'transfer',
|
||||||
details: detailArr,
|
details: detailArr,
|
||||||
};
|
};
|
||||||
|
let gasPrice = await this.web3.eth.getGasPrice();
|
||||||
return universalChainCb(
|
return universalChainCb(
|
||||||
logData,
|
logData,
|
||||||
contract.methods
|
contract.methods.safeBatchTransferFrom(from, to, tokenIds, amounts, []).send({
|
||||||
.safeBatchTransferFrom(from, to, tokenIds, amounts, [])
|
from,
|
||||||
.send({
|
gas,
|
||||||
from,
|
gasPrice,
|
||||||
gas,
|
})
|
||||||
})
|
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,8 +1,8 @@
|
|||||||
import Web3 from "web3";
|
import Web3 from 'web3';
|
||||||
import { abiERC20 } from "../abis/abiERC20";
|
import { abiERC20 } from '../abis/abiERC20';
|
||||||
import { BN, toUtf8 } from "ethereumjs-util";
|
import { BN, toUtf8 } from 'ethereumjs-util';
|
||||||
import { universalChainCb } from "../util/chain.util";
|
import { universalChainCb } from '../util/chain.util';
|
||||||
import { toWeiBn } from "../util/number.util";
|
import { toWeiBn } from '../util/number.util';
|
||||||
|
|
||||||
export class ERC20Standard {
|
export class ERC20Standard {
|
||||||
private web3: Web3;
|
private web3: Web3;
|
||||||
@ -21,17 +21,15 @@ export class ERC20Standard {
|
|||||||
async getBalanceOf(address: string, selectedAddress: string): Promise<BN> {
|
async getBalanceOf(address: string, selectedAddress: string): Promise<BN> {
|
||||||
const contract = new this.web3.eth.Contract(abiERC20, address);
|
const contract = new this.web3.eth.Contract(abiERC20, address);
|
||||||
return new Promise<BN>((resolve, reject) => {
|
return new Promise<BN>((resolve, reject) => {
|
||||||
contract.methods
|
contract.methods.balanceOf(selectedAddress).call({ from: selectedAddress }, (error: Error, result: BN) => {
|
||||||
.balanceOf(selectedAddress)
|
/* istanbul ignore if */
|
||||||
.call({ from: selectedAddress }, (error: Error, result: BN) => {
|
if (error) {
|
||||||
/* istanbul ignore if */
|
reject(error);
|
||||||
if (error) {
|
return;
|
||||||
reject(error);
|
}
|
||||||
return;
|
console.log('getBalanceOf success ' + result);
|
||||||
}
|
resolve(result);
|
||||||
console.log("getBalanceOf success " + result);
|
});
|
||||||
resolve(result);
|
|
||||||
});
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -126,10 +124,7 @@ export class ERC20Standard {
|
|||||||
decimals: string | undefined;
|
decimals: string | undefined;
|
||||||
balance: BN | undefined;
|
balance: BN | undefined;
|
||||||
}> {
|
}> {
|
||||||
const [decimals, symbol] = await Promise.all([
|
const [decimals, symbol] = await Promise.all([this.getTokenDecimals(address), this.getTokenSymbol(address)]);
|
||||||
this.getTokenDecimals(address),
|
|
||||||
this.getTokenSymbol(address),
|
|
||||||
]);
|
|
||||||
let balance;
|
let balance;
|
||||||
if (userAddress) {
|
if (userAddress) {
|
||||||
balance = await this.getBalanceOf(address, userAddress);
|
balance = await this.getBalanceOf(address, userAddress);
|
||||||
@ -138,7 +133,7 @@ export class ERC20Standard {
|
|||||||
decimals,
|
decimals,
|
||||||
symbol,
|
symbol,
|
||||||
balance,
|
balance,
|
||||||
standard: "ERC20",
|
standard: 'ERC20',
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -162,7 +157,7 @@ export class ERC20Standard {
|
|||||||
const contract = new this.web3.eth.Contract(abiERC20, address);
|
const contract = new this.web3.eth.Contract(abiERC20, address);
|
||||||
let amountBN = toWeiBn(amount, decimal);
|
let amountBN = toWeiBn(amount, decimal);
|
||||||
if (!gas) {
|
if (!gas) {
|
||||||
gas = await contract.methods.transfer(to, "0").estimateGas();
|
gas = await contract.methods.transfer(to, '0').estimateGas();
|
||||||
}
|
}
|
||||||
gas = (gas * 1.1) | 1;
|
gas = (gas * 1.1) | 1;
|
||||||
if (estimate) {
|
if (estimate) {
|
||||||
@ -170,22 +165,24 @@ export class ERC20Standard {
|
|||||||
}
|
}
|
||||||
const logData = {
|
const logData = {
|
||||||
gas,
|
gas,
|
||||||
title: "transfer",
|
title: 'transfer',
|
||||||
details: [
|
details: [
|
||||||
{
|
{
|
||||||
address,
|
address,
|
||||||
from,
|
from,
|
||||||
to,
|
to,
|
||||||
value: amountBN,
|
value: amountBN,
|
||||||
id: "0",
|
id: '0',
|
||||||
},
|
},
|
||||||
],
|
],
|
||||||
};
|
};
|
||||||
|
let gasPrice = await this.web3.eth.getGasPrice();
|
||||||
return universalChainCb(
|
return universalChainCb(
|
||||||
logData,
|
logData,
|
||||||
contract.methods.transfer(to, amountBN).send({
|
contract.methods.transfer(to, amountBN).send({
|
||||||
from,
|
from,
|
||||||
gas,
|
gas,
|
||||||
|
gasPrice,
|
||||||
})
|
})
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
@ -1,13 +1,13 @@
|
|||||||
import Web3 from "web3";
|
import Web3 from 'web3';
|
||||||
import { abiERC721 } from "../abis/abiERC721";
|
import { abiERC721 } from '../abis/abiERC721';
|
||||||
import { universalChainCb } from "../util/chain.util";
|
import { universalChainCb } from '../util/chain.util';
|
||||||
import { timeoutFetch } from "../util/net.util";
|
import { timeoutFetch } from '../util/net.util';
|
||||||
import { getFormattedIpfsUrl } from "../util/wallet.util";
|
import { getFormattedIpfsUrl } from '../util/wallet.util';
|
||||||
|
|
||||||
export const ERC721 = "ERC721";
|
export const ERC721 = 'ERC721';
|
||||||
export const ERC721_INTERFACE_ID = "0x80ac58cd";
|
export const ERC721_INTERFACE_ID = '0x80ac58cd';
|
||||||
export const ERC721_METADATA_INTERFACE_ID = "0x5b5e139f";
|
export const ERC721_METADATA_INTERFACE_ID = '0x5b5e139f';
|
||||||
export const ERC721_ENUMERABLE_INTERFACE_ID = "0x780e9d63";
|
export const ERC721_ENUMERABLE_INTERFACE_ID = '0x780e9d63';
|
||||||
|
|
||||||
export class ERC721Standard {
|
export class ERC721Standard {
|
||||||
private web3: Web3;
|
private web3: Web3;
|
||||||
@ -22,13 +22,8 @@ export class ERC721Standard {
|
|||||||
* @param address - ERC721 asset contract address.
|
* @param address - ERC721 asset contract address.
|
||||||
* @returns Promise resolving to whether the contract implements ERC721Metadata interface.
|
* @returns Promise resolving to whether the contract implements ERC721Metadata interface.
|
||||||
*/
|
*/
|
||||||
contractSupportsMetadataInterface = async (
|
contractSupportsMetadataInterface = async (address: string): Promise<boolean> => {
|
||||||
address: string
|
return this.contractSupportsInterface(address, ERC721_METADATA_INTERFACE_ID);
|
||||||
): Promise<boolean> => {
|
|
||||||
return this.contractSupportsInterface(
|
|
||||||
address,
|
|
||||||
ERC721_METADATA_INTERFACE_ID
|
|
||||||
);
|
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -37,13 +32,8 @@ export class ERC721Standard {
|
|||||||
* @param address - ERC721 asset contract address.
|
* @param address - ERC721 asset contract address.
|
||||||
* @returns Promise resolving to whether the contract implements ERC721Enumerable interface.
|
* @returns Promise resolving to whether the contract implements ERC721Enumerable interface.
|
||||||
*/
|
*/
|
||||||
contractSupportsEnumerableInterface = async (
|
contractSupportsEnumerableInterface = async (address: string): Promise<boolean> => {
|
||||||
address: string
|
return this.contractSupportsInterface(address, ERC721_ENUMERABLE_INTERFACE_ID);
|
||||||
): Promise<boolean> => {
|
|
||||||
return this.contractSupportsInterface(
|
|
||||||
address,
|
|
||||||
ERC721_ENUMERABLE_INTERFACE_ID
|
|
||||||
);
|
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -52,9 +42,7 @@ export class ERC721Standard {
|
|||||||
* @param address - ERC721 asset contract address.
|
* @param address - ERC721 asset contract address.
|
||||||
* @returns Promise resolving to whether the contract implements ERC721 interface.
|
* @returns Promise resolving to whether the contract implements ERC721 interface.
|
||||||
*/
|
*/
|
||||||
contractSupportsBase721Interface = async (
|
contractSupportsBase721Interface = async (address: string): Promise<boolean> => {
|
||||||
address: string
|
|
||||||
): Promise<boolean> => {
|
|
||||||
return this.contractSupportsInterface(address, ERC721_INTERFACE_ID);
|
return this.contractSupportsInterface(address, ERC721_INTERFACE_ID);
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -66,42 +54,31 @@ export class ERC721Standard {
|
|||||||
* @param index - A collectible counter less than `balanceOf(selectedAddress)`.
|
* @param index - A collectible counter less than `balanceOf(selectedAddress)`.
|
||||||
* @returns Promise resolving to token identifier for the 'index'th asset assigned to 'selectedAddress'.
|
* @returns Promise resolving to token identifier for the 'index'th asset assigned to 'selectedAddress'.
|
||||||
*/
|
*/
|
||||||
getCollectibleTokenId = async (
|
getCollectibleTokenId = async (address: string, selectedAddress: string, index: number): Promise<string> => {
|
||||||
address: string,
|
|
||||||
selectedAddress: string,
|
|
||||||
index: number
|
|
||||||
): Promise<string> => {
|
|
||||||
const contract = new this.web3.eth.Contract(abiERC721, address);
|
const contract = new this.web3.eth.Contract(abiERC721, address);
|
||||||
return new Promise<string>((resolve, reject) => {
|
return new Promise<string>((resolve, reject) => {
|
||||||
contract.methods
|
contract.methods.tokenOfOwnerByIndex(selectedAddress, index).call((error: Error, result: string) => {
|
||||||
.tokenOfOwnerByIndex(selectedAddress, index)
|
/* istanbul ignore if */
|
||||||
.call((error: Error, result: string) => {
|
if (error) {
|
||||||
/* istanbul ignore if */
|
reject(error);
|
||||||
if (error) {
|
return;
|
||||||
reject(error);
|
}
|
||||||
return;
|
resolve(result);
|
||||||
}
|
});
|
||||||
resolve(result);
|
|
||||||
});
|
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
getBalance = async (
|
getBalance = async (address: string, selectedAddress: string): Promise<number> => {
|
||||||
address: string,
|
|
||||||
selectedAddress: string
|
|
||||||
): Promise<number> => {
|
|
||||||
const contract = new this.web3.eth.Contract(abiERC721, address);
|
const contract = new this.web3.eth.Contract(abiERC721, address);
|
||||||
return new Promise<number>((resolve, reject) => {
|
return new Promise<number>((resolve, reject) => {
|
||||||
contract.methods
|
contract.methods.balanceOf(selectedAddress).call((error: Error, result: number) => {
|
||||||
.balanceOf(selectedAddress)
|
/* istanbul ignore if */
|
||||||
.call((error: Error, result: number) => {
|
if (error) {
|
||||||
/* istanbul ignore if */
|
reject(error);
|
||||||
if (error) {
|
return;
|
||||||
reject(error);
|
}
|
||||||
return;
|
resolve(result);
|
||||||
}
|
});
|
||||||
resolve(result);
|
|
||||||
});
|
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -114,23 +91,19 @@ export class ERC721Standard {
|
|||||||
*/
|
*/
|
||||||
getTokenURI = async (address: string, tokenId: string): Promise<string> => {
|
getTokenURI = async (address: string, tokenId: string): Promise<string> => {
|
||||||
const contract = new this.web3.eth.Contract(abiERC721, address);
|
const contract = new this.web3.eth.Contract(abiERC721, address);
|
||||||
const supportsMetadata = await this.contractSupportsMetadataInterface(
|
const supportsMetadata = await this.contractSupportsMetadataInterface(address);
|
||||||
address
|
|
||||||
);
|
|
||||||
if (!supportsMetadata) {
|
if (!supportsMetadata) {
|
||||||
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.methods
|
contract.methods.tokenURI(tokenId).call((error: Error, result: string) => {
|
||||||
.tokenURI(tokenId)
|
/* istanbul ignore if */
|
||||||
.call((error: Error, result: string) => {
|
if (error) {
|
||||||
/* istanbul ignore if */
|
reject(error);
|
||||||
if (error) {
|
return;
|
||||||
reject(error);
|
}
|
||||||
return;
|
resolve(result);
|
||||||
}
|
});
|
||||||
resolve(result);
|
|
||||||
});
|
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -202,22 +175,17 @@ export class ERC721Standard {
|
|||||||
* @param interfaceId - Interface identifier.
|
* @param interfaceId - Interface identifier.
|
||||||
* @returns Promise resolving to whether the contract implements `interfaceID`.
|
* @returns Promise resolving to whether the contract implements `interfaceID`.
|
||||||
*/
|
*/
|
||||||
private contractSupportsInterface = async (
|
private contractSupportsInterface = async (address: string, interfaceId: string): Promise<boolean> => {
|
||||||
address: string,
|
|
||||||
interfaceId: string
|
|
||||||
): Promise<boolean> => {
|
|
||||||
const contract = new this.web3.eth.Contract(abiERC721, address);
|
const contract = new this.web3.eth.Contract(abiERC721, address);
|
||||||
return new Promise<boolean>((resolve, reject) => {
|
return new Promise<boolean>((resolve, reject) => {
|
||||||
contract.methods
|
contract.methods.supportsInterface(interfaceId).call((error: Error, result: boolean) => {
|
||||||
.supportsInterface(interfaceId)
|
/* istanbul ignore if */
|
||||||
.call((error: Error, result: boolean) => {
|
if (error) {
|
||||||
/* istanbul ignore if */
|
reject(error);
|
||||||
if (error) {
|
return;
|
||||||
reject(error);
|
}
|
||||||
return;
|
resolve(result);
|
||||||
}
|
});
|
||||||
resolve(result);
|
|
||||||
});
|
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -263,14 +231,14 @@ export class ERC721Standard {
|
|||||||
if (tokenId) {
|
if (tokenId) {
|
||||||
try {
|
try {
|
||||||
tokenURI = await this.getTokenURI(address, tokenId);
|
tokenURI = await this.getTokenURI(address, tokenId);
|
||||||
if (tokenURI.startsWith("ipfs://")) {
|
if (tokenURI.startsWith('ipfs://')) {
|
||||||
tokenURI = getFormattedIpfsUrl(ipfsGateway, tokenURI, true);
|
tokenURI = getFormattedIpfsUrl(ipfsGateway, tokenURI, true);
|
||||||
}
|
}
|
||||||
|
|
||||||
const response = await timeoutFetch(tokenURI);
|
const response = await timeoutFetch(tokenURI);
|
||||||
const object = await response.json();
|
const object = await response.json();
|
||||||
image = object ? object.image : "";
|
image = object ? object.image : '';
|
||||||
if (image.startsWith("ipfs://")) {
|
if (image.startsWith('ipfs://')) {
|
||||||
image = getFormattedIpfsUrl(ipfsGateway, image, true);
|
image = getFormattedIpfsUrl(ipfsGateway, image, true);
|
||||||
}
|
}
|
||||||
} catch {
|
} catch {
|
||||||
@ -304,9 +272,7 @@ export class ERC721Standard {
|
|||||||
}) {
|
}) {
|
||||||
const contract = new this.web3.eth.Contract(abiERC721, address);
|
const contract = new this.web3.eth.Contract(abiERC721, address);
|
||||||
if (!gas) {
|
if (!gas) {
|
||||||
gas = await contract.methods
|
gas = await contract.methods.safeTransferFrom(from, to, tokenId).estimateGas();
|
||||||
.safeTransferFrom(from, to, tokenId)
|
|
||||||
.estimateGas();
|
|
||||||
}
|
}
|
||||||
gas = (gas * 1.1) | 1;
|
gas = (gas * 1.1) | 1;
|
||||||
if (estimate) {
|
if (estimate) {
|
||||||
@ -314,7 +280,7 @@ export class ERC721Standard {
|
|||||||
}
|
}
|
||||||
const logData = {
|
const logData = {
|
||||||
gas,
|
gas,
|
||||||
title: "transfer",
|
title: 'transfer',
|
||||||
details: [
|
details: [
|
||||||
{
|
{
|
||||||
address,
|
address,
|
||||||
@ -324,11 +290,13 @@ export class ERC721Standard {
|
|||||||
},
|
},
|
||||||
],
|
],
|
||||||
};
|
};
|
||||||
|
let gasPrice = await this.web3.eth.getGasPrice();
|
||||||
return universalChainCb(
|
return universalChainCb(
|
||||||
logData,
|
logData,
|
||||||
contract.methods.safeTransferFrom(from, to, tokenId).send({
|
contract.methods.safeTransferFrom(from, to, tokenId).send({
|
||||||
from,
|
from,
|
||||||
gas,
|
gas,
|
||||||
|
gasPrice,
|
||||||
})
|
})
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
@ -1,9 +1,7 @@
|
|||||||
import assert from 'assert';
|
|
||||||
import Web3 from 'web3';
|
import Web3 from 'web3';
|
||||||
import { abiMinterFactory } from '../abis/abiUserMinterFactory';
|
import { abiMinterFactory } from '../abis/abiUserMinterFactory';
|
||||||
import { GAS_BOOST, ZERO_ADDRESS } from '../config/constants';
|
import { GAS_BOOST, ZERO_ADDRESS } from '../config/constants';
|
||||||
import { getAddressByName, universalChainCb } from '../util/chain.util';
|
import { universalChainCb } from '../util/chain.util';
|
||||||
import { SAMPLE_GAS } from './ChainCommon';
|
|
||||||
import { abiMarket } from '../abis/abiMarket';
|
import { abiMarket } from '../abis/abiMarket';
|
||||||
import { abiMall } from '../abis/abiMall';
|
import { abiMall } from '../abis/abiMall';
|
||||||
import { abiGameMarket } from '../abis/abiGameMarket';
|
import { abiGameMarket } from '../abis/abiGameMarket';
|
||||||
@ -71,9 +69,10 @@ export class JCStandard {
|
|||||||
title: 'mint_nft',
|
title: 'mint_nft',
|
||||||
details: details,
|
details: details,
|
||||||
};
|
};
|
||||||
|
let gasPrice = await this.web3.eth.getGasPrice();
|
||||||
return universalChainCb(
|
return universalChainCb(
|
||||||
logData,
|
logData,
|
||||||
contract.methods.mintNft(address, tokenIds, startTime, saltNonce, signature).send({ gas })
|
contract.methods.mintNft(address, tokenIds, startTime, saltNonce, signature).send({ gas, gasPrice })
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
// begin of NFT Market
|
// begin of NFT Market
|
||||||
@ -108,16 +107,15 @@ export class JCStandard {
|
|||||||
});
|
});
|
||||||
let approved = await tokenInstance.methods.getApproved(tokenId);
|
let approved = await tokenInstance.methods.getApproved(tokenId);
|
||||||
let gasApprove = 0;
|
let gasApprove = 0;
|
||||||
|
let gasPrice = await this.web3.eth.getGasPrice();
|
||||||
if (approved != addressMarket) {
|
if (approved != addressMarket) {
|
||||||
gasApprove = cfg.gasInfo.nftApprove;
|
gasApprove = await tokenInstance.methods.approve(addressMarket, tokenId).estimateGas();
|
||||||
gasApprove = (gasApprove * GAS_BOOST) | 1;
|
gasApprove = (gasApprove * GAS_BOOST) | 1;
|
||||||
|
await tokenInstance.methods.approve(addressMarket, tokenId).send({ gas: gasApprove, gasPrice });
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!gas) {
|
if (!gas) {
|
||||||
gas = cfg.gasInfo.marketSellNFT;
|
gas = await contract.methods.sell(nftToken, currency, tokenId, price, amount).estimateGas();
|
||||||
// gas = await contract.methods
|
|
||||||
// .sell(nftToken, currency, tokenId, price, amount)
|
|
||||||
// .estimateGas();
|
|
||||||
}
|
}
|
||||||
gas = (gas * GAS_BOOST) | 1;
|
gas = (gas * GAS_BOOST) | 1;
|
||||||
|
|
||||||
@ -143,11 +141,11 @@ export class JCStandard {
|
|||||||
title: 'market_sell',
|
title: 'market_sell',
|
||||||
details: details,
|
details: details,
|
||||||
};
|
};
|
||||||
if (approved != addressMarket) {
|
|
||||||
await tokenInstance.methods.approve(addressMarket, tokenId).send({ gas: gasApprove });
|
return universalChainCb(
|
||||||
console.log('approve done');
|
logData,
|
||||||
}
|
contract.methods.sell(nftToken, currency, tokenId, price, amount).send({ gas, gasPrice })
|
||||||
return universalChainCb(logData, contract.methods.sell(nftToken, currency, tokenId, price, amount).send({ gas }));
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
async marketUpdatePrice({
|
async marketUpdatePrice({
|
||||||
@ -170,8 +168,7 @@ export class JCStandard {
|
|||||||
from: jc.wallet.currentAccAddr,
|
from: jc.wallet.currentAccAddr,
|
||||||
});
|
});
|
||||||
if (!gas) {
|
if (!gas) {
|
||||||
// gas = await contract.methods.updatePrice(orderId, price).estimateGas();
|
gas = await contract.methods.updatePrice(orderId, price).estimateGas();
|
||||||
gas = cfg.gasInfo.marketUpdatePrice;
|
|
||||||
}
|
}
|
||||||
gas = (gas * GAS_BOOST) | 1;
|
gas = (gas * GAS_BOOST) | 1;
|
||||||
if (estimate) {
|
if (estimate) {
|
||||||
@ -190,7 +187,8 @@ export class JCStandard {
|
|||||||
title: 'market_update_price',
|
title: 'market_update_price',
|
||||||
details: details,
|
details: details,
|
||||||
};
|
};
|
||||||
return universalChainCb(logData, contract.methods.updatePrice(orderId, price).send({ gas }));
|
let gasPrice = await this.web3.eth.getGasPrice();
|
||||||
|
return universalChainCb(logData, contract.methods.updatePrice(orderId, price).send({ gas, gasPrice }));
|
||||||
}
|
}
|
||||||
|
|
||||||
async marketCancelOrder({ orderId, gas, estimate }: { orderId: string; gas?: number; estimate: number }) {
|
async marketCancelOrder({ orderId, gas, estimate }: { orderId: string; gas?: number; estimate: number }) {
|
||||||
@ -227,12 +225,13 @@ export class JCStandard {
|
|||||||
id: orderInfo.tokenId,
|
id: orderInfo.tokenId,
|
||||||
},
|
},
|
||||||
];
|
];
|
||||||
|
let gasPrice = await this.web3.eth.getGasPrice();
|
||||||
const logData = {
|
const logData = {
|
||||||
gas,
|
gas,
|
||||||
title: 'market_cancel_order',
|
title: 'market_cancel_order',
|
||||||
details: details,
|
details: details,
|
||||||
};
|
};
|
||||||
return universalChainCb(logData, contract.methods.cancelOrder(orderId).send({ gas }));
|
return universalChainCb(logData, contract.methods.cancelOrder(orderId).send({ gas, gasPrice }));
|
||||||
}
|
}
|
||||||
|
|
||||||
async marketBuy({ orderId, gas, estimate }: { orderId: string; gas?: number; estimate: number }) {
|
async marketBuy({ orderId, gas, estimate }: { orderId: string; gas?: number; estimate: number }) {
|
||||||
@ -255,13 +254,14 @@ export class JCStandard {
|
|||||||
let approved = await tokenInstance.methods.allowance(jc.wallet.currentAccAddr, addressMarket).call();
|
let approved = await tokenInstance.methods.allowance(jc.wallet.currentAccAddr, addressMarket).call();
|
||||||
console.log('approved:: ', approved);
|
console.log('approved:: ', approved);
|
||||||
let gasApprove = 0;
|
let gasApprove = 0;
|
||||||
|
let gasPrice = await this.web3.eth.getGasPrice();
|
||||||
if (approved < orderInfo.price) {
|
if (approved < orderInfo.price) {
|
||||||
gasApprove = cfg.gasInfo.cecApprove;
|
gasApprove = await tokenInstance.methods.approve(addressMarket, orderInfo.price).estimateGas();
|
||||||
|
gasApprove = (gasApprove * GAS_BOOST) | 1;
|
||||||
|
await tokenInstance.methods.approve(addressMarket, orderInfo.price).send({ gas: gasApprove, gasPrice });
|
||||||
}
|
}
|
||||||
gasApprove = (gasApprove * GAS_BOOST) | 1;
|
|
||||||
if (!gas) {
|
if (!gas) {
|
||||||
// gas = await contract.methods.buy(orderId).estimateGas();
|
gas = await contract.methods.buy(orderId).estimateGas();
|
||||||
gas = cfg.gasInfo.marketBuy;
|
|
||||||
}
|
}
|
||||||
gas = (gas * GAS_BOOST) | 1;
|
gas = (gas * GAS_BOOST) | 1;
|
||||||
// begin of increase allowance
|
// begin of increase allowance
|
||||||
@ -295,10 +295,7 @@ export class JCStandard {
|
|||||||
title: 'market_buy',
|
title: 'market_buy',
|
||||||
details: details,
|
details: details,
|
||||||
};
|
};
|
||||||
if (approved < orderInfo.price) {
|
return universalChainCb(logData, contract.methods.buy(orderId).send({ gas, gasPrice }));
|
||||||
await tokenInstance.methods.approve(addressMarket, orderInfo.price).send({ gas: gasApprove });
|
|
||||||
}
|
|
||||||
return universalChainCb(logData, contract.methods.buy(orderId).send({ gas }));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async marketOrderInfo(orderId: string) {
|
async marketOrderInfo(orderId: string) {
|
||||||
@ -349,15 +346,15 @@ export class JCStandard {
|
|||||||
let approved = await tokenInstance.methods.allowance(jc.wallet.currentAccAddr, addressGameMall).call();
|
let approved = await tokenInstance.methods.allowance(jc.wallet.currentAccAddr, addressGameMall).call();
|
||||||
console.log('approved:: ', approved);
|
console.log('approved:: ', approved);
|
||||||
let gasApprove = 0;
|
let gasApprove = 0;
|
||||||
|
let gasPrice = await this.web3.eth.getGasPrice();
|
||||||
if (approved < price) {
|
if (approved < price) {
|
||||||
gasApprove = cfg.gasInfo.cecApprove;
|
gasApprove = await tokenInstance.methods.approve(addressGameMall, price).estimateGas();
|
||||||
|
gasApprove = (gasApprove * GAS_BOOST) | 1;
|
||||||
|
await tokenInstance.methods.approve(addressGameMall, price).send({ gas: gasApprove, gasPrice });
|
||||||
}
|
}
|
||||||
gasApprove = (gasApprove * GAS_BOOST) | 1;
|
|
||||||
if (!gas) {
|
if (!gas) {
|
||||||
// gas = await contract.methods
|
gas = await contract.methods.buy(orderId, currency, price, startTime, saltNonce, signature).estimateGas();
|
||||||
// .buy(orderId, currency, price, startTime, saltNonce, signature)
|
|
||||||
// .estimateGas();
|
|
||||||
gas = cfg.gasInfo.gameMallBuy;
|
|
||||||
}
|
}
|
||||||
gas = (gas * GAS_BOOST) | 1;
|
gas = (gas * GAS_BOOST) | 1;
|
||||||
if (estimate) {
|
if (estimate) {
|
||||||
@ -382,12 +379,10 @@ export class JCStandard {
|
|||||||
title: 'game_mall_buy',
|
title: 'game_mall_buy',
|
||||||
details: details,
|
details: details,
|
||||||
};
|
};
|
||||||
if (approved < price) {
|
|
||||||
await tokenInstance.methods.approve(addressGameMall, price).send({ gas: gasApprove });
|
|
||||||
}
|
|
||||||
return universalChainCb(
|
return universalChainCb(
|
||||||
logData,
|
logData,
|
||||||
contract.methods.buy(orderId, currency, price, startTime, saltNonce, signature).send({ gas })
|
contract.methods.buy(orderId, currency, price, startTime, saltNonce, signature).send({ gas, gasPrice })
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
// end of game item mall
|
// end of game item mall
|
||||||
@ -429,16 +424,15 @@ export class JCStandard {
|
|||||||
let approved = await tokenInstance.methods.allowance(jc.wallet.currentAccAddr, addressGameMarket).call();
|
let approved = await tokenInstance.methods.allowance(jc.wallet.currentAccAddr, addressGameMarket).call();
|
||||||
console.log('approved:: ', approved);
|
console.log('approved:: ', approved);
|
||||||
let gasApprove = 0;
|
let gasApprove = 0;
|
||||||
|
let gasPrice = await this.web3.eth.getGasPrice();
|
||||||
if (approved < price) {
|
if (approved < price) {
|
||||||
gasApprove = cfg.gasInfo.cecApprove;
|
gasApprove = await tokenInstance.methods.approve(addressGameMarket, price).estimateGas();
|
||||||
|
gasApprove = (gasApprove * GAS_BOOST) | 1;
|
||||||
|
await tokenInstance.methods.approve(addressGameMarket, price).send({ gas: gasApprove, gasPrice });
|
||||||
}
|
}
|
||||||
gasApprove = (gasApprove * GAS_BOOST) | 1;
|
|
||||||
console.log('gasApprove: ' + gasApprove);
|
console.log('gasApprove: ' + gasApprove);
|
||||||
if (!gas) {
|
if (!gas) {
|
||||||
// gas = await contract.methods
|
gas = await contract.methods.buy(orderId, seller, currency, price, startTime, saltNonce, signature).estimateGas();
|
||||||
// .buy(orderId, seller, currency, price, startTime, saltNonce, signature)
|
|
||||||
// .estimateGas();
|
|
||||||
gas = cfg.gasInfo.gameMarketBuy;
|
|
||||||
}
|
}
|
||||||
gas = (gas * GAS_BOOST) | 1;
|
gas = (gas * GAS_BOOST) | 1;
|
||||||
console.log('gas: ' + gas);
|
console.log('gas: ' + gas);
|
||||||
@ -465,12 +459,9 @@ export class JCStandard {
|
|||||||
title: 'game_market_buy',
|
title: 'game_market_buy',
|
||||||
details: details,
|
details: details,
|
||||||
};
|
};
|
||||||
if (approved < price) {
|
|
||||||
await tokenInstance.methods.approve(addressGameMarket, price).send({ gas: gasApprove });
|
|
||||||
}
|
|
||||||
return universalChainCb(
|
return universalChainCb(
|
||||||
logData,
|
logData,
|
||||||
contract.methods.buy(orderId, seller, currency, price, startTime, saltNonce, signature).send({ gas })
|
contract.methods.buy(orderId, seller, currency, price, startTime, saltNonce, signature).send({ gas, gasPrice })
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
// end of game item market
|
// end of game item market
|
||||||
@ -513,11 +504,12 @@ export class JCStandard {
|
|||||||
});
|
});
|
||||||
let approved = await tokenInstance.methods.allowance(jc.wallet.currentAccAddr, addressMall).call();
|
let approved = await tokenInstance.methods.allowance(jc.wallet.currentAccAddr, addressMall).call();
|
||||||
console.log('approved:: ', approved);
|
console.log('approved:: ', approved);
|
||||||
|
let gasPrice = await this.web3.eth.getGasPrice();
|
||||||
let gasApprove = 0;
|
let gasApprove = 0;
|
||||||
if (approved < values[1]) {
|
if (approved < values[1]) {
|
||||||
gasApprove = cfg.gasInfo.cecApprove;
|
gasApprove = await tokenInstance.methods.approve(addressMall, values[1]).estimateGas();
|
||||||
gasApprove = (gasApprove * GAS_BOOST) | 1;
|
gasApprove = (gasApprove * GAS_BOOST) | 1;
|
||||||
await tokenInstance.methods.approve(addressMall, values[1]).send({ gas: gasApprove });
|
await tokenInstance.methods.approve(addressMall, values[1]).send({ gas: gasApprove, gasPrice });
|
||||||
}
|
}
|
||||||
if (!gas) {
|
if (!gas) {
|
||||||
gas = await contract.methods.buyNFT(currency, addresses, ids, amounts, values, signature).estimateGas();
|
gas = await contract.methods.buyNFT(currency, addresses, ids, amounts, values, signature).estimateGas();
|
||||||
@ -557,7 +549,7 @@ export class JCStandard {
|
|||||||
};
|
};
|
||||||
return universalChainCb(
|
return universalChainCb(
|
||||||
logData,
|
logData,
|
||||||
contract.methods.buyNFT(currency, addresses, ids, amounts, values, signature).send({ gas })
|
contract.methods.buyNFT(currency, addresses, ids, amounts, values, signature).send({ gas, gasPrice })
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
// end of NFT Mall
|
// end of NFT Mall
|
||||||
|
Loading…
x
Reference in New Issue
Block a user