Skip to content

Commit

Permalink
refactor: Fix ref finance example
Browse files Browse the repository at this point in the history
  • Loading branch information
iho committed Oct 28, 2023
1 parent 9fd0001 commit 37728f5
Show file tree
Hide file tree
Showing 7 changed files with 26 additions and 21 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,7 @@ Following that we will have to init the contract again with our own metadata. Th
use near_token::NearToken;
let contract = worker
.import_contract(&contract_id, &testnet)
.initial_balance(NearToken::from_near(1000).as_yocto())
.initial_balance(NearToken::from_near(1000))
.block_height(BLOCK_HEIGHT)
.transact()
.await?;
Expand Down
1 change: 0 additions & 1 deletion examples/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ borsh = "0.10"
maplit = "1.0"
near-units = "0.2.0"
near-gas = { version = "0.2.3", features = ["serde", "borsh", "schemars"] }
near-token = { version = "0.1.0", features = ["serde", "borsh", "schemars"] }
near-jsonrpc-primitives = "0.17"
near-primitives = "0.17"
serde = "1.0"
Expand Down
17 changes: 11 additions & 6 deletions examples/src/croncat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,9 @@
// at a set amount of time we supply.

use near_gas::NearGas;
use near_token::NearToken;
use near_units::parse_near;
use serde::Deserialize;
use serde_json::json;
use workspaces::types::token::NearToken;

use workspaces::network::Sandbox;
use workspaces::{Account, AccountId, Contract, Worker};
Expand Down Expand Up @@ -107,7 +106,7 @@ pub async fn run_scheduled_tasks(
let outcome = agent
.call(contract.id(), "register_agent")
.args_json(json!({}))
.deposit("0.00226 N".parse()?)
.deposit("0.00226 N".parse::<NearToken>().unwrap().as_yoctonear())
.transact()
.await?;
println!("Registering agent outcome: {:#?}\n", outcome);
Expand Down Expand Up @@ -161,7 +160,10 @@ pub async fn run_scheduled_tasks(
.json::<Option<Agent>>()?
.unwrap();
println!("Agent details after completing task: {:#?}", agent_details);
assert_eq!(agent_details.balance, "0.00386 N".parse()?);
assert_eq!(
agent_details.balance,
"0.00386 N".parse::<NearToken>().unwrap().as_yoctonear()
);
let before_withdraw = agent_details.balance;

// Withdraw the reward from completing the task to our agent's account
Expand All @@ -181,7 +183,10 @@ pub async fn run_scheduled_tasks(
.json::<Option<Agent>>()?
.unwrap();
println!("Agent details after withdrawing task: {:#?}", agent_details);
assert_eq!(agent_details.balance, "0.00226 N".parse()?);
assert_eq!(
agent_details.balance,
"0.00226 N".parse::<NearToken>().unwrap().as_yoctonear()
);

// This shows how much the agent has profitted from executing the task:
println!(
Expand All @@ -192,7 +197,7 @@ pub async fn run_scheduled_tasks(
// Not that everything is done, let's cleanup and unregister the agent from doing anything.
agent
.call(contract.id(), "unregister_agent")
.deposit(parse_near!("1y"))
.deposit(NearToken::from_yoctonear(1).as_yoctonear())
.transact()
.await?
.into_result()?;
Expand Down
15 changes: 7 additions & 8 deletions examples/src/ref_finance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,9 @@ use std::collections::HashMap;
use std::convert::TryInto;

use near_gas::NearGas;
use near_token::NearToken;
use near_units::parse_near;
use serde_json::json;
use workspaces::network::Sandbox;
use workspaces::types::token::NearToken;
use workspaces::{Account, AccountId, Contract, Worker};
use workspaces::{BlockHeight, DevNetwork};

Expand Down Expand Up @@ -51,7 +50,7 @@ async fn create_ref(owner: &Account, worker: &Worker<Sandbox>) -> anyhow::Result
owner
.call(ref_finance.id(), "storage_deposit")
.args_json(json!({}))
.deposit(parse_near!("30 mN"))
.deposit(NearToken::from_millinear(30).as_yoctonear())
.transact()
.await?
.into_result()?;
Expand All @@ -78,7 +77,7 @@ async fn create_wnear(owner: &Account, worker: &Worker<Sandbox>) -> anyhow::Resu
owner
.call(wnear.id(), "storage_deposit")
.args_json(json!({}))
.deposit("0.008 N".parse()?)
.deposit(NearToken::from_millinear(8).as_yoctonear())
.transact()
.await?
.into_result()?;
Expand Down Expand Up @@ -119,7 +118,7 @@ async fn create_pool_with_liquidity(
"tokens": token_ids,
"fee": 25
}))
.deposit(parse_near!("3 mN"))
.deposit(NearToken::from_millinear(3).as_yoctonear())
.transact()
.await?
.json()?;
Expand Down Expand Up @@ -199,7 +198,7 @@ async fn create_custom_ft(
ft.call("new_default_meta")
.args_json(json!({
"owner_id": owner.id(),
"total_supply": NearToken::from_near(1_000_000_000).as_yoctonear() ,
"total_supply": NearToken::from_near(1_000_000_000),
}))
.transact()
.await?
Expand Down Expand Up @@ -295,7 +294,7 @@ async fn main() -> anyhow::Result<()> {
"pool_id": pool_id,
"token_in": ft.id(),
"token_out": wnear.id(),
"amount_in": NearToken::from_near(10).to_string(),
"amount_in": NearToken::from_near(1),
}))
.await?
.json()?;
Expand All @@ -313,7 +312,7 @@ async fn main() -> anyhow::Result<()> {
"pool_id": pool_id,
"token_in": ft.id(),
"token_out": wnear.id(),
"amount_in": NearToken::from_near(1).as_yoctonear().to_string(),
"amount_in": NearToken::from_near(1),
"min_amount_out": "1",
})],
}))
Expand Down
1 change: 1 addition & 0 deletions workspaces/src/types/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ pub(crate) mod chunk;

#[cfg(feature = "interop_sdk")]
mod sdk;
pub mod token;

use std::convert::TryFrom;
use std::fmt::{self, Debug, Display};
Expand Down
1 change: 1 addition & 0 deletions workspaces/src/types/token.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pub use near_token::NearToken;
10 changes: 5 additions & 5 deletions workspaces/tests/cross_contract.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use near_token::NearToken;
use workspaces::result::ExecutionFinalResult;
use workspaces::types::token::NearToken;
use workspaces::{AccountId, Contract};

/// The factory contract used in these tests can be found in
Expand All @@ -10,12 +10,12 @@ const FACTORY_CONTRACT: &[u8] =
/// Create a new contract account through a cross contract call with "deploy_status_message".
async fn cross_contract_create_contract(
status_id: &AccountId,
status_amt: &u128,
status_amt: &NearToken,
contract: &Contract,
) -> anyhow::Result<ExecutionFinalResult> {
contract
.call("deploy_status_message")
.args_json((status_id.clone(), status_amt.to_string()))
.args_json((status_id.clone(), status_amt.as_yoctonear().to_string()))
.deposit(NearToken::from_near(50).as_yoctonear())
.max_gas()
.transact()
Expand All @@ -27,7 +27,7 @@ async fn cross_contract_create_contract(
async fn test_cross_contract_create_contract() -> anyhow::Result<()> {
let worker = workspaces::sandbox().await?;
let contract = worker.dev_deploy(FACTORY_CONTRACT).await?;
let status_amt = NearToken::from_near(35).as_yoctonear();
let status_amt = NearToken::from_near(35);

// Expect to fail for trying to create a new contract account with too short of a
// top level account name, such as purely just "status"
Expand Down Expand Up @@ -58,7 +58,7 @@ async fn test_cross_contract_create_contract() -> anyhow::Result<()> {
async fn test_cross_contract_calls() -> anyhow::Result<()> {
let worker = workspaces::sandbox().await?;
let contract = worker.dev_deploy(FACTORY_CONTRACT).await?;
let status_amt = NearToken::from_near(35).as_yoctonear();
let status_amt = NearToken::from_near(35);

let status_id: AccountId = "status-top-level-account-long-name".parse().unwrap();
cross_contract_create_contract(&status_id, &status_amt, &contract)
Expand Down

0 comments on commit 37728f5

Please sign in to comment.