Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tests and contract rework for audit #69

Merged
merged 45 commits into from
Nov 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
45 commits
Select commit Hold shift + click to select a range
b3a9e01
Swap method moved to internal trait
faurdent Oct 20, 2024
d994326
Docstrings added, swap removed from public interface
faurdent Oct 20, 2024
97e9c24
New assertions
faurdent Oct 21, 2024
f69b44c
Pragma added for testing
faurdent Oct 22, 2024
7349a40
More validations for contract stability. Owner check reworked
faurdent Oct 22, 2024
3660f96
First fuzz test for looping
faurdent Oct 22, 2024
bb8c80b
New tests for liquidity loop
faurdent Oct 23, 2024
f331bce
Dependencies updated
faurdent Oct 23, 2024
8fd1a54
Use input amount computing on Ekubo to not loose token on last swap
faurdent Oct 23, 2024
81c9172
Closing position after time passed test case
faurdent Oct 23, 2024
ef1f2f3
Type aliases added
faurdent Oct 24, 2024
704c8d7
Testing amounts. Investigating liquidation.
faurdent Oct 24, 2024
b72e56a
dashboard-API-fixing
AndriiBogomolov Oct 17, 2024
28ae2cc
dashboard backendUrl update
AndriiBogomolov Oct 17, 2024
138ca25
feat/added plug to dashboard
AndriiBogomolov Oct 18, 2024
25c58e4
added missing response models
UzNaZ Oct 20, 2024
de7c7a0
fixed alembic
UzNaZ Oct 20, 2024
619a174
Created tests direcotry in web_app, created test_user.py file inside …
vitaliypopel Oct 18, 2024
8bbd9bd
Fixed draft tests
vitaliypopel Oct 18, 2024
cf45f81
Created pytest.ini file
vitaliypopel Oct 18, 2024
b2cd1c4
Fixed draft tests
vitaliypopel Oct 18, 2024
8423f7d
Removed print debuger
vitaliypopel Oct 18, 2024
ff59022
Created working tests
vitaliypopel Oct 18, 2024
437deb9
Added pytest, pytest-asyncio and httpx
vitaliypopel Oct 18, 2024
f446cd0
Replaced response.status_code == 200 to response.is_success
vitaliypopel Oct 20, 2024
ff16906
merge with audit-preparation
SoarinSkySagar Oct 26, 2024
7765235
dependency error fix
SoarinSkySagar Oct 26, 2024
d05c6b6
fix conflicts
SoarinSkySagar Oct 26, 2024
c9ab4c3
fix conflict
SoarinSkySagar Oct 26, 2024
b2db2a7
fix
SoarinSkySagar Oct 26, 2024
9c5f9dd
fix conflict
SoarinSkySagar Oct 26, 2024
06c1ed4
merge conflict
SoarinSkySagar Oct 26, 2024
0b68f30
added tests
SoarinSkySagar Oct 27, 2024
a4efff1
minor changes
SoarinSkySagar Oct 27, 2024
7db97d3
additional assert to check reward amount
SoarinSkySagar Oct 28, 2024
ad02dd9
requested changes
SoarinSkySagar Oct 28, 2024
c9cedfd
scarb fmt
SoarinSkySagar Oct 28, 2024
351caf8
Merge pull request #118 from SoarinSkySagar/sync
djeck1432 Oct 29, 2024
8b45606
Ekubo slippage parameter added
faurdent Oct 29, 2024
9dce53a
Break added to not add debt
faurdent Oct 29, 2024
412326e
Docs updated
faurdent Oct 29, 2024
2fd5ee3
Proper types set. Imports optimized
faurdent Oct 29, 2024
d691ec5
Add explanation on owner check in docs
faurdent Oct 29, 2024
259e497
Renamings, docs changes
faurdent Oct 30, 2024
0b7f0af
Borrow const moved to Deposit Data. Docs and tests updated
faurdent Oct 31, 2024
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
14 changes: 10 additions & 4 deletions Scarb.lock
Original file line number Diff line number Diff line change
Expand Up @@ -64,15 +64,20 @@ version = "0.17.0"
source = "registry+https://scarbs.xyz/"
checksum = "sha256:36d93e353f42fd6b824abcd8b4b51c3f5d02c893c5f886ae81403b0368aa5fde"

[[package]]
name = "pragma_lib"
version = "1.0.0"
source = "git+https://github.com/astraly-labs/pragma-lib?tag=2.8.2#86d7ccdc15b349b8b48d9796fc8464c947bea6e1"

[[package]]
name = "snforge_scarb_plugin"
version = "0.1.0"
source = "git+https://github.com/foundry-rs/starknet-foundry.git?tag=v0.30.0#196f06b251926697c3d66800f2a93ae595e76496"
version = "0.32.0"
source = "git+https://github.com/foundry-rs/starknet-foundry.git?tag=v0.32.0#3817c903b640201c72e743b9bbe70a97149828a2"

[[package]]
name = "snforge_std"
version = "0.30.0"
source = "git+https://github.com/foundry-rs/starknet-foundry.git?tag=v0.30.0#196f06b251926697c3d66800f2a93ae595e76496"
version = "0.32.0"
source = "git+https://github.com/foundry-rs/starknet-foundry.git?tag=v0.32.0#3817c903b640201c72e743b9bbe70a97149828a2"
dependencies = [
"snforge_scarb_plugin",
]
Expand All @@ -84,5 +89,6 @@ dependencies = [
"alexandria_math",
"ekubo",
"openzeppelin_token",
"pragma_lib",
"snforge_std",
]
4 changes: 2 additions & 2 deletions Scarb.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ alexandria_math = { git = "https://github.com/keep-starknet-strange/alexandria.g
openzeppelin_token = "0.17.0"

[dev-dependencies]
snforge_std = { git = "https://github.com/foundry-rs/starknet-foundry.git", tag = "v0.30.0" }
pragma_lib = { git = "https://github.com/astraly-labs/pragma-lib", tag = "2.8.2" }
snforge_std = { git = "https://github.com/foundry-rs/starknet-foundry.git", tag = "v0.32.0" }

[lib]

Expand All @@ -29,6 +30,5 @@ block_id.tag = "latest"

[[tool.snforge.fork]]
name = "MAINNET"
# url = "https://starknet-mainnet.public.blastapi.io/rpc/v0_7"
url = "http://127.0.0.1:5050"
block_id.tag = "latest"
63 changes: 46 additions & 17 deletions docs/spotnet.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
# Spotnet functionality documentation

## Overview
Spotnet is a dApp designed for increasing of initial collateral deposit deposit by utilizing lending
Spotnet is a dApp designed for increasing of initial collateral deposit by utilizing lending
protocols and AMMs for borrowing tokens, swapping them and redepositing up to x4 of starting capital(as for now).

## Spotnet Smart Contract
Smart contract consists of a constructor and two methods for public use.
Smart contract consists of a constructor and three methods for public use.

### Constructor
Constructor of our contract receives three parameters(ekubo_core and zk_market are serialized to Dispatcher types for starknet::interfaces):
Expand All @@ -16,10 +16,12 @@ Onwer address is an account address of the user who deploys it, only owner can d

### loop_liquidity
The `loop_liquidity` method is responsible for deposit of collateral token. For now only one position can be opened, to create the new you need to close old one.
For validating the identity of the caller is used an address of account initiated a transaction instead of a caller for testing purposes.
This method has next parameters:
* `deposit_data`: DepositData - Object of internal type which stores main deposit information.
* `pool_key`: PoolKey - Ekubo type for obtaining info about the pool and swapping tokens.
* `pool_price`: felt252 - Price of `deposit` token in terms of `debt` token in Ekubo pool(so for ex. 2400000000 USDC for ETH when depositing ETH).
* `ekubo_limits`: EkuboSlippageLimits - Object of internal type which represents upper and lower sqrt_ratio values on Ekubo. Used to control slippage while swapping.
* `pool_price`: felt252 - Price of `deposit` token in terms of `debt` token(so for ex. 2400000000 USDC for ETH when depositing ETH).

It's flow can be described as follows:

Expand Down Expand Up @@ -48,13 +50,15 @@ emit event
```

### close_position
The `close_position` method is responsible for repaying debts and withdrawing all tokens from zklend. Can be called only if there is active position.
The `close_position` method is responsible for repaying debts and withdrawing all tokens from zklend. Can be called only if there is active position. For validating the
identity of the caller is used an address of account initiated a transaction instead of a caller for testing purposes.
The method has next parameters:
* `supply_token`: ContractAddress - Address of the token used as collateral.
* `debt_token`: ContractAddress - Address of the token used as borrowing.
* `pool_key`: PoolKey - Ekubo type for obtaining info about the pool and swapping tokens.
* `supply_price`: felt252 - Price of `supply` token in terms of `debt` token in Ekubo pool(so for ex. 2400000000 USDC for ETH).
* `debt_price`: felt252 - Price of `debt` token in terms of `supply` token in Ekubo pool(for ex. 410000000000000 ETH for USDC).
* `ekubo_limits`: EkuboSlippageLimits - Object of internal type which represents upper and lower sqrt_ratio values on Ekubo. Used to control slippage while swapping.
* `supply_price`: TokenPrice - Price of `supply` token in terms of `debt` token(so for ex. 2400000000 USDC for ETH).
* `debt_price`: TokenPrice - Price of `debt` token in terms of `supply` token(for ex. 410000000000000 ETH for USDC).

It's flow can be described as follows:
```
Expand All @@ -71,41 +75,66 @@ while debt != 0 {

repay
}
swap back extra debt token
transfer tokens to user

disable collateral token

emit event
```

### claim_reward
The `claim_reward` method claims airdrop reward accumulated by the contract that deposited into zkLend. Claim only possible if position is currently open.
This method has next parameters:
* `claim_data`: Claim - contains data about claim operation
* `proof`: Span<felt252> - proof used to validate the claim
* `airdrop_addr`: ContractAddress - address of a contract responsible for claim

Ir's flow can be described as follow
```
assertions

airdrop claim

transfer half of reward to the treasury
```

## Important types, events and constants
### Types
#### DepositData
The main data about the loop to perform. The `amount` * `multiplier` value is minimal amount that will be deposited after the loop.
The `borrow_const` sets how much tokens will be borrowed from available amount(borrowing power). So if there is available 1 ETH to borrow and we passed 60%, it will borrow 0.6 ETH.
This will work up to 99% of available amount, howewer, for stability against slippage and prices difference on zkLend and our source it's better to not go higher than 98%.

```
struct DepositData {
token: ContractAddress,
amount: u256,
multiplier: u32
amount: TokenAmount,
multiplier: u32,
borrow_const: u8
}
```

#### EkuboSlippageLimits
This structure sets slippage limits for swapping on Ekubo. Maximal are `6277100250585753475930931601400621808602321654880405518632` for `upper` and `18446748437148339061` for `lower` as stated by [Ekubo docs](https://docs.ekubo.org/integration-guides/reference/error-codes#limit_mag).

### Events
```
struct LiquidityLooped {
initial_amount: u256,
deposited: u256,
initial_amount: TokenAmount,
deposited: TokenAmount,
token_deposit: ContractAddress,
borrowed: u256,
borrowed: TokenAmount,
token_borrowed: ContractAddress
}
```
```
struct PositionClosed {
deposit_token: ContractAddress,
debt_token: ContractAddress,
withdrawn_amount: u256,
repaid_amount: u256
withdrawn_amount: TokenAmount,
repaid_amount: TokenAmount
}
```

### Constants
* For swaps the EKUBO_LOWER_SQRT_LIMIT and EKUBO_UPPER_SQRT_LIMIT constants used. They define how much price can be moved by swappng. For now it is set to limiting values (`18446748437148339061` and `6277100250585753475930931601400621808602321654880405518632` respectively).

* ZK_SCALE_DECIMALS is used for scaling down values obtained by multiplying on zklend collateral and borrow factor.
* ZK_SCALE_DECIMALS is used for scaling down values obtained by multiplying on zklend collateral and borrow factors.
Loading
Loading