Skip to content

Commit

Permalink
Revert "Reformat code and decrease_member_sbt when unstake (#166)"
Browse files Browse the repository at this point in the history
This reverts commit 25148a4.
  • Loading branch information
nkysg committed Mar 2, 2023
1 parent c5bb173 commit 50bc16d
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 128 deletions.
2 changes: 1 addition & 1 deletion build/StarcoinFramework/BuildInfo.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -321,7 +321,7 @@ compiled_package_info:
? address: "0x00000000000000000000000000000001"
name: YieldFarmingV2
: StarcoinFramework
source_digest: 5F39868CB8511B5984EA5AFAC56DD524087DB8401BE9901148BA2CBF177357E4
source_digest: E9EBFB94E7BA4C1DB94C3F860A481AA819D6600B82D2F49971D1AE849AE3AA8F
build_flags:
dev_mode: false
test_mode: false
Expand Down
183 changes: 56 additions & 127 deletions sources/daospaceplugin/StakeToSBTPlugin.move
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@ module StarcoinFramework::StakeToSBTPlugin {
const ERR_PLUGIN_ITEM_CANT_FOUND: u64 = 1006;
const ERR_PLUGIN_NO_MATCH_LOCKTIME: u64 = 1007;

struct StakeToSBTPlugin has store, drop {}
struct StakeToSBTPlugin has store, drop{}

public fun initialize() {
let signer = GenesisSignerCapability::get_genesis_signer();

DAOPluginMarketplace::register_plugin<StakeToSBTPlugin>(
&signer,
b"0x1::StakeToSBTPlugin",
Expand All @@ -35,11 +35,11 @@ module StarcoinFramework::StakeToSBTPlugin {
let implement_extpoints = Vector::empty<vector<u8>>();
let depend_extpoints = Vector::empty<vector<u8>>();

let witness = StakeToSBTPlugin {};
let witness = StakeToSBTPlugin{};
DAOPluginMarketplace::publish_plugin_version<StakeToSBTPlugin>(
&signer,
&witness,
b"v0.1.0",
&witness,
b"v0.1.0",
*&implement_extpoints,
*&depend_extpoints,
b"inner-plugin://stake-to-sbt-plugin",
Expand Down Expand Up @@ -137,7 +137,8 @@ module StarcoinFramework::StakeToSBTPlugin {
}

/// Set sbt weight by given DAO root capability
public fun set_sbt_weight_with_root_cap<DAOT: store, TokenT: store>(
public fun set_sbt_weight_with_root_cap<DAOT: store,
TokenT: store>(
_cap: &DAOSpace::DAORootCap<DAOT>,
lock_time: u64,
weight: u64
Expand Down Expand Up @@ -178,26 +179,17 @@ module StarcoinFramework::StakeToSBTPlugin {
);
}

public fun stake<DAOT: store, TokenT: store>(
sender: &signer,
token: Token::Token<TokenT>,
lock_time: u64
): u64 acquires StakeList {
public fun stake<DAOT: store, TokenT: store>(sender: &signer,
token: Token::Token<TokenT>,
lock_time: u64): u64 acquires StakeList {
let sender_addr = Signer::address_of(sender);
// Increase SBT
let witness = StakeToSBTPlugin {};
let member_cap =
DAOSpace::acquire_member_cap<DAOT, StakeToSBTPlugin>(&witness);
let member_cap = DAOSpace::acquire_member_cap<DAOT, StakeToSBTPlugin>(&witness);

if (!DAOSpace::is_member<DAOT>(sender_addr)) {
IdentifierNFT::accept<DAOSpace::DAOMember<DAOT>, DAOSpace::DAOMemberBody<DAOT>>(sender);
DAOSpace::join_member<DAOT, StakeToSBTPlugin>(
&member_cap,
sender_addr,
Option::none<vector<u8>>(),
Option::none<vector<u8>>(),
0
);
DAOSpace::join_member<DAOT, StakeToSBTPlugin>(&member_cap, sender_addr, Option::none<vector<u8>>(), Option::none<vector<u8>>(), 0);
};

if (!exists<StakeList<DAOT, TokenT>>(sender_addr)) {
Expand Down Expand Up @@ -229,8 +221,7 @@ module StarcoinFramework::StakeToSBTPlugin {
stake_list.next_id = id;

let witness = StakeToSBTPlugin {};
let plugin_event_cap =
DAOSpace::acquire_plugin_event_cap<DAOT, StakeToSBTPlugin>(&witness);
let plugin_event_cap = DAOSpace::acquire_plugin_event_cap<DAOT, StakeToSBTPlugin>(&witness);
DAOSpace::emit_plugin_event<DAOT, StakeToSBTPlugin, SBTStakeEvent>(
&plugin_event_cap,
SBTStakeEvent {
Expand Down Expand Up @@ -294,8 +285,7 @@ module StarcoinFramework::StakeToSBTPlugin {
Account::deposit<TokenT>(member, unstake_item(member, poped_item));

let witness = StakeToSBTPlugin {};
let plugin_event_cap =
DAOSpace::acquire_plugin_event_cap<DAOT, StakeToSBTPlugin>(&witness);
let plugin_event_cap = DAOSpace::acquire_plugin_event_cap<DAOT, StakeToSBTPlugin>(&witness);
DAOSpace::emit_plugin_event<DAOT, StakeToSBTPlugin, SBTUnstakeEvent>(
&plugin_event_cap,
SBTUnstakeEvent {
Expand Down Expand Up @@ -327,41 +317,24 @@ module StarcoinFramework::StakeToSBTPlugin {
}

/// Unstake a item from a item object
fun unstake_item<DAOT: store, TokenT: store>(
member: address,
item: Stake<DAOT, TokenT>
): Token::Token<TokenT> {
fun unstake_item<DAOT: store, TokenT: store>(_member: address, item: Stake<DAOT, TokenT>): Token::Token<TokenT> {
let Stake<DAOT, TokenT> {
id: _,
token,
lock_time,
stake_time,
weight: _,
sbt_amount,
sbt_amount: _,
} = item;

assert!((Timestamp::now_seconds() - stake_time) > lock_time, Errors::invalid_state(ERR_PLUGIN_STILL_LOCKED));

// Deduct the corresponding SBT amount if the signer account is a DAO member while unstake
if (DAOSpace::is_member<DAOT>(member)) {
let witness = StakeToSBTPlugin {};
let member_cap =
DAOSpace::acquire_member_cap<DAOT, StakeToSBTPlugin>(&witness);

// Decrease the SBT using `sbt_amount` which from unwrapped Stake data,
// rather than the value that calculate a SBT amount from lock time and weight,
// because of the `weight` could change at any time
DAOSpace::decrease_member_sbt(&member_cap, member, sbt_amount);
};

token
}

fun get_sbt_weight<DAOT: store, TokenT: store>(lock_time: u64): Option::Option<u64> {
let config =
DAOSpace::get_custom_config<DAOT, LockWeightConfig<DAOT, TokenT>>();
let c =
&mut config.weight_vec;
let config = DAOSpace::get_custom_config<DAOT, LockWeightConfig<DAOT, TokenT>>();
let c = &mut config.weight_vec;
let len = Vector::length(c);
let idx = 0;

Expand All @@ -377,8 +350,7 @@ module StarcoinFramework::StakeToSBTPlugin {
}

fun set_sbt_weight<DAOT: store, TokenT: store>(lock_time: u64, weight: u64) {
let config =
DAOSpace::get_custom_config<DAOT, LockWeightConfig<DAOT, TokenT>>();
let config = DAOSpace::get_custom_config<DAOT, LockWeightConfig<DAOT, TokenT>>();
let c = &mut config.weight_vec;
let len = Vector::length(c);
let idx = 0;
Expand Down Expand Up @@ -413,10 +385,7 @@ module StarcoinFramework::StakeToSBTPlugin {
});
}

fun find_item<DAOT: store, TokenT: store>(
id: u64,
c: &vector<Stake<DAOT, TokenT>>
): Option::Option<u64> {
fun find_item<DAOT: store, TokenT: store>(id: u64, c: &vector<Stake<DAOT, TokenT>>): Option::Option<u64> {
let len = Vector::length(c);
let idx = 0;
while (idx < len) {
Expand All @@ -434,13 +403,11 @@ module StarcoinFramework::StakeToSBTPlugin {
}

/// Create proposal that to specific a weight for a locktime
public fun create_weight_proposal<DAOT: store, TokenT: store>(
sender: &signer,
description: vector<u8>,
lock_time: u64,
weight: u64,
action_delay: u64
) {
public fun create_weight_proposal<DAOT: store, TokenT: store>(sender: &signer,
description: vector<u8>,
lock_time: u64,
weight: u64,
action_delay: u64) {
let witness = StakeToSBTPlugin {};

let cap =
Expand All @@ -453,20 +420,15 @@ module StarcoinFramework::StakeToSBTPlugin {
action_delay);
}

public(script) fun create_weight_proposal_entry<DAOT: store, TokenT: store>(
sender: signer,
description: vector<u8>,
lock_time: u64,
weight: u64,
action_delay: u64
) {
create_weight_proposal<DAOT, TokenT>(&sender, description, lock_time, weight, action_delay);
public(script) fun create_weight_proposal_entry<DAOT: store, TokenT: store>(sender: signer,
description:vector<u8>,
lock_time: u64,
weight: u64,
action_delay: u64) {
create_weight_proposal<DAOT, TokenT>(&sender, description, lock_time, weight, action_delay);
}

public fun execute_weight_proposal<DAOT: store, TokenT: store>(
sender: &signer,
proposal_id: u64
) {
public fun execute_weight_proposal<DAOT: store, TokenT: store>(sender: &signer,
proposal_id: u64) {
let witness = StakeToSBTPlugin {};
let proposal_cap =
DAOSpace::acquire_proposal_cap<DAOT, StakeToSBTPlugin>(&witness);
Expand All @@ -483,44 +445,30 @@ module StarcoinFramework::StakeToSBTPlugin {
set_sbt_weight<DAOT, TokenT>(lock_time, weight);
}

public(script) fun execute_weight_proposal_entry<DAOT: store, TokenT: store>(
sender: signer,
proposal_id: u64
) {
public(script) fun execute_weight_proposal_entry<DAOT: store, TokenT: store>(sender: signer,
proposal_id: u64) {
execute_weight_proposal<DAOT, TokenT>(&sender, proposal_id);
}

/// Create proposal that to accept a token type, which allow user to convert amount of token to SBT
public fun create_token_accept_proposal<DAOT: store, TokenT: store>(
sender: &signer,
description: vector<u8>,
action_delay: u64
) {
public fun create_token_accept_proposal<DAOT: store, TokenT: store>(sender: &signer,
description: vector<u8>,
action_delay: u64) {
let witness = StakeToSBTPlugin {};

let cap =
DAOSpace::acquire_proposal_cap<DAOT, StakeToSBTPlugin>(&witness);
DAOSpace::create_proposal(
&cap,
sender,
AcceptTokenCap<DAOT, TokenT> {},
description,
action_delay
);
DAOSpace::create_proposal(&cap, sender, AcceptTokenCap<DAOT, TokenT> {}, description, action_delay);
}

public(script) fun create_token_accept_proposal_entry<DAOT: store, TokenT: store>(
sender: signer,
description: vector<u8>,
action_delay: u64
) {
create_token_accept_proposal<DAOT, TokenT>(&sender, description, action_delay);
public(script) fun create_token_accept_proposal_entry<DAOT: store, TokenT: store>(sender: signer,
description:vector<u8>,
action_delay: u64) {
create_token_accept_proposal<DAOT, TokenT>(&sender, description, action_delay);
}

public fun execute_token_accept_proposal<DAOT: store, TokenT: store>(
sender: &signer,
proposal_id: u64
) {
public fun execute_token_accept_proposal<DAOT: store, TokenT: store>(sender: &signer,
proposal_id: u64) {
let witness = StakeToSBTPlugin {};
let proposal_cap =
DAOSpace::acquire_proposal_cap<DAOT, StakeToSBTPlugin>(&witness);
Expand All @@ -535,49 +483,30 @@ module StarcoinFramework::StakeToSBTPlugin {
}


public(script) fun execute_token_accept_proposal_entry<DAOT: store, TokenT: store>(
sender: signer,
proposal_id: u64
) {
execute_token_accept_proposal<DAOT, TokenT>(&sender, proposal_id);
public(script) fun execute_token_accept_proposal_entry<DAOT: store, TokenT: store>(sender: signer,
proposal_id: u64) {
execute_token_accept_proposal<DAOT, TokenT>(&sender, proposal_id);
}

public fun install_plugin_proposal<DAOT: store>(
sender: &signer,
description: vector<u8>,
action_delay: u64
) {
InstallPluginProposalPlugin::create_proposal<DAOT, StakeToSBTPlugin>(
sender,
required_caps(),
description,
action_delay
);
public fun install_plugin_proposal<DAOT: store>(sender: &signer, description:vector<u8>, action_delay: u64) {
InstallPluginProposalPlugin::create_proposal<DAOT, StakeToSBTPlugin>(sender, required_caps(), description,action_delay);
}

public(script) fun install_plugin_proposal_entry<DAOT: store>(
sender: signer,
description: vector<u8>,
action_delay: u64
) {
public (script) fun install_plugin_proposal_entry<DAOT:store>(sender:signer, description: vector<u8>, action_delay:u64){
install_plugin_proposal<DAOT>(&sender, description, action_delay);
}

/// Called by script
public(script) fun stake_entry<DAOT: store, TokenT: store>(
sender: signer,
amount: u128,
lock_time: u64
) acquires StakeList {
public(script) fun stake_entry<DAOT: store, TokenT: store>(sender: signer,
amount: u128,
lock_time: u64) acquires StakeList {
let token = Account::withdraw<TokenT>(&sender, amount);
stake<DAOT, TokenT>(&sender, token, lock_time);
}

/// Called by script
public(script) fun unstake_item_entry<DAOT: store, TokenT: store>(
member: address,
id: u64
) acquires StakeList {
public(script) fun unstake_item_entry<DAOT: store, TokenT: store>(member: address,
id: u64) acquires StakeList {
unstake_by_id<DAOT, TokenT>(member, id);
}
}

0 comments on commit 50bc16d

Please sign in to comment.