mirror of
https://github.com/oceanprotocol/ocean.js.git
synced 2024-11-26 20:39:05 +01:00
Merge pull request #1504 from oceanprotocol/issue-1474-refactoring-move-functions-from-utils-to-classes
Issue-#1474: Refactoring(5). Move functions from \utils to classes
This commit is contained in:
commit
66527216f0
@ -34,7 +34,7 @@ export abstract class SmartContract {
|
|||||||
this.abi = abi || (this.getDefaultAbi() as AbiItem[])
|
this.abi = abi || (this.getDefaultAbi() as AbiItem[])
|
||||||
}
|
}
|
||||||
|
|
||||||
async amountToUnits(
|
protected async amountToUnits(
|
||||||
token: string,
|
token: string,
|
||||||
amount: string,
|
amount: string,
|
||||||
tokenDecimals?: number
|
tokenDecimals?: number
|
||||||
@ -42,7 +42,7 @@ export abstract class SmartContract {
|
|||||||
return amountToUnits(this.web3, token, amount, tokenDecimals)
|
return amountToUnits(this.web3, token, amount, tokenDecimals)
|
||||||
}
|
}
|
||||||
|
|
||||||
async unitsToAmount(
|
protected async unitsToAmount(
|
||||||
token: string,
|
token: string,
|
||||||
amount: string,
|
amount: string,
|
||||||
tokenDecimals?: number
|
tokenDecimals?: number
|
||||||
@ -50,11 +50,15 @@ export abstract class SmartContract {
|
|||||||
return unitsToAmount(this.web3, token, amount, tokenDecimals)
|
return unitsToAmount(this.web3, token, amount, tokenDecimals)
|
||||||
}
|
}
|
||||||
|
|
||||||
async getFairGasPrice(): Promise<string> {
|
protected async getFairGasPrice(): Promise<string> {
|
||||||
return getFairGasPrice(this.web3, this.config)
|
return getFairGasPrice(this.web3, this.config)
|
||||||
}
|
}
|
||||||
|
|
||||||
getContract(address: string, account?: string, abi?: AbiItem | AbiItem[]): Contract {
|
protected getContract(
|
||||||
|
address: string,
|
||||||
|
account?: string,
|
||||||
|
abi?: AbiItem | AbiItem[]
|
||||||
|
): Contract {
|
||||||
const contract = new this.web3.eth.Contract(abi || this.abi, address, {
|
const contract = new this.web3.eth.Contract(abi || this.abi, address, {
|
||||||
from: account
|
from: account
|
||||||
})
|
})
|
||||||
|
@ -2,15 +2,7 @@ import Web3 from 'web3'
|
|||||||
import { TransactionReceipt } from 'web3-core'
|
import { TransactionReceipt } from 'web3-core'
|
||||||
import { AbiItem } from 'web3-utils'
|
import { AbiItem } from 'web3-utils'
|
||||||
import ERC721Factory from '@oceanprotocol/contracts/artifacts/contracts/ERC721Factory.sol/ERC721Factory.json'
|
import ERC721Factory from '@oceanprotocol/contracts/artifacts/contracts/ERC721Factory.sol/ERC721Factory.json'
|
||||||
import {
|
import { LoggerInstance, generateDtName, estimateGas, ZERO_ADDRESS } from '../../utils'
|
||||||
LoggerInstance,
|
|
||||||
generateDtName,
|
|
||||||
getFreCreationParams,
|
|
||||||
getErcCreationParams,
|
|
||||||
getPoolCreationParams,
|
|
||||||
estimateGas,
|
|
||||||
ZERO_ADDRESS
|
|
||||||
} from '../../utils'
|
|
||||||
import {
|
import {
|
||||||
FreCreationParams,
|
FreCreationParams,
|
||||||
Erc20CreateParams,
|
Erc20CreateParams,
|
||||||
@ -583,7 +575,7 @@ export class NftFactory extends SmartContractWithAddress {
|
|||||||
nftCreateData: NftCreateData,
|
nftCreateData: NftCreateData,
|
||||||
ercParams: Erc20CreateParams
|
ercParams: Erc20CreateParams
|
||||||
): Promise<any> {
|
): Promise<any> {
|
||||||
const ercCreateData = getErcCreationParams(ercParams)
|
const ercCreateData = this.getErcCreationParams(ercParams)
|
||||||
return estimateGas(
|
return estimateGas(
|
||||||
address,
|
address,
|
||||||
this.contract.methods.createNftWithErc20,
|
this.contract.methods.createNftWithErc20,
|
||||||
@ -606,7 +598,7 @@ export class NftFactory extends SmartContractWithAddress {
|
|||||||
nftCreateData: NftCreateData,
|
nftCreateData: NftCreateData,
|
||||||
ercParams: Erc20CreateParams
|
ercParams: Erc20CreateParams
|
||||||
): Promise<TransactionReceipt> {
|
): Promise<TransactionReceipt> {
|
||||||
const ercCreateData = getErcCreationParams(ercParams)
|
const ercCreateData = this.getErcCreationParams(ercParams)
|
||||||
|
|
||||||
const estGas = await estimateGas(
|
const estGas = await estimateGas(
|
||||||
address,
|
address,
|
||||||
@ -641,8 +633,8 @@ export class NftFactory extends SmartContractWithAddress {
|
|||||||
ercParams: Erc20CreateParams,
|
ercParams: Erc20CreateParams,
|
||||||
poolParams: PoolCreationParams
|
poolParams: PoolCreationParams
|
||||||
): Promise<any> {
|
): Promise<any> {
|
||||||
const ercCreateData = getErcCreationParams(ercParams)
|
const ercCreateData = this.getErcCreationParams(ercParams)
|
||||||
const poolData = await getPoolCreationParams(this.web3, poolParams)
|
const poolData = await this.getPoolCreationParams(poolParams)
|
||||||
return estimateGas(
|
return estimateGas(
|
||||||
address,
|
address,
|
||||||
this.contract.methods.createNftWithErc20WithPool,
|
this.contract.methods.createNftWithErc20WithPool,
|
||||||
@ -668,8 +660,8 @@ export class NftFactory extends SmartContractWithAddress {
|
|||||||
ercParams: Erc20CreateParams,
|
ercParams: Erc20CreateParams,
|
||||||
poolParams: PoolCreationParams
|
poolParams: PoolCreationParams
|
||||||
): Promise<TransactionReceipt> {
|
): Promise<TransactionReceipt> {
|
||||||
const ercCreateData = getErcCreationParams(ercParams)
|
const ercCreateData = this.getErcCreationParams(ercParams)
|
||||||
const poolData = await getPoolCreationParams(this.web3, poolParams)
|
const poolData = await this.getPoolCreationParams(poolParams)
|
||||||
|
|
||||||
const estGas = await estimateGas(
|
const estGas = await estimateGas(
|
||||||
address,
|
address,
|
||||||
@ -704,8 +696,8 @@ export class NftFactory extends SmartContractWithAddress {
|
|||||||
ercParams: Erc20CreateParams,
|
ercParams: Erc20CreateParams,
|
||||||
freParams: FreCreationParams
|
freParams: FreCreationParams
|
||||||
): Promise<any> {
|
): Promise<any> {
|
||||||
const ercCreateData = getErcCreationParams(ercParams)
|
const ercCreateData = this.getErcCreationParams(ercParams)
|
||||||
const fixedData = await getFreCreationParams(freParams)
|
const fixedData = await this.getFreCreationParams(freParams)
|
||||||
return estimateGas(
|
return estimateGas(
|
||||||
address,
|
address,
|
||||||
this.contract.methods.createNftWithErc20WithFixedRate,
|
this.contract.methods.createNftWithErc20WithFixedRate,
|
||||||
@ -731,8 +723,8 @@ export class NftFactory extends SmartContractWithAddress {
|
|||||||
ercParams: Erc20CreateParams,
|
ercParams: Erc20CreateParams,
|
||||||
freParams: FreCreationParams
|
freParams: FreCreationParams
|
||||||
): Promise<TransactionReceipt> {
|
): Promise<TransactionReceipt> {
|
||||||
const ercCreateData = getErcCreationParams(ercParams)
|
const ercCreateData = this.getErcCreationParams(ercParams)
|
||||||
const fixedData = getFreCreationParams(freParams)
|
const fixedData = this.getFreCreationParams(freParams)
|
||||||
|
|
||||||
const estGas = await estimateGas(
|
const estGas = await estimateGas(
|
||||||
address,
|
address,
|
||||||
@ -767,7 +759,7 @@ export class NftFactory extends SmartContractWithAddress {
|
|||||||
ercParams: Erc20CreateParams,
|
ercParams: Erc20CreateParams,
|
||||||
dispenserParams: DispenserCreationParams
|
dispenserParams: DispenserCreationParams
|
||||||
): Promise<any> {
|
): Promise<any> {
|
||||||
const ercCreateData = getErcCreationParams(ercParams)
|
const ercCreateData = this.getErcCreationParams(ercParams)
|
||||||
return estimateGas(
|
return estimateGas(
|
||||||
address,
|
address,
|
||||||
this.contract.methods.createNftWithErc20WithDispenser,
|
this.contract.methods.createNftWithErc20WithDispenser,
|
||||||
@ -793,7 +785,7 @@ export class NftFactory extends SmartContractWithAddress {
|
|||||||
ercParams: Erc20CreateParams,
|
ercParams: Erc20CreateParams,
|
||||||
dispenserParams: DispenserCreationParams
|
dispenserParams: DispenserCreationParams
|
||||||
): Promise<TransactionReceipt> {
|
): Promise<TransactionReceipt> {
|
||||||
const ercCreateData = getErcCreationParams(ercParams)
|
const ercCreateData = this.getErcCreationParams(ercParams)
|
||||||
|
|
||||||
dispenserParams.maxBalance = Web3.utils.toWei(dispenserParams.maxBalance)
|
dispenserParams.maxBalance = Web3.utils.toWei(dispenserParams.maxBalance)
|
||||||
dispenserParams.maxTokens = Web3.utils.toWei(dispenserParams.maxTokens)
|
dispenserParams.maxTokens = Web3.utils.toWei(dispenserParams.maxTokens)
|
||||||
@ -817,4 +809,73 @@ export class NftFactory extends SmartContractWithAddress {
|
|||||||
|
|
||||||
return trxReceipt
|
return trxReceipt
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private getErcCreationParams(ercParams: Erc20CreateParams): any {
|
||||||
|
let name: string, symbol: string
|
||||||
|
// Generate name & symbol if not present
|
||||||
|
if (!ercParams.name || !ercParams.symbol) {
|
||||||
|
;({ name, symbol } = generateDtName())
|
||||||
|
}
|
||||||
|
return {
|
||||||
|
templateIndex: ercParams.templateIndex,
|
||||||
|
strings: [ercParams.name || name, ercParams.symbol || symbol],
|
||||||
|
addresses: [
|
||||||
|
ercParams.minter,
|
||||||
|
ercParams.paymentCollector,
|
||||||
|
ercParams.mpFeeAddress,
|
||||||
|
ercParams.feeToken
|
||||||
|
],
|
||||||
|
uints: [Web3.utils.toWei(ercParams.cap), Web3.utils.toWei(ercParams.feeAmount)],
|
||||||
|
bytess: []
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private getFreCreationParams(freParams: FreCreationParams): any {
|
||||||
|
if (!freParams.allowedConsumer) freParams.allowedConsumer = ZERO_ADDRESS
|
||||||
|
const withMint = freParams.withMint ? 1 : 0
|
||||||
|
|
||||||
|
return {
|
||||||
|
fixedPriceAddress: freParams.fixedRateAddress,
|
||||||
|
addresses: [
|
||||||
|
freParams.baseTokenAddress,
|
||||||
|
freParams.owner,
|
||||||
|
freParams.marketFeeCollector,
|
||||||
|
freParams.allowedConsumer
|
||||||
|
],
|
||||||
|
uints: [
|
||||||
|
freParams.baseTokenDecimals,
|
||||||
|
freParams.datatokenDecimals,
|
||||||
|
Web3.utils.toWei(freParams.fixedRate),
|
||||||
|
Web3.utils.toWei(freParams.marketFee),
|
||||||
|
withMint
|
||||||
|
]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private async getPoolCreationParams(poolParams: PoolCreationParams): Promise<any> {
|
||||||
|
return {
|
||||||
|
addresses: [
|
||||||
|
poolParams.ssContract,
|
||||||
|
poolParams.baseTokenAddress,
|
||||||
|
poolParams.baseTokenSender,
|
||||||
|
poolParams.publisherAddress,
|
||||||
|
poolParams.marketFeeCollector,
|
||||||
|
poolParams.poolTemplateAddress
|
||||||
|
],
|
||||||
|
ssParams: [
|
||||||
|
Web3.utils.toWei(poolParams.rate),
|
||||||
|
poolParams.baseTokenDecimals,
|
||||||
|
Web3.utils.toWei(poolParams.vestingAmount),
|
||||||
|
poolParams.vestedBlocks,
|
||||||
|
await this.amountToUnits(
|
||||||
|
poolParams.baseTokenAddress,
|
||||||
|
poolParams.initialBaseTokenLiquidity
|
||||||
|
)
|
||||||
|
],
|
||||||
|
swapFees: [
|
||||||
|
Web3.utils.toWei(poolParams.swapFeeLiquidityProvider),
|
||||||
|
Web3.utils.toWei(poolParams.swapFeeMarketRunner)
|
||||||
|
]
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -950,7 +950,7 @@ export class FixedRateExchange extends SmartContractWithAddress {
|
|||||||
* Get Router address set in fixed rate contract
|
* Get Router address set in fixed rate contract
|
||||||
* @return {String}
|
* @return {String}
|
||||||
*/
|
*/
|
||||||
async getRouter(): Promise<string> {
|
public async getRouter(): Promise<string> {
|
||||||
let result = null
|
let result = null
|
||||||
try {
|
try {
|
||||||
result = await this.contract.methods.router().call()
|
result = await this.contract.methods.router().call()
|
||||||
|
@ -7,12 +7,10 @@ import Bpool from '@oceanprotocol/contracts/artifacts/contracts/pools/balancer/B
|
|||||||
import {
|
import {
|
||||||
LoggerInstance,
|
LoggerInstance,
|
||||||
estimateGas,
|
estimateGas,
|
||||||
getMaxAddLiquidity,
|
|
||||||
getMaxRemoveLiquidity,
|
|
||||||
getMaxSwapExactIn,
|
|
||||||
getMaxSwapExactOut,
|
|
||||||
MAX_UINT_256,
|
MAX_UINT_256,
|
||||||
decimals
|
decimals,
|
||||||
|
calcMaxExactOut,
|
||||||
|
calcMaxExactIn
|
||||||
} from '../../utils'
|
} from '../../utils'
|
||||||
import {
|
import {
|
||||||
CurrentFees,
|
CurrentFees,
|
||||||
@ -706,7 +704,7 @@ export class Pool extends SmartContract {
|
|||||||
): Promise<TransactionReceipt> {
|
): Promise<TransactionReceipt> {
|
||||||
const pool = this.getContract(poolAddress)
|
const pool = this.getContract(poolAddress)
|
||||||
|
|
||||||
const maxSwap = await getMaxSwapExactIn(this, poolAddress, tokenInOutMarket.tokenIn)
|
const maxSwap = await this.getMaxSwapExactIn(poolAddress, tokenInOutMarket.tokenIn)
|
||||||
if (new Decimal(amountsInOutMaxFee.tokenAmountIn).greaterThan(maxSwap)) {
|
if (new Decimal(amountsInOutMaxFee.tokenAmountIn).greaterThan(maxSwap)) {
|
||||||
throw new Error(`tokenAmountIn is greater than ${maxSwap.toString()}`)
|
throw new Error(`tokenAmountIn is greater than ${maxSwap.toString()}`)
|
||||||
}
|
}
|
||||||
@ -845,7 +843,7 @@ export class Pool extends SmartContract {
|
|||||||
const pool = this.getContract(poolAddress)
|
const pool = this.getContract(poolAddress)
|
||||||
let result = null
|
let result = null
|
||||||
|
|
||||||
const maxSwap = await getMaxSwapExactOut(this, poolAddress, tokenInOutMarket.tokenOut)
|
const maxSwap = await this.getMaxSwapExactOut(poolAddress, tokenInOutMarket.tokenOut)
|
||||||
if (new Decimal(amountsInOutMaxFee.tokenAmountOut).greaterThan(maxSwap)) {
|
if (new Decimal(amountsInOutMaxFee.tokenAmountOut).greaterThan(maxSwap)) {
|
||||||
throw new Error(`tokenAmountOut is greater than ${maxSwap.toString()}`)
|
throw new Error(`tokenAmountOut is greater than ${maxSwap.toString()}`)
|
||||||
}
|
}
|
||||||
@ -959,7 +957,7 @@ export class Pool extends SmartContract {
|
|||||||
const pool = this.getContract(poolAddress)
|
const pool = this.getContract(poolAddress)
|
||||||
let result = null
|
let result = null
|
||||||
const tokenIn = await this.getBaseToken(poolAddress)
|
const tokenIn = await this.getBaseToken(poolAddress)
|
||||||
const maxSwap = await getMaxAddLiquidity(this, poolAddress, tokenIn)
|
const maxSwap = await this.getMaxAddLiquidity(poolAddress, tokenIn)
|
||||||
if (new Decimal(tokenAmountIn).greaterThan(maxSwap)) {
|
if (new Decimal(tokenAmountIn).greaterThan(maxSwap)) {
|
||||||
throw new Error(`tokenAmountOut is greater than ${maxSwap.toString()}`)
|
throw new Error(`tokenAmountOut is greater than ${maxSwap.toString()}`)
|
||||||
}
|
}
|
||||||
@ -1048,7 +1046,7 @@ export class Pool extends SmartContract {
|
|||||||
poolAmountIn
|
poolAmountIn
|
||||||
)
|
)
|
||||||
|
|
||||||
const maxSwap = await getMaxRemoveLiquidity(this, poolAddress, tokenOut)
|
const maxSwap = await this.getMaxRemoveLiquidity(poolAddress, tokenOut)
|
||||||
if (new Decimal(tokenAmountOut).greaterThan(maxSwap)) {
|
if (new Decimal(tokenAmountOut).greaterThan(maxSwap)) {
|
||||||
throw new Error(`tokenAmountOut is greater than ${maxSwap.toString()}`)
|
throw new Error(`tokenAmountOut is greater than ${maxSwap.toString()}`)
|
||||||
}
|
}
|
||||||
@ -1156,7 +1154,7 @@ export class Pool extends SmartContract {
|
|||||||
): Promise<PoolPriceAndFees> {
|
): Promise<PoolPriceAndFees> {
|
||||||
const pool = this.getContract(poolAddress)
|
const pool = this.getContract(poolAddress)
|
||||||
|
|
||||||
const maxSwap = await getMaxSwapExactOut(this, poolAddress, tokenOut)
|
const maxSwap = await this.getMaxSwapExactOut(poolAddress, tokenOut)
|
||||||
|
|
||||||
if (new Decimal(tokenAmountOut).greaterThan(maxSwap)) {
|
if (new Decimal(tokenAmountOut).greaterThan(maxSwap)) {
|
||||||
throw new Error(`tokenAmountOut is greater than ${maxSwap.toString()}`)
|
throw new Error(`tokenAmountOut is greater than ${maxSwap.toString()}`)
|
||||||
@ -1233,7 +1231,7 @@ export class Pool extends SmartContract {
|
|||||||
): Promise<PoolPriceAndFees> {
|
): Promise<PoolPriceAndFees> {
|
||||||
const pool = this.getContract(poolAddress)
|
const pool = this.getContract(poolAddress)
|
||||||
|
|
||||||
const maxSwap = await getMaxSwapExactIn(this, poolAddress, tokenIn)
|
const maxSwap = await this.getMaxSwapExactIn(poolAddress, tokenIn)
|
||||||
if (new Decimal(tokenAmountIn).greaterThan(maxSwap)) {
|
if (new Decimal(tokenAmountIn).greaterThan(maxSwap)) {
|
||||||
throw new Error(`tokenAmountIn is greater than ${maxSwap.toString()}`)
|
throw new Error(`tokenAmountIn is greater than ${maxSwap.toString()}`)
|
||||||
}
|
}
|
||||||
@ -1461,4 +1459,40 @@ export class Pool extends SmartContract {
|
|||||||
const topic = this.web3.eth.abi.encodeEventSignature(eventdata as any)
|
const topic = this.web3.eth.abi.encodeEventSignature(eventdata as any)
|
||||||
return topic
|
return topic
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private async getMaxSwapExactOut(
|
||||||
|
poolAddress: string,
|
||||||
|
tokenAddress: string
|
||||||
|
): Promise<Decimal> {
|
||||||
|
const reserve = await this.getReserve(poolAddress, tokenAddress)
|
||||||
|
|
||||||
|
return calcMaxExactOut(reserve)
|
||||||
|
}
|
||||||
|
|
||||||
|
private async getMaxSwapExactIn(
|
||||||
|
poolAddress: string,
|
||||||
|
tokenAddress: string
|
||||||
|
): Promise<Decimal> {
|
||||||
|
const reserve = await this.getReserve(poolAddress, tokenAddress)
|
||||||
|
|
||||||
|
return calcMaxExactIn(reserve)
|
||||||
|
}
|
||||||
|
|
||||||
|
private async getMaxAddLiquidity(
|
||||||
|
poolAddress: string,
|
||||||
|
tokenAddress: string
|
||||||
|
): Promise<Decimal> {
|
||||||
|
const reserve = await this.getReserve(poolAddress, tokenAddress)
|
||||||
|
|
||||||
|
return calcMaxExactIn(reserve)
|
||||||
|
}
|
||||||
|
|
||||||
|
private async getMaxRemoveLiquidity(
|
||||||
|
poolAddress: string,
|
||||||
|
tokenAddress: string
|
||||||
|
): Promise<Decimal> {
|
||||||
|
const reserve = await this.getReserve(poolAddress, tokenAddress)
|
||||||
|
|
||||||
|
return calcMaxExactIn(reserve)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -321,7 +321,7 @@ export class SideStaking extends SmartContract {
|
|||||||
* @param {String} datatokenAddress datatokenAddress
|
* @param {String} datatokenAddress datatokenAddress
|
||||||
* @return {TransactionReceipt}
|
* @return {TransactionReceipt}
|
||||||
*/
|
*/
|
||||||
async setPoolSwapFee(
|
private async setPoolSwapFee(
|
||||||
account: string,
|
account: string,
|
||||||
ssAddress: string,
|
ssAddress: string,
|
||||||
datatokenAddress: string,
|
datatokenAddress: string,
|
||||||
@ -358,7 +358,7 @@ export class SideStaking extends SmartContract {
|
|||||||
* @param {String} ssAddress side staking contract address
|
* @param {String} ssAddress side staking contract address
|
||||||
* @return {String}
|
* @return {String}
|
||||||
*/
|
*/
|
||||||
async getRouter(ssAddress: string): Promise<string> {
|
public async getRouter(ssAddress: string): Promise<string> {
|
||||||
const sideStaking = this.getContract(ssAddress)
|
const sideStaking = this.getContract(ssAddress)
|
||||||
let result = null
|
let result = null
|
||||||
try {
|
try {
|
||||||
|
@ -5,7 +5,7 @@ import { Contract } from 'web3-eth-contract'
|
|||||||
import Decimal from 'decimal.js'
|
import Decimal from 'decimal.js'
|
||||||
import ERC20Template from '@oceanprotocol/contracts/artifacts/contracts/templates/ERC20Template.sol/ERC20Template.json'
|
import ERC20Template from '@oceanprotocol/contracts/artifacts/contracts/templates/ERC20Template.sol/ERC20Template.json'
|
||||||
import ERC20TemplateEnterprise from '@oceanprotocol/contracts/artifacts/contracts/templates/ERC20TemplateEnterprise.sol/ERC20TemplateEnterprise.json'
|
import ERC20TemplateEnterprise from '@oceanprotocol/contracts/artifacts/contracts/templates/ERC20TemplateEnterprise.sol/ERC20TemplateEnterprise.json'
|
||||||
import { LoggerInstance, getFreOrderParams, estimateGas, ZERO_ADDRESS } from '../../utils'
|
import { LoggerInstance, estimateGas, ZERO_ADDRESS } from '../../utils'
|
||||||
import {
|
import {
|
||||||
ConsumeMarketFee,
|
ConsumeMarketFee,
|
||||||
FreOrderParams,
|
FreOrderParams,
|
||||||
@ -907,7 +907,7 @@ export class Datatoken extends SmartContract {
|
|||||||
): Promise<TransactionReceipt> {
|
): Promise<TransactionReceipt> {
|
||||||
const dtContract = this.getContract(dtAddress, null, this.abiEnterprise)
|
const dtContract = this.getContract(dtAddress, null, this.abiEnterprise)
|
||||||
try {
|
try {
|
||||||
const freContractParams = getFreOrderParams(freParams)
|
const freContractParams = this.getFreOrderParams(freParams)
|
||||||
|
|
||||||
const estGas = await estimateGas(
|
const estGas = await estimateGas(
|
||||||
address,
|
address,
|
||||||
@ -1243,4 +1243,14 @@ export class Datatoken extends SmartContract {
|
|||||||
}
|
}
|
||||||
return returnValues
|
return returnValues
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private getFreOrderParams(freParams: FreOrderParams): any {
|
||||||
|
return {
|
||||||
|
exchangeContract: freParams.exchangeContract,
|
||||||
|
exchangeId: freParams.exchangeId,
|
||||||
|
maxBaseTokenAmount: Web3.utils.toWei(freParams.maxBaseTokenAmount),
|
||||||
|
swapMarketFee: Web3.utils.toWei(freParams.swapMarketFee),
|
||||||
|
marketFeeAddress: freParams.marketFeeAddress
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
import Web3 from 'web3'
|
import Web3 from 'web3'
|
||||||
import fetch from 'cross-fetch'
|
import fetch from 'cross-fetch'
|
||||||
import { LoggerInstance, getData, noZeroX } from '../utils'
|
import { LoggerInstance } from '../utils'
|
||||||
import {
|
import {
|
||||||
FileMetadata,
|
FileMetadata,
|
||||||
ComputeJob,
|
ComputeJob,
|
||||||
@ -21,7 +21,7 @@ export class Provider {
|
|||||||
*/
|
*/
|
||||||
async getEndpoints(providerUri: string): Promise<any> {
|
async getEndpoints(providerUri: string): Promise<any> {
|
||||||
try {
|
try {
|
||||||
const endpoints = await getData(providerUri)
|
const endpoints = await this.getData(providerUri)
|
||||||
return await endpoints.json()
|
return await endpoints.json()
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
LoggerInstance.error('Finding the service endpoints failed:', e)
|
LoggerInstance.error('Finding the service endpoints failed:', e)
|
||||||
@ -526,7 +526,7 @@ export class Provider {
|
|||||||
|
|
||||||
let signatureMessage = consumerAddress
|
let signatureMessage = consumerAddress
|
||||||
signatureMessage += jobId || ''
|
signatureMessage += jobId || ''
|
||||||
signatureMessage += (did && `${noZeroX(did)}`) || ''
|
signatureMessage += (did && `${this.noZeroX(did)}`) || ''
|
||||||
signatureMessage += nonce
|
signatureMessage += nonce
|
||||||
const signature = await this.signProviderRequest(
|
const signature = await this.signProviderRequest(
|
||||||
web3,
|
web3,
|
||||||
@ -535,7 +535,7 @@ export class Provider {
|
|||||||
)
|
)
|
||||||
const payload = Object()
|
const payload = Object()
|
||||||
payload.signature = signature
|
payload.signature = signature
|
||||||
payload.documentId = noZeroX(did)
|
payload.documentId = this.noZeroX(did)
|
||||||
payload.consumerAddress = consumerAddress
|
payload.consumerAddress = consumerAddress
|
||||||
if (jobId) payload.jobId = jobId
|
if (jobId) payload.jobId = jobId
|
||||||
|
|
||||||
@ -590,7 +590,7 @@ export class Provider {
|
|||||||
: null
|
: null
|
||||||
|
|
||||||
let url = `?consumerAddress=${consumerAddress}`
|
let url = `?consumerAddress=${consumerAddress}`
|
||||||
url += (did && `&documentId=${noZeroX(did)}`) || ''
|
url += (did && `&documentId=${this.noZeroX(did)}`) || ''
|
||||||
url += (jobId && `&jobId=${jobId}`) || ''
|
url += (jobId && `&jobId=${jobId}`) || ''
|
||||||
|
|
||||||
if (!computeStatusUrl) return null
|
if (!computeStatusUrl) return null
|
||||||
@ -699,7 +699,7 @@ export class Provider {
|
|||||||
|
|
||||||
let signatureMessage = consumerAddress
|
let signatureMessage = consumerAddress
|
||||||
signatureMessage += jobId || ''
|
signatureMessage += jobId || ''
|
||||||
signatureMessage += (did && `${noZeroX(did)}`) || ''
|
signatureMessage += (did && `${this.noZeroX(did)}`) || ''
|
||||||
signatureMessage += nonce
|
signatureMessage += nonce
|
||||||
const signature = await this.signProviderRequest(
|
const signature = await this.signProviderRequest(
|
||||||
web3,
|
web3,
|
||||||
@ -707,7 +707,7 @@ export class Provider {
|
|||||||
signatureMessage
|
signatureMessage
|
||||||
)
|
)
|
||||||
const payload = Object()
|
const payload = Object()
|
||||||
payload.documentId = noZeroX(did)
|
payload.documentId = this.noZeroX(did)
|
||||||
payload.consumerAddress = consumerAddress
|
payload.consumerAddress = consumerAddress
|
||||||
payload.jobId = jobId
|
payload.jobId = jobId
|
||||||
if (signature) payload.signature = signature
|
if (signature) payload.signature = signature
|
||||||
@ -766,6 +766,51 @@ export class Provider {
|
|||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private zeroX(input: string): string {
|
||||||
|
return this.zeroXTransformer(input, true)
|
||||||
|
}
|
||||||
|
|
||||||
|
private noZeroX(input: string): string {
|
||||||
|
return this.zeroXTransformer(input, false)
|
||||||
|
}
|
||||||
|
|
||||||
|
private zeroXTransformer(input = '', zeroOutput: boolean): string {
|
||||||
|
const { valid, output } = this.inputMatch(
|
||||||
|
input,
|
||||||
|
/^(?:0x)*([a-f0-9]+)$/i,
|
||||||
|
'zeroXTransformer'
|
||||||
|
)
|
||||||
|
return (zeroOutput && valid ? '0x' : '') + output
|
||||||
|
}
|
||||||
|
|
||||||
|
// Shared functions
|
||||||
|
private inputMatch(
|
||||||
|
input: string,
|
||||||
|
regexp: RegExp,
|
||||||
|
conversorName: string
|
||||||
|
): { valid: boolean; output: string } {
|
||||||
|
if (typeof input !== 'string') {
|
||||||
|
LoggerInstance.debug('Not input string:')
|
||||||
|
LoggerInstance.debug(input)
|
||||||
|
throw new Error(`[${conversorName}] Expected string, input type: ${typeof input}`)
|
||||||
|
}
|
||||||
|
const match = input.match(regexp)
|
||||||
|
if (!match) {
|
||||||
|
LoggerInstance.warn(`[${conversorName}] Input transformation failed.`)
|
||||||
|
return { valid: false, output: input }
|
||||||
|
}
|
||||||
|
return { valid: true, output: match[1] }
|
||||||
|
}
|
||||||
|
|
||||||
|
private async getData(url: string): Promise<Response> {
|
||||||
|
return fetch(url, {
|
||||||
|
method: 'GET',
|
||||||
|
headers: {
|
||||||
|
'Content-type': 'application/json'
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export const ProviderInstance = new Provider()
|
export const ProviderInstance = new Provider()
|
||||||
|
@ -1,14 +1,8 @@
|
|||||||
import Web3 from 'web3'
|
import Web3 from 'web3'
|
||||||
import BigNumber from 'bignumber.js'
|
import BigNumber from 'bignumber.js'
|
||||||
import { Contract } from 'web3-eth-contract'
|
import { Contract } from 'web3-eth-contract'
|
||||||
import {
|
|
||||||
Erc20CreateParams,
|
|
||||||
FreCreationParams,
|
|
||||||
FreOrderParams,
|
|
||||||
PoolCreationParams
|
|
||||||
} from '../@types'
|
|
||||||
import { Config } from '../config'
|
import { Config } from '../config'
|
||||||
import { generateDtName, minAbi, LoggerInstance, GASLIMIT_DEFAULT, ZERO_ADDRESS } from '.'
|
import { minAbi, LoggerInstance, GASLIMIT_DEFAULT } from '.'
|
||||||
|
|
||||||
export function setContractDefaults(contract: Contract, config: Config): Contract {
|
export function setContractDefaults(contract: Contract, config: Config): Contract {
|
||||||
if (config) {
|
if (config) {
|
||||||
@ -32,89 +26,6 @@ export async function getFairGasPrice(web3: Web3, config: Config): Promise<strin
|
|||||||
else return x.toString(10)
|
else return x.toString(10)
|
||||||
}
|
}
|
||||||
|
|
||||||
export function getErcCreationParams(ercParams: Erc20CreateParams): any {
|
|
||||||
let name: string, symbol: string
|
|
||||||
// Generate name & symbol if not present
|
|
||||||
if (!ercParams.name || !ercParams.symbol) {
|
|
||||||
;({ name, symbol } = generateDtName())
|
|
||||||
}
|
|
||||||
return {
|
|
||||||
templateIndex: ercParams.templateIndex,
|
|
||||||
strings: [ercParams.name || name, ercParams.symbol || symbol],
|
|
||||||
addresses: [
|
|
||||||
ercParams.minter,
|
|
||||||
ercParams.paymentCollector,
|
|
||||||
ercParams.mpFeeAddress,
|
|
||||||
ercParams.feeToken
|
|
||||||
],
|
|
||||||
uints: [Web3.utils.toWei(ercParams.cap), Web3.utils.toWei(ercParams.feeAmount)],
|
|
||||||
bytess: []
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
export function getFreOrderParams(freParams: FreOrderParams): any {
|
|
||||||
return {
|
|
||||||
exchangeContract: freParams.exchangeContract,
|
|
||||||
exchangeId: freParams.exchangeId,
|
|
||||||
maxBaseTokenAmount: Web3.utils.toWei(freParams.maxBaseTokenAmount),
|
|
||||||
swapMarketFee: Web3.utils.toWei(freParams.swapMarketFee),
|
|
||||||
marketFeeAddress: freParams.marketFeeAddress
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
export function getFreCreationParams(freParams: FreCreationParams): any {
|
|
||||||
if (!freParams.allowedConsumer) freParams.allowedConsumer = ZERO_ADDRESS
|
|
||||||
const withMint = freParams.withMint ? 1 : 0
|
|
||||||
|
|
||||||
return {
|
|
||||||
fixedPriceAddress: freParams.fixedRateAddress,
|
|
||||||
addresses: [
|
|
||||||
freParams.baseTokenAddress,
|
|
||||||
freParams.owner,
|
|
||||||
freParams.marketFeeCollector,
|
|
||||||
freParams.allowedConsumer
|
|
||||||
],
|
|
||||||
uints: [
|
|
||||||
freParams.baseTokenDecimals,
|
|
||||||
freParams.datatokenDecimals,
|
|
||||||
Web3.utils.toWei(freParams.fixedRate),
|
|
||||||
Web3.utils.toWei(freParams.marketFee),
|
|
||||||
withMint
|
|
||||||
]
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
export async function getPoolCreationParams(
|
|
||||||
web3: Web3,
|
|
||||||
poolParams: PoolCreationParams
|
|
||||||
): Promise<any> {
|
|
||||||
return {
|
|
||||||
addresses: [
|
|
||||||
poolParams.ssContract,
|
|
||||||
poolParams.baseTokenAddress,
|
|
||||||
poolParams.baseTokenSender,
|
|
||||||
poolParams.publisherAddress,
|
|
||||||
poolParams.marketFeeCollector,
|
|
||||||
poolParams.poolTemplateAddress
|
|
||||||
],
|
|
||||||
ssParams: [
|
|
||||||
Web3.utils.toWei(poolParams.rate),
|
|
||||||
poolParams.baseTokenDecimals,
|
|
||||||
Web3.utils.toWei(poolParams.vestingAmount),
|
|
||||||
poolParams.vestedBlocks,
|
|
||||||
await amountToUnits(
|
|
||||||
web3,
|
|
||||||
poolParams.baseTokenAddress,
|
|
||||||
poolParams.initialBaseTokenLiquidity
|
|
||||||
)
|
|
||||||
],
|
|
||||||
swapFees: [
|
|
||||||
Web3.utils.toWei(poolParams.swapFeeLiquidityProvider),
|
|
||||||
Web3.utils.toWei(poolParams.swapFeeMarketRunner)
|
|
||||||
]
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
export async function unitsToAmount(
|
export async function unitsToAmount(
|
||||||
web3: Web3,
|
web3: Web3,
|
||||||
token: string,
|
token: string,
|
||||||
|
@ -1,27 +0,0 @@
|
|||||||
import { LoggerInstance } from '.'
|
|
||||||
|
|
||||||
export const zeroX = (input: string): string => zeroXTransformer(input, true)
|
|
||||||
export const noZeroX = (input: string): string => zeroXTransformer(input, false)
|
|
||||||
export function zeroXTransformer(input = '', zeroOutput: boolean): string {
|
|
||||||
const { valid, output } = inputMatch(input, /^(?:0x)*([a-f0-9]+)$/i, 'zeroXTransformer')
|
|
||||||
return (zeroOutput && valid ? '0x' : '') + output
|
|
||||||
}
|
|
||||||
|
|
||||||
// Shared functions
|
|
||||||
function inputMatch(
|
|
||||||
input: string,
|
|
||||||
regexp: RegExp,
|
|
||||||
conversorName: string
|
|
||||||
): { valid: boolean; output: string } {
|
|
||||||
if (typeof input !== 'string') {
|
|
||||||
LoggerInstance.debug('Not input string:')
|
|
||||||
LoggerInstance.debug(input)
|
|
||||||
throw new Error(`[${conversorName}] Expected string, input type: ${typeof input}`)
|
|
||||||
}
|
|
||||||
const match = input.match(regexp)
|
|
||||||
if (!match) {
|
|
||||||
LoggerInstance.warn(`[${conversorName}] Input transformation failed.`)
|
|
||||||
return { valid: false, output: input }
|
|
||||||
}
|
|
||||||
return { valid: true, output: match[1] }
|
|
||||||
}
|
|
@ -1,16 +1,5 @@
|
|||||||
import fetch from 'cross-fetch'
|
import fetch from 'cross-fetch'
|
||||||
import { DownloadResponse } from '../@types'
|
import { DownloadResponse } from '../@types'
|
||||||
import { LoggerInstance } from '.'
|
|
||||||
|
|
||||||
export async function fetchData(url: string, opts: RequestInit): Promise<Response> {
|
|
||||||
const result = await fetch(url, opts)
|
|
||||||
if (!result.ok) {
|
|
||||||
LoggerInstance.error(`Error requesting [${opts.method}] ${url}`)
|
|
||||||
LoggerInstance.error(`Response message: \n${await result.text()}`)
|
|
||||||
throw result
|
|
||||||
}
|
|
||||||
return result
|
|
||||||
}
|
|
||||||
|
|
||||||
export async function downloadFileBrowser(url: string): Promise<void> {
|
export async function downloadFileBrowser(url: string): Promise<void> {
|
||||||
const anchor = document.createElement('a')
|
const anchor = document.createElement('a')
|
||||||
@ -42,37 +31,3 @@ export async function downloadFile(
|
|||||||
|
|
||||||
return { data: await response.arrayBuffer(), filename }
|
return { data: await response.arrayBuffer(), filename }
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function getData(url: string): Promise<Response> {
|
|
||||||
return fetch(url, {
|
|
||||||
method: 'GET',
|
|
||||||
headers: {
|
|
||||||
'Content-type': 'application/json'
|
|
||||||
}
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
async function postWithHeaders(
|
|
||||||
url: string,
|
|
||||||
payload: BodyInit,
|
|
||||||
headers: any
|
|
||||||
): Promise<Response> {
|
|
||||||
if (payload != null) {
|
|
||||||
return fetch(url, {
|
|
||||||
method: 'POST',
|
|
||||||
body: payload,
|
|
||||||
headers
|
|
||||||
})
|
|
||||||
} else {
|
|
||||||
return fetch(url, {
|
|
||||||
method: 'POST'
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
export async function postData(url: string, payload: BodyInit): Promise<Response> {
|
|
||||||
const headers = {
|
|
||||||
'Content-type': 'application/json'
|
|
||||||
}
|
|
||||||
return postWithHeaders(url, payload, headers)
|
|
||||||
}
|
|
||||||
|
@ -1,5 +1,4 @@
|
|||||||
import Decimal from 'decimal.js'
|
import Decimal from 'decimal.js'
|
||||||
import { Pool } from '../contracts'
|
|
||||||
|
|
||||||
export function calcMaxExactOut(balance: string): Decimal {
|
export function calcMaxExactOut(balance: string): Decimal {
|
||||||
return new Decimal(balance).div(2)
|
return new Decimal(balance).div(2)
|
||||||
@ -8,40 +7,3 @@ export function calcMaxExactOut(balance: string): Decimal {
|
|||||||
export function calcMaxExactIn(balance: string): Decimal {
|
export function calcMaxExactIn(balance: string): Decimal {
|
||||||
return new Decimal(balance).div(2)
|
return new Decimal(balance).div(2)
|
||||||
}
|
}
|
||||||
export async function getMaxSwapExactOut(
|
|
||||||
poolInstance: Pool,
|
|
||||||
poolAddress: string,
|
|
||||||
tokenAddress: string
|
|
||||||
): Promise<Decimal> {
|
|
||||||
const reserve = await poolInstance.getReserve(poolAddress, tokenAddress)
|
|
||||||
return calcMaxExactOut(reserve)
|
|
||||||
}
|
|
||||||
|
|
||||||
export async function getMaxSwapExactIn(
|
|
||||||
poolInstance: Pool,
|
|
||||||
poolAddress: string,
|
|
||||||
tokenAddress: string
|
|
||||||
): Promise<Decimal> {
|
|
||||||
const reserve = await poolInstance.getReserve(poolAddress, tokenAddress)
|
|
||||||
return calcMaxExactIn(reserve)
|
|
||||||
}
|
|
||||||
|
|
||||||
export async function getMaxAddLiquidity(
|
|
||||||
poolInstance: Pool,
|
|
||||||
poolAddress: string,
|
|
||||||
tokenAddress: string
|
|
||||||
): Promise<Decimal> {
|
|
||||||
const reserve = await poolInstance.getReserve(poolAddress, tokenAddress)
|
|
||||||
|
|
||||||
return calcMaxExactIn(reserve)
|
|
||||||
}
|
|
||||||
|
|
||||||
export async function getMaxRemoveLiquidity(
|
|
||||||
poolInstance: Pool,
|
|
||||||
poolAddress: string,
|
|
||||||
tokenAddress: string
|
|
||||||
): Promise<Decimal> {
|
|
||||||
const reserve = await poolInstance.getReserve(poolAddress, tokenAddress)
|
|
||||||
|
|
||||||
return calcMaxExactIn(reserve)
|
|
||||||
}
|
|
||||||
|
@ -1,6 +1,5 @@
|
|||||||
export * from './Constants'
|
export * from './Constants'
|
||||||
export * from './ContractUtils'
|
export * from './ContractUtils'
|
||||||
export * from './ConversionTypeHelper'
|
|
||||||
export * from './DatatokenName'
|
export * from './DatatokenName'
|
||||||
export * from './DdoHelpers'
|
export * from './DdoHelpers'
|
||||||
export * from './FetchHelper'
|
export * from './FetchHelper'
|
||||||
|
Loading…
x
Reference in New Issue
Block a user