|
2 | 2 | import Big from '~/assets/big.js'; |
3 | 3 | import {validationMixin} from 'vuelidate'; |
4 | 4 | import required from 'vuelidate/lib/validators/required.js'; |
| 5 | +import minValue from 'vuelidate/lib/validators/minValue.js'; |
5 | 6 | import maxValue from 'vuelidate/lib/validators/maxValue.js'; |
6 | 7 | import minLength from 'vuelidate/lib/validators/minLength.js'; |
7 | 8 | import autosize from 'v-autosize'; |
@@ -139,9 +140,11 @@ export default { |
139 | 140 | const inverseRate = new Big(this.hubFeeRate).div(new Big(1).minus(this.hubFeeRate)); |
140 | 141 | return amount.times(inverseRate).toString(); |
141 | 142 | }, |
| 143 | + /* |
142 | 144 | totalFee() { |
143 | 145 | return new Big(this.coinFee).plus(this.hubFee).toString(); |
144 | 146 | }, |
| 147 | + */ |
145 | 148 | amountToSend() { |
146 | 149 | return new Big(this.form.amount || 0).plus(this.coinFee).plus(this.hubFee).toString(); |
147 | 150 | }, |
@@ -170,6 +173,9 @@ export default { |
170 | 173 | return maxAmount.toString(); |
171 | 174 | } |
172 | 175 | }, |
| 176 | + minAmount() { |
| 177 | + return getHubMinAmount(this.coinFee, this.hubFeeRate); |
| 178 | + }, |
173 | 179 | suggestionList() { |
174 | 180 | return this.hubCoinList |
175 | 181 | // show only available coins for selected network |
@@ -224,7 +230,7 @@ export default { |
224 | 230 | amount: { |
225 | 231 | required, |
226 | 232 | // validAmount: isValidAmount, |
227 | | - minValue: (value) => value > 0, |
| 233 | + minValue: minValue(this.minAmount), |
228 | 234 | maxValue: maxValue(this.maxAmount || 0), |
229 | 235 | }, |
230 | 236 | }, |
@@ -375,6 +381,48 @@ export default { |
375 | 381 | }, |
376 | 382 | }, |
377 | 383 | }; |
| 384 | +
|
| 385 | +/** |
| 386 | + * // Minter Hub not consider discount in amount validation, so we need compensate amount for discount difference |
| 387 | + * @param {number|string} destinationNetworkFee |
| 388 | + * @param {number|string} hubFeeRate |
| 389 | + * @param {number|string} hubFeeBaseRate - hub fee rate without discount (0.01) |
| 390 | + * @return {number|string} |
| 391 | + */ |
| 392 | +function getHubMinAmount(destinationNetworkFee, hubFeeRate, hubFeeBaseRate = 0.01) { |
| 393 | + // minAmount = hubFeeBase - hubFee |
| 394 | + // But while form.amount increase hubFee increase too, so we need to find such formAmount which will be equal minAmount, it will be maximum minAmount |
| 395 | +
|
| 396 | + // Some 7 grade math below |
| 397 | + // hubFeeBase = (destinationNetworkFee + formAmount) * (0.01 / (1 - 0.01)); |
| 398 | + // hubFee = (destinationNetworkFee + formAmount) * (hubFeeRate / (1 - hubFeeRate)) |
| 399 | + // define (a = hubFeeBaseRate; b = hubFeeRate) |
| 400 | + // minAmount = (destinationNetworkFee + formAmount) * (a / (1 - a)) - (destinationNetworkFee + formAmount) * (b / (1 - b)) |
| 401 | + // minAmount = (destinationNetworkFee + formAmount) * ((a / (1 - a) - (b / (1 - b)); |
| 402 | + // minAmount = (destinationNetworkFee + formAmount) * x; |
| 403 | +
|
| 404 | + // Let's calculate factor x |
| 405 | + // x = a / (1 - a) - b / (1 - b) |
| 406 | + // x = a * (1-b) / ((1-a)*(1-b)) - b * (1-a) / ((1-a)*(1-b)) |
| 407 | + // x = (a * (1-b) - b * (1-a)) / ((1-a)*(1-b)) |
| 408 | + // x = (a - ab - b + ab) / ((1-a)*(1-b)) |
| 409 | + // x = (a - b) / ((1-a)*(1-b)) |
| 410 | + // const factor = (hubFeeBaseRate - hubFeeRate) / ((1 - hubFeeBaseRate) * (1 - hubFeeRate)); |
| 411 | + const factor = new Big(hubFeeBaseRate).minus(hubFeeRate).div(new Big(1).minus(hubFeeBaseRate).times(new Big(1).minus(hubFeeRate))).toString(); |
| 412 | +
|
| 413 | + // We are finding formAmount equal to minAmount (fa = formAmount, dnf = destinationNetworkFee) |
| 414 | + // fa = minAmount |
| 415 | + // fa = (fa + dnf) * x |
| 416 | + // fa = fa * x + dnf * x |
| 417 | + // fa - fa * x = dnf * x |
| 418 | + // fa * 1 - fa * x = dnf * x |
| 419 | + // fa * (1 -x) = dnf * x |
| 420 | + // fa = dnf * x / (1 - x) |
| 421 | + // const minAmount = destinationNetworkFee * factor / (1 - factor); |
| 422 | + const minAmount = new Big(destinationNetworkFee).times(factor).div(new Big(1).minus(factor)).toString(); |
| 423 | + // add 1 pip because 0 will not pass validation too |
| 424 | + return new Big(minAmount).plus(1e-18).toString(); |
| 425 | +} |
378 | 426 | </script> |
379 | 427 |
|
380 | 428 | <template> |
@@ -426,7 +474,7 @@ export default { |
426 | 474 | :max-value="maxAmount" |
427 | 475 | /> |
428 | 476 | <span class="form-field__error" v-if="$v.form.amount.$dirty && !$v.form.amount.required">{{ $td('Enter amount', 'form.amount-error-required') }}</span> |
429 | | - <span class="form-field__error" v-else-if="$v.form.amount.$dirty && (!$v.form.amount.minValue)">{{ $td('Invalid amount', 'form.amount-error-invalid') }}</span> |
| 477 | + <span class="form-field__error" v-else-if="$v.form.amount.$dirty && (!$v.form.amount.minValue)">{{ $td(`Minimum ${minAmount}`, 'form.amount-error-min', {min: minAmount}) }}</span> |
430 | 478 | <span class="form-field__error" v-else-if="$v.form.amount.$dirty && !$v.form.amount.maxValue">{{ $td('Not enough', 'form.amount-error-not-enough') }} {{ form.coin }} ({{ $td('max.', 'hub.max') }} {{ pretty(maxAmount) }})</span> |
431 | 479 | </div> |
432 | 480 | <div class="u-cell u-cell--large--1-2 u-cell--large-down--order-minus"> |
|
0 commit comments