Skip to content

Commit

Permalink
[apps] Add dex integration test
Browse files Browse the repository at this point in the history
  • Loading branch information
jolestar committed Jan 21, 2025
1 parent d722771 commit 3123432
Show file tree
Hide file tree
Showing 4 changed files with 100 additions and 5 deletions.
11 changes: 10 additions & 1 deletion apps/rooch_dex/sources/liquidity_incentive.move
Original file line number Diff line number Diff line change
Expand Up @@ -141,12 +141,20 @@ module rooch_dex::liquidity_incentive {
gain: u128,
}

/// Create a new farming asset pool
/// The pool will start to release reward token after start_time(seconds)
public entry fun create_pool<X: key+store, Y: key+store, RewardToken: key+store>(
account: &signer,
release_per_second: u128,
coin_amount: u256,
start_time: u64,
start_time_in_seconds: u64,
){
let now = now_seconds();
let start_time = if (start_time_in_seconds > now) {
start_time_in_seconds
}else {
now
};
let reward_coin = account_coin_store::withdraw<RewardToken>(account, coin_amount);
create_pool_with_coin<X, Y, RewardToken>(account, release_per_second, reward_coin, start_time)
}
Expand All @@ -156,6 +164,7 @@ module rooch_dex::liquidity_incentive {
farming_asset_obj: &mut Object<FarmingAsset<X, Y, RewardToken>>,
coin_amount: u256,
){

let reward_coin = account_coin_store::withdraw<RewardToken>(account, coin_amount);
let farming_asset = object::borrow_mut(farming_asset_obj);
coin_store::deposit(&mut farming_asset.coin_store, reward_coin);
Expand Down
4 changes: 2 additions & 2 deletions apps/rooch_dex/sources/swap.move
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ module rooch_dex::swap {

let coin_info = coin::register_extend<LPToken<X, Y>>(
lp_name,
string::utf8(b"RDex-LP"),
string::utf8(b"RDexLP"),
none(),
8,
);
Expand Down Expand Up @@ -651,7 +651,7 @@ module rooch_dex::swap {
public fun init_lp_for_test(amount: u256) : Coin<LPToken<TestCoinX, TestCoinY>> {
let coin_info = coin::register_extend<LPToken<TestCoinX, TestCoinY>>(
string::utf8(b"RoochDex LPs"),
string::utf8(b"RDex-LP"),
string::utf8(b"RDexLP"),
none(),
8,
);
Expand Down
86 changes: 86 additions & 0 deletions crates/testsuite/features/dex.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
Feature: RoochDAO Apps contract tests


@serial
Scenario: rooch_dex
Given a server for rooch_dex

Then cmd: "account create --json"
Then cmd: "account create --json"
Then cmd: "account list --json"

Then cmd: "move run --sender {{$.account[2].account0.address}} --function rooch_framework::gas_coin::faucet_entry --args u256:1000000000000000 --json"
Then assert: "{{$.move[-1].execution_info.status.type}} == executed"

Then cmd: "move run --sender {{$.account[2].account1.address}} --function rooch_framework::gas_coin::faucet_entry --args u256:1000000000000000 --json"
Then assert: "{{$.move[-1].execution_info.status.type}} == executed"

# publish rooch_dex via default address
Then cmd: "move publish -p ../../apps/app_admin --named-addresses app_admin=default --json"
Then assert: "{{$.move[-1].execution_info.status.type}} == executed"
Then cmd: "move publish -p ../../apps/rooch_dex --named-addresses app_admin=default,rooch_dex=default --json"
Then assert: "{{$.move[-1].execution_info.status.type}} == executed"

# publish examples coins via default address
Then cmd: "move publish -p ../../examples/coins --named-addresses coins=default --json"
Then assert: "{{$.move[-1].execution_info.status.type}} == executed"

Then cmd: "object -t default::admin::AdminCap"
Then cmd: "object -t default::fixed_supply_coin::Treasury"

#Get some test coins
Then cmd: "move run --function default::fixed_supply_coin::faucet --args object:{{$.object[1].data[0].id}} --json"
Then assert: "{{$.move[-1].execution_info.status.type}} == executed"

# Create toke pair
Then cmd: "move run --function default::router::create_token_pair --type-args 0x3::gas_coin::RGas --type-args default::fixed_supply_coin::FSC --args u64:100000000000 --args u64:100000000000 --args u64:0 --args u64:0 --json"
Then assert: "{{$.move[-1].execution_info.status.type}} == executed"

Then cmd: "object -t '0x3::coin::CoinInfo<default::swap::LPToken<0x3::gas_coin::RGas,default::fixed_supply_coin::FSC>>'"

#Add add_liquidity
Then cmd: "move run --function default::router::add_liquidity --type-args 0x3::gas_coin::RGas --type-args default::fixed_supply_coin::FSC --args u64:100000000000 --args u64:100000000000 --args u64:0 --args u64:0 --json"
Then assert: "{{$.move[-1].execution_info.status.type}} == executed"

#Check lp token balance
Then cmd: "account balance --coin-type default::swap::LPToken<0x3::gas_coin::RGas,default::fixed_supply_coin::FSC> --json"
Then assert: "{{$.account[3].RDexLP.balance}} != 0"

#Before swap the balance should be 0
Then cmd: "account balance --coin-type default::fixed_supply_coin::FSC --address {{$.account[2].account1.address}} --json"
Then assert: "{{$.account[-1].FSC.balance}} == 0"

#SWAP
Then cmd: "move run --sender {{$.account[2].account1.address}} --function default::router::swap_with_exact_input --type-args 0x3::gas_coin::RGas --type-args default::fixed_supply_coin::FSC --args u64:100000000 --args u64:0 --json"
Then assert: "{{$.move[-1].execution_info.status.type}} == executed"

Then cmd: "account balance --coin-type default::fixed_supply_coin::FSC --address {{$.account[2].account1.address}} --json"
Then assert: "{{$.account[-1].FSC.balance}} != 0"

#Create incentive pool
Then cmd: "move run --function default::liquidity_incentive::create_pool --type-args 0x3::gas_coin::RGas --type-args default::fixed_supply_coin::FSC --type-args default::liquid_xp::LiquidXP --args u128:10000 --args u256:10000000000 --args u64:0 --json"
Then assert: "{{$.move[-1].execution_info.status.type}} == executed"

Then cmd: "object -t 'default::liquidity_incentive::FarmingAsset<0x3::gas_coin::RGas,default::fixed_supply_coin::FSC,default::liquid_xp::LiquidXP>'"

#stake the LPToken
Then cmd: "move run --function default::liquidity_incentive::stake --type-args 0x3::gas_coin::RGas --type-args default::fixed_supply_coin::FSC --type-args default::liquid_xp::LiquidXP --args u256:{{$.account[3].RDexLP.balance}} --args object:{{$.object[-1].data[0].id}} --json"
Then assert: "{{$.move[-1].execution_info.status.type}} == executed"

#Fast forward timestamp
Then cmd: "move run --function 0x3::timestamp::fast_forward_seconds_for_local --args u64:1000 --json"
Then assert: "{{$.move[-1].execution_info.status.type}} == executed"

#Harvest
Then cmd: "move run --function default::liquidity_incentive::harvest --type-args 0x3::gas_coin::RGas --type-args default::fixed_supply_coin::FSC --type-args default::liquid_xp::LiquidXP --args object:{{$.object[-1].data[0].id}} --json"
Then assert: "{{$.move[-1].execution_info.status.type}} == executed"

#Check LPXP balance
Then cmd: "account balance --coin-type default::liquid_xp::LiquidXP --json"
Then assert: "{{$.account[-1].LPXP.balance}} != 0"

#Unstake
Then cmd: "move run --function default::liquidity_incentive::unstake --type-args 0x3::gas_coin::RGas --type-args default::fixed_supply_coin::FSC --type-args default::liquid_xp::LiquidXP --args object:{{$.object[-1].data[0].id}} --json"
Then assert: "{{$.move[-1].execution_info.status.type}} == executed"


4 changes: 2 additions & 2 deletions examples/coins/sources/fixed_supply_coin.move
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ module coins::fixed_supply_coin {
use rooch_framework::coin_store::{Self, CoinStore};
use rooch_framework::account_coin_store;

const TOTAL_SUPPLY: u256 = 210_000_000_000u256;
const TOTAL_SUPPLY: u256 = 210_000_000_000_00000000u256;
const DECIMALS: u8 = 1u8;

// The `FSC` CoinType has `key` and `store` ability.
Expand Down Expand Up @@ -47,7 +47,7 @@ module coins::fixed_supply_coin {
public entry fun faucet(account: &signer, treasury_obj: &mut Object<Treasury>) {
let account_addr = signer::address_of(account);
let treasury = object::borrow_mut(treasury_obj);
let coin = coin_store::withdraw(&mut treasury.coin_store, 10000);
let coin = coin_store::withdraw(&mut treasury.coin_store, 1000000000000);
account_coin_store::deposit(account_addr, coin);
}
}

0 comments on commit 3123432

Please sign in to comment.