Skip to content

Commit

Permalink
Merge remote-tracking branch 'cc/main' into farming
Browse files Browse the repository at this point in the history
  • Loading branch information
deuszx committed Sep 4, 2023
2 parents bae4750 + 41935cb commit b708499
Show file tree
Hide file tree
Showing 21 changed files with 1,095 additions and 851 deletions.
4 changes: 3 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ e2e-tests: ## Runs all the e2e tests in sequence.
.PHONY: build-and-wrap-all
build-and-wrap-all: build-all wrap-all ## Builds all contracts and generates code for contract interaction.

INK_DEV_IMAGE = public.ecr.aws/p6e8q1z1/ink-dev:1.6.0
INK_DEV_IMAGE = public.ecr.aws/p6e8q1z1/ink-dev:1.7.0

.PHONY: check-all-dockerized
check-all-dockerized: ## Runs cargo checks and unit tests on all contracts in a container.
Expand Down
10 changes: 7 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
# DEX - UniswapV2
# Common AMM

This folder contains the line by line implementation of [uniswap-v2 core](https://github.com/Uniswap/v2-core) and [uniswap-v2 periphery](https://github.com/Uniswap/v2-periphery) with its tests.
This repository contains implementations of AMM DEXes written for Common product.

There will be multiple AMM models implemented, each being the most suitable model for a certain token pair (stablecoin pairs being created in a CFM based on Curve StableSwap, PSP22 token pair on UniswapV2, etc.)

Currently, this repository contains the line by line implementation of [uniswap-v2 core](https://github.com/Uniswap/v2-core) and [uniswap-v2 periphery](https://github.com/Uniswap/v2-periphery). Code was adapted to match Subatrate platform and ink! language.

### Purpose

Expand Down Expand Up @@ -78,7 +82,7 @@ First start your local node. You can do that by running `make start-node` in the

To deploy contracts, execute `npm run deploy:local` in the root directory.

To create sample tokens and register them as pairs in the DEX, run `npm run example`.
To create sample tokens and register them as pairs in the DEX, run `npm run example:local`.

Note that this requires rebuilding TypeScript wrappers first: `npm run compile:release`.

Expand Down
2 changes: 1 addition & 1 deletion docker/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM public.ecr.aws/p6e8q1z1/aleph-node:r-10.0
FROM public.ecr.aws/p6e8q1z1/aleph-node:r-11.4

# 1. Save node and sudo account ids for convenience
# 2. Prepare the chainspec
Expand Down
1 change: 1 addition & 0 deletions e2e-tests/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
[package]
name = "e2e-tests"
version = "0.1.0"
authors = ["Cardinal Cryptography"]
edition = "2021"

[dependencies]
Expand Down
2 changes: 1 addition & 1 deletion rust-toolchain.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[toolchain]
channel = "nightly-2023-01-10"
channel = "nightly-2023-08-10"
components = ["rustfmt", "rust-src", "clippy"]
targets = ["wasm32-unknown-unknown"]
profile = "minimal"
2 changes: 1 addition & 1 deletion uniswap-v2/contracts/factory/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]
name = "factory_contract"
version = "0.1.0"
authors = ["Stake Technologies <[email protected]>"]
authors = ["Cardinal Cryptography"]
edition = "2021"

[dependencies]
Expand Down
105 changes: 102 additions & 3 deletions uniswap-v2/contracts/factory/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,26 @@ pub mod factory {
EmitEvent,
Env,
},
env::hash::Blake2x256,
ToAccountId,
};
use openbrush::traits::Storage;
use openbrush::{
modifiers,
traits::{
AccountIdExt,
Storage,
},
};
use pair_contract::pair::PairContractRef;
use uniswap_v2::{
impls::factory::*,
ensure,
impls::factory::{
factory::{
only_fee_setter,
Internal,
},
*,
},
traits::factory::*,
};

Expand All @@ -34,7 +48,92 @@ pub mod factory {
factory: data::Data,
}

impl Factory for FactoryContract {}
impl Factory for FactoryContract {
#[ink(message)]
fn all_pairs(&self, pid: u64) -> Option<AccountId> {
self.factory.all_pairs.get(&pid)
}

#[ink(message)]
fn all_pairs_length(&self) -> u64 {
self.factory.all_pairs_length
}

#[ink(message)]
fn pair_contract_code_hash(&self) -> Hash {
self.factory.pair_contract_code_hash
}

#[ink(message)]
fn create_pair(
&mut self,
token_a: AccountId,
token_b: AccountId,
) -> Result<AccountId, FactoryError> {
ensure!(token_a != token_b, FactoryError::IdenticalAddresses);
let token_pair = if token_a < token_b {
(token_a, token_b)
} else {
(token_b, token_a)
};
ensure!(!token_pair.0.is_zero(), FactoryError::ZeroAddress);
ensure!(
self.factory.get_pair.get(&token_pair).is_none(),
FactoryError::PairExists
);

let salt = self.env().hash_encoded::<Blake2x256, _>(&token_pair);
let pair_contract =
self._instantiate_pair(salt.as_ref(), token_pair.0, token_pair.1)?;

self.factory
.get_pair
.insert(&(token_pair.0, token_pair.1), &pair_contract);
self.factory
.get_pair
.insert(&(token_pair.1, token_pair.0), &pair_contract);

self._add_new_pair(pair_contract);

self._emit_create_pair_event(
token_pair.0,
token_pair.1,
pair_contract,
self.all_pairs_length(),
);

Ok(pair_contract)
}

#[modifiers(only_fee_setter)]
#[ink(message)]
fn set_fee_to(&mut self, fee_to: AccountId) -> Result<(), FactoryError> {
self.factory.fee_to = fee_to;
Ok(())
}

#[modifiers(only_fee_setter)]
#[ink(message)]
fn set_fee_to_setter(&mut self, fee_to_setter: AccountId) -> Result<(), FactoryError> {
self.factory.fee_to_setter = fee_to_setter;
Ok(())
}

#[ink(message)]
fn fee_to(&self) -> AccountId {
self.factory.fee_to
}

#[ink(message)]
fn fee_to_setter(&self) -> AccountId {
self.factory.fee_to_setter
}

#[ink(message)]
fn get_pair(&self, token_a: AccountId, token_b: AccountId) -> Option<AccountId> {
self.factory.get_pair.get(&(token_a, token_b))
}
}

impl factory::Internal for FactoryContract {
fn _instantiate_pair(
Expand Down
6 changes: 5 additions & 1 deletion uniswap-v2/contracts/pair/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]
name = "pair_contract"
version = "0.1.0"
authors = ["Stake Technologies <[email protected]>"]
authors = ["Cardinal Cryptography"]
edition = "2021"

[dependencies]
Expand All @@ -19,6 +19,10 @@ openbrush = { git = "https://github.com/727-Ventures/openbrush-contracts", tag =
"reentrancy_guard",
] }
uniswap_v2 = { path = "../../logics", default-features = false }
primitive-types = { version = "0.11.1", default-features = false, features = [
"codec",
] }
sp-arithmetic = { git = "https://github.com/paritytech/substrate", default-features = false, branch = "polkadot-v0.9.37" }

[lib]
name = "pair_contract"
Expand Down
Loading

0 comments on commit b708499

Please sign in to comment.