Skip to content

Commit

Permalink
Merge pull request #37 from diwu1989/uniswapV2Fix
Browse files Browse the repository at this point in the history
express uniswap v2 fee in bips
  • Loading branch information
zviadm authored Jun 20, 2022
2 parents 0f43b12 + 9ed68bb commit 04d1328
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 9 deletions.
13 changes: 7 additions & 6 deletions contracts/swappa/PairUniswapV2.sol
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,11 @@ contract PairUniswapV2 is ISwappaPairV1 {

function parseData(bytes memory data) private pure returns (address pairAddr, uint fee) {
require(data.length == 21, "PairUniswapV2: invalid data!");
fee = uint(1000).sub(uint8(data[20]));
assembly {
pairAddr := mload(add(data, 20))
}
// fee in bips
fee = uint(10000).sub(uint8(data[20]));
assembly {
pairAddr := mload(add(data, 20))
}
}

function getOutputAmount(
Expand All @@ -55,9 +56,9 @@ contract PairUniswapV2 is ISwappaPairV1 {
function getAmountOut(uint amountIn, uint reserveIn, uint reserveOut, uint feeK) internal pure returns (uint amountOut) {
uint amountInWithFee = amountIn.mul(feeK);
uint numerator = amountInWithFee.mul(reserveOut);
uint denominator = reserveIn.mul(1000).add(amountInWithFee);
uint denominator = reserveIn.mul(10000).add(amountInWithFee);
amountOut = numerator / denominator;
}

receive() external payable {}
}
}
7 changes: 4 additions & 3 deletions src/pairs/uniswapv2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,11 @@ export class PairUniswapV2 extends PairXYeqK {
private pairAddr: Address,
private fixedFee: BigNumber = new BigNumber(0.997),
) {
super(selectAddress(chainId, {mainnet: pairUniswapV2Address}))
super(selectAddress(chainId, { mainnet: pairUniswapV2Address }))
this.pair = new this.web3.eth.Contract(PairABI, pairAddr) as unknown as IUniswapV2Pair
const feeKInv = new BigNumber(1000).minus(this.fixedFee.multipliedBy(1000))
if (!feeKInv.isInteger() || !feeKInv.gt(0) || !feeKInv.lt(100)) {
const feeKInv = new BigNumber(10000).minus(this.fixedFee.multipliedBy(10000))
if (!feeKInv.isInteger() || !feeKInv.gt(0) || !feeKInv.lt(256)) {
// feeKInv must fit into uint8
throw new Error(`Invalid fixedFee: ${this.fixedFee}!`)
}
this.feeKData = feeKInv.toString(16).padStart(2, "0")
Expand Down

0 comments on commit 04d1328

Please sign in to comment.