7个版本
0.0.7 | 2022年5月3日 |
---|---|
0.0.6 | 2022年3月28日 |
0.0.4 | 2022年2月21日 |
#36 在 #cosmwasm-contracts
25KB
474 行
Cudos为CosmWasm提供的绑定
此crate提供用于从CosmWasm智能合约与Cudos网络上自定义模块通信的自定义绑定。目前仅公开了NFT模块绑定。
安装
将crate添加到您的智能合约的Cargo.toml文件中
[dependencies]
cudos-cosmwasm = { version = "0.0.4" }
公开的绑定
截至目前,此crate仅导出NFT模块的绑定。将来将添加更多自定义绑定。所有来自NFT Module
的命令都可在智能合约中调用。
创建消息
**注意**:Cudos绑定不包括CosmWasm团队已实现的消息,如与质押相关的消息和基本的如MsgSend
等消息。您可能希望您的合约在其执行结束时执行如IssueDenom
和MintNft
等操作。为此,使用预定义的函数创建消息:
create_issue_denom_msg
create_mint_nft_msg
create_edit_nft_msg
create_transfer_nft_msg
create_transfer_denom_msg
create_burn_nft_msg
create_approve_nft_msg
create_approve_all_msg
create_revoke_msg
并将它添加到您的响应中,如下所示
use cosmwasm_std::CosmosMsg;
use cudos_cosmwasm::{create_mint_nft_msg};
...
pub fn execute_msg_mint_nft(
_deps: DepsMut,
_env: Env,
_info: MessageInfo,
denom_id: String,
name: String,
uri: String,
data: String,
sender: String,
recipient: String,
) -> StdResult<Response<CudosMsg>> {
let msg = create_mint_nft_msg(denom_id, name, uri, data, sender, recipient);
Ok(Response::new().add_message(msg))
}
查询
为了使用由绑定启用的查询函数,在您的合约逻辑中创建一个CudosQuerier
实例——在init()
或query()
入口点中。您可以通过此对象访问所有启用的查询。
// src/contract.rs
use cudos_cosmwasm::{ CudosQuerier, DenomResponse, Denom};
...
pub fn query_denom_by_id(deps: Deps, denom_id: String) -> StdResult<DenomResponse> {
let querier = CudosQuerier::new(&deps.querier);
let res: DenomResponse = querier.query_denom_by_id(denom_id)?;
Ok(res)
}
示例
请参考/contracts/tester中的示例智能合约 - 那里您可以看到如何从智能合约向自定义模块发出交易或进行查询的示例。您可以通过以下步骤上传它并与之交互(以及通过它与Cudos链交互):
clonedDir='path/to/the/test/smart/contract/binary'
cudos-noded tx wasm store $clonedDir/bindings_tester.wasm --from=<address> --chain-id=<chain-id> --gas=auto -y
INIT='{}'
CODE='1'
cudos-noded tx wasm instantiate $CODE $INIT --from=<address> --label="tester" --chain-id=<chain-id> --gas=auto -y
TESTER=$(cudos-noded 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
issueDenomQuery='{
"issue_denom_msg": {
"id": "testdenom",
"name": "TESTDENOM",
"symbol": "testSymbol",
"schema": "testschema"
}
}'
cudos-noded tx wasm execute $TESTER $issueDenomQuery --from=<address> --chain-id=<chain-id> --gas=auto -y
# mint a NFT
# put the desired recipient address in the json below. Only denom_id, name and recipient are mandatory fields.
mintNft='{
"mint_nft_msg": {
"denom_id": "testdenom",
"name": "",
"uri": "",
"data": "",
"recipient": ""
}
}'
cudos-noded tx wasm execute $TESTER $mintNft --from=<address> --chain-id=<chain-id> --gas=auto -y
# edit a NFT
# NOTE: only denomId and tokenId are mandatory. You can provide some or all of the other optional fields
editNft='{
"edit_nft_msg": {
"denom_id": "testdenom",
"token_id": "1",
"name": "",
"uri": ""
}
}'
cudos-noded tx wasm execute $TESTER $editNft --from=<address> --chain-id=<chain-id> --gas=auto -y
# transfer a NFT
# put the desired addresses in from and to fields in the json below
transferNft='{
"transfer_nft_msg": {
"denom_id": "testdenom",
"token_id": "1",
"from": "",
"to": ""
}
}'
cudos-noded tx wasm execute $TESTER $transferNft --from=<address> --chain-id=<chain-id> --gas=auto -y
# transfer a NFT collection
# put the desired recipient adress in to field in the json below
transferDenom='{
"transfer_denom_msg": {
"denom_id": "testdenom",
"to": ""
}
}'
cudos-noded tx wasm execute $TESTER $transferDenom --from=<address> --chain-id=<chain-id> --gas=auto -y
# add approved address for a NFT
addApprovedAddress='{
"approve_nft_msg": {
"denom_id": "testdenom",
"token_id": "1",
"approved_address": ""
}
}'
cudos-noded tx wasm execute $TESTER $addApprovedAddress --from=<address> --chain-id=<chain-id> --gas=auto -y
# add approve all for an address. PLEASE NOTE the lack of quotes around the boolean.
addApproveAll='{
"approve_all_msg": {
"approved_operator": "",
"approved": true
}
}'
cudos-noded tx wasm execute $TESTER $addApproveAll --from=<address> --chain-id=<chain-id> --gas=auto -y
# revoke approval for a NFT
revokeApprovalNFT='{
"revoke_approval_msg": {
"denom_id": "testdenom",
"token_id": "1",
"address_to_revoke": ""
}
}'
cudos-noded tx wasm execute $TESTER $revokeNFT --from=<address> --chain-id=<chain-id> --gas=auto -y
# burn nft
burnNft='{
"burn_nft_msg": {
"denom_id": "testdenom",
"token_id": "1"
}
}'
cudos-noded tx wasm execute $TESTER $burnNft --from=<address> --chain-id=<chain-id> --gas=auto -y
# query a denom by ID
denomByIdQuery='{
"query_denom_by_id": {
"denom_id": "testdenom"
}
}'
cudos-noded query wasm contract-state smart $TESTER $denomByIdQuery --output json
# query a denom by Name
denomByNameQuery='{
"query_denom_by_name": {
"denom_name": "TESTDENOM"
}
}'
cudos-noded query wasm contract-state smart $TESTER $denomByNameQuery --output json
# query a denom by Symbol
denomBySymbolQuery='{
"query_denom_by_symbol": {
"denom_symbol": "testSymbol"
}
}'
cudos-noded query wasm contract-state smart $TESTER $denomBySymbolQuery --output json
# query all denoms
denomsQuery='{
"query_denoms": {}
}'
cudos-noded query wasm contract-state smart $TESTER $denomsQuery --output json
# query all NFTs related with a given denom
collectionQuery='{
"query_collection": {
"denom_id": "testdenom"
}
}'
cudos-noded query wasm contract-state smart $TESTER $collectionQuery --output json
# query the total count of minted NFTs from a given denom
supplyQuery='{
"query_supply": {
"denom_id": "testdenom"
}
}'
cudos-noded query wasm contract-state smart $TESTER $supplyQuery --output json
# query the NFTs owned by the given address from the given denom
# denom_id is mandatory field. If not provided, returns all NFTs from all denoms owned by the address
ownerQuery='{
"query_owner": {
"address": "",
"denom_id": "testdenom"
}
}'
cudos-noded query wasm contract-state smart $TESTER $ownerQuery --output json
# query for a NFT
nftQuery='{
"query_token": {
"denom_id": "testdenom",
"token_id": "1"
}
}'
cudos-noded query wasm contract-state smart $TESTER $nftQuery --output json
# query for the approved addresses associated with the given token of the denom
approvalsQuery='{
"query_approvals": {
"denom_id": "testdenom",
"token_id": "1"
}
}'
cudos-noded query wasm contract-state smart $TESTER $approvalsQuery --output json
# Query if an address is an authorized operator for another address
approvedForAllQuery='{
"query_approved_for_all": {
"owner_address": "",
"operator_address": ""
}
}'
cudos-noded query wasm contract-state smart $TESTER $approvedForAllQuery --output json
依赖项
~4.5–6MB
~130K SLoC