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

Reformat code and decrease_member_sbt when unstake #166

Merged
merged 6 commits into from
Sep 28, 2022
Merged
Show file tree
Hide file tree
Changes from 5 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
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: 92C4BD219F80CC0A3708B819EA1E57967727A3ED42F5627ACA2408BCF40FC74C
source_digest: 5F39868CB8511B5984EA5AFAC56DD524087DB8401BE9901148BA2CBF177357E4
build_flags:
dev_mode: false
test_mode: false
Expand Down
183 changes: 127 additions & 56 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,8 +137,7 @@ 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 @@ -179,17 +178,26 @@ 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 {
Comment on lines +181 to +185
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这种应该是插件的格式化吧,不如把整个framework 也都格式化一下?再统一 Address::module{}的方式? @jolestar

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这种应该是插件的格式化吧,不如把整个framework 也都格式化一下?再统一 Address::module{}的方式? @jolestar

单独一个 PR,只做 format,别的啥都不动

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 @@ -221,7 +229,8 @@ 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 @@ -285,7 +294,8 @@ 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 @@ -317,24 +327,41 @@ 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 @@ -350,7 +377,8 @@ 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 @@ -385,7 +413,10 @@ 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 @@ -403,11 +434,13 @@ 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 @@ -420,15 +453,20 @@ 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 @@ -445,30 +483,44 @@ 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 @@ -483,30 +535,49 @@ 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);
}
}