1 个不稳定版本
0.1.0 | 2024年3月4日 |
---|
#4 in #instantiate
73KB
1.5K SLoC
简单NFT合约
本模块可以作为导入和实现CW721规范的示例。为了确认正确运行,已实现基础规范的一小部分。有关其余实现的帮助,请参阅CW721-base。
区块链部署
为了测试,让我们使用malaga测试网。检查此页面以设置测试环境。
编译
- 要编译用于部署的智能合约,请使用以下命令(在M1 Mac上)
$ docker run --rm -v "$(pwd)":/code \
--mount type=volume,source="$(basename "$(pwd)")_cache",target=/code/target \
--mount type=volume,source=registry_cache,target=/usr/local/cargo/registry \
cosmwasm/rust-optimizer-arm64:0.12.6
这将在artifacts/文件夹中创建一个.wasm二进制文件。
部署
- 要将在区块链上存储二进制文件,请使用wasmd。
$ RES=$(wasmd tx wasm store artifacts/simple_nft-aarch64.wasm --from wallet $TXFLAG -y --output json -b block)
echo $RES | jq .
{
"height": "614976",
"txhash": "F8503588FCBC18F4EF22A3B52D2C3E0A15ED4A839AF92E5934F8A2EF518DA5E8",
"data": "0A250A1E2F636F736D7761736D2E7761736D2E76312E4D736753746F7265436F6465120308B302",
"raw_log": "[{\"events\":[{\"type\":\"message\",\"attributes\":[{\"key\":\"action\",\"value\":\"/cosmwasm.wasm.v1.MsgStoreCode\"},{\"key\":\"module\",\"value\":\"wasm\"},{\"key\":\"sender\",\"value\":\"wasm1g9urk8rj9news03dv7wfckcu49a6yk8z5rldwf\"}]},{\"type\":\"store_code\",\"attributes\":[{\"key\":\"code_id\",\"value\":\"307\"}]}]}]",
// more text
}
- 检索已上传二进制文件的代码ID。
$ CODE_ID=$(echo $RES | jq -r '.logs[0].events[-1].attributes[0].value')
$ echo $CODE_ID
307
区块链交互
任何智能合约都由3个动作组成:实例化、执行和查询。本合约已实现每个动作的一个示例。
实例化
- 创建实例化消息
$ INIT='{"name":"TestNFT","symbol":"TNFT"}'
- 实例化合约
$ wasmd tx wasm instantiate $CODE_ID $INIT --from wallet --label "Test simple NFT" $TXFLAG -y --no-admin
logs: []
raw_log: '[]'
txhash: A9B8E576F4B3AD2F183AFFB7A2E1593E00594E375B9C522AC5B0710984223C2D
- 检索合约地址
$ CONTRACT=$(wasmd query wasm list-contract-by-code $CODE_ID $NODE --output json | jq -r '.contracts[-1]')
$ echo $CONTRACT
wasm1ydfppp7qhh5m28zvwy2gk98j2hu8fs4ky3h4h0yt7rhwrymjqdlssh63sg
- 查询合约信息
$ wasmd query wasm contract $CONTRACT $NODE
address: wasm1ydfppp7qhh5m28zvwy2gk98j2hu8fs4ky3h4h0yt7rhwrymjqdlssh63sg
contract_info:
code_id: "307"
creator: wasm1g9urk8rj9news03dv7wfckcu49a6yk8z5rldwf
label: Test simple NFT
- 检索合约的状态。名称和符号应与INIT消息相同。请注意,发行者地址设置为实例化合约的地址。这样做是为了简化。
$ wasmd query wasm contract-state all $CONTRACT $NODE --output json | jq -r '.models[0].value' | base64 -d | jq .
{
"name": "TestNFT",
"symbol": "TNFT",
"minter": "wasm1g9urk8rj9news03dv7wfckcu49a6yk8z5rldwf",
"num_tokens": 0
}
执行
- 让我们发行新的代币
$ MINT='{"mint":{"token_id":1,"owner":"wasm1qka2er800suxsy7y9yz9wqgt8p3ktw5ptpf28s","token_uri":"None","price":[{"amount":"1000","denom":"umlg"}]}}'
$ echo $MINT | jq .
{
"mint": {
"token_id": 1,
"owner": "wasm1qka2er800suxsy7y9yz9wqgt8p3ktw5ptpf28s",
"token_uri": "None",
"price": [
{
"amount": "1000",
"denom": "umlg"
}
]
}
}
- 在合约中执行发行命令
$ wasmd tx wasm execute $CONTRACT "$MINT" --from wallet $TXFLAG -y
logs: []
raw_log: '[]'
txhash: 79C89F6CC934C2921B284458D7B0DC33AA9EFCD2A95BB5C54C217E8EF019FA3E
- 查看新发行的代币信息
$ wasmd query wasm contract-state all $CONTRACT $NODE --output json | jq -r '.models[0].value' | base64 -d | jq .
{
"owner": "wasm1qka2er800suxsy7y9yz9wqgt8p3ktw5ptpf28s",
"approvals": null,
"base_price": [
{
"denom": "umlg",
"amount": "1000"
}
],
"token_uri": null,
"token_id": 1
}
- 让我们以不同的所有者发行另一代币
$ MINT='{"mint":{"token_id":2,"owner":"wasm1g9urk8rj9news03dv7wfckcu49a6yk8z5rldwf","token_uri":"None","price":[{"amount":"1000","denom":"umlg"}]}}'
$ echo $MINT | jq .
{
"mint": {
"token_id": 2,
"owner": "wasm1g9urk8rj9news03dv7wfckcu49a6yk8z5rldwf",
"token_uri": "None",
"price": [
{
"amount": "1000",
"denom": "umlg"
}
]
}
}
- 执行发行命令
$ wasmd tx wasm execute $CONTRACT "$MINT" --from wallet $TXFLAG -y
gas estimate: 160602
logs: []
raw_log: '[]'
txhash: 5AE13707AD8C944607FEF23B43648F983CFBE298724A3EA4966934254BABB984
- 查看另一个新发行的代币
$ wasmd query wasm contract-state all $CONTRACT $NODE --output json | jq -r '.models[1].value' | base64 -d | jq .
{
"owner": "wasm1g9urk8rj9news03dv7wfckcu49a6yk8z5rldwf",
"approvals": null,
"base_price": [
{
"denom": "umlg",
"amount": "1000"
}
],
"token_uri": null,
"token_id": 2
}
- 将代币2转移给另一个所有者
$ EXECUTE='{"transfer_nft":{"recipient":"wasm1qka2er800suxsy7y9yz9wqgt8p3ktw5ptpf28s","token_id":2}}'
$ echo $EXECUTE | jq .
{
"transfer_nft": {
"recipient": "wasm1qka2er800suxsy7y9yz9wqgt8p3ktw5ptpf28s",
"token_id": 2
}
}
- 执行转移命令
$ wasmd tx wasm execute $CONTRACT "$EXECUTE" --from wallet $TXFLAG -y --output json | jq .
{
"txhash": "5CD670F4EF8A836E8AFDC28E84CFCEC4046174AA63C6899409856EF9C756B500",
"raw_log": "[]",
"logs": []
}
- 这两个代币现在应该属于同一个所有者。
$ wasmd query wasm contract-state all $CONTRACT $NODE --output json | jq -r '.models[0].value' | base64 -d | jq .
{
"owner": "wasm1qka2er800suxsy7y9yz9wqgt8p3ktw5ptpf28s",
"approvals": null,
"base_price": [
{
"denom": "umlg",
"amount": "1000"
}
],
"token_uri": null,
"token_id": 1
}
$ wasmd query wasm contract-state all $CONTRACT $NODE --output json | jq -r '.models[1].value' | base64 -d | jq .
{
"owner": "wasm1qka2er800suxsy7y9yz9wqgt8p3ktw5ptpf28s",
"approvals": null,
"base_price": [
{
"denom": "umlg",
"amount": "1000"
}
],
"token_uri": null,
"token_id": 2
}
查询
在此智能合约中实现了一个简单的查询,返回NFT的价格。
- 创建查询消息
$ QUERY='{"asking_price":{"token_id":1}}'
- 查询智能合约以获取价格
$ wasmd query wasm contract-state smart $CONTRACT "$QUERY" $NODE --output json | jq .
{
"data": {
"price": {
"denom": "umlg",
"amount": "1000"
}
}
}
智能查询是比使用命令:wasmd query wasm contract-state all $CONTRACT $NODE --output json | jq -r '.models[0].value' | base64 -d | jq .
更好的选择。这是因为.models[0].value
在执行过程中进行状态变更后,可能不会始终引用相同的值。
依赖项
~4–6MB
~125K SLoC