Skip to content

Commit

Permalink
Merge pull request #90 from tonlabs/update_last_depool
Browse files Browse the repository at this point in the history
Update last depool
  • Loading branch information
SilkovAlexander authored Feb 1, 2021
2 parents bc96ae5 + 78823e7 commit caf4858
Show file tree
Hide file tree
Showing 11 changed files with 279 additions and 18 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.2.1"
version = "0.3.0"

[dependencies]
async-trait = "0.1.42"
Expand Down
76 changes: 73 additions & 3 deletions src/depool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,11 @@ 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 donor_arg = Arg::with_name("DONOR")
.takes_value(true)
.long("--donor")
.short("-d")
.help("Donor smart contract address.");
let dest_arg = Arg::with_name("DEST")
.takes_value(true)
.long("--dest")
Expand All @@ -65,6 +70,20 @@ pub fn create_depool_command<'a, 'b>() -> App<'a, 'b> {
.takes_value(true)
.long("--addr")
.help("DePool contract address. if the parameter is omitted, then the value `addr` from the config is used"))
.subcommand(SubCommand::with_name("donor")
.about(r#"Top level command for specifying donor for exotic stakes in depool."#)
.subcommand(SubCommand::with_name("vesting")
.about("Set the address from which participant can receive a vesting stake.")
.setting(AppSettings::AllowLeadingHyphen)
.arg(wallet_arg.clone())
.arg(keys_arg.clone())
.arg(donor_arg.clone()))
.subcommand(SubCommand::with_name("lock")
.about("Set the address from which participant can receive a lock stake.")
.setting(AppSettings::AllowLeadingHyphen)
.arg(wallet_arg.clone())
.arg(keys_arg.clone())
.arg(donor_arg.clone())))
.subcommand(SubCommand::with_name("stake")
.about(r#"Top level command for managing stakes in depool. Uses a supplied multisignature wallet to send internal message with stake to depool."#)
.subcommand(SubCommand::with_name("ordinary")
Expand Down Expand Up @@ -192,6 +211,15 @@ pub async fn depool_command(m: &ArgMatches<'_>, conf: Config) -> Result<(), Stri
load_ton_address(&depool)
.map_err(|e| format!("invalid depool address: {}", e))?;

if let Some(m) = m.subcommand_matches("donor") {
let matches = m.subcommand_matches("vesting").or(m.subcommand_matches("lock"));
if let Some(matches) = matches {
let is_vesting = m.subcommand_matches("vesting").is_some();
let (wallet, keys) = parse_wallet_data(&matches, &conf)?;
return set_donor_command(matches, conf, &depool, &wallet, &keys, is_vesting).await;
}
}

if let Some(m) = m.subcommand_matches("stake") {
if let Some(m) = m.subcommand_matches("ordinary") {
return ordinary_stake_command(m,
Expand Down Expand Up @@ -338,11 +366,13 @@ async fn wait_for_event(conf: Config, depool: &str) -> Result<(), String> {
collection: "messages".to_owned(),
filter: Some(events_filter(depool, now())),
result: "id body created_at created_at_string".to_owned(),
timeout: None,
timeout: Some(conf.timeout),
},

).await.map_err(|e| format!("failed to query event: {}", e.to_string()))?;
print_event(ton.clone(), &event.result);
).await.map_err(|e| println!("failed to query event: {}", e.to_string()));
if event.is_ok() {
print_event(ton.clone(), &event.unwrap().result);
}
Ok(())
}
/*
Expand Down Expand Up @@ -393,6 +423,21 @@ async fn transfer_stake_command(
transfer_stake(cmd, dest.unwrap()).await
}

async fn set_donor_command(
m: &ArgMatches<'_>,
conf: Config,
depool: &str,
wallet: &str,
keys: &str,
is_vesting: bool,
) -> Result<(), String> {
let (depool, wallet, keys) = (Some(depool), Some(wallet), Some(keys));
let donor = Some(m.value_of("DONOR")
.ok_or("donor is not defined.".to_string())?);
print_args!(m, depool, wallet, keys, donor);
set_donor(conf, depool.unwrap(), wallet.unwrap(), keys.unwrap(), is_vesting, donor.unwrap()).await
}

async fn exotic_stake_command(
m: &ArgMatches<'_>,
cmd: CommandData<'_>,
Expand Down Expand Up @@ -544,6 +589,19 @@ async fn set_withdraw(
send_with_body(conf, wallet, depool, &value, keys, &body).await
}

async fn set_donor(
conf: Config,
depool: &str,
wallet: &str,
keys: &str,
is_vesting: bool,
donor: &str,
) -> Result<(), String> {
let body = encode_set_donor(is_vesting, donor).await?;
let value = conf.depool_fee.to_string();
send_with_body(conf, wallet, depool, &value, keys, &body).await
}

async fn encode_body(func: &str, params: serde_json::Value) -> Result<String, String> {
let client = create_client_local()?;
ton_client::abi::encode_message_body(
Expand Down Expand Up @@ -596,6 +654,18 @@ async fn encode_add_vesting_stake(
})).await
}

async fn encode_set_donor(is_vesting: bool, donor: &str) -> Result<String, String> {
if is_vesting {
encode_body("setVestingDonor", json!({
"donor": donor
}))
} else {
encode_body("setLockDonor", json!({
"donor": donor
}))
}.await
}

async fn encode_add_lock_stake(
stake: u64,
beneficiary: &str,
Expand Down
36 changes: 31 additions & 5 deletions src/depool_abi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@
* limitations under the License.
*/

pub const DEPOOL_ABI: &str = r#"
{
pub const DEPOOL_ABI: &str = r#"
{
"ABI version": 2,
"header": ["time", "expire"],
"functions": [
Expand Down Expand Up @@ -88,6 +88,22 @@
"outputs": [
]
},
{
"name": "setVestingDonor",
"inputs": [
{"name":"donor","type":"address"}
],
"outputs": [
]
},
{
"name": "setLockDonor",
"inputs": [
{"name":"donor","type":"address"}
],
"outputs": [
]
},
{
"name": "transferStake",
"inputs": [
Expand Down Expand Up @@ -215,7 +231,9 @@
{"name":"reward","type":"uint64"},
{"name":"stakes","type":"map(uint64,uint64)"},
{"components":[{"name":"remainingAmount","type":"uint64"},{"name":"lastWithdrawalTime","type":"uint64"},{"name":"withdrawalPeriod","type":"uint32"},{"name":"withdrawalValue","type":"uint64"},{"name":"owner","type":"address"}],"name":"vestings","type":"map(uint64,tuple)"},
{"components":[{"name":"remainingAmount","type":"uint64"},{"name":"lastWithdrawalTime","type":"uint64"},{"name":"withdrawalPeriod","type":"uint32"},{"name":"withdrawalValue","type":"uint64"},{"name":"owner","type":"address"}],"name":"locks","type":"map(uint64,tuple)"}
{"components":[{"name":"remainingAmount","type":"uint64"},{"name":"lastWithdrawalTime","type":"uint64"},{"name":"withdrawalPeriod","type":"uint32"},{"name":"withdrawalValue","type":"uint64"},{"name":"owner","type":"address"}],"name":"locks","type":"map(uint64,tuple)"},
{"name":"vestingDonor","type":"address"},
{"name":"lockDonor","type":"address"}
]
},
{
Expand Down Expand Up @@ -257,7 +275,7 @@
"inputs": [
],
"outputs": [
{"components":[{"name":"id","type":"uint64"},{"name":"supposedElectedAt","type":"uint32"},{"name":"unfreeze","type":"uint32"},{"name":"stakeHeldFor","type":"uint32"},{"name":"vsetHashInElectionPhase","type":"uint256"},{"name":"step","type":"uint8"},{"name":"completionReason","type":"uint8"},{"name":"stake","type":"uint64"},{"name":"recoveredStake","type":"uint64"},{"name":"unused","type":"uint64"},{"name":"isValidatorStakeCompleted","type":"bool"},{"name":"rewards","type":"uint64"},{"name":"participantQty","type":"uint32"},{"name":"validatorStake","type":"uint64"},{"name":"validatorRemainingStake","type":"uint64"},{"name":"handledStakesAndRewards","type":"uint64"}],"name":"rounds","type":"map(uint64,tuple)"}
{"components":[{"name":"id","type":"uint64"},{"name":"supposedElectedAt","type":"uint32"},{"name":"unfreeze","type":"uint32"},{"name":"stakeHeldFor","type":"uint32"},{"name":"vsetHashInElectionPhase","type":"uint256"},{"name":"step","type":"uint8"},{"name":"completionReason","type":"uint8"},{"name":"stake","type":"uint64"},{"name":"recoveredStake","type":"uint64"},{"name":"unused","type":"uint64"},{"name":"isValidatorStakeCompleted","type":"bool"},{"name":"participantReward","type":"uint64"},{"name":"participantQty","type":"uint32"},{"name":"validatorStake","type":"uint64"},{"name":"validatorRemainingStake","type":"uint64"},{"name":"handledStakesAndRewards","type":"uint64"}],"name":"rounds","type":"map(uint64,tuple)"}
]
}
],
Expand Down Expand Up @@ -308,7 +326,7 @@
{
"name": "RoundCompleted",
"inputs": [
{"components":[{"name":"id","type":"uint64"},{"name":"supposedElectedAt","type":"uint32"},{"name":"unfreeze","type":"uint32"},{"name":"stakeHeldFor","type":"uint32"},{"name":"vsetHashInElectionPhase","type":"uint256"},{"name":"step","type":"uint8"},{"name":"completionReason","type":"uint8"},{"name":"stake","type":"uint64"},{"name":"recoveredStake","type":"uint64"},{"name":"unused","type":"uint64"},{"name":"isValidatorStakeCompleted","type":"bool"},{"name":"rewards","type":"uint64"},{"name":"participantQty","type":"uint32"},{"name":"validatorStake","type":"uint64"},{"name":"validatorRemainingStake","type":"uint64"},{"name":"handledStakesAndRewards","type":"uint64"}],"name":"round","type":"tuple"}
{"components":[{"name":"id","type":"uint64"},{"name":"supposedElectedAt","type":"uint32"},{"name":"unfreeze","type":"uint32"},{"name":"stakeHeldFor","type":"uint32"},{"name":"vsetHashInElectionPhase","type":"uint256"},{"name":"step","type":"uint8"},{"name":"completionReason","type":"uint8"},{"name":"stake","type":"uint64"},{"name":"recoveredStake","type":"uint64"},{"name":"unused","type":"uint64"},{"name":"isValidatorStakeCompleted","type":"bool"},{"name":"participantReward","type":"uint64"},{"name":"participantQty","type":"uint32"},{"name":"validatorStake","type":"uint64"},{"name":"validatorRemainingStake","type":"uint64"},{"name":"handledStakesAndRewards","type":"uint64"}],"name":"round","type":"tuple"}
],
"outputs": [
]
Expand Down Expand Up @@ -338,6 +356,14 @@
],
"outputs": [
]
},
{
"name": "InternalError",
"inputs": [
{"name":"ec","type":"uint16"}
],
"outputs": [
]
}
]
}
Expand Down
74 changes: 68 additions & 6 deletions tests/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ fn test_depool() -> Result<(), Box<dyn std::error::Error>> {
let giver_addr = "0:841288ed3b55d9cdafa806807f02a0ae0c169aa5edfe88a789a6482429756a94";
let depool_abi = "tests/samples/fakeDepool.abi.json";
let depool_tvc = "tests/samples/fakeDepool.tvc";
let depool_addr = "0:2e66e882d24b087cadbc075d6cea394241c50b7111dab080b1677d304cef1b8e";
let depool_addr = "0:cf72b41b704b7173467ffcd2c7bbc2a30d251996e3e3d848a74f9f72c8bc65e6";
let msig_abi = "ton-labs-contracts/solidity/safemultisig/SafeMultisigWallet.abi.json";
let msig_tvc = "ton-labs-contracts/solidity/safemultisig/SafeMultisigWallet.tvc";
let msig_addr = "0:507fc74745d5a259b9939dfbdfd97cd186d13e8a7160206f3054746c1f0518cd";
Expand All @@ -236,7 +236,7 @@ fn test_depool() -> Result<(), Box<dyn std::error::Error>> {
.arg(giver_addr)
.arg("sendGrams")
// use precalculated depool address
.arg(r#"{"dest":"0:2e66e882d24b087cadbc075d6cea394241c50b7111dab080b1677d304cef1b8e","amount":10000000000}"#);
.arg(r#"{"dest":"0:cf72b41b704b7173467ffcd2c7bbc2a30d251996e3e3d848a74f9f72c8bc65e6","amount":10000000000}"#);
cmd.assert()
.success();

Expand Down Expand Up @@ -283,7 +283,7 @@ fn test_depool() -> Result<(), Box<dyn std::error::Error>> {
#[test]
fn test_depool_1() -> Result<(), Box<dyn std::error::Error>> {
let depool_abi = "tests/samples/fakeDepool.abi.json";
let depool_addr = "0:2e66e882d24b087cadbc075d6cea394241c50b7111dab080b1677d304cef1b8e";
let depool_addr = "0:cf72b41b704b7173467ffcd2c7bbc2a30d251996e3e3d848a74f9f72c8bc65e6";
let msig_addr = "0:507fc74745d5a259b9939dfbdfd97cd186d13e8a7160206f3054746c1f0518cd";
let seed_phrase = "blanket time net universe ketchup maid way poem scatter blur limit drill";

Expand Down Expand Up @@ -346,13 +346,20 @@ fn test_depool_1() -> Result<(), Box<dyn std::error::Error>> {
.stdout(predicate::str::contains(r#"StakeSigningRequested"#))
.stdout(predicate::str::contains(r#"{"electionId":"1","proxy":"0:0000000000000000000000000000000000000000000000000000000000000002"}"#));

let mut cmd = Command::cargo_bin(BIN_NAME)?;
cmd.arg("depool")
.arg("events")
.arg("-w");
cmd.assert()
.success();

Ok(())
}

#[test]
fn test_depool_2() -> Result<(), Box<dyn std::error::Error>> {
let depool_abi = "tests/samples/fakeDepool.abi.json";
let depool_addr = "0:2e66e882d24b087cadbc075d6cea394241c50b7111dab080b1677d304cef1b8e";
let depool_addr = "0:cf72b41b704b7173467ffcd2c7bbc2a30d251996e3e3d848a74f9f72c8bc65e6";
let seed_phrase = "blanket time net universe ketchup maid way poem scatter blur limit drill";

let mut cmd = Command::cargo_bin(BIN_NAME)?;
Expand Down Expand Up @@ -420,7 +427,7 @@ fn test_depool_2() -> Result<(), Box<dyn std::error::Error>> {
fn test_depool_3() -> Result<(), Box<dyn std::error::Error>> {
let giver_addr = "0:841288ed3b55d9cdafa806807f02a0ae0c169aa5edfe88a789a6482429756a94";
let depool_abi = "tests/samples/fakeDepool.abi.json";
let depool_addr = "0:2e66e882d24b087cadbc075d6cea394241c50b7111dab080b1677d304cef1b8e";
let depool_addr = "0:cf72b41b704b7173467ffcd2c7bbc2a30d251996e3e3d848a74f9f72c8bc65e6";
let seed_phrase = "blanket time net universe ketchup maid way poem scatter blur limit drill";

let mut cmd = Command::cargo_bin(BIN_NAME)?;
Expand Down Expand Up @@ -546,7 +553,7 @@ fn test_depool_3() -> Result<(), Box<dyn std::error::Error>> {
#[test]
fn test_depool_4() -> Result<(), Box<dyn std::error::Error>> {
let depool_abi = "tests/samples/fakeDepool.abi.json";
let depool_addr = "0:2e66e882d24b087cadbc075d6cea394241c50b7111dab080b1677d304cef1b8e";
let depool_addr = "0:cf72b41b704b7173467ffcd2c7bbc2a30d251996e3e3d848a74f9f72c8bc65e6";
let seed_phrase = "blanket time net universe ketchup maid way poem scatter blur limit drill";

let mut cmd = Command::cargo_bin(BIN_NAME)?;
Expand Down Expand Up @@ -602,6 +609,61 @@ fn test_depool_4() -> Result<(), Box<dyn std::error::Error>> {
Ok(())
}

#[test]
fn test_depool_5() -> Result<(), Box<dyn std::error::Error>> {
let depool_abi = "tests/samples/fakeDepool.abi.json";
let depool_addr = "0:cf72b41b704b7173467ffcd2c7bbc2a30d251996e3e3d848a74f9f72c8bc65e6";
let seed_phrase = "blanket time net universe ketchup maid way poem scatter blur limit drill";

let mut cmd = Command::cargo_bin(BIN_NAME)?;
cmd.arg("depool")
.arg("donor")
.arg("lock")
.arg("--sign")
.arg(seed_phrase)
.arg("--donor")
.arg("0:0123456789012345012345678901234501234567890123450123456789012345");
cmd.assert()
.success()
.stdout(predicate::str::contains(r#"transId": "0"#));

let mut cmd = Command::cargo_bin(BIN_NAME)?;
cmd.arg("run")
.arg("--abi")
.arg(depool_abi)
.arg(depool_addr)
.arg("getData")
.arg("{}");
cmd.assert()
.success()
.stdout(predicate::str::contains(r#"receiver": "0:0123456789012345012345678901234501234567890123450123456789012345"#));

let mut cmd = Command::cargo_bin(BIN_NAME)?;
cmd.arg("depool")
.arg("donor")
.arg("vesting")
.arg("--sign")
.arg(seed_phrase)
.arg("--donor")
.arg("0:0123456789012345012345678901234501234567890123450123456789012346");
cmd.assert()
.success()
.stdout(predicate::str::contains(r#"transId": "0"#));

let mut cmd = Command::cargo_bin(BIN_NAME)?;
cmd.arg("run")
.arg("--abi")
.arg(depool_abi)
.arg(depool_addr)
.arg("getData")
.arg("{}");
cmd.assert()
.success()
.stdout(predicate::str::contains(r#"receiver": "0:0123456789012345012345678901234501234567890123450123456789012346"#));

Ok(())
}

#[test]
fn test_callex() -> Result<(), Box<dyn std::error::Error>> {
let giver_abi_name = "tests/samples/giver.abi.json";
Expand Down
Binary file modified tests/data.tvc
Binary file not shown.
16 changes: 16 additions & 0 deletions tests/samples/fakeDepool.abi.json
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,22 @@
{"name":"value","type":"uint128"}
]
},
{
"name": "setVestingDonor",
"inputs": [
{"name":"donor","type":"address"}
],
"outputs": [
]
},
{
"name": "setLockDonor",
"inputs": [
{"name":"donor","type":"address"}
],
"outputs": [
]
},
{
"name": "constructor",
"inputs": [
Expand Down
Loading

0 comments on commit caf4858

Please sign in to comment.