Crate abstract_sdk

Source
Expand description

§Abstract-SDK

This crate provides a set of modular APIs for developers to use in their CosmWasm smart-contracts.

§Getting started

To get started with the Abstract SDK you first need to understand the basic features that we provide and how you can use those features to create composable smart-contract APIs.

§Features

Abstract features are traits that can be implemented on a struct. Depending on the use-case that struct can represent a smart-contract or it can be a simple struct that just implements a single feature. Each feature unlocks a function on the object which allows you to retrieve some information. By composing these features it is possible to write advanced APIs that are automatically implemented on objects that support its required features.

§APIs

The Abstract APIs are objects that can only be retrieved if a contract or feature-object implements the required features/api traits. If the trait constraints for the Adapter is met it is automatically implemented on the object and allows you to retrieve the Adapter object.

§Example

The Bank Adapter allows developers to transfer assets from and to the Account through their module object. We now want to use this Adapter to create a Splitter API that splits the transfer of some amount of funds between a set of receivers.

use abstract_sdk::{TransferInterface,AbstractSdkResult, Execution, AccountAction};
use abstract_std::objects::AnsAsset;
use cosmwasm_std::{Addr, CosmosMsg, Deps, StdResult, Uint128, Env};

// Trait to retrieve the Splitter object
// Depends on the ability to transfer funds
pub trait SplitterInterface: TransferInterface {
    fn splitter<'a>(&'a self, deps: Deps<'a>) -> Splitter<'a, Self> {
        Splitter { base: self, deps }
    }
}

// Implement for every object that can transfer funds
impl<T> SplitterInterface for T where T: TransferInterface {}

#[derive(Clone)]
pub struct Splitter<'a, T: SplitterInterface> {
    base: &'a T,
    deps: Deps<'a>,
}

impl<'a, T: SplitterInterface> Splitter<'a, T> {
    /// Split an asset to multiple users
    pub fn split(&self, env: &Env, asset: AnsAsset, receivers: &[Addr]) -> AbstractSdkResult<AccountAction> {
        // split the asset between all receivers
        let receives_each = AnsAsset {
            amount: asset
                .amount
                .multiply_ratio(Uint128::one(), Uint128::from(receivers.len() as u128)),
            ..asset
        };

        // Retrieve the bank API
        let bank = self.base.bank(self.deps);
        receivers
            .iter()
            .map(|receiver| {
                // Construct the transfer message
                bank.transfer(vec![&receives_each], receiver)
            })
            .try_fold(AccountAction::default(), |mut acc, v| match v {
                Ok(action) => {
                    acc.merge(action);
                    Ok(acc)
                }
                Err(e) => Err(e),
            })
    }
}

The API can then be used by any contract that implements its required traits, in this case the TransferInterface.

  use abstract_sdk::TransferInterface;

  fn forward_deposit(deps: Deps, my_contract: MyContract, message_info: MessageInfo) -> AbstractSdkResult<Vec<CosmosMsg>> {
      let forward_deposit_msg = my_contract.bank(deps).deposit(message_info.funds)?;

      Ok(forward_deposit_msg)
  }

§Abstract Base

To use an API either construct a feature object or use an Abstract base contract as the starting-point of your application.
The available base contracts are:

KindMigratableInstallable
App
Adapter

Each base supports a set of endpoints that can accept custom handlers. These handlers can be added to the base using a static builder pattern. All the available endpoints are discussed here.

§Usage

Add abstract-sdk to your Cargo.toml by running:

cargo add abstract-sdk

Re-exports§

pub use crate::features::AbstractNameServiceClient;
pub use abstract_std as std;

Modules§

base
Base of an Abstract Module and its features.
cw_helpers
Helper functions and objects for working with the CosmWasm framework.
feature_objects
Structs that implement a feature trait
features
Feature traits
namespaces
Common state-store namespaces.
prelude
SDK Prelude
register
Abstract reserved registry entries.

Macros§

execute_update_ownership
Macro to update the ownership of an Abstract contract.
query_ownership
Macro to query the ownership of a contract.

Structs§

AccountAction
Encapsulates an action on the account. When a method returns an AccountAction, this means this message needs to be dispatched to the account using the Execution api.
AccountRegistry
API for querying and verifying a sender’s identity in the context of Abstract Accounts.
Adapters
API for accessing Abstract Adapters installed on the account.
Apps
API for accessing Abstract Apps installed on the account.
Bank
API for transferring funds to and from the account.
EndpointError
Error type for the abstract module endpoints.
Executor
API for executing AccountActions on the Account. Group your actions together in a single execute call if possible.
ExecutorMsg
CosmosMsg from the executor methods
HookMemoBuilder
Builder for IbcHooks memo field.
IbcClient
API for interacting with the Abstract IBC client.
ModuleRegistry
API for querying module information from the Abstract registry contract.
Modules
API for retrieving information about installed modules.
PfmMemoBuilder
Builder for Packet Forward Middleware memos.

Enums§

AbstractSdkError
Error type for the abstract sdk crate.

Traits§

AbstractResponse
Construct a Response with an abstract-specific event. The event contains the contract name and the action, and any additional attributes.
AccountVerification
Verify if an addresses is associated with an Abstract Account.
AdapterInterface
Interact with other modules on the Account.
AppInterface
Interact with other modules on the Account.
Execution
Execute an AccountAction on the Account.
IbcInterface
Interact with other chains over IBC.
ModuleInterface
Interact with other modules on the Account.
ModuleRegistryInterface
Access the Abstract Registry and access module information.
Resolve
Resolve an AbstractNameService entry into its value.
TransferInterface
Query and Transfer assets from and to the Abstract Account.
Transferable
Turn an object that represents an asset into the blockchain representation of an asset, i.e. Asset.

Type Aliases§

AbstractSdkResult
Result returned by the Abstract SDK APIs and features.