Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
77 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
2efc001
minor
clean-423 Apr 11, 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