@mozaic-fi/intent-swapper-sdk-common
Version:
Intent Swapper Common SDK
35 lines • 1.61 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.calcBps = calcBps;
exports.validateBps = validateBps;
exports.calcFee = calcFee;
exports.reverseAmountWithFee = reverseAmountWithFee;
const constants_1 = require("../constants");
const ethers_1 = require("ethers");
function calcBps(numerator, denominator) {
return ethers_1.BigNumber.from(numerator).mul(constants_1.BPS_BASE).div(denominator);
}
function validateBps(bps) {
bps = ethers_1.BigNumber.from(bps);
return bps.gt(0) && bps.lte(constants_1.BPS_BASE);
}
// Aave v2: floor
// - https://github.com/aave/protocol-v2/blob/master/contracts/protocol/lendingpool/LendingPool.sol#L483
// - https://github.com/aave/protocol-v2/blob/master/contracts/protocol/lendingpool/LendingPool.sol#L504
// Aave v3: round
// - https://github.com/aave/aave-v3-core/blob/v1.19.1/contracts/protocol/libraries/logic/FlashLoanLogic.sol#L70
// - https://github.com/aave/aave-v3-core/blob/v1.19.1/contracts/protocol/libraries/logic/FlashLoanLogic.sol#L94
// - https://github.com/aave/aave-v3-core/blob/v1.19.1/contracts/protocol/libraries/math/PercentageMath.sol#L9
function calcFee(amountWei, feeBps, mode = 'floor') {
let fee = ethers_1.BigNumber.from(amountWei).mul(feeBps);
if (mode === 'round') {
fee = fee.add(constants_1.BPS_BASE / 2);
}
return fee.div(constants_1.BPS_BASE);
}
function reverseAmountWithFee(amountWithFeeWei, feeBps) {
return ethers_1.BigNumber.from(amountWithFeeWei)
.mul(constants_1.BPS_BASE)
.div(constants_1.BPS_BASE + feeBps);
}
//# sourceMappingURL=bps.js.map