Skip to content

Commit

Permalink
Merge pull request #19 from CudoVentures/cudos-dev
Browse files Browse the repository at this point in the history
Cudos dev
  • Loading branch information
avalkov authored Jun 2, 2022
2 parents aabe619 + 3158669 commit a0c0583
Showing 1 changed file with 90 additions and 71 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -313,31 +313,32 @@ export default class CudosBridgeComponent extends ContextPageComponent<Props, St
onClickMaxAmount = async () => {
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,
})
}

Expand All @@ -356,28 +357,54 @@ export default class CudosBridgeComponent extends ContextPageComponent<Props, St
clearTimeout(this.inputTimeouts.amount);
const bigAmount = new BigNumber(amount);
const fromNetwork = this.state.selectedFromNetwork;
let validAmount = false;
let amountError = S.INT_TRUE;
let simulatedCost = new BigNumber(0);

if (this.isFromCosmos(fromNetwork)) {
await this.setSimulatedMsgsCost(amount);
}

this.setState({
amount: bigAmount,
displayAmount: amount,
amountError: S.INT_FALSE,
})
});

this.inputTimeouts.amount = setTimeout(() => {
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) => {
Expand Down Expand Up @@ -627,57 +654,49 @@ export default class CudosBridgeComponent extends ContextPageComponent<Props, St
return checkResult === "OK";
}

setSimulatedMsgsCost = async (amount: string): Promise<BigNumber> => {
simulatedMsgsCost = async (amount: string): Promise<BigNumber> => {
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;
}

Expand Down

0 comments on commit a0c0583

Please sign in to comment.