Skip to content

Commit

Permalink
Merge pull request #26 from fluxus-finance/develop
Browse files Browse the repository at this point in the history
merge develop to main
  • Loading branch information
pmarangone authored Aug 10, 2022
2 parents 93e8280 + c73711c commit b493aa3
Show file tree
Hide file tree
Showing 40 changed files with 3,620 additions and 1,260 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
target/**
Cargo.lock
neardev/**
.env
1 change: 1 addition & 0 deletions fluxus-safe/.env.example
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,4 @@ export token_id=":$pool_id"
export seed_min_deposit="1000000000000000000"
export croncat_manager="manager_v1.croncat.testnet"
export total_gas=300000000000000
export treasure_contract_id=dev-1656420526638-61041719201929
1 change: 1 addition & 0 deletions fluxus-safe/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ borsh = "0.9"
maplit = "1.0"
near-units = "0.1.0"
# arbitrary_precision enabled for u128 types that workspaces requires for Balance types
serde = { version = "1", features = ["derive"] }
serde_json = { version = "1.0", features = ["arbitrary_precision"] }
tracing = "0.1"
tracing-subscriber = { version = "0.3.5", features = ["env-filter"] }
5 changes: 5 additions & 0 deletions fluxus-safe/Makefile
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# e2e test
test:
export RUST_BACKTRACE=full
./build.sh
cargo test --test '*' -- --nocapture

unit_test:
export RUST_BACKTRACE=full
./build.sh
cargo test --lib -- --nocapture
2 changes: 1 addition & 1 deletion fluxus-safe/build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,5 @@ fi

RUSTFLAGS='-C link-arg=-s' cargo build --target wasm32-unknown-unknown --release

cp target/wasm32-unknown-unknown/release/auto_compounder.wasm res/
cp ../target/wasm32-unknown-unknown/release/fluxus_safe.wasm res/

Binary file removed fluxus-safe/res/auto_compounder.wasm
Binary file not shown.
Binary file added fluxus-safe/res/fluxus_safe.wasm
Binary file not shown.
22 changes: 17 additions & 5 deletions fluxus-safe/scripts/add_strategy.sh
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,30 @@ echo $reward_token
#### Create first strategy
near call $CONTRACT_NAME create_strategy '{
"_strategy": "",
"protocol_fee": 10,
"strategy_fee": 5,
"strat_creator": { "account_id": "'$username'", "fee_percentage": 5, "current_amount" : 0 },
"sentry_fee": 10,
"token1_address": "'$token1_address'",
"token2_address": "'$token2_address'",
"pool_id_token1_reward": '$pool_id_token1_reward',
"pool_id_token2_reward": '$pool_id_token2_reward',
"reward_token": "'$reward_token'",
"farm": "'$farm_id'",
"pool_id": '$pool_id',
"seed_min_deposit": "1000000000000000000"
}' --accountId $CONTRACT_NAME --gas $total_gas

near call $CONTRACT_NAME add_farm_to_strategy '{
"pool_id": '$pool_id',
"pool_id_token1_reward": '$pool_id_token1_reward',
"pool_id_token2_reward": '$pool_id_token2_reward',
"reward_token": "'$reward_token'",
"farm_id": "'$farm_id'"
}' --accountId $CONTRACT_NAME --gas $total_gas

# Register the contract in the pool
#### TODO: move this call to create_auto_compounder method
near call $exchange_contract_id mft_register '{ "token_id" : ":'$pool_id'", "account_id": "'$CONTRACT_NAME'" }' --accountId $CONTRACT_NAME --deposit 1

#At reward token
near call $reward_token storage_deposit '{"account_id": "'$CONTRACT_NAME'", "registration_only": false}' --accountId $CONTRACT_NAME --gas 300000000000000 --deposit 0.00125

# Register reward_token in the exchange in the contracts account whitelisted tokens
# only necessary for tokens that arent registered in the exchange already
near call $exchange_contract_id register_tokens '{ "token_ids" : [ "'$reward_token'" ] }' --accountId $CONTRACT_NAME --gas 300000000000000 --depositYocto 1
11 changes: 4 additions & 7 deletions fluxus-safe/scripts/auto_compound.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,7 @@ source .env


#### Functions managed by auto-compound
# near call $CONTRACT_NAME claim_reward '{"token_id": "'$token_id'"}' --accountId $CONTRACT_NAME --gas $total_gas

# near call $CONTRACT_NAME withdraw_of_reward '{"token_id": "'$token_id'"}' --accountId $CONTRACT_NAME --gas $total_gas

# near call $CONTRACT_NAME autocompounds_swap '{"token_id": "'$token_id'"}' --accountId $CONTRACT_NAME --gas $total_gas

# near call $CONTRACT_NAME autocompounds_liquidity_and_stake '{"token_id": "'$token_id'"}' --accountId $CONTRACT_NAME --gas $total_gas
near call $CONTRACT_NAME harvest '{"farm_id_str": "'$farm_id_str'"}' --accountId $username --gas $total_gas
near call $CONTRACT_NAME harvest '{"farm_id_str": "'$farm_id_str'"}' --accountId $username --gas $total_gas
near call $CONTRACT_NAME harvest '{"farm_id_str": "'$farm_id_str'"}' --accountId $username --gas $total_gas
near call $CONTRACT_NAME harvest '{"farm_id_str": "'$farm_id_str'"}' --accountId $username --gas $total_gas
3 changes: 1 addition & 2 deletions fluxus-safe/scripts/build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,4 @@ fi

RUSTFLAGS='-C link-arg=-s' cargo build --target wasm32-unknown-unknown --release

cp ../target/wasm32-unknown-unknown/release/auto_compounder.wasm ../res/

cp ../../target/wasm32-unknown-unknown/release/fluxus_safe.wasm ../res/
5 changes: 5 additions & 0 deletions fluxus-safe/scripts/deploy.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# build wasm
./build.sh

#Deploy with near-dev
near dev-deploy --wasmFile ../res/fluxus_safe.wasm
7 changes: 0 additions & 7 deletions fluxus-safe/scripts/initialize.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ source .env
echo $username
echo $reward_token


#### Initialize contract
near call $CONTRACT_NAME new '{ "owner_id":"'$username'", "exchange_contract_id": "'$exchange_contract_id'",
"farm_contract_id": "'$farm_contract_id'", "treasure_contract_id": "'$treasure_contract_id'" }' --accountId $CONTRACT_NAME
Expand All @@ -18,10 +17,4 @@ near call $CONTRACT_NAME call_user_register '{"account_id": "'$CONTRACT_NAME'"}'
#At the farm
near call $farm_contract_id storage_deposit '{"account_id": "'$CONTRACT_NAME'", "registration_only": false}' --accountId $CONTRACT_NAME --deposit 0.1

#At reward token
near call $reward_token storage_deposit '{"account_id": "'$CONTRACT_NAME'", "registration_only": false}' --accountId $CONTRACT_NAME --gas 300000000000000 --deposit 0.00125

# Register reward_token in the exchange in the contracts account whitelisted tokens
# only necessary for tokens that arent registered in the exchange already
near call $exchange_contract_id register_tokens '{ "token_ids" : [ "'$reward_token'" ] }' --accountId $CONTRACT_NAME --gas 300000000000000 --depositYocto 1

10 changes: 10 additions & 0 deletions fluxus-safe/scripts/mft_test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
source .env

source neardev/dev-account.env
echo $CONTRACT_NAME


near call $CONTRACT_NAME register_seed '{"fft_share" : "seed1"}' --accountId $CONTRACT_NAME
near call $CONTRACT_NAME mft_mint '{"fft_share" : "seed1", "balance":100, "user": "'$username'"}' --accountId $CONTRACT_NAME
near call $CONTRACT_NAME mft_burn '{"fft_share" : "seed1", "balance":1, "user": '$username'}' --accountId $CONTRACT_NAME
near call $CONTRACT_NAME users_fft_share_amount '{"fft_share" : "seed1", "user": '$username'}' --accountId $CONTRACT_NAME
8 changes: 4 additions & 4 deletions fluxus-safe/scripts/run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
./build.sh

#Deploy with near-dev
near dev-deploy --wasmFile ../res/auto_compounder.wasm
near dev-deploy --wasmFile ../res/fluxus_safe.wasm

source neardev/dev-account.env
echo $CONTRACT_NAME
Expand All @@ -16,13 +16,13 @@ echo $username
#### initializes the contract, create strategy and registers in the necessary contracts
./initialize.sh

#### create strategy from .env
# #### create strategy from .env
./add_strategy.sh

#### storage_deposit + wrap_near + stake
# #### storage_deposit + wrap_near + stake
./stake_process.sh

#### unstake_and_remove_liquidity + withdraw_to_contract
# ./unstake_process.sh



7 changes: 5 additions & 2 deletions fluxus-safe/scripts/stake_process.sh
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,10 @@ echo $username
near call $exchange_contract_id mft_transfer_call '{"token_id": ":'$pool_id'", "receiver_id": "'$CONTRACT_NAME'", "amount": "1000000000000000000", "msg": "" }' --accountId $username --gas $total_gas --depositYocto 1

# ### Should have the previous amount plus the user shares
near view $farm_contract_id list_user_seeds '{ "account_id": "'$CONTRACT_NAME'" }'
# old farm contract
# near view $farm_contract_id list_user_seeds '{ "account_id": "'$CONTRACT_NAME'" }'
# boost farm contract
near view $farm_contract_id list_farmer_seeds '{ "farmer_id": "'$CONTRACT_NAME'" }'

# ### Should be the same amount as passed in mft_transfer_call
near view $CONTRACT_NAME get_user_shares '{ "account_id": "'$username'", "token_id": "'$token_id'" }'
near view $CONTRACT_NAME user_share_seed_id '{ "seed_id": "'$seed_id'", "user": "'$username'" }'
8 changes: 4 additions & 4 deletions fluxus-safe/scripts/unstake_process.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,22 @@ source .env
echo $username

# #### User shares on auto-compouder contract
near view $CONTRACT_NAME get_user_shares '{ "account_id": "'$username'", "token_id": "'$token_id'" }'
near view $CONTRACT_NAME user_share_seed_id '{ "seed_id": "'$seed_id'", "user": "'$username'" }'

### Auto-compoter staked shares
near view $farm_contract_id list_user_seeds '{ "account_id": "'$CONTRACT_NAME'" }'

#### Unstake the total amount available
near call $CONTRACT_NAME unstake '{ "token_id": ":'$pool_id'" }' --accountId $username --gas 300000000000000
# near call $CONTRACT_NAME unstake '{ "token_id": ":'$pool_id'" }' --accountId $username --gas 300000000000000

#### Unstake given amount from contract
# near call $CONTRACT_NAME unstake '{ "token_id": ":'$pool_id'", "amount_withdrawal": "1005611400372449400" }' --accountId $username --gas 300000000000000
near call $CONTRACT_NAME unstake '{ "token_id": ":'$pool_id'", "amount_withdrawal": "500000000000000000" }' --accountId $username --gas 300000000000000

#### Shoud have the contract shares minus the user shares
near view $farm_contract_id list_user_seeds '{ "account_id": "'$CONTRACT_NAME'" }'

### Should be 0 after successful unstake
near view $CONTRACT_NAME get_user_shares '{ "account_id": "'$username'", "token_id": "'$token_id'" }'
near view $CONTRACT_NAME user_share_seed_id '{ "seed_id": "'$seed_id'", "user": "'$username'" }'

### Should have the previous shares on the auto-compounder contract
near view $exchange_contract_id get_pool_shares '{ "pool_id": '$pool_id', "account_id" : "'$username'" }'
Expand Down
97 changes: 86 additions & 11 deletions fluxus-safe/scripts/views.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,39 +5,114 @@ source .env
source neardev/dev-account.env
echo $CONTRACT_NAME

######## Farm contract

#### Get farm state, Running, Ended, etc.
######## Old farm contract

#### Get all farms from given seed
# near view $farm_contract_id list_farms_by_seed '{ "seed_id": "'$exchange_contract_id'@'$pool_id'" }'

### Get farm state, Running, Ended, etc.
# near view $farm_contract_id get_farm '{ "farm_id": "'$exchange_contract_id'@'$pool_id'#'$farm_id'" }'

#### Get min deposit for seed
# near view $farm_contract_id get_seed_info '{ "seed_id": "'$exchange_contract_id'@'$pool_id'" }'
### Get min deposit for seed
# near view $farm_contract_id list_seeds_info '{ "seed_id": "'$exchange_contract_id'@'$pool_id'" }'

#### Get unclaimed reward
# near view $farm_contract_id get_unclaimed_reward '{ "account_id": "'$CONTRACT_NAME'", "farm_id": "'$exchange_contract_id'@'$pool_id'#'$farm_id'" }'

######## Boost farm contract
# near view $farm_contract_id list_seed_farms '{ "seed_id": "'$exchange_contract_id'@'$pool_id'" }'
# near view $farm_contract_id list_farmer_seeds '{ "farmer_id": "'$CONTRACT_NAME'" }'
# near view $farm_contract_id list_farmer_rewards '{ "farmer_id": "'$CONTRACT_NAME'" }'
# near view $farm_contract_id get_unclaimed_rewards '{ "farmer_id": "'$CONTRACT_NAME'", "seed_id": "'$seed_id'" }'
# near view $farm_contract_id list_seeds_info '{ "from_index": 0, "limit": 300 }'

######## Safe contract

#### Get the current contract state
near view $CONTRACT_NAME get_contract_state
# 'contract_id is Running'

#### Get exchange and farm contracts
near view $CONTRACT_NAME get_contract_info
# {
# exchange_address: 'ref-finance-101.testnet',
# farm_address: 'boostfarm.ref-finance.testnet'
# }

#### Get tokens_id, :10, for strategies that are running
### Get tokens_id, :10, for strategies that are running
near view $CONTRACT_NAME get_allowed_tokens '{}'
# [ ':50' ]

#### Get all strats infos, such as state, token_id, reward_token, min_deposit
near view $CONTRACT_NAME get_strats '{}'
near view $CONTRACT_NAME get_strategies '{}'
# [
# {
# token_id: ':50',
# is_active: true,
# reward_tokens: [ 'skyward.fakes.testnet' ]
# }
# ]

#### Get state from strat, if its Running, Ended, etc.
near view $CONTRACT_NAME get_strat_state '{"token_id": "'$token_id'" }'
near view $CONTRACT_NAME get_strat_state '{"farm_id_str": "'$farm_id_str'" }'
# 'Running'

#### Returns struct from user {deposited: x, total: y}
near view $CONTRACT_NAME get_user_shares '{ "account_id": "'$username'", "token_id": ":'$pool_id'" }'
#### Returns number of shares the user has for given seed_id
near view $CONTRACT_NAME user_share_seed_id '{ "seed_id": "'$seed_id'", "user": "'$username'" }'
# 1000000000000000000

#### Get guardians
near view $CONTRACT_NAME get_guardians '{}'
# []

#### Get total amount staked on contract
near view $CONTRACT_NAME get_contract_amount '{}'
# '0'

#### Returns the total number of strategies/farms in this contract
near view $CONTRACT_NAME number_of_strategies '{}'
# 1

#### Returns the total staked for given seed
near view $CONTRACT_NAME seed_total_amount '{ "token_id": "'$token_id'" }'
# 1000000000000000000

#### Get fee percentage
near view $CONTRACT_NAME check_fee_by_strategy '{ "token_id": "'$token_id'" }'
# '5%'

#### Returns true/false for given strategy
near view $CONTRACT_NAME is_strategy_active '{ "token_id": "'$token_id'" }'

#### Returns strat step, ['claim_reward, 'withdraw', 'swap', 'stake']
near view $CONTRACT_NAME current_strat_step '{ "farm_id_str": "'$farm_id_str'" }'
# 'claim_reward'

#### Returns farm ids from given token_id
near view $CONTRACT_NAME get_farm_ids_by_seed '{ "token_id": "'$token_id'" }'
# [ ':50#1' ]

#### Returns the timestamp of the last harvest for given token_id, '0' if it never occurred
near view $CONTRACT_NAME get_harvest_timestamp '{ "token_id": "'$token_id'" }'
# 1659470914303

#### Returns all infos for all strategies
near view $CONTRACT_NAME get_strategies_info '{}'
# [
# {
# state: 'Running',
# cycle_stage: 'ClaimReward',
# slippage: 99,
# last_reward_amount: 0,
# last_fee_amount: 0,
# pool_id_token1_reward: 9999,
# pool_id_token2_reward: 50,
# reward_token: 'skyward.fakes.testnet',
# available_balance: [ 0, 0 ],
# id: '1'
# }
# ]

# #### Get total amount staked on contract
near view $CONTRACT_NAME get_contract_amount '{}'
near view $CONTRACT_NAME get_strategy_kind '{}'
# 'AUTO_COMPOUNDER'
44 changes: 22 additions & 22 deletions fluxus-safe/src/account_deposit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -362,24 +362,24 @@ impl Contract {
self.internal_save_account(&account_id, account);
}

pub(crate) fn internal_register_account_sub(
&mut self,
account_id: &AccountId,
amount: Balance,
) {
let mut account = self.internal_unwrap_or_default_account(&account_id);
log!(
"account.near_amount is = {} and amount = {}",
account.near_amount,
amount
);
account.near_amount -= amount;
log!(
"the new balance after subtracting = {}",
account.near_amount
);
self.internal_save_account(&account_id, account);
}
// pub(crate) fn internal_register_account_sub(
// &mut self,
// account_id: &AccountId,
// amount: Balance,
// ) {
// let mut account = self.internal_unwrap_or_default_account(&account_id);
// log!(
// "account.near_amount is = {} and amount = {}",
// account.near_amount,
// amount
// );
// account.near_amount -= amount;
// log!(
// "the new balance after subtracting = {}",
// account.near_amount
// );
// self.internal_save_account(&account_id, account);
// }

/// storage withdraw
pub(crate) fn internal_storage_withdraw(
Expand Down Expand Up @@ -571,15 +571,15 @@ mod tests {
// assert no storage is available until near is added
assert_eq!(account.storage_available(), 0u128);
account.deposit_with_storage_check(&to_account_id("uxu.near"), 10u128);
let uxu_balance: Option<Balance> = account.get_balance(&to_account_id("uxu.near"));
assert_eq!(uxu_balance.unwrap_or(0u128), 0u128);
let fft_balance: Option<Balance> = account.get_balance(&to_account_id("uxu.near"));
assert_eq!(fft_balance.unwrap_or(0u128), 0u128);

account.near_amount = 100000000000000000000000u128;
assert_ne!(account.storage_available(), 0u128);

// deposit token with balance
account.deposit_with_storage_check(&to_account_id("uxu.near"), 10u128);
let uxu_balance: Option<Balance> = account.get_balance(&to_account_id("uxu.near"));
assert_eq!(uxu_balance.unwrap_or(1u128), 10u128);
let fft_balance: Option<Balance> = account.get_balance(&to_account_id("uxu.near"));
assert_eq!(fft_balance.unwrap_or(1u128), 10u128);
}
}
Loading

0 comments on commit b493aa3

Please sign in to comment.