1
0
mirror of https://github.com/oceanprotocol/ocean.js.git synced 2024-11-26 20:39:05 +01:00

add estimateGas parameter to FixedRateExchange

This commit is contained in:
Miquel A. Cabot 2022-06-13 10:59:32 +02:00
parent 95d50bdd61
commit a988e03fa0

View File

@ -33,14 +33,15 @@ export class FixedRateExchange extends SmartContractWithAddress {
* @param {String} consumeMarketFee consumeMarketFee in fraction * @param {String} consumeMarketFee consumeMarketFee in fraction
* @return {Promise<TransactionReceipt>} transaction receipt * @return {Promise<TransactionReceipt>} transaction receipt
*/ */
public async buyDT( public async buyDT<G extends boolean = false>(
address: string, address: string,
exchangeId: string, exchangeId: string,
datatokenAmount: string, datatokenAmount: string,
maxBaseTokenAmount: string, maxBaseTokenAmount: string,
consumeMarketAddress: string = ZERO_ADDRESS, consumeMarketAddress: string = ZERO_ADDRESS,
consumeMarketFee: string = '0' consumeMarketFee: string = '0',
): Promise<TransactionReceipt> { estimateGas?: G
): Promise<G extends false ? TransactionReceipt : number> {
const exchange = await this.getExchange(exchangeId) const exchange = await this.getExchange(exchangeId)
const consumeMarketFeeFormatted = this.web3.utils.toWei(consumeMarketFee) const consumeMarketFeeFormatted = this.web3.utils.toWei(consumeMarketFee)
const dtAmountFormatted = await this.amountToUnits( const dtAmountFormatted = await this.amountToUnits(
@ -63,6 +64,8 @@ export class FixedRateExchange extends SmartContractWithAddress {
consumeMarketAddress, consumeMarketAddress,
consumeMarketFeeFormatted consumeMarketFeeFormatted
) )
if (estimateGas) return estGas
try { try {
const trxReceipt = await this.contract.methods const trxReceipt = await this.contract.methods
.buyDT( .buyDT(
@ -94,14 +97,15 @@ export class FixedRateExchange extends SmartContractWithAddress {
* @param {String} consumeMarketFee consumeMarketFee in fraction * @param {String} consumeMarketFee consumeMarketFee in fraction
* @return {Promise<TransactionReceipt>} transaction receipt * @return {Promise<TransactionReceipt>} transaction receipt
*/ */
public async sellDT( public async sellDT<G extends boolean = false>(
address: string, address: string,
exchangeId: string, exchangeId: string,
datatokenAmount: string, datatokenAmount: string,
minBaseTokenAmount: string, minBaseTokenAmount: string,
consumeMarketAddress: string = ZERO_ADDRESS, consumeMarketAddress: string = ZERO_ADDRESS,
consumeMarketFee: string = '0' consumeMarketFee: string = '0',
): Promise<TransactionReceipt> { estimateGas?: G
): Promise<G extends false ? TransactionReceipt : number> {
const exchange = await this.getExchange(exchangeId) const exchange = await this.getExchange(exchangeId)
const consumeMarketFeeFormatted = this.web3.utils.toWei(consumeMarketFee) const consumeMarketFeeFormatted = this.web3.utils.toWei(consumeMarketFee)
const dtAmountFormatted = await this.amountToUnits( const dtAmountFormatted = await this.amountToUnits(
@ -123,6 +127,8 @@ export class FixedRateExchange extends SmartContractWithAddress {
consumeMarketAddress, consumeMarketAddress,
consumeMarketFeeFormatted consumeMarketFeeFormatted
) )
if (estimateGas) return estGas
try { try {
const trxReceipt = await this.contract.methods const trxReceipt = await this.contract.methods
.sellDT( .sellDT(
@ -162,17 +168,20 @@ export class FixedRateExchange extends SmartContractWithAddress {
* @param {String} address User account * @param {String} address User account
* @return {Promise<TransactionReceipt>} transaction receipt * @return {Promise<TransactionReceipt>} transaction receipt
*/ */
public async setRate( public async setRate<G extends boolean = false>(
address: string, address: string,
exchangeId: string, exchangeId: string,
newRate: string newRate: string,
): Promise<TransactionReceipt> { estimateGas?: G
): Promise<G extends false ? TransactionReceipt : number> {
const estGas = await calculateEstimatedGas( const estGas = await calculateEstimatedGas(
address, address,
this.contract.methods.setRate, this.contract.methods.setRate,
exchangeId, exchangeId,
this.web3.utils.toWei(newRate) this.web3.utils.toWei(newRate)
) )
if (estimateGas) return estGas
const trxReceipt = await this.contract.methods const trxReceipt = await this.contract.methods
.setRate(exchangeId, this.web3.utils.toWei(newRate)) .setRate(exchangeId, this.web3.utils.toWei(newRate))
.send({ .send({
@ -190,17 +199,20 @@ export class FixedRateExchange extends SmartContractWithAddress {
* @param {String} address User account * @param {String} address User account
* @return {Promise<TransactionReceipt>} transaction receipt * @return {Promise<TransactionReceipt>} transaction receipt
*/ */
public async setAllowedSwapper( public async setAllowedSwapper<G extends boolean = false>(
address: string, address: string,
exchangeId: string, exchangeId: string,
newAllowedSwapper: string newAllowedSwapper: string,
): Promise<TransactionReceipt> { estimateGas?: G
): Promise<G extends false ? TransactionReceipt : number> {
const estGas = await calculateEstimatedGas( const estGas = await calculateEstimatedGas(
address, address,
this.contract.methods.setAllowedSwapper, this.contract.methods.setAllowedSwapper,
exchangeId, exchangeId,
newAllowedSwapper newAllowedSwapper
) )
if (estimateGas) return estGas
const trxReceipt = await this.contract.methods const trxReceipt = await this.contract.methods
.setAllowedSwapper(exchangeId, newAllowedSwapper) .setAllowedSwapper(exchangeId, newAllowedSwapper)
.send({ .send({
@ -217,10 +229,11 @@ export class FixedRateExchange extends SmartContractWithAddress {
* @param {String} address User address * @param {String} address User address
* @return {Promise<TransactionReceipt>} transaction receipt * @return {Promise<TransactionReceipt>} transaction receipt
*/ */
public async activate( public async activate<G extends boolean = false>(
address: string, address: string,
exchangeId: string exchangeId: string,
): Promise<TransactionReceipt> { estimateGas?: G
): Promise<G extends false ? TransactionReceipt : number> {
const exchange = await this.getExchange(exchangeId) const exchange = await this.getExchange(exchangeId)
if (!exchange) return null if (!exchange) return null
if (exchange.active === true) return null if (exchange.active === true) return null
@ -229,6 +242,8 @@ export class FixedRateExchange extends SmartContractWithAddress {
this.contract.methods.toggleExchangeState, this.contract.methods.toggleExchangeState,
exchangeId exchangeId
) )
if (estimateGas) return estGas
const trxReceipt = await this.contract.methods.toggleExchangeState(exchangeId).send({ const trxReceipt = await this.contract.methods.toggleExchangeState(exchangeId).send({
from: address, from: address,
gas: estGas + 1, gas: estGas + 1,
@ -243,10 +258,11 @@ export class FixedRateExchange extends SmartContractWithAddress {
* @param {String} address User address * @param {String} address User address
* @return {Promise<TransactionReceipt>} transaction receipt * @return {Promise<TransactionReceipt>} transaction receipt
*/ */
public async deactivate( public async deactivate<G extends boolean = false>(
address: string, address: string,
exchangeId: string exchangeId: string,
): Promise<TransactionReceipt> { estimateGas?: G
): Promise<G extends false ? TransactionReceipt : number> {
const exchange = await this.getExchange(exchangeId) const exchange = await this.getExchange(exchangeId)
if (!exchange) return null if (!exchange) return null
if (exchange.active === false) return null if (exchange.active === false) return null
@ -256,6 +272,7 @@ export class FixedRateExchange extends SmartContractWithAddress {
this.contract.methods.toggleExchangeState, this.contract.methods.toggleExchangeState,
exchangeId exchangeId
) )
if (estimateGas) return estGas
const trxReceipt = await this.contract.methods.toggleExchangeState(exchangeId).send({ const trxReceipt = await this.contract.methods.toggleExchangeState(exchangeId).send({
from: address, from: address,
@ -473,10 +490,11 @@ export class FixedRateExchange extends SmartContractWithAddress {
* @param {String} address User address * @param {String} address User address
* @return {Promise<TransactionReceipt>} transaction receipt * @return {Promise<TransactionReceipt>} transaction receipt
*/ */
public async activateMint( public async activateMint<G extends boolean = false>(
address: string, address: string,
exchangeId: string exchangeId: string,
): Promise<TransactionReceipt> { estimateGas?: G
): Promise<G extends false ? TransactionReceipt : number> {
const exchange = await this.getExchange(exchangeId) const exchange = await this.getExchange(exchangeId)
if (!exchange) return null if (!exchange) return null
if (exchange.withMint === true) return null if (exchange.withMint === true) return null
@ -487,6 +505,8 @@ export class FixedRateExchange extends SmartContractWithAddress {
exchangeId, exchangeId,
true true
) )
if (estimateGas) return estGas
const trxReceipt = await this.contract.methods const trxReceipt = await this.contract.methods
.toggleMintState(exchangeId, true) .toggleMintState(exchangeId, true)
.send({ .send({
@ -503,10 +523,11 @@ export class FixedRateExchange extends SmartContractWithAddress {
* @param {String} address User address * @param {String} address User address
* @return {Promise<TransactionReceipt>} transaction receipt * @return {Promise<TransactionReceipt>} transaction receipt
*/ */
public async deactivateMint( public async deactivateMint<G extends boolean = false>(
address: string, address: string,
exchangeId: string exchangeId: string,
): Promise<TransactionReceipt> { estimateGas?: G
): Promise<G extends false ? TransactionReceipt : number> {
const exchange = await this.getExchange(exchangeId) const exchange = await this.getExchange(exchangeId)
if (!exchange) return null if (!exchange) return null
if (exchange.withMint === false) return null if (exchange.withMint === false) return null
@ -517,6 +538,7 @@ export class FixedRateExchange extends SmartContractWithAddress {
exchangeId, exchangeId,
false false
) )
if (estimateGas) return estGas
const trxReceipt = await this.contract.methods const trxReceipt = await this.contract.methods
.toggleMintState(exchangeId, false) .toggleMintState(exchangeId, false)
@ -536,11 +558,12 @@ export class FixedRateExchange extends SmartContractWithAddress {
* @param {String} amount amount to be collected * @param {String} amount amount to be collected
* @return {Promise<TransactionReceipt>} transaction receipt * @return {Promise<TransactionReceipt>} transaction receipt
*/ */
public async collectBT( public async collectBT<G extends boolean = false>(
address: string, address: string,
exchangeId: string, exchangeId: string,
amount: string amount: string,
): Promise<TransactionReceipt> { estimateGas?: G
): Promise<G extends false ? TransactionReceipt : number> {
const exchange = await this.getExchange(exchangeId) const exchange = await this.getExchange(exchangeId)
if (!exchange) return null if (!exchange) return null
@ -559,6 +582,7 @@ export class FixedRateExchange extends SmartContractWithAddress {
exchangeId, exchangeId,
amountWei amountWei
) )
if (estimateGas) return estGas
const trxReceipt = await this.contract.methods.collectBT(exchangeId, amountWei).send({ const trxReceipt = await this.contract.methods.collectBT(exchangeId, amountWei).send({
from: address, from: address,
@ -575,11 +599,12 @@ export class FixedRateExchange extends SmartContractWithAddress {
* @param {String} amount amount to be collected * @param {String} amount amount to be collected
* @return {Promise<TransactionReceipt>} transaction receipt * @return {Promise<TransactionReceipt>} transaction receipt
*/ */
public async collectDT( public async collectDT<G extends boolean = false>(
address: string, address: string,
exchangeId: string, exchangeId: string,
amount: string amount: string,
): Promise<TransactionReceipt> { estimateGas?: G
): Promise<G extends false ? TransactionReceipt : number> {
const exchange = await this.getExchange(exchangeId) const exchange = await this.getExchange(exchangeId)
if (!exchange) return null if (!exchange) return null
@ -598,6 +623,7 @@ export class FixedRateExchange extends SmartContractWithAddress {
exchangeId, exchangeId,
amountWei amountWei
) )
if (estimateGas) return estGas
const trxReceipt = await this.contract.methods.collectDT(exchangeId, amountWei).send({ const trxReceipt = await this.contract.methods.collectDT(exchangeId, amountWei).send({
from: address, from: address,
@ -613,10 +639,11 @@ export class FixedRateExchange extends SmartContractWithAddress {
* @param {String} address User address * @param {String} address User address
* @return {Promise<TransactionReceipt>} transaction receipt * @return {Promise<TransactionReceipt>} transaction receipt
*/ */
public async collectMarketFee( public async collectMarketFee<G extends boolean = false>(
address: string, address: string,
exchangeId: string exchangeId: string,
): Promise<TransactionReceipt> { estimateGas?: G
): Promise<G extends false ? TransactionReceipt : number> {
const exchange = await this.getExchange(exchangeId) const exchange = await this.getExchange(exchangeId)
if (!exchange) return null if (!exchange) return null
@ -625,6 +652,8 @@ export class FixedRateExchange extends SmartContractWithAddress {
this.contract.methods.collectMarketFee, this.contract.methods.collectMarketFee,
exchangeId exchangeId
) )
if (estimateGas) return estGas
const trxReceipt = await this.contract.methods.collectMarketFee(exchangeId).send({ const trxReceipt = await this.contract.methods.collectMarketFee(exchangeId).send({
from: address, from: address,
gas: estGas + 1, gas: estGas + 1,
@ -639,10 +668,11 @@ export class FixedRateExchange extends SmartContractWithAddress {
* @param {String} address User address * @param {String} address User address
* @return {Promise<TransactionReceipt>} transaction receipt * @return {Promise<TransactionReceipt>} transaction receipt
*/ */
public async collectOceanFee( public async collectOceanFee<G extends boolean = false>(
address: string, address: string,
exchangeId: string exchangeId: string,
): Promise<TransactionReceipt> { estimateGas?: G
): Promise<G extends false ? TransactionReceipt : number> {
const exchange = await this.getExchange(exchangeId) const exchange = await this.getExchange(exchangeId)
if (!exchange) return null if (!exchange) return null
@ -651,6 +681,8 @@ export class FixedRateExchange extends SmartContractWithAddress {
this.contract.methods.collectOceanFee, this.contract.methods.collectOceanFee,
exchangeId exchangeId
) )
if (estimateGas) return estGas
const trxReceipt = await this.contract.methods.collectOceanFee(exchangeId).send({ const trxReceipt = await this.contract.methods.collectOceanFee(exchangeId).send({
from: address, from: address,
gas: estGas + 1, gas: estGas + 1,
@ -709,17 +741,20 @@ export class FixedRateExchange extends SmartContractWithAddress {
* @param {String} newMarketFee New market fee * @param {String} newMarketFee New market fee
* @return {Promise<TransactionReceipt>} transaction receipt * @return {Promise<TransactionReceipt>} transaction receipt
*/ */
public async updateMarketFee( public async updateMarketFee<G extends boolean = false>(
address: string, address: string,
exchangeId: string, exchangeId: string,
newMarketFee: string newMarketFee: string,
): Promise<TransactionReceipt> { estimateGas?: G
): Promise<G extends false ? TransactionReceipt : number> {
const estGas = await calculateEstimatedGas( const estGas = await calculateEstimatedGas(
address, address,
this.contract.methods.updateMarketFee, this.contract.methods.updateMarketFee,
exchangeId, exchangeId,
this.web3.utils.toWei(newMarketFee) this.web3.utils.toWei(newMarketFee)
) )
if (estimateGas) return estGas
const trxReceipt = await this.contract.methods const trxReceipt = await this.contract.methods
.updateMarketFee(exchangeId, this.web3.utils.toWei(newMarketFee)) .updateMarketFee(exchangeId, this.web3.utils.toWei(newMarketFee))
.send({ .send({
@ -737,17 +772,20 @@ export class FixedRateExchange extends SmartContractWithAddress {
* @param {String} newMarketFeeCollector New market fee collector * @param {String} newMarketFeeCollector New market fee collector
* @return {Promise<TransactionReceipt>} transaction receipt * @return {Promise<TransactionReceipt>} transaction receipt
*/ */
public async updateMarketFeeCollector( public async updateMarketFeeCollector<G extends boolean = false>(
address: string, address: string,
exchangeId: string, exchangeId: string,
newMarketFeeCollector: string newMarketFeeCollector: string,
): Promise<TransactionReceipt> { estimateGas?: G
): Promise<G extends false ? TransactionReceipt : number> {
const estGas = await calculateEstimatedGas( const estGas = await calculateEstimatedGas(
address, address,
this.contract.methods.updateMarketFeeCollector, this.contract.methods.updateMarketFeeCollector,
exchangeId, exchangeId,
newMarketFeeCollector newMarketFeeCollector
) )
if (estimateGas) return estGas
const trxReceipt = await this.contract.methods const trxReceipt = await this.contract.methods
.updateMarketFeeCollector(exchangeId, newMarketFeeCollector) .updateMarketFeeCollector(exchangeId, newMarketFeeCollector)
.send({ .send({