手把手教你區(qū)塊鏈java開發(fā)智能合約nft-第五篇(鑄造第一個NFT)
回顧
初學(xué)區(qū)塊鏈,那真叫一個痛苦并無助。如果沒有人帶你的話
今天寫的這篇是在前面文章基礎(chǔ)上寫的,初學(xué)區(qū)塊鏈的朋友建議先看我前面寫的文章
手把手教你區(qū)塊鏈java開發(fā)智能合約nft-第一篇
手把手教你區(qū)塊鏈java開發(fā)智能合約nft-第二篇(部署第一個NFT智能合約)
手把手教你區(qū)塊鏈java開發(fā)智能合約nft token-第三篇(部署token)
手把手教你區(qū)塊鏈java開發(fā)智能合約nft(第四篇)-如何動態(tài)獲取gasPrice和gasLimit?
部署NFT721
關(guān)于部署NFT721 ,可以參考之前的文章:
手把手教你區(qū)塊鏈java開發(fā)智能合約nft-第二篇(部署第一個NFT智能合約)
部署完成后,將得到一個NFT721的智能合約地址:contractAddress,這個智能合約地址值需要保存好,待會鑄造NFT時需要用到它
比如我在部署合約完成后,得到的contractAddress值為:0x1fc51bf18b49d29f7a40825914e8c68f4ce7b255
鑄造NFT
鑄造NFT也是比較簡單的,直接調(diào)用NFT721 的方法mint 就可以了,主要難點(diǎn)是在對數(shù)據(jù)的簽名上
直接上代碼吧
@Test
public void mint() throws Exception {
//NFT721合約地址
String contractAddress="0x1fc51bf18b49d29f7a40825914e8c68f4ce7b255";
String tokenId="2"; //上鏈的NFT id
String tokenURI="/static/uploadQmVJoMsgcsCCz75ZHzup7e8boqhBz9R8SKYwKknFEWamzA.png"; //上鏈的tokenId對應(yīng)的值
//鑄造NFT需要用到的私鑰,在部署合約時指定的地址對應(yīng)的私鑰
String miner="0x5369576889297382912123166219811286283737728368986117127236719812";
TransactionManager transactionManager = getRawTransactionManager();
ContractGasProvider contractGasProvider = getContractGasProvider();
NFT721 nft721 = NFT721.load(contractAddress, web3j, transactionManager, contractGasProvider);
Credentials credentials = getCredentials();
ECKeyPair ecKeyPair = ECKeyPair.create(Numeric.hexStringToByteArray(miner));
Type<BigInteger> tId = new Uint(new BigInteger(tokenId));
String a = contractAddress + Numeric.cleanHexPrefix(TypeEncoder.encode(tId));
a = Hash.sha3(a);
byte[] b = Numeric.hexStringToByteArray(a);
Sign.SignatureData data = Sign.signPrefixedMessage(b, ecKeyPair);
ArrayList<NFT721.Fee> fees = Lists.newArrayList(new NFT721.Fee(credentials.getAddress(), BigInteger.valueOf(200)));
TransactionReceipt transactionReceipt = nft721.mint(new BigInteger(tokenId), new BigInteger(data.getV()), data.getR(), data.getS(),
fees, tokenURI).send();
System.out.println("mint:hash::"+transactionReceipt.getTransactionHash());
System.out.println("mint:getContractAddress::"+transactionReceipt.getContractAddress());
}
關(guān)鍵代碼說明:
- 首先定義了一些傳入的參數(shù),這里當(dāng)做局部變量定義出來
- 加載智能合約NFT721對象,要用這個對象來調(diào)用mint鑄造NFT
NFT721 nft721 = NFT721.load(contractAddress, web3j, transactionManager, contractGasProvider);
- 對上鏈數(shù)據(jù)進(jìn)行簽名
//利用簽名賬戶對應(yīng)的私鑰對上鏈的tokenId等數(shù)據(jù)進(jìn)行簽名
ECKeyPair ecKeyPair = ECKeyPair.create(Numeric.hexStringToByteArray(miner));
Type<BigInteger> tId = new Uint(new BigInteger(tokenId));
String a = contractAddress + Numeric.cleanHexPrefix(TypeEncoder.encode(tId));
a = Hash.sha3(a);
byte[] b = Numeric.hexStringToByteArray(a);
Sign.SignatureData data = Sign.signPrefixedMessage(b, ecKeyPair);
- 鑄造NFT
ArrayList<NFT721.Fee> fees = Lists.newArrayList(new NFT721.Fee(credentials.getAddress(), BigInteger.valueOf(200)));
TransactionReceipt transactionReceipt = nft721.mint(new BigInteger(tokenId), new BigInteger(data.getV()), data.getR(), data.getS(),
fees, tokenURI).send();
mint方法參數(shù)說明:
public RemoteFunctionCall mint(BigInteger tokenId, BigInteger v, byte[] r, byte[] s, List _fees, String tokenURI)
tokenId:上鏈的tokenId,或者可以理解為NFT的id,必須保證在鏈上唯一
v: 簽名數(shù)據(jù)的v
r:簽名數(shù)據(jù)的R
s:簽名數(shù)據(jù)的S
_fees: (我也不太理解具體是什么)從字面上理解是費(fèi)用信息,
Fee構(gòu)造方法需要傳入兩個參數(shù):
public Fee(String recipient, BigInteger value) {
super(new Address(recipient),
new Uint256(value));
this.recipient = recipient;
this.value = value;
}
從命名上看:
recipient : 接受者
value:費(fèi)用
tokenURI:tokenURI,具體上鏈的數(shù)據(jù),上鏈后可以通過tokenId查看的數(shù)據(jù),一個tokenId需要指定一個tokenURI文章來源:http://www.zghlxwxcb.cn/news/detail-401993.html
執(zhí)行完以上代碼,沒有報錯的話,等一會就會鑄造成功了
這樣就完成了NFT的鑄造了文章來源地址http://www.zghlxwxcb.cn/news/detail-401993.html
根據(jù)tokenId查看NFT鏈上數(shù)據(jù)
@Test
public void tokenURI() throws Exception {
String contractAddress="0x1fc51bf18b49d29f7a40825914e8c68f4ce7b255";
String tokenId="2";
TransactionManager transactionManager = getRawTransactionManager();
ContractGasProvider contractGasProvider = getContractGasProvider();
NFT721 nft721 = NFT721.load(contractAddress, web3j, transactionManager, contractGasProvider);
String tokenURI = nft721.tokenURI(new BigInteger(tokenId)).send();
System.out.println("tokenURI::"+tokenURI);
}
完整代碼
package net.sandboxol.support.deploy.mint;
import net.sandboxol.support.deploy.NFT721;
import net.sandboxol.support.utils.Web3Util;
import org.assertj.core.util.Lists;
import org.junit.Test;
import org.web3j.abi.TypeEncoder;
import org.web3j.abi.datatypes.Type;
import org.web3j.abi.datatypes.Uint;
import org.web3j.crypto.Credentials;
import org.web3j.crypto.ECKeyPair;
import org.web3j.crypto.Hash;
import org.web3j.crypto.Sign;
import org.web3j.protocol.Web3j;
import org.web3j.protocol.core.methods.response.TransactionReceipt;
import org.web3j.protocol.http.HttpService;
import org.web3j.tx.RawTransactionManager;
import org.web3j.tx.TransactionManager;
import org.web3j.tx.gas.ContractGasProvider;
import org.web3j.tx.gas.StaticGasProvider;
import org.web3j.utils.Numeric;
import java.io.IOException;
import java.math.BigInteger;
import java.util.ArrayList;
public class DeployMintTest {
Web3j web3j = Web3j.build(new HttpService("http://192.168.159.101:8545/"));
private String userId="214158";
private String siteId="ishow";
@Test
public void mint() throws Exception {
String contractAddress="0x1fc51bf18b49d29f7a40825914e8c68f4ce7b255";
String tokenId="2";
String tokenURI="/static/uploadQmVJoMsgcsCCz75ZHzup7e8boqhBz9R8SKYwKknFEWamzA.png";
//私鑰
String miner="0x5369576889297382912123166219811286283737728368986117127236719812";
TransactionManager transactionManager = getRawTransactionManager();
ContractGasProvider contractGasProvider = getContractGasProvider();
NFT721 nft721 = NFT721.load(contractAddress, web3j, transactionManager, contractGasProvider);
Credentials credentials = getCredentials();
ECKeyPair ecKeyPair = ECKeyPair.create(Numeric.hexStringToByteArray(miner));
Type<BigInteger> tId = new Uint(new BigInteger(tokenId));
String a = contractAddress + Numeric.cleanHexPrefix(TypeEncoder.encode(tId));
a = Hash.sha3(a);
byte[] b = Numeric.hexStringToByteArray(a);
Sign.SignatureData data = Sign.signPrefixedMessage(b, ecKeyPair);
ArrayList<NFT721.Fee> fees = Lists.newArrayList(new NFT721.Fee(credentials.getAddress(), BigInteger.valueOf(200)));
TransactionReceipt transactionReceipt = nft721.mint(new BigInteger(tokenId), new BigInteger(data.getV()), data.getR(), data.getS(),
fees, tokenURI).send();
System.out.println("mint:hash::"+transactionReceipt.getTransactionHash());
System.out.println("mint:getContractAddress::"+transactionReceipt.getContractAddress());
}
@Test
public void tokenURI() throws Exception {
String contractAddress="0x1fc51bf18b49d29f7a40825914e8c68f4ce7b255";
String tokenId="1";
TransactionManager transactionManager = getRawTransactionManager();
ContractGasProvider contractGasProvider = getContractGasProvider();
NFT721 nft721 = NFT721.load(contractAddress, web3j, transactionManager, contractGasProvider);
String tokenURI = nft721.tokenURI(new BigInteger(tokenId)).send();
System.out.println("tokenURI::"+tokenURI);
}
private Credentials getCredentials () {
//這個Web3Util 是自己定義的工具,能根據(jù)userId和siteId生成Credentials 對象的工具,大家可以自己構(gòu)造,我這個有其他依賴,不方便提供源碼
return Web3Util.getUserCredentials(userId,siteId);
}
private TransactionManager getRawTransactionManager() throws IOException {
BigInteger chainId = web3j.ethChainId().send().getChainId();
return new RawTransactionManager(web3j, getCredentials(),chainId.longValue());
}
private ContractGasProvider getContractGasProvider(){
return new StaticGasProvider(BigInteger.valueOf(22_000_000_000l),BigInteger.valueOf(6_700_000l));
}
}
NFT721 相關(guān)智能合約源碼下載
到了這里,關(guān)于手把手教你區(qū)塊鏈java開發(fā)智能合約nft-第五篇(鑄造第一個NFT)的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!