Skip to content

Commit

Permalink
Merge pull request #60 from tonlabs/update_depool
Browse files Browse the repository at this point in the history
Updated functionality to work with the last depool contract
  • Loading branch information
SilkovAlexander authored Nov 6, 2020
2 parents bba7ae5 + 58d6ea2 commit 46be40e
Show file tree
Hide file tree
Showing 5 changed files with 178 additions and 99 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ readme = "README.md"
license = "Apache-2.0"
keywords = ["TON", "SDK", "smart contract", "tonlabs", "solidity"]
edition = "2018"
version = "0.1.24"
version = "0.1.25"

[dependencies]
base64 = "0.10.1"
Expand Down
15 changes: 15 additions & 0 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ fn default_retries() -> u8 {
5
}

fn default_depool_fee() -> f32 {
0.5
}

fn default_timeout() -> u32 {
60000
}
Expand All @@ -45,6 +49,8 @@ pub struct Config {
pub timeout: u32,
#[serde(default = "default_false")]
pub is_json: bool,
#[serde(default = "default_depool_fee")]
pub depool_fee: f32,
}

impl Config {
Expand All @@ -59,6 +65,7 @@ impl Config {
retries: default_retries(),
timeout: default_timeout(),
is_json: default_false(),
depool_fee: default_depool_fee(),
}
}

Expand All @@ -80,6 +87,7 @@ pub fn set_config(
wc: Option<&str>,
retries: Option<&str>,
timeout: Option<&str>,
depool_fee: Option<&str>,
) -> Result<(), String> {
if let Some(s) = url {
conf.url = s.to_string();
Expand Down Expand Up @@ -108,6 +116,13 @@ pub fn set_config(
conf.wc = i32::from_str_radix(wc, 10)
.map_err(|e| format!(r#"failed to parse "workchain id": {}"#, e))?;
}
if let Some(depool_fee) = depool_fee {
conf.depool_fee = depool_fee.parse::<f32>()
.map_err(|e| format!(r#"failed to parse "depool_fee": {}"#, e))?;
}
if conf.depool_fee < 0.5 {
return Err("Minimal value for depool fee is 0.5".to_string());
}
let conf_str = serde_json::to_string(&conf)
.map_err(|_| "failed to serialize config object".to_string())?;

Expand Down
108 changes: 77 additions & 31 deletions src/depool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ pub fn create_depool_command<'a, 'b>() -> App<'a, 'b> {
.takes_value(true)
.long("--value")
.short("-v")
.help("Stake value.");
.help("Value in tons.");
let keys_arg = Arg::with_name("SIGN")
.takes_value(true)
.long("--sign")
Expand All @@ -53,9 +53,6 @@ pub fn create_depool_command<'a, 'b>() -> App<'a, 'b> {
.long("--beneficiary")
.short("-b")
.help("Smart contract address which will own lock stake rewards.");
let reinvest_arg = Arg::with_name("AUTORESUME")
.long("--autoresume-off")
.help("Disables autoresume flag for participant. In this case stake will be invested only to one round.");
let dest_arg = Arg::with_name("DEST")
.takes_value(true)
.long("--dest")
Expand All @@ -76,8 +73,7 @@ pub fn create_depool_command<'a, 'b>() -> App<'a, 'b> {
.setting(AppSettings::AllowLeadingHyphen)
.arg(wallet_arg.clone())
.arg(value_arg.clone())
.arg(keys_arg.clone())
.arg(reinvest_arg))
.arg(keys_arg.clone()))
.subcommand(SubCommand::with_name("vesting")
.about("Deposits vesting stake in depool from multisignature wallet.")
.setting(AppSettings::AllowLeadingHyphen)
Expand Down Expand Up @@ -121,6 +117,11 @@ pub fn create_depool_command<'a, 'b>() -> App<'a, 'b> {
.arg(wallet_arg.clone())
.arg(value_arg.clone())
.arg(keys_arg.clone()))
.subcommand(SubCommand::with_name("ticktock")
.about("Call DePool 'ticktock()' function to update its state. 1 ton is attached to this call (change will be returned).")
.setting(AppSettings::AllowLeadingHyphen)
.arg(wallet_arg.clone())
.arg(keys_arg.clone()))
.subcommand(SubCommand::with_name("withdraw")
.about("Allows to disable auto investment of the stake into next round and withdraw all the stakes after round completion.")
.setting(AppSettings::AllowLeadingHyphen)
Expand Down Expand Up @@ -150,12 +151,14 @@ struct CommandData<'a> {
wallet: String,
keys: String,
stake: &'a str,
depool_fee: String,
}

impl<'a> CommandData<'a> {
pub fn from_matches_and_conf(m: &'a ArgMatches, conf: Config, depool: String) -> Result<Self, String> {
let (wallet, stake, keys) = parse_stake_data(m, &conf)?;
Ok(CommandData {conf, depool, wallet, stake, keys})
let depool_fee = conf.depool_fee.clone().to_string();
Ok(CommandData {conf, depool, wallet, stake, keys, depool_fee})
}
}

Expand Down Expand Up @@ -238,6 +241,10 @@ pub fn depool_command(m: &ArgMatches, conf: Config) -> Result<(), String> {
CommandData::from_matches_and_conf(m, conf, depool)?,
);
}
if let Some(m) = m.subcommand_matches("ticktock") {
let (wallet, keys) = parse_wallet_data(&m, &conf)?;
return ticktock_command(m, conf, &depool, &wallet, &keys);
}
Err("unknown depool command".to_owned())
}

Expand Down Expand Up @@ -331,11 +338,9 @@ fn ordinary_stake_command<'a>(
m: &ArgMatches,
cmd: CommandData
) -> Result<(), String> {
let disable_reinvest = m.is_present("AUTORESUME");
let autoresume = Some(if disable_reinvest { "false" } else { "true" });
let (depool, wallet, stake, keys) = (Some(&cmd.depool), Some(&cmd.wallet), Some(cmd.stake), Some(&cmd.keys));
print_args!(m, depool, wallet, stake, keys, autoresume);
add_ordinary_stake(cmd, !disable_reinvest)
print_args!(m, depool, wallet, stake, keys);
add_ordinary_stake(cmd)
}

fn replenish_command<'a>(
Expand All @@ -347,6 +352,18 @@ fn replenish_command<'a>(
replenish_stake(cmd)
}

fn ticktock_command<'a>(
m: &ArgMatches,
conf: Config,
depool: &str,
wallet: &str,
keys: &str,
) -> Result<(), String> {
let (depool, wallet, keys) = (Some(depool), Some(wallet), Some(keys));
print_args!(m, depool, wallet, keys);
call_ticktock(conf, depool.unwrap(), wallet.unwrap(), keys.unwrap())
}

fn transfer_stake_command<'a>(
m: &ArgMatches,
cmd: CommandData
Expand Down Expand Up @@ -421,12 +438,16 @@ fn set_withdraw_command(
set_withdraw(conf, depool.unwrap(), wallet.unwrap(), keys.unwrap(), enable)
}

fn add_ordinary_stake(
fn add_ordinary_stake(
cmd: CommandData,
autoresume: bool,
) -> Result<(), String> {
let body = encode_add_ordinary_stake(autoresume)?;
send_with_body(cmd.conf, &cmd.wallet, &cmd.depool, cmd.stake, &cmd.keys, &body)
let stake = u64::from_str_radix(&convert::convert_token(cmd.stake)?, 10)
.map_err(|e| format!(r#"failed to parse stake value: {}"#, e))?;
let body = encode_add_ordinary_stake(stake)?;
let fee = u64::from_str_radix(&convert::convert_token(&cmd.depool_fee)?, 10)
.map_err(|e| format!(r#"failed to parse depool fee value: {}"#, e))?;
let value = (fee + stake) as f64 * 1.0 / 1e9;
send_with_body(cmd.conf, &cmd.wallet, &cmd.depool, &format!("{}", value), &cmd.keys, &body)
}

fn replenish_stake(
Expand All @@ -436,6 +457,16 @@ fn replenish_stake(
send_with_body(cmd.conf, &cmd.wallet, &cmd.depool, cmd.stake, &cmd.keys, &body)
}

fn call_ticktock(
conf: Config,
depool: &str,
wallet: &str,
keys: &str,
) -> Result<(), String> {
let body = encode_ticktock()?;
send_with_body(conf, wallet, depool, "1", keys, &body)
}

fn add_exotic_stake(
cmd: CommandData,
beneficiary: &str,
Expand All @@ -444,12 +475,17 @@ fn add_exotic_stake(
is_vesting: bool,
) -> Result<(), String> {
load_ton_address(beneficiary)?;
let stake = u64::from_str_radix(&convert::convert_token(cmd.stake)?, 10)
.map_err(|e| format!(r#"failed to parse stake value: {}"#, e))?;
let body = if is_vesting {
encode_add_vesting_stake(beneficiary, tp, wp)?
encode_add_vesting_stake(stake, beneficiary, tp, wp)?
} else {
encode_add_lock_stake(beneficiary, tp, wp)?
encode_add_lock_stake(stake, beneficiary, tp, wp)?
};
send_with_body(cmd.conf, &cmd.wallet, &cmd.depool, cmd.stake, &cmd.keys, &body)
let fee = u64::from_str_radix(&convert::convert_token(&cmd.depool_fee)?, 10)
.map_err(|e| format!(r#"failed to parse depool fee value: {}"#, e))?;
let value = (fee + stake) as f64 * 1.0 / 1e9;
send_with_body(cmd.conf, &cmd.wallet, &cmd.depool, &format!("{}", value), &cmd.keys, &body)
}

fn remove_stake(
Expand All @@ -459,7 +495,7 @@ fn remove_stake(
&convert::convert_token(cmd.stake)?, 10,
).unwrap();
let body = encode_remove_stake(stake)?;
send_with_body(cmd.conf, &cmd.wallet, &cmd.depool, "0.05", &cmd.keys, &body)
send_with_body(cmd.conf, &cmd.wallet, &cmd.depool, &cmd.depool_fee, &cmd.keys, &body)
}

fn withdraw_stake(
Expand All @@ -469,7 +505,7 @@ fn withdraw_stake(
&convert::convert_token(cmd.stake)?, 10,
).unwrap();
let body = encode_withdraw_stake(stake)?;
send_with_body(cmd.conf, &cmd.wallet, &cmd.depool, "0.05", &cmd.keys, &body)
send_with_body(cmd.conf, &cmd.wallet, &cmd.depool, &cmd.depool_fee, &cmd.keys, &body)
}

fn transfer_stake(cmd: CommandData, dest: &str) -> Result<(), String> {
Expand All @@ -478,7 +514,7 @@ fn transfer_stake(cmd: CommandData, dest: &str) -> Result<(), String> {
&convert::convert_token(cmd.stake)?, 10,
).unwrap();
let body = encode_transfer_stake(dest, stake)?;
send_with_body(cmd.conf, &cmd.wallet, &cmd.depool, "0.135", &cmd.keys, &body)
send_with_body(cmd.conf, &cmd.wallet, &cmd.depool, &cmd.depool_fee, &cmd.keys, &body)
}

fn set_withdraw(
Expand All @@ -489,7 +525,8 @@ fn set_withdraw(
enable: bool,
) -> Result<(), String> {
let body = encode_set_withdraw(enable)?;
send_with_body(conf, wallet, depool, "0.03", keys, &body)
let value = conf.depool_fee.clone().to_string();
send_with_body(conf, wallet, depool, &value, keys, &body)
}

fn encode_body(func: &str, params: serde_json::Value) -> Result<String, String> {
Expand All @@ -514,14 +551,16 @@ fn encode_body(func: &str, params: serde_json::Value) -> Result<String, String>
}

fn encode_set_withdraw(flag: bool) -> Result<String, String> {
encode_body("withdrawAllAfterCompleting", json!({
"doWithdrawAll": flag
}))
if flag {
encode_body("withdrawAll", json!({}))
} else {
encode_body("cancelWithdrawal", json!({}))
}
}

fn encode_add_ordinary_stake(reinvest: bool) -> Result<String, String> {
fn encode_add_ordinary_stake(stake: u64) -> Result<String, String> {
encode_body("addOrdinaryStake", json!({
"reinvest": reinvest
"stake": stake
}))
}

Expand All @@ -530,30 +569,37 @@ fn encode_replenish_stake() -> Result<String, String> {
}))
}

fn encode_add_vesting_stake(beneficiary: &str, tperiod: u32, wperiod: u32) -> Result<String, String> {
fn encode_ticktock() -> Result<String, String> {
encode_body("ticktock", json!({
}))
}

fn encode_add_vesting_stake(stake: u64, beneficiary: &str, tperiod: u32, wperiod: u32) -> Result<String, String> {
encode_body("addVestingStake", json!({
"stake": stake,
"beneficiary": beneficiary,
"withdrawalPeriod": wperiod,
"totalPeriod": tperiod
}))
}

fn encode_add_lock_stake(beneficiary: &str, tperiod: u32, wperiod: u32) -> Result<String, String> {
fn encode_add_lock_stake(stake: u64, beneficiary: &str, tperiod: u32, wperiod: u32) -> Result<String, String> {
encode_body("addLockStake", json!({
"stake": stake,
"beneficiary": beneficiary,
"withdrawalPeriod": wperiod,
"totalPeriod": tperiod
}))
}

fn encode_remove_stake(target_value: u64) -> Result<String, String> {
encode_body("removeOrdinaryStake", json!({
encode_body("withdrawFromPoolingRound", json!({
"withdrawValue": target_value
}))
}

fn encode_withdraw_stake(target_value: u64) -> Result<String, String> {
encode_body("withdrawPartAfterCompleting", json!({
encode_body("withdrawPart", json!({
"withdrawValue": target_value
}))
}
Expand Down
Loading

0 comments on commit 46be40e

Please sign in to comment.