Skip to content

Commit

Permalink
Merge branch 'main' of https://github.com/paltalabs/defindex into fea…
Browse files Browse the repository at this point in the history
…t/workflow
  • Loading branch information
MattPoblete committed Nov 4, 2024
2 parents 52f2fe4 + 241b37c commit fb33649
Show file tree
Hide file tree
Showing 251 changed files with 20,270 additions and 1,551 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ First run the following code in a new terminal located in the repo folder to ini

```sh
docker compose up -d
```
```
then, once the container is up, run:
```sh
bash run.sh
Expand Down
58 changes: 30 additions & 28 deletions apps/contracts/Cargo.lock

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

4 changes: 2 additions & 2 deletions apps/contracts/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[workspace]
members = ["strategies/*", "defindex", "factory"]
exclude = [
"strategies/external_wasms"
"strategies/external_wasms",
]
resolver = "2"

Expand All @@ -12,7 +12,7 @@ license = "GPL-3.0"
repository = "https://github.com/paltalabs/defindex"

[workspace.dependencies]
soroban-sdk = "21.0.1-preview.3"
soroban-sdk = "21.7.6"
soroban-token-sdk = { version = "21.0.1-preview.3" }
# soroswap-library = "0.3.0"
defindex-strategy-core={ path="./strategies/core", package="defindex-strategy-core" }
Expand Down
20 changes: 20 additions & 0 deletions apps/contracts/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,29 @@ to deploy the factory you can use
yarn deploy-factory <network>
```

Make sure you have the hodl strategy deployed, if not, you can run:

```bash
yarn deploy-hodl <network>
```

to test the factory to deploy a DeFindex Vault

```bash
yarn test <network>
```
If you only want to test a specific vault you should modify the contract address in the test file `apps/contracts/src/tests/testOnlyVault.ts` and run the following command:

```bash
yarn test-vault <network>
```

### Generate Docs
```bash
cargo doc --package defindex-strategy-core --package defindex-factory --package defindex-vault --no-deps
```

to publish them, run this to copy all files into /rust_docs
```bash
cp -rf /workspace/apps/contracts/target/doc/* /workspace/apps/rust_docs/
```
66 changes: 66 additions & 0 deletions apps/contracts/defindex/src/aggregator.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
use soroban_sdk::{vec, Address, Env, IntoVal, Symbol, Val, Vec};

use crate::{models::DexDistribution, storage::{get_assets, get_factory}, ContractError};

fn fetch_aggregator_address(e: &Env) -> Address {
let factory_address = get_factory(e);

e.invoke_contract(
&factory_address,
&Symbol::new(&e, "aggregator"),
Vec::new(&e)
)
}

fn is_supported_asset(e: &Env, token: &Address) -> bool {
let assets = get_assets(e);
assets.iter().any(|asset| &asset.address == token)
}

pub fn internal_swap_exact_tokens_for_tokens(e: &Env, token_in: &Address, token_out: &Address, amount_in: &i128, amount_out_min: &i128, distribution: &Vec<DexDistribution>, deadline: &u64) -> Result<(), ContractError> {
let aggregator_address = fetch_aggregator_address(e);

// Check if both tokens are supported by the vault
if !is_supported_asset(e, token_in) || !is_supported_asset(e, token_out) {
return Err(ContractError::UnsupportedAsset);
}

let mut init_args: Vec<Val> = vec![&e];
init_args.push_back(token_in.to_val());
init_args.push_back(token_out.to_val());
init_args.push_back(amount_in.into_val(e));
init_args.push_back(amount_out_min.into_val(e));
init_args.push_back(distribution.into_val(e));
init_args.push_back(e.current_contract_address().to_val());
init_args.push_back(deadline.into_val(e));

e.invoke_contract(
&aggregator_address,
&Symbol::new(&e, "swap_exact_tokens_for_tokens"),
Vec::new(&e)
)
}

pub fn internal_swap_tokens_for_exact_tokens(e: &Env, token_in: &Address, token_out: &Address, amount_out: &i128, amount_in_max: &i128, distribution: &Vec<DexDistribution>, deadline: &u64) -> Result<(), ContractError> {
let aggregator_address = fetch_aggregator_address(e);

// Check if both tokens are supported by the vault
if !is_supported_asset(e, token_in) || !is_supported_asset(e, token_out) {
return Err(ContractError::UnsupportedAsset);
}

let mut init_args: Vec<Val> = vec![&e];
init_args.push_back(token_in.to_val());
init_args.push_back(token_out.to_val());
init_args.push_back(amount_out.into_val(e));
init_args.push_back(amount_in_max.into_val(e));
init_args.push_back(distribution.into_val(e));
init_args.push_back(e.current_contract_address().to_val());
init_args.push_back(deadline.into_val(e));

e.invoke_contract(
&aggregator_address,
&Symbol::new(&e, "swap_tokens_for_exact_tokens"),
Vec::new(&e)
)
}
2 changes: 2 additions & 0 deletions apps/contracts/defindex/src/constants.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
pub(crate) const MAX_BPS: i128 = 10_000;
pub(crate) const SECONDS_PER_YEAR: i128 = 31_536_000;
17 changes: 17 additions & 0 deletions apps/contracts/defindex/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,32 @@ pub enum ContractError {
NotInitialized = 100,
AlreadyInitialized = 101,
InvalidRatio = 102,
StrategyDoesNotSupportAsset=103,

// Validation Errors (11x)
NegativeNotAllowed = 110,
InsufficientBalance = 111,
WrongAmuntsLength = 112,
NotEnoughIdleFunds = 113,
InsufficientManagedFunds = 114,
MissingInstructionData = 115,
UnsupportedAsset = 116,

// Arithmetic Errors (12x)
ArithmeticError = 120,
Overflow = 121,

// Authorization/Role-based Errors (13x)
Unauthorized = 130,
RoleNotFound = 131,

// Strategy Errors (14x)
StrategyNotFound = 140,
StrategyPausedOrNotFound = 141,
StrategyWithdrawError = 142,
StrategyInvestError = 143,

// Asset Errors (15x)
AssetNotFound = 150,
NoAssetsProvided = 151,
}
Loading

0 comments on commit fb33649

Please sign in to comment.