diff --git a/src/frontend/resources/general/js/components-pages/CudosBridgePageComponent.tsx b/src/frontend/resources/general/js/components-pages/CudosBridgePageComponent.tsx index 1f59efb..c1813de 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,28 +357,54 @@ export default class CudosBridgeComponent extends ContextPageComponent { - if (amount === S.Strings.EMPTY) { - return; - } + let minBridgeFeeAmount = new BigNumber(0); - 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, - }) - } - }, 200); + if (this.isFromCosmos(fromNetwork)) { + minBridgeFeeAmount = this.state.minBridgeFeeAmount; + } + + if (!bigAmount.isNaN() && + !bigAmount.isLessThan(new BigNumber(1).dividedBy(CosmosNetworkH.CURRENCY_1_CUDO)) && + this.validCudosNumber(amount) && + !bigAmount.isGreaterThan(BigNumber.minimum(this.state.walletBalance, this.state.contractBalance).minus(minBridgeFeeAmount).absoluteValue())) { + + let maximumAmount = this.state.walletBalance.minus(minBridgeFeeAmount).minus(amount); + + this.inputTimeouts.amount = setTimeout( async () => { + if (this.isFromCosmos(fromNetwork)) { + simulatedCost = await this.simulatedMsgsCost(amount); + maximumAmount = maximumAmount.minus(simulatedCost); + } + + if (maximumAmount.isGreaterThanOrEqualTo(0) && maximumAmount.isLessThan(this.state.contractBalance)) { + validAmount = true + amountError = S.INT_FALSE + } + + this.setState({ + validAmount: validAmount, + amountError: amountError, + estimatedGasFees: simulatedCost + }); + }, 200); + + return; + } + + this.setState({ + validAmount: validAmount, + amountError: amountError, + estimatedGasFees: simulatedCost + }); } onChangeDestinationAddress = (address) => { @@ -627,57 +654,49 @@ 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; + 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(), - 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' + ); - this.setState({ estimatedGasFees: simulatedCost }); + const estimatedCost = approxCost.amount[0]?approxCost.amount[0].amount:'0'; + simulatedCost = new BigNumber(estimatedCost).dividedBy(CosmosNetworkH.CURRENCY_1_CUDO) return simulatedCost; }