near nft example

先决条件

  • 安装了near-cli

获取example代码并编译wasm

git clone https://github.com/near-examples/NFT
cd NFT
./build.sh

检查res下的wasm是否生成

创建账户

浏览器进入https://wallet.testnet.near.org/,输入用户名pld2(替换成你自己的用户名),保存好助记词

登录

登录方式一

near login

登录方式二

没有浏览器的登录方式,创建access key

near generate-key pld2.testnet --seedPhrase="助记词"

检查~/.near-credentials/testnet/pld2.testnet.json文件是否创建好

部署智能合约

为了更易于复制/粘贴,我们将为我们的帐户 ID 设置一个环境变量。

ID=pld2.testnet

检查环境变量是否生效

echo $ID

执行部署

near deploy --wasmFile res/non_fungible_token.wasm --accountId $ID

返回内容如下

Starting deployment. Account id: pld2.testnet, node: https://rpc.testnet.near.org, helper: https://helper.testnet.near.org, file: res/non_fungible_token.wasm
Transaction Id HTFC3FbEc3jg3yFM4oShQdQRcg1S3Ts6Mmx8LKvWNWxk
To see the transaction in the transaction explorer, please open this url in your browser
https://explorer.testnet.near.org/transactions/HTFC3FbEc3jg3yFM4oShQdQRcg1S3Ts6Mmx8LKvWNWxk
Done deploying to pld2.testnet

区块浏览器tx:
在这里插入图片描述

初始化元数据

near call $ID new_default_meta '{"owner_id": "'$ID'"}' --accountId $ID

返回内容如下

Scheduling a call: pld2.testnet.new_default_meta({"owner_id": "pld2.testnet"})
Transaction Id CNBLQC35snZLnUzc3VQMBZysUTc8HxjXtsL4cEJaSo3
To see the transaction in the transaction explorer, please open this url in your browser
https://explorer.testnet.near.org/transactions/CNBLQC35snZLnUzc3VQMBZysUTc8HxjXtsL4cEJaSo3

区块浏览器tx:
在这里插入图片描述

查看元数据

near view $ID nft_metadata

返回内容如下

View call: pld2.testnet.nft_metadata()
{ spec: 'nft-1.0.0',
  name: 'Example NEAR non-fungible token',
  symbol: 'EXAMPLE',
  icon:
   'data:image/svg+xml,%3Csvg xmlns=\'http://www.w3.org/2000/svg\' viewBox=\'0 0 288 288\'%3E%3Cg id=\'l\' data-name=\'l\'%3E%3Cpath d=\'M187.58,79.81l-30.1,44.69a3.2,3.2,0,0,0,4.75,4.2L191.86,103a1.2,1.2,0,0,1,2,.91v80.46a1.2,1.2,0,0,1-2.12.77L102.18,77.93A15.35,15.35,0,0,0,90.47,72.5H87.34A15.34,15.34,0,0,0,72,87.84V201.16A15.34,15.34,0,0,0,87.34,216.5h0a15.35,15.35,0,0,0,13.08-7.31l30.1-44.69a3.2,3.2,0,0,0-4.75-4.2L96.14,186a1.2,1.2,0,0,1-2-.91V104.61a1.2,1.2,0,0,1,2.12-.77l89.55,107.23a15.35,15.35,0,0,0,11.71,5.43h3.13A15.34,15.34,0,0,0,216,201.16V87.84A15.34,15.34,0,0,0,200.66,72.5h0A15.35,15.35,0,0,0,187.58,79.81Z\'/%3E%3C/g%3E%3C/svg%3E',
  base_uri: null,
  reference: null,
  reference_hash: null }

铸造第一个nft令牌

near call $ID nft_mint '{"token_id": "0", "receiver_id": "'$ID'", "token_metadata": { "title": "Olympus Mons", "description": "Tallest mountain in charted solar system", "media": "https://upload.wikimedia.org/wikipedia/commons/thumb/0/00/Olympus_Mons_alt.jpg/1024px-Olympus_Mons_alt.jpg", "copies": 1}}' --accountId $ID --deposit 10

返回内容如下

Transaction Id 9D4gQvxwb4MW82YdtRaimiM1aEVp1r69AqWmhzrG1c8G
To see the transaction in the transaction explorer, please open this url in your browser
https://explorer.testnet.near.org/transactions/9D4gQvxwb4MW82YdtRaimiM1aEVp1r69AqWmhzrG1c8G
{ token_id: '0',
  owner_id: 'pld2.testnet',
  metadata:
   { title: 'Olympus Mons',
     description: 'Tallest mountain in charted solar system',
     media:
      'https://upload.wikimedia.org/wikipedia/commons/thumb/0/00/Olympus_Mons_alt.jpg/1024px-Olympus_Mons_alt.jpg',
     media_hash: null,
     copies: 1,
     issued_at: null,
     expires_at: null,
     starts_at: null,
     updated_at: null,
     extra: null,
     reference: null,
     reference_hash: null },
  approved_account_ids: {} }

区块链浏览器tx:
在这里插入图片描述
在这里插入图片描述

转移nft

创建一个子账号

near create-account alice.$ID --masterAccount $ID --initialBalance 10

返回以下内容

Saving key to '/root/.near-credentials/testnet/alice.pld2.testnet.json'
Account alice.pld2.testnet for network "testnet" was created

检查是否有代币

near view $ID nft_tokens_for_owner '{"account_id": "'alice.$ID'"}'

返回以下内容:

View call: pld2.testnet.nft_tokens_for_owner({"account_id": "alice.pld2.testnet"})
[]

转账

我们将通NFT 转入 Alice 的账户。应附上 1 yoctoNEAR 的存款

near call $ID nft_transfer '{"token_id": "0", "receiver_id": "alice.'$ID'", "memo": "transfer ownership"}' --accountId $ID --deposit 0.000000000000000000000001
Scheduling a call: pld2.testnet.nft_transfer({"token_id": "0", "receiver_id": "alice.pld2.testnet", "memo": "transfer ownership"}) with attached 0.000000000000000000000001 NEAR
Receipt: 5pirbCUpz4t8XSV2spRRVbJZcgTw8FZ8JKHve2cVZCrt
	Log [pld2.testnet]: Transfer 0 from pld2.testnet to alice.pld2.testnet
	Log [pld2.testnet]: Memo: transfer ownership
Transaction Id Ei18jp8pCTwo5wTscRs4sJU3CStHxP5DkMgwuYTU8BHZ
To see the transaction in the transaction explorer, please open this url in your browser
https://explorer.testnet.near.org/transactions/Ei18jp8pCTwo5wTscRs4sJU3CStHxP5DkMgwuYTU8BHZ
''

再次检查是否有代币

near view $ID nft_tokens_for_owner '{"account_id": "'alice.$ID'"}'

返回以下内容:

View call: pld2.testnet.nft_tokens_for_owner({"account_id": "alice.pld2.testnet"})
[ { token_id: '0',
    owner_id: 'alice.pld2.testnet',
    metadata:
     { title: 'Olympus Mons',
       description: 'Tallest mountain in charted solar system',
       media:
        'https://upload.wikimedia.org/wikipedia/commons/thumb/0/00/Olympus_Mons_alt.jpg/1024px-Olympus_Mons_alt.jpg',
       media_hash: null,
       copies: 1,
       issued_at: null,
       expires_at: null,
       starts_at: null,
       updated_at: null,
       extra: null,
       reference: null,
       reference_hash: null },
    approved_account_ids: {} } ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值