#contract #query #interact #bindings #helper #blockchain #sei

sei-cosmwasm

Bindings and helpers for cosmwasm contracts to interact with sei blockchain

17 releases

0.4.15 Apr 22, 2024
0.4.12 Oct 23, 2023
0.4.10 Feb 23, 2023
0.4.9 Nov 23, 2022
0.2.0 Jul 21, 2022

#60 in #contract

Download history 7/week @ 2024-05-07 10/week @ 2024-05-14 12/week @ 2024-05-21 31/week @ 2024-05-28 16/week @ 2024-06-04 42/week @ 2024-06-11 42/week @ 2024-06-18 26/week @ 2024-06-25 133/week @ 2024-07-02 115/week @ 2024-07-09 68/week @ 2024-07-16 91/week @ 2024-07-23 19/week @ 2024-07-30 24/week @ 2024-08-06 8/week @ 2024-08-13 1/week @ 2024-08-20

52 downloads per month
Used in sei-integration-tests

Apache-2.0

62KB
1K SLoC

Sei Bindings for Cosmwasm Contracts

This crate provides Sei specific bindings for cosmwasm contract to be able to interact with the Sei blockchain by exposing custom messages, queries, and structs that correspond to custom module functionality in the Sei chain.

Installation

Add the sei-cosmwasm dependency to your smart contract's Cargo.toml file:

[dependencies]
sei-cosmwasm = { version = "0.4.15" }

Functionality

Currently, Sei Bindings support query and message support for the sei custom modules Oracle, Dex, Epoch and TokenFactory. The supported functionality includes the following:

  • Oracle
    • Query
      • ExchangeRates
        • Gets the exchange rates for supported assets
      • OracleTwaps
        • Gets the time weighted average price for supported assets
  • Dex
    • Query
      • DexTwaps
        • Gets time weighted average prices for assets on the specific order book
      • GetOrders
        • Get orders by a specific account on the dex order book
      • GetLatestPrice
        • Get latest price by a specific contract and asset pair
      • GetOrderById
        • Get individual order by order ID
    • Message
      • PlaceOrders
        • Bulk place orders with the dex order book
      • CancelOrders
        • Bulk cancel orders with the dex order book
  • Epoch
    • Query
      • Epoch
        • Get current epoch information
  • TokenFactory
    • Query
      • DenomAuthorityMetadata
        • Gets the denom authority metadata for a tokenfactory denom
      • DenomsFromCreator
        • Gets all the tokenfactory denoms from a creator
    • Message
      • CreateDenom
        • Creates a denom of type factory/{creator address}/{subdenom} given a subdenom.
      • MintTokens
        • Mint an amount of a factory denom. Only the creator of the denom (admin) can mint.
      • BurnTokens
        • Burns an amount of a factory denom. Only the creater of the denom (admin) can mint.
      • ChangeAdmin
        • Change the Admin of the Denom. Only the current admin can change the admin.
      • SetMetadata
        • Set the denom metadata of a factory denom. Only the current admin can set metadata.
  • EVM
    • Query
      • StaticCall
        • Generic query endpoint for EVM contracts
      • Erc20TransferPayload
        • Gets the Erc20 transfer payload from on recipient and amount
      • Erc20TransferFromPayload
        • Gets the Erc20 transfer from payload based on owner, recipient and amount
      • Erc20ApprovePayload
        • Gets the Erc20 approve payload from spender and amount
      • Erc20Allowance
        • Gets the Erc20 allowance from contract address, owner and spender
      • Erc20TokenInfo
        • Gets the Erc20 token info from contract address and caller
      • Erc20Balance
        • Gets the Erc20 balance from contract address and account
      • Erc721TransferPayload
        • Similar to the Erc20 equivalent
      • Erc721ApprovePayload
        • Similar to the Erc20 equivalent
      • Erc721Approved
        • Checks if a caller is approved to send an Erc721 on behalf of the owner. Requires contract address and token id
      • Erc721IsApprovedForAll
        • Checks if the caller is approved to operate all Erc721s on behalf of the owner. Requires caller, contract address, owner and operator
      • Erc721SetApprovalAllPayload
        • Gets the Erc721 SetApproveAll payload from caller, contract address, owner and operator
      • Erc721NameSymbol
        • Gets the Erc721 name and symbol based on the caller and contract address
      • Erc721Uri
        • Gets the Erc721 URI based on caller, contract_address and token_id
      • GetEvmAddress
        • Get the EVM address associated with a Sei address
      • GetSeiAddress
        • Get the Sei address associated with an EVM address
    • Message
      • DelegateCallEvm
        • Performs an EVM delegate call. Requires to and data
      • CallEvm
        • Performs an EVM call. Requires value, to and data

Usage

Querying

To use these custom queries with the sei chain, you can create an instance of SeiQuerier and call the query helper functions with the appropriate parameters.

let querier = SeiQuerier::new(&deps.querier);
let res: ExchangeRatesResponse = querier.query_exchange_rates()?;

Messages

To use the custom messages, the messages need to be returned in the contract response to be executed by the sei chain.

let test_order = sei_cosmwasm::SeiMsg::PlaceOrders {
    contract_address: env.contract.address,
    funds: vec![some_funds],
    orders: vec![some_order],
};
Ok(Response::new().add_message(test_order))

Tokenfactory

The tokenfactory supports any Sei user to create, mint, burn and change owner of custom tokens.

// create a new coin denom through the tokenfactory module.
// This will create a denom with fullname "factory/{creator address}/{subdenom}"
let test_create_denom = sei_cosmwasm::SeiMsg::CreateDenom {
    subdenom: "subdenom".to_string(),
};
Ok(Response::new().add_message(test_create_denom))


// mint a token and send to a designated receiver
// note here the denom name provided must be the fullname in format of "factory/{creator address}/{subdenom}"
let tokenfactory_denom =
    "factory/".to_string() + env.contract.address.to_string().as_ref() + "/subdenom";
let amount = coin(100, tokenfactory_denom);

let test_mint = sei_cosmwasm::SeiMsg::MintTokens {
    amount: amount.to_owned(),
};
let send_msg = SubMsg::new(BankMsg::Send {
    to_address: info.sender.to_string(),
    amount: vec![amount],
});

Ok(Response::new()
    .add_message(test_mint)
    .add_submessage(send_msg))


// burn a token, the denom name provided must be the fullname in format of "factory/{creator address}/{subdenom}"
let tokenfactory_denom =
    "factory/".to_string() + env.contract.address.to_string().as_ref() + "/subdenom";
let amount = coin(10, tokenfactory_denom);
let test_burn = sei_cosmwasm::SeiMsg::BurnTokens { amount };
Ok(Response::new().add_message(test_burn))

// change the owner of a token 
let tokenfactory_denom =
    "factory/".to_string() + env.contract.address.to_string().as_ref() + "/subdenom";
let new_admin_address = "${NEW_ADMIN_ADDRESS}".to_string();
let test_change_admin = sei_cosmwasm::SeiMsg::ChangeAdmin {
    denom: tokenfactory_denom,
    new_admin_address,
};
Ok(Response::new().add_message(test_change_admin))

Dependencies

~5.5–7.5MB
~158K SLoC