mirror of
https://github.com/oceanprotocol/ocean.js.git
synced 2024-11-26 20:39:05 +01:00
global updates
This commit is contained in:
parent
f431f1d2c6
commit
8d6288f63b
@ -1,5 +1,5 @@
|
||||
import { TransactionReceipt } from 'web3-core'
|
||||
|
||||
// import { TransactionReceipt } from 'web3-core'
|
||||
import { TransactionResponse } from 'ethers'
|
||||
export type ReceiptOrEstimate<G extends boolean = false> = G extends false
|
||||
? TransactionReceipt
|
||||
: number
|
||||
? TransactionResponse
|
||||
: bigint
|
||||
|
@ -1,6 +1,7 @@
|
||||
import Web3 from 'web3'
|
||||
import { Contract } from 'web3-eth-contract'
|
||||
import { AbiItem } from 'web3-utils'
|
||||
// import Web3 from 'web3'
|
||||
// import { Contract } from 'web3-eth-contract'
|
||||
// import { AbiItem } from 'web3-utils'
|
||||
import { ethers, Signer, Interface, Contract, InterfaceAbi } from 'ethers'
|
||||
import { Config, ConfigHelper } from '../config'
|
||||
import {
|
||||
amountToUnits,
|
||||
@ -10,28 +11,28 @@ import {
|
||||
} from '../utils'
|
||||
|
||||
export abstract class SmartContract {
|
||||
public web3: Web3
|
||||
public signer: Signer
|
||||
public config: Config
|
||||
public abi: AbiItem | AbiItem[]
|
||||
public abi: InterfaceAbi
|
||||
|
||||
abstract getDefaultAbi(): AbiItem | AbiItem[]
|
||||
abstract getDefaultAbi()
|
||||
|
||||
/**
|
||||
* Instantiate the smart contract.
|
||||
* @param {Web3} web3
|
||||
* @param {Signer} signer
|
||||
* @param {string | number} network Network id or name
|
||||
* @param {Config} config Configutation of the smart contract
|
||||
* @param {AbiItem | AbiItem[]} abi ABI of the smart contract
|
||||
*/
|
||||
constructor(
|
||||
web3: Web3,
|
||||
signer: Signer,
|
||||
network?: string | number,
|
||||
config?: Config,
|
||||
abi?: AbiItem | AbiItem[]
|
||||
abi?: InterfaceAbi
|
||||
) {
|
||||
this.web3 = web3
|
||||
this.signer = signer
|
||||
this.config = config || new ConfigHelper().getConfig(network || 'unknown')
|
||||
this.abi = abi || (this.getDefaultAbi() as AbiItem[])
|
||||
this.abi = abi || this.getDefaultAbi()
|
||||
}
|
||||
|
||||
protected async amountToUnits(
|
||||
@ -39,7 +40,7 @@ export abstract class SmartContract {
|
||||
amount: string,
|
||||
tokenDecimals?: number
|
||||
): Promise<string> {
|
||||
return amountToUnits(this.web3, token, amount, tokenDecimals)
|
||||
return amountToUnits(this.signer, token, amount, tokenDecimals)
|
||||
}
|
||||
|
||||
protected async unitsToAmount(
|
||||
@ -47,21 +48,15 @@ export abstract class SmartContract {
|
||||
amount: string,
|
||||
tokenDecimals?: number
|
||||
): Promise<string> {
|
||||
return unitsToAmount(this.web3, token, amount, tokenDecimals)
|
||||
return unitsToAmount(this.signer, token, amount, tokenDecimals)
|
||||
}
|
||||
|
||||
protected async getFairGasPrice(): Promise<string> {
|
||||
return getFairGasPrice(this.web3, this.config?.gasFeeMultiplier)
|
||||
return getFairGasPrice(this.signer, this.config?.gasFeeMultiplier)
|
||||
}
|
||||
|
||||
protected getContract(
|
||||
address: string,
|
||||
account?: string,
|
||||
abi?: AbiItem | AbiItem[]
|
||||
): Contract {
|
||||
const contract = new this.web3.eth.Contract(abi || this.abi, address, {
|
||||
from: account
|
||||
})
|
||||
protected getContract(address: string, account?: string, abi?: InterfaceAbi): Contract {
|
||||
const contract = new ethers.Contract(address, abi || this.abi, this.signer)
|
||||
return setContractDefaults(contract, this.config)
|
||||
}
|
||||
}
|
||||
|
@ -1,6 +1,8 @@
|
||||
import Web3 from 'web3'
|
||||
import { Contract } from 'web3-eth-contract'
|
||||
import { AbiItem } from 'web3-utils'
|
||||
// import Web3 from 'web3'
|
||||
// import { Contract } from 'web3-eth-contract'
|
||||
// import { AbiItem } from 'web3-utils'
|
||||
|
||||
import { Signer, InterfaceAbi, Contract } from 'ethers'
|
||||
import { Config } from '../config'
|
||||
import { SmartContract } from './SmartContract'
|
||||
|
||||
@ -11,19 +13,19 @@ export abstract class SmartContractWithAddress extends SmartContract {
|
||||
/**
|
||||
* Instantiate the smart contract.
|
||||
* @param {string} address Address of the smart contract
|
||||
* @param {Web3} web3
|
||||
* @param {Signer} signer
|
||||
* @param {string | number} network Network id or name
|
||||
* @param {Config} config Configutation of the smart contract
|
||||
* @param {AbiItem | AbiItem[]} abi ABI of the smart contract
|
||||
* @param {InterfaceAbi} abi ABI of the smart contract
|
||||
*/
|
||||
constructor(
|
||||
address: string,
|
||||
web3: Web3,
|
||||
signer: Signer,
|
||||
network?: string | number,
|
||||
config?: Config,
|
||||
abi?: AbiItem | AbiItem[]
|
||||
abi?: InterfaceAbi
|
||||
) {
|
||||
super(web3, network, config, abi)
|
||||
super(signer, network, config, abi)
|
||||
this.address = address
|
||||
this.contract = this.getContract(this.address)
|
||||
}
|
||||
|
@ -1,5 +1,5 @@
|
||||
export const ZERO_ADDRESS = '0x0000000000000000000000000000000000000000'
|
||||
export const GASLIMIT_DEFAULT = 1000000
|
||||
export const GASLIMIT_DEFAULT = BigInt(1000000)
|
||||
export const MAX_UINT_256 =
|
||||
'115792089237316195423570985008687907853269984665640564039457584007913129639934'
|
||||
export const FEE_HISTORY_NOT_SUPPORTED =
|
||||
|
@ -1,16 +1,25 @@
|
||||
import Web3 from 'web3'
|
||||
// import Web3 from 'web3'
|
||||
// import { Contract } from 'web3-eth-contract'
|
||||
// import { TransactionReceipt } from 'web3-core'
|
||||
import {
|
||||
ethers,
|
||||
Signer,
|
||||
InterfaceAbi,
|
||||
Contract,
|
||||
TransactionResponse,
|
||||
ContractMethod
|
||||
} from 'ethers'
|
||||
import BigNumber from 'bignumber.js'
|
||||
import { Contract } from 'web3-eth-contract'
|
||||
import { Config } from '../config'
|
||||
import { minAbi, GASLIMIT_DEFAULT, LoggerInstance, FEE_HISTORY_NOT_SUPPORTED } from '.'
|
||||
import { TransactionReceipt } from 'web3-core'
|
||||
|
||||
const MIN_GAS_FEE_POLYGON = 30000000000 // minimum recommended 30 gwei polygon main and mumbai fees
|
||||
const POLYGON_NETWORK_ID = 137
|
||||
const MUMBAI_NETWORK_ID = 80001
|
||||
|
||||
export function setContractDefaults(contract: Contract, config: Config): Contract {
|
||||
if (config) {
|
||||
// TO DO - since ethers does not provide this
|
||||
/* if (config) {
|
||||
if (config.transactionBlockTimeout)
|
||||
contract.transactionBlockTimeout = config.transactionBlockTimeout
|
||||
if (config.transactionConfirmationBlocks)
|
||||
@ -18,14 +27,16 @@ export function setContractDefaults(contract: Contract, config: Config): Contrac
|
||||
if (config.transactionPollingTimeout)
|
||||
contract.transactionPollingTimeout = config.transactionPollingTimeout
|
||||
}
|
||||
*/
|
||||
return contract
|
||||
}
|
||||
|
||||
export async function getFairGasPrice(
|
||||
web3: Web3,
|
||||
signer: Signer,
|
||||
gasFeeMultiplier: number
|
||||
): Promise<string> {
|
||||
const x = new BigNumber(await web3.eth.getGasPrice())
|
||||
const price = await (await signer.provider.getFeeData()).gasPrice
|
||||
const x = BigNumber(price.toString())
|
||||
if (gasFeeMultiplier)
|
||||
return x
|
||||
.multipliedBy(gasFeeMultiplier)
|
||||
@ -34,14 +45,17 @@ export async function getFairGasPrice(
|
||||
else return x.toString(10)
|
||||
}
|
||||
|
||||
export async function getTokenDecimals(signer: Signer, token: string) {
|
||||
const tokenContract = new ethers.Contract(token, minAbi, signer)
|
||||
return tokenContract.decimals()
|
||||
}
|
||||
export async function unitsToAmount(
|
||||
web3: Web3,
|
||||
signer: Signer,
|
||||
token: string,
|
||||
amount: string,
|
||||
tokenDecimals?: number
|
||||
): Promise<string> {
|
||||
const tokenContract = new web3.eth.Contract(minAbi, token)
|
||||
let decimals = tokenDecimals || (await tokenContract.methods.decimals().call())
|
||||
let decimals = tokenDecimals || (await getTokenDecimals(signer, token))
|
||||
if (decimals === '0') {
|
||||
decimals = 18
|
||||
}
|
||||
@ -55,13 +69,12 @@ export async function unitsToAmount(
|
||||
}
|
||||
|
||||
export async function amountToUnits(
|
||||
web3: Web3,
|
||||
signer: Signer,
|
||||
token: string,
|
||||
amount: string,
|
||||
tokenDecimals?: number
|
||||
): Promise<string> {
|
||||
const tokenContract = new web3.eth.Contract(minAbi, token)
|
||||
let decimals = tokenDecimals || (await tokenContract.methods.decimals().call())
|
||||
let decimals = tokenDecimals || (await getTokenDecimals(signer, token))
|
||||
if (decimals === '0') {
|
||||
decimals = 18
|
||||
}
|
||||
@ -75,20 +88,27 @@ export async function amountToUnits(
|
||||
|
||||
/**
|
||||
* Estimates the gas used when a function would be executed on chain
|
||||
* @param {string} from account that calls the function
|
||||
* @param {Function} functionToEstimateGas function that we need to estimate the gas
|
||||
* @param {...any[]} args arguments of the function
|
||||
* @return {Promise<number>} gas cost of the function
|
||||
*/
|
||||
export async function calculateEstimatedGas(
|
||||
from: string,
|
||||
functionToEstimateGas: Function,
|
||||
functionToEstimateGas: ContractMethod,
|
||||
...args: any[]
|
||||
): Promise<number> {
|
||||
const estimatedGas = await functionToEstimateGas
|
||||
): Promise<bigint> {
|
||||
const estimate = await functionToEstimateGas.estimateGas(args)
|
||||
return estimate || GASLIMIT_DEFAULT
|
||||
/* const estimatedGas = await functionToEstimateGas
|
||||
.apply(null, args)
|
||||
.estimateGas({ from }, (err, estGas) => (err ? GASLIMIT_DEFAULT : estGas))
|
||||
return estimatedGas
|
||||
*/
|
||||
}
|
||||
|
||||
export function getEventFromTx(txReceipt, eventName) {
|
||||
return txReceipt.events.filter((log) => {
|
||||
return log.event === eventName
|
||||
})[0]
|
||||
}
|
||||
|
||||
/**
|
||||
@ -101,20 +121,30 @@ export async function calculateEstimatedGas(
|
||||
* @return {Promise<any>} transaction receipt
|
||||
*/
|
||||
export async function sendTx(
|
||||
from: string,
|
||||
estGas: number,
|
||||
web3: Web3,
|
||||
estGas: bigint,
|
||||
signer: Signer,
|
||||
gasFeeMultiplier: number,
|
||||
functionToSend: Function,
|
||||
...args: any[]
|
||||
): Promise<TransactionReceipt> {
|
||||
const sendTxValue: Record<string, any> = {
|
||||
from,
|
||||
gas: estGas + 1
|
||||
): Promise<TransactionResponse> {
|
||||
const { chainId } = await signer.provider.getNetwork()
|
||||
const feeHistory = await signer.provider.getFeeData()
|
||||
let overrides
|
||||
if (feeHistory.maxPriorityFeePerGas) {
|
||||
overrides = {
|
||||
maxPriorityFeePerGas: feeHistory.maxPriorityFeePerGas,
|
||||
maxFeePerGas: feeHistory.maxFeePerGas
|
||||
}
|
||||
} else {
|
||||
overrides = {
|
||||
gasPrice: feeHistory.gasPrice
|
||||
}
|
||||
}
|
||||
const networkId = await web3.eth.getChainId()
|
||||
try {
|
||||
const feeHistory = await web3.eth.getFeeHistory(1, 'latest', [75])
|
||||
overrides.gasLimit = estGas
|
||||
const trxReceipt = await functionToSend(args, overrides)
|
||||
return trxReceipt
|
||||
/* try {
|
||||
const feeHistory = await signer.provider,web3.eth.getFeeHistory(1, 'latest', [75])
|
||||
if (feeHistory && feeHistory?.baseFeePerGas?.[0] && feeHistory?.reward?.[0]?.[0]) {
|
||||
let aggressiveFee = new BigNumber(feeHistory?.reward?.[0]?.[0])
|
||||
if (gasFeeMultiplier > 1) {
|
||||
@ -161,4 +191,5 @@ export async function sendTx(
|
||||
|
||||
const trxReceipt = await functionToSend.apply(null, args).send(sendTxValue)
|
||||
return trxReceipt
|
||||
*/
|
||||
}
|
||||
|
@ -1,4 +1,5 @@
|
||||
import { AbiItem } from 'web3-utils/types'
|
||||
// import { AbiItem } from 'web3-utils/types'
|
||||
import { ethers } from 'ethers'
|
||||
|
||||
export const minAbi = [
|
||||
{
|
||||
@ -221,4 +222,4 @@ export const minAbi = [
|
||||
name: 'Transfer',
|
||||
type: 'event'
|
||||
}
|
||||
] as AbiItem[]
|
||||
]
|
||||
|
Loading…
x
Reference in New Issue
Block a user