Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
154 commits
Select commit Hold shift + click to select a range
76256c4
initial commit
clean-423 Dec 7, 2021
a842bee
clean-423 Dec 7, 2021
bae09eb
clean-423 Dec 7, 2021
db24202
clean-423 Dec 9, 2021
57cdec5
clean-423 Dec 10, 2021
9e7d58f
clean-423 Dec 10, 2021
c185e7d
clean-423 Dec 10, 2021
d307b6a
clean-423 Dec 10, 2021
a060f3a
hardhat files delete
clean-423 Dec 10, 2021
2f9013f
clean-423 Dec 11, 2021
4755400
clean-423 Dec 11, 2021
d59f79f
hardhat compile
clean-423 Dec 11, 2021
8e40545
hardhat compile
clean-423 Dec 11, 2021
bccaefc
hardhat compile
clean-423 Dec 11, 2021
a6d7e8b
clean-423 Dec 11, 2021
8fd9f98
clean-423 Dec 11, 2021
5473472
hardhat compile
clean-423 Dec 11, 2021
0beae2a
clean-423 Dec 12, 2021
5646a2e
factory init code
clean-423 Dec 12, 2021
9e25a49
hardhat
clean-423 Dec 12, 2021
7716e4f
clean-423 Dec 12, 2021
b0adfcd
clean-423 Dec 12, 2021
3fce312
clean-423 Dec 12, 2021
cfaece7
deploy contract
clean-423 Dec 12, 2021
388c004
clean-423 Dec 12, 2021
1ed784d
clean-423 Dec 13, 2021
03723f7
clean-423 Dec 13, 2021
cbd226f
clean-423 Dec 13, 2021
8345690
clean-423 Dec 13, 2021
2bdb8ff
clean-423 Dec 13, 2021
1e3c465
clean-423 Dec 13, 2021
4ded88e
hardhat compile
clean-423 Dec 13, 2021
346385e
clean-423 Dec 15, 2021
d2be10e
clean-423 Dec 15, 2021
76ab719
clean-423 Dec 15, 2021
236ac4b
clean-423 Dec 15, 2021
4cac981
clean-423 Jan 5, 2022
b215f50
Merge branch 'v2' of https://github.com/PulsarSwap/TWAMM-Contracts in…
clean-423 Jan 6, 2022
93e3896
clean-423 Jan 6, 2022
5b398c4
minor
clean-423 Jan 9, 2022
4e609fc
clean-423 Jan 17, 2022
c2c43a8
clean-423 Jan 18, 2022
639af2c
use solmate reentrancy guard
clean-423 Feb 9, 2022
ce2084e
add price oracle
clean-423 Feb 10, 2022
cbc0992
test contracts
clean-423 Feb 17, 2022
bbca138
minor
clean-423 Feb 18, 2022
103a971
swap function changes
clean-423 Feb 20, 2022
1b31cc1
minor
clean-423 Feb 26, 2022
79d5f3c
initial
clean-423 Feb 26, 2022
dfc9272
minor
clean-423 Feb 26, 2022
16b63b8
Merge branch 'v2_development' into main_to_be_merged
clean-423 Feb 28, 2022
096272e
successful compilation after merge
clean-423 Feb 28, 2022
59ee8a9
partially finished test
clean-423 Mar 5, 2022
c61ae88
major parts completed for testing
clean-423 Mar 10, 2022
d972d3a
major compeletion of testing
clean-423 Mar 14, 2022
97666ab
success test after merge
clean-423 Mar 14, 2022
282a6d7
success test after merge
clean-423 Mar 14, 2022
543ed3a
debugging major parts finished
clean-423 Mar 17, 2022
b7c552d
finished contract debugging and testing
clean-423 Mar 21, 2022
fd2a4da
safe check
clean-423 Mar 21, 2022
918007a
clean-423 Mar 21, 2022
c1e6862
contract debugging and testing
clean-423 Mar 21, 2022
4a2912e
contract debugging and testing
clean-423 Mar 22, 2022
b085500
code cleaning
clean-423 Mar 22, 2022
90e80e2
contract debugging and testing
clean-423 Mar 22, 2022
c8fd964
clean-423 Mar 22, 2022
74cd078
contract debugging and testing
clean-423 Mar 22, 2022
451b89d
clean-423 Mar 22, 2022
655ed78
clean-423 Mar 22, 2022
ea4bc76
clean-423 Mar 22, 2022
751ab1a
minor
clean-423 Mar 24, 2022
90a91bc
update weth10 eth conversion fix
clean-423 Mar 25, 2022
4a1aafd
update weth10 eth conversion fix (withdraw part)
clean-423 Mar 25, 2022
d042e6d
debug withdraw functions (pending for test double-check)
clean-423 Mar 25, 2022
4884e9e
minor
clean-423 Mar 25, 2022
065a7de
minor
clean-423 Mar 25, 2022
69d5d76
contracts audit fix
clean-423 Apr 4, 2022
20b774c
fix test file issue with safeTransfer
clean-423 Apr 4, 2022
6bf6bfc
contracts audit fix
clean-423 Apr 4, 2022
7a0cabe
Merge branch 'v2.0.5' of https://github.com/PulsarSwap/TWAMM-Contract…
clean-423 Apr 4, 2022
2e37a54
tmp save
clean-423 Apr 5, 2022
9e68c4b
contracts audit fix
clean-423 Apr 5, 2022
f74d1e4
math check
clean-423 Apr 5, 2022
42f8680
fix audit issue No1
clean-423 Apr 6, 2022
44301df
fix audit issue No6
clean-423 Apr 6, 2022
a3f3154
contracts audit fix
clean-423 Apr 6, 2022
b9ff4b0
audit fix NO1
clean-423 Apr 6, 2022
5332817
contracts audit fix
clean-423 Apr 6, 2022
61d0948
clean-423 Apr 6, 2022
68992a8
clean-423 Apr 6, 2022
27a7432
clean-423 Apr 6, 2022
81c5a58
delet twamm safeerc20
clean-423 Apr 6, 2022
884443f
modify reserve max value
clean-423 Apr 6, 2022
9855b6c
Merge remote-tracking branch 'origin/v2.0.8' into v2.0.7
clean-423 Apr 6, 2022
d833e21
test fix after merge
clean-423 Apr 6, 2022
4d91309
fix No1 audit issue
clean-423 Apr 8, 2022
d5709cc
initial
clean-423 Apr 8, 2022
862a366
initial
clean-423 Apr 8, 2022
076b253
Merge branch 'v2.0.10' of https://github.com/PulsarSwap/TWAMM-Contrac…
clean-423 Apr 8, 2022
398ea87
initial
clean-423 Apr 8, 2022
34185e7
fix weth step 1
clean-423 Apr 8, 2022
ee047b1
Merge remote-tracking branch 'origin/v2.0.7' into v2.0.10
clean-423 Apr 8, 2022
35e238d
merge branch v2.0.7
clean-423 Apr 8, 2022
b632aaf
change caller check to modifier
clean-423 Apr 9, 2022
5c7d53e
make initialize external
clean-423 Apr 10, 2022
6a6642a
optimize executeVirtualOrders loop
clean-423 Apr 10, 2022
4b97268
clean-423 Apr 10, 2022
cd35f81
redundant require statements
clean-423 Apr 10, 2022
487a222
minor
clean-423 Apr 11, 2022
e9a8fda
formatting
clean-423 Apr 11, 2022
32132ec
clean-423 Apr 11, 2022
fcded3a
tmp
clean-423 Apr 11, 2022
18c7da7
partial fix WETH, no allowance issue, but with some testing errors
clean-423 Apr 11, 2022
4c1847f
formatting
clean-423 Apr 11, 2022
c88050d
formatting
clean-423 Apr 11, 2022
1218bb4
minor
clean-423 Apr 11, 2022
1cd9ba2
optimize executeVirtualOrders loop
clean-423 Apr 11, 2022
bfceb1d
optimize executeVirtualOrders loop
clean-423 Apr 11, 2022
a821207
optimize executeVirtualOrders loop
clean-423 Apr 11, 2022
b7204d9
minor
clean-423 Apr 11, 2022
5179fde
weth update
clean-423 Apr 13, 2022
fef5ec4
formatting
clean-423 Apr 13, 2022
4cb7e1c
most updated solved all audit issues
clean-423 Apr 13, 2022
aeb623c
formatting
clean-423 Apr 13, 2022
3801748
minor
clean-423 Apr 13, 2022
a041dd8
Merge remote-tracking branch 'origin/v2.0.7' into v2.0.10
clean-423 Apr 13, 2022
23653eb
contract audit bug fix
clean-423 Apr 17, 2022
4869990
fix comment 2 & 5
clean-423 Apr 18, 2022
69e1b6c
fix comment 1
clean-423 Apr 18, 2022
ad4edf3
fix an important bug for withdraw, cancel and remove liquidity with W…
clean-423 Apr 18, 2022
2769b3f
Merge remote-tracking branch 'origin/v2.0.10' into v2.1.0
clean-423 Apr 18, 2022
dacc00d
fix proceedETH bool value
clean-423 Apr 18, 2022
1af059c
minor
clean-423 Apr 18, 2022
8eeafe3
update solution for out-of-gas handler & code cleaning and formatting
clean-423 Apr 26, 2022
bcc52c1
add require statement for longtermorder.sol
clean-423 Apr 26, 2022
8397b53
minor
clean-423 Apr 26, 2022
31c5737
twamm-test.js code fix
clean-423 May 5, 2022
b1bab58
minor
clean-423 May 5, 2022
45edab5
fix all test bugs (mostly about ETH related balance issue)
clean-423 May 6, 2022
0f3ae61
code cleaning
clean-423 May 6, 2022
d4d14a9
minor updates
clean-423 May 6, 2022
55ba623
minor
clean-423 May 6, 2022
29c7c67
adding a platform fee model
clean-423 May 16, 2022
fa4be77
minor
clean-423 May 16, 2022
40e10f3
minor
clean-423 May 16, 2022
e80e43f
twap oracle example
clean-423 May 26, 2022
d4502d7
minor
clean-423 May 28, 2022
1d13b5f
minor
clean-423 May 28, 2022
37ff447
temp save
clean-423 May 31, 2022
45883ea
term swap version, just need to reduce term swap size
clean-423 May 31, 2022
a374c87
optimizer reduce size
clean-423 May 31, 2022
3d0342d
temp check
clean-423 Jun 2, 2022
90cf562
check
clean-423 Jun 2, 2022
3377327
update
clean-423 Jun 3, 2022
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
6 changes: 6 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
PRIVATE_KEY=
ROPSTEN_URL=https://eth-ropsten.alchemyapi.io/v2/<YOUR ALCHEMY KEY>
INFURA_API_KEY =
ALCHEMY_KEY =
ETHERSCAN_API_KEY=
COINMARKETCAP_API_KEY =
4 changes: 4 additions & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
node_modules
artifacts
cache
coverage
24 changes: 24 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
module.exports = {
env: {
browser: false,
es2021: true,
mocha: true,
node: true,
},
plugins: ["@typescript-eslint"],
extends: [
"standard",
"plugin:prettier/recommended",
"plugin:node/recommended",
],
parser: "@typescript-eslint/parser",
parserOptions: {
ecmaVersion: 12,
},
rules: {
"node/no-unsupported-features/es-syntax": [
"error",
{ ignores: ["modules"] },
],
},
};
10 changes: 5 additions & 5 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@ typechain

node_modules

# Hardhat files
cache
artifacts

# environment
.env

.eslintcache

.DS_Store
.DS_Store

#Hardhat files
cache
artifacts
3 changes: 3 additions & 0 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
hardhat.config.ts
scripts
test
5 changes: 5 additions & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
node_modules
artifacts
cache
coverage*
gasReporterOutput.json
1 change: 1 addition & 0 deletions .prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}
6 changes: 5 additions & 1 deletion .solhint.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
{
"extends": "solhint:default"
"extends": "solhint:recommended",
"rules": {
"compiler-version": ["error", "^0.8.0"],
"func-visibility": ["warn", { "ignoreConstructors": true }]
}
}
1 change: 1 addition & 0 deletions .solhintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
node_modules
39 changes: 24 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,44 +4,53 @@

[![built-with openzeppelin](https://img.shields.io/badge/built%20with-OpenZeppelin-3677FF)](https://docs.openzeppelin.com/)


**Time-Weighted Average Market Maker ([TWAMM](https://www.paradigm.xyz/2021/07/twamm/))** is a new on-chain market making model, designed by [@\_Dave\_\_White\_](https://twitter.com/_Dave__White_), [@danrobinson](https://twitter.com/danrobinson) and [@haydenzadams](https://twitter.com/haydenzadams). TWAMM allows market participants to efficiently execute large orders over multiple blocks on Ethereum.

**Pulsar TWAMM** is the first implementation of TWAMM. The math involved in TWAMM can be found in this article: https://hackmd.io/6buFzQikSGegwrqNNelMYg?both#Mathematical-Principle-Of-TWAMM
**Pulsar TWAMM** is the first implementation of TWAMM. The math involved in TWAMM can be found in this article: <https://hackmd.io/@luffy/SJxSsOH1Y>

## Implementation Notes

### Overview
### Overview

`TWAMM.sol` directly implements most of the standard AMM functionality (liquidity provision, liquidity removal, and swapping). The logic for execution of long term orders is split across two libraries, `OrderPool.sol` and `LongTermOrders.sol`.
`TWAMM.sol` directly implements most of the standard AMM functionality (liquidity provision, liquidity removal, and swapping). The logic for execution of long term orders is split across two libraries, `OrderPool.sol` and `LongTermOrders.sol`.

### Order Pool
### Order Pool

The main abstraction for implementing long term orders is the `Order Pool`. The order pool represents a set of long term orders, which sell a given token to the embedded AMM at a constant rate. The token pool also handles the logic for the distribution of sales proceeds to the owners of the long term orders.
The main abstraction for implementing long term orders is the `Order Pool`. The order pool represents a set of long term orders, which sell a given token to the embedded AMM at a constant rate. The token pool also handles the logic for the distribution of sales proceeds to the owners of the long term orders.

The distribution of rewards is done through a modified version of algorithm from [Scalable Reward Distribution on the Ethereum Blockchain](https://uploads-ssl.webflow.com/5ad71ffeb79acc67c8bcdaba/5ad8d1193a40977462982470_scalable-reward-distribution-paper.pdf). Since order expiries are decopuled from reward distribution in the TWAMM model, the modified algorithm needs to keep track of additional parameters to compute rewards correctly.
The distribution of rewards is done through a modified version of algorithm from [Scalable Reward Distribution on the Ethereum Blockchain](https://uploads-ssl.webflow.com/5ad71ffeb79acc67c8bcdaba/5ad8d1193a40977462982470_scalable-reward-distribution-paper.pdf). Since order expiries are decopuled from reward distribution in the TWAMM model, the modified algorithm needs to keep track of additional parameters to compute rewards correctly.

### Long Term Orders

In addition to the order pools, the `LongTermOrders` struct keep the state of the virtual order execution. Most importantly, it keep track of the last block where virtual orders were executed. Before every interaction with the embedded AMM, the state of virtual order execution is brought forward to the present block. We can do this efficiently because only certain blocks are eligible for virtual order expiry. Thus, we can advance the state by a full block interval in a single computation. Crucially, advancing the state of long term order execution is linear only in the number of block intervals since the last interaction with TWAMM, not linear in the number of orders.
In addition to the order pools, the `LongTermOrders` struct keep the state of the virtual order execution. Most importantly, it keep track of the last block where virtual orders were executed. Before every interaction with the embedded AMM, the state of virtual order execution is brought forward to the present block. We can do this efficiently because only certain blocks are eligible for virtual order expiry. Thus, we can advance the state by a full block interval in a single computation. Crucially, advancing the state of long term order execution is linear only in the number of block intervals since the last interaction with TWAMM, not linear in the number of orders.

### Fixed Point Math

This implementation uses the [PBRMath Library](https://github.com/hifi-finance/prb-math) for fixed point arithmetic, in order to implement the closed form solution to settling long term trades. Efforts were made to make the computation numerically stable, but there's remaining work to be done here in order to ensure that the computation is correct for the full set of expected inputs.
This implementation uses the [PRBMath Library](https://github.com/hifi-finance/prb-math) for fixed point arithmetic, in order to implement the closed form solution to settling long term trades. Efforts were made to make the computation numerically stable, but there's remaining work to be done here in order to ensure that the computation is correct for the full set of expected inputs.

## How to run
## How to run

```bash
# Install dependencies
npm install

# test contracts with hardhat
# Compile contracts with hardhat
npx hardhat compile

# Test contracts with hardhat
npx hardhat test
```
## TWAMM Contract Address

**Mainnet:**
## Contract Address

**Mainnet**

`Factory:`

`TWAMM:`

**Ropsten**

**Ropsten:** [0xbfF3Bce78DE8b5829fE0476692279c57d81aF7e1](https://ropsten.etherscan.io/address/0xbfF3Bce78DE8b5829fE0476692279c57d81aF7e1)
`Factory:`

**Kovan:** [0x6DDFEb84c9e9E9681f2B9AbbD0726d834f0305c6](https://kovan.etherscan.io/address/0x6DDFEb84c9e9E9681f2B9AbbD0726d834f0305c6)
`TWAMM:`

This file was deleted.

Loading