From 2421fdbe965650ef7bf20dc1ae2f9c8c6a6b4392 Mon Sep 17 00:00:00 2001 From: SpaghettiOverload Date: Mon, 30 May 2022 12:03:04 +0300 Subject: [PATCH 1/5] Fix precision --- .../general/js/components-pages/CudosBridgePageComponent.tsx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/frontend/resources/general/js/components-pages/CudosBridgePageComponent.tsx b/src/frontend/resources/general/js/components-pages/CudosBridgePageComponent.tsx index 1f59efb..a72b1d6 100644 --- a/src/frontend/resources/general/js/components-pages/CudosBridgePageComponent.tsx +++ b/src/frontend/resources/general/js/components-pages/CudosBridgePageComponent.tsx @@ -656,7 +656,7 @@ export default class CudosBridgeComponent extends ContextPageComponent Date: Mon, 30 May 2022 13:58:30 +0300 Subject: [PATCH 2/5] Updating validation --- .../components-pages/CudosBridgePageComponent.tsx | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/src/frontend/resources/general/js/components-pages/CudosBridgePageComponent.tsx b/src/frontend/resources/general/js/components-pages/CudosBridgePageComponent.tsx index a72b1d6..d1dd800 100644 --- a/src/frontend/resources/general/js/components-pages/CudosBridgePageComponent.tsx +++ b/src/frontend/resources/general/js/components-pages/CudosBridgePageComponent.tsx @@ -356,10 +356,6 @@ export default class CudosBridgeComponent extends ContextPageComponent { + this.inputTimeouts.amount = setTimeout( async () => { + if (amount === S.Strings.EMPTY) { return; } @@ -375,7 +372,12 @@ export default class CudosBridgeComponent extends ContextPageComponent Date: Mon, 30 May 2022 14:44:00 +0300 Subject: [PATCH 3/5] Estimation and Validation refactoring --- .../CudosBridgePageComponent.tsx | 125 +++++++++--------- 1 file changed, 62 insertions(+), 63 deletions(-) diff --git a/src/frontend/resources/general/js/components-pages/CudosBridgePageComponent.tsx b/src/frontend/resources/general/js/components-pages/CudosBridgePageComponent.tsx index d1dd800..38f00fa 100644 --- a/src/frontend/resources/general/js/components-pages/CudosBridgePageComponent.tsx +++ b/src/frontend/resources/general/js/components-pages/CudosBridgePageComponent.tsx @@ -356,30 +356,34 @@ export default class CudosBridgeComponent extends ContextPageComponent { + if (!bigAmount.isNaN() && + !bigAmount.isLessThan(new BigNumber(1).dividedBy(CosmosNetworkH.CURRENCY_1_CUDO)) && + !bigAmount.isGreaterThan(BigNumber.minimum(this.state.walletBalance, this.state.contractBalance).minus(this.state.minBridgeFeeAmount).absoluteValue()) && + this.validCudosNumber(amount)) { - if (amount === S.Strings.EMPTY) { - return; - } - - if (bigAmount.isNaN() || bigAmount.isLessThan(new BigNumber(1).dividedBy(CosmosNetworkH.CURRENCY_1_CUDO)) || bigAmount.isGreaterThan(BigNumber.minimum(this.state.walletBalance, this.state.contractBalance).minus(this.state.minBridgeFeeAmount).absoluteValue())) { - this.setState({ - amountError: S.INT_TRUE, - }); - return; - } + this.setState({ + amountError: S.INT_FALSE, + validAmount: true + }); - if (this.isFromCosmos(fromNetwork)) { - await this.setSimulatedMsgsCost(amount); - } - }, 200); + this.inputTimeouts.amount = setTimeout( async () => { + if (this.isFromCosmos(fromNetwork)) { + await this.setSimulatedMsgsCost(amount); + } + }, 200); + + } else { + this.setState({ + validAmount: false, + amountError: S.INT_TRUE + }); + } } onChangeDestinationAddress = (address) => { @@ -631,53 +635,48 @@ export default class CudosBridgeComponent extends ContextPageComponent => { let simulatedCost = new BigNumber(0); + const stringifiedAmount = new BigNumber(amount).multipliedBy(CosmosNetworkH.CURRENCY_1_CUDO).toString(10); + const ledger = this.props.networkStore.networkHolders[this.state.selectedFromNetwork].ledger; + const [client, account] = await ledger.GetKeplrClientAndAccount(); + let destination: string; + let sender: string; - if (this.validCudosNumber(amount)) { - this.setState({ validAmount: true }); - const stringifiedAmount = new BigNumber(amount).multipliedBy(CosmosNetworkH.CURRENCY_1_CUDO).toString(10); - const ledger = this.props.networkStore.networkHolders[this.state.selectedFromNetwork].ledger; - const [client, account] = await ledger.GetKeplrClientAndAccount(); - let destination: string; - let sender: string; - - if (this.isFromCosmos(this.state.selectedFromNetwork) === true) { - sender = this.getAddress(this.state.selectedFromNetwork, 0); - destination = this.getAddress(this.state.selectedToNetwork, 0); - } else { - destination = this.getAddress(this.state.selectedFromNetwork, 0); - sender = this.getAddress(this.state.selectedToNetwork, 0); - } - - const simulatedMsg = [{ - typeUrl: Config.CUDOS_NETWORK.MESSAGE_TYPE_URL, - value: { - sender: sender, - ethDest:destination, - amount: { - amount: stringifiedAmount, - denom: CosmosNetworkH.CURRENCY_DENOM, - }, - bridgeFee: { - amount: this.state.minBridgeFeeAmount.multipliedBy(CosmosNetworkH.CURRENCY_1_CUDO).toString(10), - denom: CosmosNetworkH.CURRENCY_DENOM, - }, - }, - - }]; - - const approxCost = await ledger.EstimateFee( - client, - GasPrice.fromString(Config.CUDOS_NETWORK.FEE+'acudos'), - account.address, - simulatedMsg, - 'Fee Estimation Message' - ); - - const estimatedCost = approxCost.amount[0]?approxCost.amount[0].amount:'0'; - simulatedCost = new BigNumber(estimatedCost).dividedBy(CosmosNetworkH.CURRENCY_1_CUDO) + if (this.isFromCosmos(this.state.selectedFromNetwork) === true) { + sender = this.getAddress(this.state.selectedFromNetwork, 0); + destination = this.getAddress(this.state.selectedToNetwork, 0); } else { - this.setState({ validAmount: false }); - } + destination = this.getAddress(this.state.selectedFromNetwork, 0); + sender = this.getAddress(this.state.selectedToNetwork, 0); + } + + const simulatedMsg = [{ + typeUrl: Config.CUDOS_NETWORK.MESSAGE_TYPE_URL, + value: { + sender: sender, + ethDest:destination, + amount: { + amount: stringifiedAmount, + denom: CosmosNetworkH.CURRENCY_DENOM, + }, + bridgeFee: { + amount: this.state.minBridgeFeeAmount.multipliedBy(CosmosNetworkH.CURRENCY_1_CUDO).toString(10), + denom: CosmosNetworkH.CURRENCY_DENOM, + }, + }, + + }]; + + const approxCost = await ledger.EstimateFee( + client, + GasPrice.fromString(Config.CUDOS_NETWORK.FEE+'acudos'), + account.address, + simulatedMsg, + 'Fee Estimation Message' + ); + + const estimatedCost = approxCost.amount[0]?approxCost.amount[0].amount:'0'; + simulatedCost = new BigNumber(estimatedCost).dividedBy(CosmosNetworkH.CURRENCY_1_CUDO) + this.setState({ estimatedGasFees: simulatedCost }); return simulatedCost; From 7cf461ad91b1b7454621285fa240a8c8210c502f Mon Sep 17 00:00:00 2001 From: SpaghettiOverload Date: Mon, 30 May 2022 16:39:42 +0300 Subject: [PATCH 4/5] code refactoring and estimate improvements --- .../CudosBridgePageComponent.tsx | 68 ++++++++++++------- 1 file changed, 43 insertions(+), 25 deletions(-) diff --git a/src/frontend/resources/general/js/components-pages/CudosBridgePageComponent.tsx b/src/frontend/resources/general/js/components-pages/CudosBridgePageComponent.tsx index 38f00fa..4ffff97 100644 --- a/src/frontend/resources/general/js/components-pages/CudosBridgePageComponent.tsx +++ b/src/frontend/resources/general/js/components-pages/CudosBridgePageComponent.tsx @@ -313,31 +313,32 @@ export default class CudosBridgeComponent extends ContextPageComponent { const ledger = await this.checkWalletConnected(); const fromNetwork = this.state.selectedFromNetwork; + const maxButtonMultiplier = 1.05; // Fixing issue, when fee estimates are less for MAX amount than smaller number entered by hand. let balance = await ledger.getBalance(); + let simulatedCost = new BigNumber(0); - if (!balance) { - balance = new BigNumber(0); - } + if (!balance) { return } let maximumAmount = BigNumber.maximum(balance, this.state.walletBalance).minus(this.state.minBridgeFeeAmount); if (maximumAmount.gt(0) && this.isFromCosmos(fromNetwork)) { - const simulatedCost = await this.setSimulatedMsgsCost(maximumAmount.toString()); - maximumAmount = maximumAmount.minus(simulatedCost); + simulatedCost = await this.simulatedMsgsCost(maximumAmount.toString()); + maximumAmount = maximumAmount.minus(simulatedCost.multipliedBy(maxButtonMultiplier)); } if (!this.isFromCosmos(fromNetwork) === true) { maximumAmount = balance; } - if (maximumAmount.lt(0)) { - maximumAmount = new BigNumber(0); - } + if (maximumAmount.lte(0)) { return } this.setState({ amount: maximumAmount, displayAmount: maximumAmount.toFixed(), walletBalance: balance, + estimatedGasFees: simulatedCost.multipliedBy(maxButtonMultiplier), + validAmount: true, + amountError: S.INT_FALSE, }) } @@ -356,34 +357,54 @@ export default class CudosBridgeComponent extends ContextPageComponent { if (this.isFromCosmos(fromNetwork)) { - await this.setSimulatedMsgsCost(amount); + simulatedCost = await this.simulatedMsgsCost(amount); + maximumAmount = maximumAmount.minus(simulatedCost); } + + if (maximumAmount.isGreaterThan(0) && maximumAmount.isLessThan(this.state.contractBalance)) { + validAmount = true + amountError = S.INT_FALSE + } + + this.setState({ + validAmount: validAmount, + amountError: amountError, + estimatedGasFees: simulatedCost + }); }, 200); - - } else { - this.setState({ - validAmount: false, - amountError: S.INT_TRUE - }); + + return; } + + this.setState({ + validAmount: validAmount, + amountError: amountError, + estimatedGasFees: simulatedCost + }); } onChangeDestinationAddress = (address) => { @@ -633,7 +654,7 @@ export default class CudosBridgeComponent extends ContextPageComponent => { + simulatedMsgsCost = async (amount: string): Promise => { let simulatedCost = new BigNumber(0); const stringifiedAmount = new BigNumber(amount).multipliedBy(CosmosNetworkH.CURRENCY_1_CUDO).toString(10); const ledger = this.props.networkStore.networkHolders[this.state.selectedFromNetwork].ledger; @@ -676,9 +697,6 @@ export default class CudosBridgeComponent extends ContextPageComponent Date: Mon, 30 May 2022 16:55:11 +0300 Subject: [PATCH 5/5] minor fix --- .../general/js/components-pages/CudosBridgePageComponent.tsx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/frontend/resources/general/js/components-pages/CudosBridgePageComponent.tsx b/src/frontend/resources/general/js/components-pages/CudosBridgePageComponent.tsx index 4ffff97..c1813de 100644 --- a/src/frontend/resources/general/js/components-pages/CudosBridgePageComponent.tsx +++ b/src/frontend/resources/general/js/components-pages/CudosBridgePageComponent.tsx @@ -385,7 +385,7 @@ export default class CudosBridgeComponent extends ContextPageComponent