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

Various things #3

Merged
merged 3 commits into from
Oct 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
24 changes: 24 additions & 0 deletions .github/workflows/pull-request.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
name: Pull Request

on:
pull_request:
push:
branches: [mainnet, devel, add_ci]

jobs:
move-test:
runs-on: ubuntu-latest
container:
image: mysten/sui-tools:testnet
ports:
- 80
steps:
- name: Checkout repository
uses: actions/checkout@v2

# bugfix: install git
- name: install git
run: apt-get install -y git

- name: test
run: cd contracts && sui move test
86 changes: 71 additions & 15 deletions contracts/sources/liquid_staking.move
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,20 @@ module liquid_staking::liquid_staking {
use liquid_staking::version::{Self, Version};
use liquid_staking::events::{emit_event};
use sui_system::staking_pool::{FungibleStakedSui};
use std::type_name::{Self, TypeName};
use sui::package;

/* Errors */
const EInvalidLstCreation: u64 = 0;
const EMintInvariantViolated: u64 = 1;
const ERedeemInvariantViolated: u64 = 2;

/* Constants */
const CURRENT_VERSION: u16 = 1;
const MIN_STAKE_AMOUNT: u64 = 1_000_000_000;

public struct LIQUID_STAKING has drop {}

public struct LiquidStakingInfo<phantom P> has key, store {
id: UID,
lst_treasury_cap: TreasuryCap<P>,
Expand All @@ -36,33 +42,44 @@ module liquid_staking::liquid_staking {
}

/* Events */
public struct MintEvent<phantom P> has copy, drop {
public struct CreateEvent has copy, drop {
typename: TypeName,
liquid_staking_info_id: ID
}

public struct MintEvent has copy, drop {
typename: TypeName,
sui_amount_in: u64,
lst_amount_out: u64,
fee_amount: u64
}

public struct RedeemEvent<phantom P> has copy, drop {
public struct RedeemEvent has copy, drop {
typename: TypeName,
lst_amount_in: u64,
sui_amount_out: u64,
fee_amount: u64
}

public struct DecreaseValidatorStakeEvent<phantom P> has copy, drop {
public struct DecreaseValidatorStakeEvent has copy, drop {
typename: TypeName,
staking_pool_id: ID,
amount: u64
}

public struct IncreaseValidatorStakeEvent<phantom P> has copy, drop {
public struct IncreaseValidatorStakeEvent has copy, drop {
typename: TypeName,
staking_pool_id: ID,
amount: u64
}

public struct CollectFeesEvent<phantom P> has copy, drop {
public struct CollectFeesEvent has copy, drop {
typename: TypeName,
amount: u64
}

public struct EpochChangedEvent<phantom P> has copy, drop {
public struct EpochChangedEvent has copy, drop {
typename: TypeName,
old_sui_supply: u64,
new_sui_supply: u64,
lst_supply: u64,
Expand Down Expand Up @@ -99,6 +116,9 @@ module liquid_staking::liquid_staking {
}

/* Public Mutative Functions */
fun init(otw: LIQUID_STAKING, ctx: &mut TxContext) {
package::claim_and_keep(otw, ctx)
}

public fun create_lst<P: drop>(
fee_config: FeeConfig,
Expand Down Expand Up @@ -152,10 +172,17 @@ module liquid_staking::liquid_staking {
storage: Storage,
ctx: &mut TxContext
): (AdminCap<P>, LiquidStakingInfo<P>) {
let uid = object::new(ctx);

emit_event(CreateEvent {
typename: type_name::get<P>(),
liquid_staking_info_id: uid.to_inner()
});

(
AdminCap<P> { id: object::new(ctx) },
LiquidStakingInfo {
id: object::new(ctx),
id: uid,
lst_treasury_cap: lst_treasury_cap,
fee_config: cell::new(fee_config),
fees: balance::zero(),
Expand All @@ -176,6 +203,9 @@ module liquid_staking::liquid_staking {
): Coin<P> {
self.refresh(system_state, ctx);

let old_sui_supply = (self.total_sui_supply() as u128);
let old_lst_supply = (self.total_lst_supply() as u128);

let mut sui_balance = sui.into_balance();
let sui_amount_in = sui_balance.value();

Expand All @@ -184,15 +214,26 @@ module liquid_staking::liquid_staking {
self.fees.join(sui_balance.split(mint_fee_amount));

let lst_mint_amount = self.sui_amount_to_lst_amount(sui_balance.value());
self.storage.join_to_sui_pool(sui_balance);

emit_event(MintEvent<P> {
emit_event(MintEvent {
typename: type_name::get<P>(),
sui_amount_in,
lst_amount_out: lst_mint_amount,
fee_amount: mint_fee_amount
});

self.lst_treasury_cap.mint(lst_mint_amount, ctx)
let lst = self.lst_treasury_cap.mint(lst_mint_amount, ctx);

// invariant: lst_out / sui_in <= old_lst_supply / old_sui_supply
// -> lst_out * old_sui_supply <= sui_in * old_lst_supply
assert!(
(lst.value() as u128) * old_sui_supply <= (sui_balance.value() as u128) * old_lst_supply,
EMintInvariantViolated
);

self.storage.join_to_sui_pool(sui_balance);

lst
}

public fun redeem<P: drop>(
Expand All @@ -203,19 +244,30 @@ module liquid_staking::liquid_staking {
): Coin<SUI> {
self.refresh(system_state, ctx);

let old_sui_supply = (self.total_sui_supply() as u128);
let old_lst_supply = (self.total_lst_supply() as u128);

let sui_amount_out = self.lst_amount_to_sui_amount(lst.value());
let mut sui = self.storage.split_n_sui(system_state, sui_amount_out, ctx);

// deduct fee
let redeem_fee_amount = self.fee_config.get().calculate_redeem_fee(sui.value());
self.fees.join(sui.split(redeem_fee_amount as u64));

emit_event(RedeemEvent<P> {
emit_event(RedeemEvent {
typename: type_name::get<P>(),
lst_amount_in: lst.value(),
sui_amount_out: sui.value(),
fee_amount: redeem_fee_amount
});

// invariant: sui_out / lst_in <= old_sui_supply / old_lst_supply
// -> sui_out * old_lst_supply <= lst_in * old_sui_supply
assert!(
(sui.value() as u128) * old_lst_supply <= (lst.value() as u128) * old_sui_supply,
ERedeemInvariantViolated
);

self.lst_treasury_cap.burn(lst);

coin::from_balance(sui, ctx)
Expand Down Expand Up @@ -255,7 +307,8 @@ module liquid_staking::liquid_staking {
);
let staked_sui_amount = staked_sui.staked_sui_amount();

emit_event(IncreaseValidatorStakeEvent<P> {
emit_event(IncreaseValidatorStakeEvent {
typename: type_name::get<P>(),
staking_pool_id: staked_sui.pool_id(),
amount: staked_sui.staked_sui_amount()
});
Expand All @@ -282,7 +335,8 @@ module liquid_staking::liquid_staking {
ctx
);

emit_event(DecreaseValidatorStakeEvent<P> {
emit_event(DecreaseValidatorStakeEvent {
typename: type_name::get<P>(),
staking_pool_id: self.storage.validators()[validator_index].staking_pool_id(),
amount: sui_amount
});
Expand All @@ -304,7 +358,8 @@ module liquid_staking::liquid_staking {
let mut fees = self.fees.withdraw_all();
fees.join(spread_fees);

emit_event(CollectFeesEvent<P> {
emit_event(CollectFeesEvent {
typename: type_name::get<P>(),
amount: fees.value()
});

Expand Down Expand Up @@ -344,7 +399,8 @@ module liquid_staking::liquid_staking {
* (self.fee_config.get().spread_fee_bps() as u128)
/ (10_000 as u128)) as u64;

emit_event(EpochChangedEvent<P> {
emit_event(EpochChangedEvent {
typename: type_name::get<P>(),
old_sui_supply: old_total_supply,
new_sui_supply: new_total_supply,
lst_supply: self.total_lst_supply(),
Expand Down
Loading