Skip to content

Commit

Permalink
Merge pull request #69 from djeck1432/fix/audit-preparation
Browse files Browse the repository at this point in the history
Tests and contract rework for audit
  • Loading branch information
djeck1432 authored Nov 1, 2024
2 parents 498b811 + 0b7f0af commit 18bd439
Show file tree
Hide file tree
Showing 9 changed files with 803 additions and 344 deletions.
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

0 comments on commit 18bd439

Please sign in to comment.