Skip to content

zkp2p-v2-contracts: Initial support for BRL on Mercado Pago #34

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 24 additions & 4 deletions contracts/verifiers/MercadoPagoReclaimVerifier.sol
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ contract MercadoPagoReclaimVerifier is IPaymentVerifier, BaseReclaimPaymentVerif

using StringConversionUtils for string;
using Bytes32ConversionUtils for bytes32;
using { StringConversionUtils.substring } for string;

/* ============ Structs ============ */

Expand Down Expand Up @@ -141,7 +142,7 @@ contract MercadoPagoReclaimVerifier is IPaymentVerifier, BaseReclaimPaymentVerif
uint256 expectedAmount = _verifyPaymentData.intentAmount * _verifyPaymentData.conversionRate / PRECISE_UNIT;
uint8 decimals = IERC20Metadata(_verifyPaymentData.depositToken).decimals();

uint256 paymentAmount = _parseAmount(paymentDetails.amountString, paymentDetails.amountCentsString, decimals);
uint256 paymentAmount = _parseAmount(paymentDetails.amountString, paymentDetails.amountCentsString, paymentDetails.currencyCode, decimals);
require(paymentAmount >= expectedAmount, "Incorrect payment amount");

if (_isAppclipProof) {
Expand Down Expand Up @@ -218,12 +219,31 @@ contract MercadoPagoReclaimVerifier is IPaymentVerifier, BaseReclaimPaymentVerif
* @param _amountCents The cents amount to parse.
* @param _decimals The decimals of the token.
*/
function _parseAmount(string memory _amount, string memory _amountCents, uint8 _decimals) internal pure returns(uint256) {
uint256 baseAmount = _amount.stringToUint(
0x2C, // period character, which is the decimal character for ARS amounts
function _parseAmount(string memory _amount, string memory _amountCents, string memory _currencyCode, uint8 _decimals) internal pure returns(uint256) {
// Check if the amount is negative (starts with '-')
bytes memory amountBytes = bytes(_amount);
bool isNegative = amountBytes.length > 0 && amountBytes[0] == 0x2D; // '-' character

// Different currencies may represent outgoing payments differently
// BRL uses negative values for outgoing payments
// ARS uses positive values for outgoing payments
bool isBRL = keccak256(abi.encodePacked(_currencyCode)) == keccak256(abi.encodePacked("BRL"));

// If negative and it's BRL, remove the '-' sign for parsing
// For other currencies, use the value as is
string memory amountToProcess;
if (isNegative && isBRL) {
amountToProcess = substring(_amount, 1, amountBytes.length);
} else {
amountToProcess = _amount;
}

uint256 baseAmount = amountToProcess.stringToUint(
0x2C, // comma character, which is the decimal character for both ARS and BRL amounts
_decimals
);
uint256 centsAmount = _amountCents.stringToUint(_decimals - 2);

return baseAmount + centsAmount;
}
}
3 changes: 2 additions & 1 deletion deployments/verifiers/mercado_pago_reclaim.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ export const getMercadoReclaimProviderHashes = async (length: number) => {
export const MERCADO_APPCLIP_PROVIDER_HASHES = []

export const MERCADO_RECLAIM_CURRENCIES: any = [
Currency.ARS
Currency.ARS,
Currency.BRL
];

export const MERCADO_RECLAIM_TIMESTAMP_BUFFER = BigNumber.from(30); // 30 seconds
Expand Down
1 change: 1 addition & 0 deletions utils/protocolUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ export const Currency = {
AED: getKeccak256Hash("AED"),
ARS: getKeccak256Hash("ARS"),
AUD: getKeccak256Hash("AUD"),
BRL: getKeccak256Hash("BRL"),
CAD: getKeccak256Hash("CAD"),
CHF: getKeccak256Hash("CHF"),
CNY: getKeccak256Hash("CNY"),
Expand Down