Skip to content

Commit

Permalink
Merge remote-tracking branch 'cc/main' into add-u256
Browse files Browse the repository at this point in the history
  • Loading branch information
deuszx committed Sep 14, 2023
2 parents 503aec4 + 2d6fd06 commit d7da368
Show file tree
Hide file tree
Showing 17 changed files with 166 additions and 242 deletions.
1 change: 0 additions & 1 deletion Cargo.lock

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

3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
[workspace]

resolver = "2"

members = ["amm/contracts/**", "farm/contracts"]

exclude = ["e2e-tests", "amm/logics", "helpers"]
18 changes: 9 additions & 9 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -34,20 +34,20 @@ build-node-arm64:
build-node-x86_64:
@docker buildx build --pull --platform linux/amd64 -t aleph-onenode-chain-x86_64 --load docker

UNI_CONTRACTS = ./amm/contracts
UNI_CONTRACTS_PATHS := $(shell find $(UNI_CONTRACTS) -mindepth 1 -maxdepth 1 -type d)
AMM_CONTRACTS = ./amm/contracts
AMM_CONTRACTS_PATHS := $(shell find $(AMM_CONTRACTS) -mindepth 1 -maxdepth 1 -type d)

FARM_CONTRACTS = ./farm
FARM_CONTRACTS_PATHS := $(shell find $(FARM_CONTRACTS) -mindepth 1 -maxdepth 1 -type d)
FARM_CONTRACTS = ./farm/contracts
FARM_PATHS := $(shell find ./farm -mindepth 1 -maxdepth 1 -type d)

.PHONY: build-all
build-all: ## Builds all contracts.
@for d in $(UNI_CONTRACTS_PATHS); do \
@for d in $(AMM_CONTRACTS_PATHS); do \
echo "Building $$d contract" ; \
cargo contract build --quiet --manifest-path $$d/Cargo.toml --release ; \
done
@for d in $(FARM_CONTRACTS_PATHS); do \
echo "Checking $$d" ; \
@for d in $(FARM_CONTRACTS); do \
echo "Building $$d contract" ; \
cargo contract build --quiet --manifest-path $$d/Cargo.toml --release ; \
done

Expand All @@ -56,11 +56,11 @@ check-all: ## Runs cargo checks and unit tests on all contracts.
@cargo check --quiet --all-targets --all-features --all
@cargo clippy --quiet --all-features -- --no-deps -D warnings
@cargo fmt --quiet --all --check
@for d in $(UNI_CONTRACTS_PATHS); do \
@for d in $(AMM_CONTRACTS_PATHS); do \
echo "Checking $$d" ; \
cargo contract check --quiet --manifest-path $$d/Cargo.toml ; \
done
@for d in $(FARM_CONTRACTS_PATHS); do \
@for d in $(FARM_PATHS); do \
echo "Checking $$d" ; \
cargo contract check --quiet --manifest-path $$d/Cargo.toml ; \
done
Expand Down
8 changes: 0 additions & 8 deletions amm/contracts/factory/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ scale-info = { version = "2.3", default-features = false, features = [
pair_contract = { path = "../pair", default-features = false, features = [
"ink-as-dependency",
] }
openbrush = { git = "https://github.com/727-Ventures/openbrush-contracts", tag = "3.1.1", default-features = false }
amm = { path = "../../logics", default-features = false }

[lib]
Expand All @@ -30,14 +29,7 @@ std = [
"ink/std",
"scale/std",
"scale-info/std",
"openbrush/std",
"amm/std",
"pair_contract/std",
]
ink-as-dependency = []

[profile.dev]
overflow-checks = false

[profile.release]
overflow-checks = false
110 changes: 55 additions & 55 deletions amm/contracts/factory/lib.rs
Original file line number Diff line number Diff line change
@@ -1,34 +1,21 @@
#![cfg_attr(not(feature = "std"), no_std, no_main)]
#![feature(min_specialization)]

#[openbrush::contract]
#[ink::contract]
pub mod factory {
use amm::{
ensure,
impls::factory::{
factory::{
only_fee_setter,
Internal,
},
*,
helpers::ZERO_ADDRESS,
traits::factory::{
Factory,
FactoryError,
},
traits::factory::*,
};
use ink::{
codegen::{
EmitEvent,
Env,
},
codegen::EmitEvent,
env::hash::Blake2x256,
storage::Mapping,
ToAccountId,
};
use openbrush::{
modifiers,
traits::{
AccountIdExt,
Storage,
},
};
use pair_contract::pair::PairContractRef;

#[ink(event)]
Expand All @@ -42,26 +29,29 @@ pub mod factory {
}

#[ink(storage)]
#[derive(Default, Storage)]
pub struct FactoryContract {
#[storage_field]
factory: data::Data,
get_pair: Mapping<(AccountId, AccountId), AccountId>,
all_pairs: Mapping<u64, AccountId>,
all_pairs_length: u64,
pair_contract_code_hash: Hash,
fee_to: AccountId,
fee_to_setter: AccountId,
}

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

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

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

#[ink(message)]
Expand All @@ -76,22 +66,19 @@ pub mod factory {
} else {
(token_b, token_a)
};
ensure!(!token_pair.0.is_zero(), FactoryError::ZeroAddress);
ensure!(
self.factory.get_pair.get(&token_pair).is_none(),
self.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.get_pair
.insert((token_pair.0, token_pair.1), &pair_contract);
self.get_pair
.insert((token_pair.1, token_pair.0), &pair_contract);

self._add_new_pair(pair_contract);

Expand All @@ -105,44 +92,59 @@ pub mod factory {
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;
self._only_fee_setter()?;
self.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;
self._only_fee_setter()?;
self.fee_to_setter = fee_to_setter;
Ok(())
}

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

#[ink(message)]
fn fee_to_setter(&self) -> AccountId {
self.factory.fee_to_setter
self.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))
self.get_pair.get((token_a, token_b))
}
}

impl factory::Internal for FactoryContract {
impl FactoryContract {
#[ink(constructor)]
pub fn new(fee_to_setter: AccountId, pair_code_hash: Hash) -> Self {
let mut instance = Self {
get_pair: Default::default(),
all_pairs: Default::default(),
all_pairs_length: 0,
pair_contract_code_hash: Default::default(),
fee_to: ZERO_ADDRESS.into(),
fee_to_setter: ZERO_ADDRESS.into(),
};
instance.pair_contract_code_hash = pair_code_hash;
instance.fee_to_setter = fee_to_setter;
instance
}

fn _instantiate_pair(
&mut self,
salt_bytes: &[u8],
token_0: AccountId,
token_1: AccountId,
) -> Result<AccountId, FactoryError> {
let pair_hash = self.factory.pair_contract_code_hash;
let pair_hash = self.pair_contract_code_hash;
let pair = match PairContractRef::new(token_0, token_1)
.endowment(0)
.code_hash(pair_hash)
Expand Down Expand Up @@ -174,21 +176,19 @@ pub mod factory {
}

fn _add_new_pair(&mut self, pair: AccountId) {
let pair_len = self.factory.all_pairs_length;
self.factory.all_pairs.insert(&pair_len, &pair);
self.factory.all_pairs_length += 1;
let pair_len = self.all_pairs_length;
self.all_pairs.insert(pair_len, &pair);
self.all_pairs_length += 1;
}
}

impl FactoryContract {
#[ink(constructor)]
pub fn new(fee_to_setter: AccountId, pair_code_hash: Hash) -> Self {
let mut instance = Self::default();
instance.factory.pair_contract_code_hash = pair_code_hash;
instance.factory.fee_to_setter = fee_to_setter;
instance
fn _only_fee_setter(&self) -> Result<(), FactoryError> {
if self.env().caller() != self.fee_to_setter {
return Err(FactoryError::CallerIsNotFeeSetter)
}
Ok(())
}
}

#[cfg(test)]
mod tests {
use ink::{
Expand All @@ -202,7 +202,7 @@ pub mod factory {
fn initialize_works() {
let accounts = default_accounts::<ink::env::DefaultEnvironment>();
let factory = FactoryContract::new(accounts.alice, Hash::default());
assert_eq!(factory.factory.fee_to, amm::helpers::ZERO_ADDRESS.into());
assert_eq!(factory.fee_to, amm::helpers::ZERO_ADDRESS.into());
}
}
}
6 changes: 0 additions & 6 deletions amm/contracts/pair/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,3 @@ std = [
"amm-helpers/std",
]
ink-as-dependency = []

[profile.dev]
overflow-checks = false

[profile.release]
overflow-checks = false
6 changes: 0 additions & 6 deletions amm/contracts/router/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,3 @@ std = [
"amm/std",
]
ink-as-dependency = []

[profile.dev]
overflow-checks = false

[profile.release]
overflow-checks = false
6 changes: 0 additions & 6 deletions amm/contracts/wnative/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,3 @@ std = [
"amm/std",
]
ink-as-dependency = []

[profile.dev]
overflow-checks = false

[profile.release]
overflow-checks = false
8 changes: 6 additions & 2 deletions amm/logics/helpers/helper.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
use crate::traits::{
factory::FactoryRef,
factory::{
Factory,
FactoryRef,
},
pair::PairRef,
};
use amm_helpers::math::casted_mul;
Expand Down Expand Up @@ -47,7 +50,8 @@ pub fn pair_for_on_chain(
token_a: AccountId,
token_b: AccountId,
) -> Option<AccountId> {
FactoryRef::get_pair(factory, token_a, token_b)
let factory_ref: FactoryRef = (*factory).into();
factory_ref.get_pair(token_a, token_b)
}

/// Returns balances of token reserves for particular `Factory` instance.
Expand Down
33 changes: 0 additions & 33 deletions amm/logics/impls/factory/data.rs

This file was deleted.

Loading

0 comments on commit d7da368

Please sign in to comment.