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