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

Docs don't say how to specify the caller #172

Closed
ryancwalsh opened this issue Aug 4, 2022 · 4 comments
Closed

Docs don't say how to specify the caller #172

ryancwalsh opened this issue Aug 4, 2022 · 4 comments

Comments

@ryancwalsh
Copy link
Contributor

I've spent a long time looking through docs here and elsewhere and can't find how to call a function while specifying who is the signer (i.e. what is the account making the call)?

I'm trying to figure out how to fix this (see the "TODO" below).

I appreciate any hints. Thanks!

Ryan Walsh
NEAR Foundation Engineering

// https://github.com/near/workspaces-rs/blob/8f12f3dc3b0251ac3f44ddf6ab6fc63003579139/workspaces/tests/create_account.rs

#![recursion_limit = "256"]

use anyhow::Error;
use donation_matcher_contract::{
    generic::{near_string_to_yocto, yocto_to_near_string},
    GAS_FOR_ACCOUNT_CALLBACK,
};
use near_sdk::serde_json::json;
use test_log::test;
use workspaces::{network::Sandbox, prelude::*, Account, Worker};

async fn create_subaccount(
    worker: &Worker<Sandbox>,
    parent_account: &Account,
    name: &str,
    initial_balance: &str,
) -> Result<Account, Error> {
    let subaccount = parent_account
        .create_subaccount(&worker, name)
        .initial_balance(near_string_to_yocto(initial_balance.to_string()))
        .transact()
        .await?
        .into_result()?;

    Ok(subaccount)
}

#[test(tokio::test)]
async fn test_offer_matching_funds_and_get_commitments_and_rescind_matching_funds_and_donate(
) -> anyhow::Result<()> {
    let worker = workspaces::sandbox().await?;
    let contract = worker
        .dev_deploy(&include_bytes!("../target/res/donation_matcher_contract.wasm").to_vec())
        .await?;
    let parent_account = worker.dev_create_account().await?;
    contract
        .call(&worker, "new")
        //.args_json(json!({"recipient": &recipient.id()}))?
        .gas(GAS_FOR_ACCOUNT_CALLBACK.0)
        .transact()
        .await?;

    let recipient = create_subaccount(&worker, &parent_account, "recipient", "1 Ⓝ").await?;

    assert_eq!(
        yocto_to_near_string(&recipient.view_account(&worker).await?.balance),
        "1 Ⓝ".to_string()
    );

    let matcher1 = create_subaccount(&worker, &parent_account, "matcher1", "1 Ⓝ").await?;

    // TODO: How to set matcher1 as the caller.
    let _matcher1_offer_result = contract
        .call(&worker, "offer_matching_funds")
        .args_json(json!({"recipient": &recipient.id()}))?
        .gas(GAS_FOR_ACCOUNT_CALLBACK.0)
        .deposit(near_string_to_yocto("0.3".to_string()))
        .transact()
        .await?;

    assert_eq!(
        yocto_to_near_string(&recipient.view_account(&worker).await?.balance),
        "1 Ⓝ".to_string()
    ); // The recipient hasn't received any donation yet.
    assert_eq!(
        yocto_to_near_string(&matcher1.view_account(&worker).await?.balance),
        "0.7 Ⓝ".to_string()
    );
    // ...

    Ok(())
}

@willemneal
Copy link

willemneal commented Aug 4, 2022

You need to use a normal account.

See https://github.com/AhaLabs/bootme/blob/feat/launcher/tests/workspaces/bootloader.rs#L30-L35

You can also use a transaction builder.

@ChaoticTempest
Copy link
Member

For your example, all you need to do to specify matcher1 as the signer is to directly use it and call into call from it like so:

matcher1.call(&worker, contract.id(), "offer_matching_funds")
   ... // rest of the options
   .transact()
   .await?

@ryancwalsh
Copy link
Contributor Author

ryancwalsh commented Aug 8, 2022

Thanks @willemneal and @ChaoticTempest .

That helped. Once I figure out more of Workspaces, I'll be in a better position to suggest specifically how to improve the docs. But yeah I definitely think this tip belongs in there.

ryancwalsh added a commit to ryancwalsh/workspaces-rs that referenced this issue Aug 10, 2022
ChaoticTempest added a commit that referenced this issue Aug 16, 2022
* added a section to Readme to show how to check account balances

See #172

* Update README.md with better example of balance check from @ChaoticTempest

Co-authored-by: Phuong Nguyen <[email protected]>

* Using `max_gas()` in the new Common Usage section of Readme

Co-authored-by: Phuong Nguyen <[email protected]>
ChaoticTempest added a commit that referenced this issue Aug 16, 2022
* added a section to Readme to show how to check account balances

See #172

* Update README.md with better example of balance check from @ChaoticTempest

Co-authored-by: Phuong Nguyen <[email protected]>

* Using `max_gas()` in the new Common Usage section of Readme

Co-authored-by: Phuong Nguyen <[email protected]>
@ChaoticTempest
Copy link
Member

closed with #186

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants