#cosmwasm #module #contract #bindings #query #bit-badges #bitbadges

bitbadges-cosmwasm

Bindings for CosmWasm contracts to call into custom modules of BitBadges Node

9 releases

0.0.10 Aug 12, 2024
0.0.9 Apr 13, 2024
0.0.8 Jan 15, 2024
0.0.3 Nov 27, 2023
0.0.2 Apr 23, 2023

#42 in #cosmwasm

21 downloads per month

Apache-2.0

40KB
837 lines

BitBadges bindings for CosmWasm

This crate provides the custom bindings that are used to communicate with the custom modules (x/badges) on the BitBadges network from a CosmWasm smart contract.

Installation

Add the crate to your smart contract's' Cargo.toml

[dependencies]
bitbadges-cosmwasm = { version = "X.X.X" }

Exposed bindings

This crate, as of now, exports binding only for the x/badges module. In the future, more custom bindings will be added.

Creating Messages

NOTE: The BitBadges bindings do not cover messages that have already been implemented by the CosmWasm team, such as staking-related messages and fundamental ones like MsgSend. ​ You may want your contract to perform messages such as DeleteCollection and TransferBadges operations at the end of its execution. To do this, create a message using the predefined functions: ​

  • transfer_badges_msg
  • ...

And add it to your response, like below ​

use cosmwasm_std::CosmosMsg;
use bitbadges_cosmwasm::{transfer_badges_msg};...pub fn execute_msg_transfer_badges(
    collection_id: String,
    transfers: Vec<Transfer>,
) -> StdResult<Response<BitBadgesMsg>> {
    let msg = transfer_badges_msg(
        collection_id,
        transfers,
    );

    Ok(Response::new().add_message(msg))
}

Querying

​ In order to use the query functions enabled by the bindings, create a BitBadgesQuerier instance within your contract logic -- in either init() or query() entrypoints. You can access all the enabled queries through this object. ​

// src/contract.rs
use bitbadges_cosmwasm::{ BitBadgesQuerier };...pub fn query_collection(deps: Deps, collection_id: String) -> StdResult<BadgeCollection> {
    let querier = BitBadgesQuerier::new(&deps.querier);
    let res: BadgeCollection = querier.query_collection(collection_id)?;

    Ok(res)
}

Example

Please consult the example smart contracts in /contracts - there you can see an example how to issue a transaction or make a query from the smart contract to the custom module. You can upload it and interact with it (and through it - with the bitbadges chain) with the following steps. This assumes you have a local bitbadges chain running.

clonedDir='path/to/the/test/smart/contract/binary'
bitbadgeschaind tx wasm store $clonedDir/bindings_tester.wasm --from=<address> --chain-id=<chain-id> --gas=auto -y
INIT='{}'

# Find the code_id of your stored contract (in the raw_log of output of the previous command or can query with bitbadgeschaind query wasm list-code)

bitbadgeschaind tx wasm instantiate your_code_id $INIT --from=<address> --label="your_label" --chain-id=<chain-id> --gas=auto -y
TESTER=$(bitbadgeschaind query wasm list-contract-by-code $CODE --output json | jq -r '.contracts[-1]')
echo $TESTER

# NOTE: sender field in the queries should be the address of your contract, in this case - $TESTER
# issueDenom
# NOTE: schema is optional field

# NOTE: we convert to camelCase which should be used here
msgDetails='{
    "transferBadgesMsg": {
        "collectionId": "test",
        "transfers": [
            {
              ....
            }
        ]
    }
}'
bitbadgeschaind tx wasm execute $TESTER $msgDetails --from=<address> --chain-id=<chain-id> --gas=auto -y 

Acknowledgements

This repository was forked form the Cudos cosmwasm bindings. We would like to thank the Cudos team for their work on this project.

Dependencies

~3.5–5.5MB
~114K SLoC