Abstract Client

As previously mentioned you can use our abstract-client package to interact with any instance of Abstract. For this example we’ll use the Mock environment for simplicity. However, the same functions can be used for any CwEnv.

You can read the abstract-client documentation for more information.

Example

#![allow(unused)]
fn main() {
    // Create environment
    let env: MockBech32 = MockBech32::new("mock");
    let sender: Addr = env.sender_addr();

    // Build the client
    let client: AbstractClient<MockBech32> = AbstractClient::builder(env.clone()).build()?;
}

These three lines:

  • Created a mock environment to deploy to.
  • Deployed Abstract to that environment and returned a client.

You can then start using the client to do all sorts of things. For example, you can set and query balances easily.

#![allow(unused)]
fn main() {
    let coins = &[Coin::new(50u128, "eth"), Coin::new(20u128, "btc")];

    // Set a balance
    client.set_balance(&sender, coins)?;

    // Add to an address's balance
    client.add_balance(&sender, &[Coin::new(50u128, "eth")])?;

    // Query an address's balance
    let coin1_balance = client.query_balance(&sender, "eth")?;

    assert_eq!(coin1_balance.u128(), 100);
}

Then, you can use the client to create a Publisher to publish an App to the platform.

#![allow(unused)]
fn main() {
    // Create a publisher
    let publisher: Publisher<MockBech32> = client
        .account_builder()
        .namespace(Namespace::from_id(TEST_MODULE_ID)?)
        .build()?
        .publisher()?;

    // Publish an app
    publisher.publish_app::<MockAppI<MockBech32>>()?;
}

Now that the App is published anyone can create an Account and install it!

#![allow(unused)]
fn main() {
    let accounti: Account<MockBech32> = client.account_builder().build()?;

    // Install an app
    let app: Application<MockBech32, MockAppI<MockBech32>> =
        accounti.install_app::<MockAppI<MockBech32>>(&MockInitMsg {}, &[])?;
}

Et voila! You’ve just deployed Abstract and an App to a mock environment. You can now start testing your module.

The Account object also has some useful helper methods:

#![allow(unused)]
fn main() {
    // Get account info
    let account_info: AccountInfo = accounti.info()?;
    // Get the owner
    let owner: Addr = accounti.owner()?;
    // Add or set balance
    accounti.add_balance(&[Coin::new(100u128, "btc")])?;
    // ...
}

You can explore more of its functions in the type’s documentation.

Your App Interface

The Application<_, MockAppI<_>> object returned from the install_app function is a wrapper around an Account that has an App installed on it (in this case MockAppI).

The MockAppI is a cw-orchestrator interface that exposes the contract’s functions as methods. This allows you to easily interact with your module directly or as a different address.

#![allow(unused)]
fn main() {
    // Install an app
    let app: Application<MockBech32, MockAppI<MockBech32>> =
        accounti.install_app::<MockAppI<MockBech32>>(&MockInitMsg {}, &[])?;
    // Call a function on the app
    app.do_something()?;

    // Call as someone else
    let account: Addr = accounti.address()?;
    app.call_as(&account).do_something()?;

    // Query the app
    let something: MockQueryResponse = app.get_something()?;
}