Skip to content

Commit

Permalink
Verify a USDC swap's targetAmount is not too low
Browse files Browse the repository at this point in the history
Must be >= 99% of the input `amount`.
  • Loading branch information
sisou committed Feb 22, 2024
1 parent f520440 commit 6062a25
Showing 1 changed file with 13 additions and 0 deletions.
13 changes: 13 additions & 0 deletions src/request/sign-polygon-transaction/SignPolygonTransactionApi.js
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,19 @@ class SignPolygonTransactionApi extends PolygonRequestParserMixin(TopLevelApi) {
if (!['swap', 'swapWithApproval'].includes(description.name)) {
throw new Errors.InvalidRequestError('Requested Polygon contract method is invalid');
}

// Ensure swap `targetAmount` is not too low
const inputAmount = /** @type {PolygonSwapDescription | PolygonSwapWithApprovalDescription} */ (description)
.args
.amount;
const targetAmount = /** @type {PolygonSwapDescription | PolygonSwapWithApprovalDescription} */ (description) // eslint-disable-line max-len
.args
.targetAmount;
if (targetAmount.lt(inputAmount.mul(99).div(100))) {
throw new Errors.InvalidRequestError(
'Requested Polygon swap `targetAmount` more than 1% lower than the input `amount`',
);
}
} else {
throw new Errors.InvalidRequestError('request.to address is not allowed');
}
Expand Down

0 comments on commit 6062a25

Please sign in to comment.