|
1 |
| -import { add, div, gte, lt, mul } from 'biggystring' |
| 1 | +import { abs, add, div, gte, lt, lte, mul, sub } from 'biggystring' |
2 | 2 | import {
|
3 | 3 | asMaybeInsufficientFundsError,
|
4 | 4 | asMaybeNoAmountSpecifiedError,
|
@@ -110,12 +110,15 @@ interface FioSenderInfo {
|
110 | 110 | skipRecord?: boolean
|
111 | 111 | }
|
112 | 112 |
|
113 |
| -// TODO: For now, do not allow multiple targets to be added via GUI. UX is very poor until |
114 |
| -// animation is added. Waiting for reanimated v3 which fixes crashes in Layout animations. |
115 |
| -// Note: multiple targets can be added via JSON payment protocol to fix payments to Anypay |
116 |
| -// invoices. |
117 | 113 | const ALLOW_MULTIPLE_TARGETS = true
|
118 | 114 |
|
| 115 | +/** |
| 116 | + * If the prior two spend targets of a multi-out payment have the same amount |
| 117 | + * within 0.5%, then use the same amount for the new spend target. |
| 118 | + * This makes it MUCH easier to load many gift cards without having to enter |
| 119 | + * amounts manually. |
| 120 | + */ |
| 121 | +const MULTI_OUT_DIFF_PERCENT = '0.005' |
119 | 122 | const PIN_MAX_LENGTH = 4
|
120 | 123 | const INFINITY_STRING = '999999999999999999999999999999999999999'
|
121 | 124 |
|
@@ -242,6 +245,20 @@ const SendComponent = (props: Props) => {
|
242 | 245 | spendTarget.uniqueIdentifier = parsedUri?.uniqueIdentifier
|
243 | 246 | spendTarget.publicAddress = parsedUri?.publicAddress
|
244 | 247 | spendTarget.nativeAmount = parsedUri?.nativeAmount
|
| 248 | + |
| 249 | + if (spendInfo.spendTargets.length > 2 && spendTarget.nativeAmount == null) { |
| 250 | + // Check if the last two spend targets have the same amount within 0.5% |
| 251 | + const prevAmount = spendInfo.spendTargets[spendInfo.spendTargets.length - 2].nativeAmount |
| 252 | + const pprevAmount = spendInfo.spendTargets[spendInfo.spendTargets.length - 3].nativeAmount |
| 253 | + |
| 254 | + if (prevAmount != null && pprevAmount != null) { |
| 255 | + const diff = abs(sub(prevAmount, pprevAmount)) |
| 256 | + const diffPercent = div(diff, prevAmount, DECIMAL_PRECISION) |
| 257 | + if (lte(diffPercent, MULTI_OUT_DIFF_PERCENT)) { |
| 258 | + spendTarget.nativeAmount = prevAmount |
| 259 | + } |
| 260 | + } |
| 261 | + } |
245 | 262 | spendTarget.otherParams = {
|
246 | 263 | fioAddress
|
247 | 264 | }
|
|
0 commit comments