Skip to content

Commit

Permalink
Merge branch 'master' into create-collection-tests
Browse files Browse the repository at this point in the history
  • Loading branch information
zielvna committed Nov 28, 2023
2 parents aefed14 + 6b9d763 commit a643748
Show file tree
Hide file tree
Showing 7 changed files with 532 additions and 182 deletions.
89 changes: 89 additions & 0 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
name: Unit and e2e tests

on:
push:
branches:
- master
pull_request:
branches:
- master

jobs:
checks:
name: Run clippy and unit tests
runs-on: ubuntu-latest

steps:
- name: Checkout source code
uses: actions/checkout@v3

- name: Install Rust toolchain
uses: Cardinal-Cryptography/github-actions/install-rust-toolchain@v1
# - name: Install Rust toolchain
# run: |
# rustup component add rust-src

- name: Run cargo fmt
uses: actions-rs/cargo@v1
with:
command: fmt
args: --all -- --check

- name: Run clippy
uses: actions-rs/cargo@v1
with:
command: clippy
# args: --all-targets -- --no-deps -D warnings (TODO: fix clippy warnings first)

# sudo apt update && apt upgrade -y
- name: Install Protobuf
run: |
sudo apt-get install -y protobuf-compiler libprotobuf-dev
- name: Add installing required dependency
run: |
cargo install cargo-dylint dylint-link
- name: Install substrate-contracts-node
uses: actions-rs/cargo@v1
with:
command: install
args: --force --locked contracts-node

# TODO: cache substrate-contracts-node OR downlaod binary from github release
# args: --locked substrate-contracts-node

- name: Run unit tests
uses: actions-rs/cargo@v1
with:
command: test
args: --features e2e-tests

build:
name: Build the contract
runs-on: ubuntu-latest

steps:
- name: Checkout source code
uses: actions/checkout@v3

- name: Cache Crates
uses: actions/cache@v3
with:
path: ~/.cargo
key: ${{ runner.os }}-contract-build-env-${{ hashFiles('rust-toolchain.toml') }}

- name: Install Rust toolchain
uses: Cardinal-Cryptography/github-actions/install-rust-toolchain@v1

- name: Install cargo contract
uses: actions-rs/cargo@v1
with:
command: install
args: --locked cargo-contract

- name: Build contract
uses: actions-rs/cargo@v1
with:
command: contract
args: build --release
5 changes: 5 additions & 0 deletions rust-toolchain.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[toolchain]
channel = "1.72.1"
components = ["rustfmt", "rust-src", "clippy"]
targets = ["wasm32-unknown-unknown"]
profile = "minimal"
132 changes: 132 additions & 0 deletions src/contracts/entrypoints.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
use crate::{
contract::{CalculateSwapResult, Hop},
contracts::{FeeTier, FeeTierKey, Pool, PoolKey, Position, Tick},
math::{
liquidity::Liquidity, percentage::Percentage, sqrt_price::sqrt_price::SqrtPrice,
token_amount::TokenAmount,
},
InvariantError,
};
use alloc::vec::Vec;
use ink::primitives::AccountId;

#[ink::trait_definition]
pub trait Invariant {
#[ink(message)]
fn get_protocol_fee(&self) -> Percentage;

#[ink(message)]
fn withdraw_protocol_fee(&mut self, pool_key: PoolKey) -> Result<(), InvariantError>;

#[ink(message)]
fn change_protocol_fee(&mut self, protocol_fee: Percentage) -> Result<(), InvariantError>;

#[ink(message)]
fn change_fee_receiver(
&mut self,
pool_key: PoolKey,
fee_receiver: AccountId,
) -> Result<(), InvariantError>;

#[ink(message)]
fn create_position(
&mut self,
pool_key: PoolKey,
lower_tick: i32,
upper_tick: i32,
liquidity_delta: Liquidity,
slippage_limit_lower: SqrtPrice,
slippage_limit_upper: SqrtPrice,
) -> Result<Position, InvariantError>;

#[ink(message)]
fn swap(
&mut self,
pool_key: PoolKey,
x_to_y: bool,
amount: TokenAmount,
by_amount_in: bool,
sqrt_price_limit: SqrtPrice,
) -> Result<CalculateSwapResult, InvariantError>;

#[ink(message)]
fn swap_route(
&mut self,
amount_in: TokenAmount,
expected_amount_out: TokenAmount,
slippage: Percentage,
swaps: Vec<Hop>,
) -> Result<(), InvariantError>;

#[ink(message)]
fn quote(
&self,
pool_key: PoolKey,
x_to_y: bool,
amount: TokenAmount,
by_amount_in: bool,
sqrt_price_limit: SqrtPrice,
) -> Result<(TokenAmount, TokenAmount, SqrtPrice, Vec<Tick>), InvariantError>;

#[ink(message)]
fn quote_route(
&mut self,
amount_in: TokenAmount,
swaps: Vec<Hop>,
) -> Result<TokenAmount, InvariantError>;

#[ink(message)]
fn transfer_position(&mut self, index: u32, receiver: AccountId) -> Result<(), InvariantError>;

#[ink(message)]
fn get_position(&mut self, index: u32) -> Result<Position, InvariantError>;

#[ink(message)]
fn get_all_positions(&mut self) -> Vec<Position>;

#[ink(message)]
fn update_position_seconds_per_liquidity(
&mut self,
index: u32,
pool_key: PoolKey,
) -> Result<(), InvariantError>;

#[ink(message)]
fn claim_fee(&mut self, index: u32) -> Result<(TokenAmount, TokenAmount), InvariantError>;

#[ink(message)]
fn remove_position(&mut self, index: u32)
-> Result<(TokenAmount, TokenAmount), InvariantError>;

#[ink(message)]
fn add_fee_tier(&mut self, fee_tier: FeeTier) -> Result<(), InvariantError>;

#[ink(message)]
fn get_fee_tier(&self, key: FeeTierKey) -> Option<()>;

#[ink(message)]
fn remove_fee_tier(&mut self, key: FeeTierKey);

#[ink(message)]
fn create_pool(
&mut self,
token_0: AccountId,
token_1: AccountId,
fee_tier: FeeTier,
init_tick: i32,
) -> Result<(), InvariantError>;

#[ink(message)]
fn get_pool(
&self,
token_0: AccountId,
token_1: AccountId,
fee_tier: FeeTier,
) -> Result<Pool, InvariantError>;

#[ink(message)]
fn get_tick(&self, key: PoolKey, index: i32) -> Result<Tick, InvariantError>;

#[ink(message)]
fn get_tickmap_bit(&self, key: PoolKey, index: i32) -> bool;
}
3 changes: 3 additions & 0 deletions src/contracts/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
pub mod collections;
pub mod entrypoints;
pub mod logic;
pub mod storage;

pub use collections::*;
pub use entrypoints::*;
pub use logic::*;
pub use storage::*;
13 changes: 7 additions & 6 deletions src/contracts/storage/tickmap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -190,9 +190,10 @@ impl Tickmap {
match closes_tick_index {
Some(index) => {
let sqrt_price = calculate_sqrt_price(index).unwrap();
if x_to_y && sqrt_price > sqrt_price_limit {
(sqrt_price, Some((index, true)))
} else if !x_to_y && sqrt_price < sqrt_price_limit {

if (x_to_y && sqrt_price > sqrt_price_limit)
|| (!x_to_y && sqrt_price < sqrt_price_limit)
{
(sqrt_price, Some((index, true)))
} else {
(sqrt_price_limit, None)
Expand All @@ -204,9 +205,9 @@ impl Tickmap {

assert!(current_tick != index, "LimitReached");

if x_to_y && sqrt_price > sqrt_price_limit {
(sqrt_price, Some((index, false)))
} else if !x_to_y && sqrt_price < sqrt_price_limit {
if (x_to_y && sqrt_price > sqrt_price_limit)
|| (!x_to_y && sqrt_price < sqrt_price_limit)
{
(sqrt_price, Some((index, false)))
} else {
(sqrt_price_limit, None)
Expand Down
Loading

0 comments on commit a643748

Please sign in to comment.