forked from paco0x/amm-arbitrageur
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix overflow/underflow issue in math calc
Signed-off-by: Penghui Liao <[email protected]>
- Loading branch information
Showing
9 changed files
with
140 additions
and
55 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,4 @@ | ||
{ | ||
"overrides": [ | ||
{ | ||
"files": "*.sol", | ||
"options": { | ||
"printWidth": 120, | ||
"tabWidth": 4, | ||
"useTabs": false, | ||
"singleQuote": true, | ||
"bracketSpacing": false, | ||
"explicitTypes": "always" | ||
} | ||
} | ||
] | ||
"singleQuote": true, | ||
"printWidth": 120 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,81 @@ | ||
import { ethers } from "hardhat"; | ||
import { TestMath } from "../typechain/TestMath"; | ||
|
||
import { expect } from "./shared/expect"; | ||
import lodash from 'lodash'; | ||
import { expect } from 'chai'; | ||
import { ethers } from 'hardhat'; | ||
import { TestMath } from '../typechain/TestMath'; | ||
|
||
const { BigNumber } = ethers; | ||
|
||
describe("MathTest", () => { | ||
describe('MathTest', () => { | ||
let math: TestMath; | ||
|
||
beforeEach(async () => { | ||
const mathTestFactory = await ethers.getContractFactory("TestMath"); | ||
const mathTestFactory = await ethers.getContractFactory('TestMath'); | ||
math = (await mathTestFactory.deploy()) as TestMath; | ||
}); | ||
|
||
it("Should calculate square root corectly", async () => { | ||
const input = BigNumber.from("100"); | ||
const res = await math._sqrt(input); | ||
console.log(res.toString()); | ||
expect(res).to.be.eq(BigNumber.from(10)); | ||
describe('#sqrt', () => { | ||
it('calculate square root correctly with small input', async () => { | ||
const input = BigNumber.from('100'); | ||
const res = await math._sqrt(input); | ||
expect(res).to.be.eq(BigNumber.from(10)); | ||
}); | ||
|
||
it('calculate square root correctly with large input', async () => { | ||
const input = ethers.utils.parseEther('10000'); | ||
const res = await math._sqrt(input); | ||
expect(res).to.be.eq(BigNumber.from('100000000000')); | ||
}); | ||
}); | ||
|
||
describe('#calcSolutionForQuadratic', () => { | ||
it('calculate right solution for quadratic', async () => { | ||
const [a, b, c] = ['59995000000', '120100000000000', '59500000000000000'].map((v) => ethers.utils.parseEther(v)); | ||
const [x1, x2] = await math._calcSolutionForQuadratic(a, b, c); | ||
|
||
expect(x1).to.be.eq(BigNumber.from('-900')); | ||
expect(x2).to.be.eq(BigNumber.from('-1101')); | ||
}); | ||
|
||
it('calculate right solution for quadratic with negative number', async () => { | ||
const [a, b, c] = ['-10000000000', '2200000000000000', '-1000000000000000000'].map((v) => | ||
ethers.utils.parseEther(v) | ||
); | ||
const [x1, x2] = await math._calcSolutionForQuadratic(a, b, c); | ||
|
||
expect(x1).to.be.eq(BigNumber.from('455')); | ||
expect(x2).to.be.eq(BigNumber.from('219544')); | ||
}); | ||
}); | ||
|
||
describe('#calcBorrowAmount', () => { | ||
it('returns right amount with small liquidity pairs', async () => { | ||
const reserves = { a1: '5000', b1: '10', a2: '6000', b2: '10' }; | ||
const input = lodash.mapValues(reserves, (v) => ethers.utils.parseEther(v)); | ||
const res = await math._calcBorrowAmount(input); | ||
// @ts-ignore | ||
expect(res).to.be.closeTo(ethers.utils.parseEther('0.45'), ethers.utils.parseEther('0.1')); | ||
}); | ||
|
||
it('returns right amount with large liquidity pairs', async () => { | ||
const reserves = { a1: '1200000000', b1: '600000', a2: '1000000000', b2: '300000' }; | ||
const input = lodash.mapValues(reserves, (v) => ethers.utils.parseEther(v)); | ||
const res = await math._calcBorrowAmount(input); | ||
// @ts-ignore | ||
expect(res).to.be.closeTo(ethers.utils.parseEther('53052.8604'), ethers.utils.parseEther('1500')); | ||
}); | ||
|
||
it('returns right amount with high difference liquidity pairs', async () => { | ||
const reserves = { a1: '1200000000', b1: '600000', a2: '100000', b2: '30' }; | ||
const input = lodash.mapValues(reserves, (v) => ethers.utils.parseEther(v)); | ||
const res = await math._calcBorrowAmount(input); | ||
// @ts-ignore | ||
expect(res).to.be.closeTo(ethers.utils.parseEther('8.729'), ethers.utils.parseEther('0.5')); | ||
}); | ||
|
||
it('revert with wrong order input', async () => { | ||
const reserves = { a1: '1000000000', b1: '300000', a2: '1200000000', b2: '600000' }; | ||
const input = lodash.mapValues(reserves, (v) => ethers.utils.parseEther(v)); | ||
await expect(math._calcBorrowAmount(input)).to.be.revertedWith('Wrong input order'); | ||
}); | ||
}); | ||
}); |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -626,6 +626,11 @@ | |
resolved "https://registry.npm.taobao.org/@types/chai/download/@types/chai-4.2.16.tgz?cache=0&sync_timestamp=1617390997822&other_urls=https%3A%2F%2Fregistry.npm.taobao.org%2F%40types%2Fchai%2Fdownload%2F%40types%2Fchai-4.2.16.tgz#f09cc36e18d28274f942e7201147cce34d97e8c8" | ||
integrity sha1-8JzDbhjSgnT5QucgEUfM402X6Mg= | ||
|
||
"@types/lodash@^4.14.168": | ||
version "4.14.168" | ||
resolved "https://registry.npm.taobao.org/@types/lodash/download/@types/lodash-4.14.168.tgz#fe24632e79b7ade3f132891afff86caa5e5ce008" | ||
integrity sha1-/iRjLnm3rePxMoka//hsql5c4Ag= | ||
|
||
"@types/lru-cache@^5.1.0": | ||
version "5.1.0" | ||
resolved "https://registry.npm.taobao.org/@types/lru-cache/download/@types/lru-cache-5.1.0.tgz#57f228f2b80c046b4a1bd5cac031f81f207f4f03" | ||
|
@@ -4619,7 +4624,7 @@ [email protected]: | |
resolved "https://registry.npm.taobao.org/lodash/download/lodash-4.17.20.tgz#b44a9b6297bcb698f1c51a3545a2b3b368d59c52" | ||
integrity sha1-tEqbYpe8tpjxxRo1RaKzs2jVnFI= | ||
|
||
lodash@^4.17.11, lodash@^4.17.14, lodash@^4.17.15, lodash@^4.17.4: | ||
lodash@^4.17.11, lodash@^4.17.14, lodash@^4.17.15, lodash@^4.17.21, lodash@^4.17.4: | ||
version "4.17.21" | ||
resolved "https://registry.npm.taobao.org/lodash/download/lodash-4.17.21.tgz#679591c564c3bffaae8454cf0b3df370c3d6911c" | ||
integrity sha1-Z5WRxWTDv/quhFTPCz3zcMPWkRw= | ||
|