文章目录

  • 手把手教你区块链java开发智能合约nft token-第三篇(部署token)
  • token是什么?
  • 编写token合约
  • 将token合约编译并转为java代码
  • 依赖web3j
  • java代码中部署token


手把手教你区块链java开发智能合约nft token-第三篇(部署token)

做区块链开发真不是随便转的,如果没有人带的话,那真叫一个痛苦并无助。

token是什么?

在区块链系统中,也称为通证。与一般token不同,区块链token具有权益性、加密性和可流通性。使得它成为区块链系统的“燃料”,资产流转、权益分享和资产升值等经济行为和机制变得顺其自然。

编写token合约

里面的contract文件夹下,token.sol文件就是token合约

将token合约编译并转为java代码


依赖web3j


java代码中部署token

关键代码如下:

/**
     * 部署token
     * @param apiUrl 链地址
     * @param privateKey 部署人私钥,要扣gas的
     * @param tokenName token的名称
     * @param tokenSymbol token符合
     * @param supply 发行总数
     * @throws Exception
     */
    public static String deployToken(String apiUrl,String privateKey,String tokenName,String tokenSymbol,String supply) throws Exception {
        System.out.println("------------------------deployToken start------------------------------");
        Web3j web3j = Web3j.build(new HttpService(apiUrl));
        TransactionManager transactionManager = getRawTransactionManager(web3j, privateKey);
        ContractGasProvider contractGasProvider = getContractGasProvider();
        Token token = Token.deploy(web3j, transactionManager, contractGasProvider, tokenName, tokenSymbol,new BigInteger(supply)).send();
        //0xbbbdd7d07d194b9be54bbae21c892b138c8f6cfd
        System.out.println("token:contract::"+token.getContractAddress());
        BigInteger totalSupply = token.totalSupply().send();
        System.out.println("totalSupply:::"+totalSupply);
        System.out.println("------------------------deployToken done------------------------------");
        return token.getContractAddress();
    }

代码中,使用Token的静态方法deploy方法进行部署,需要注意的是,在部署时要指定token 的名称,符号,发行总量

整体代码如下:

package net.sandboxol.support.deploy.contract;

import com.alibaba.fastjson.JSON;
import net.sandboxol.support.deploy.*;
import org.web3j.crypto.Credentials;
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 java.io.IOException;
import java.math.BigInteger;
import java.util.HashMap;
import java.util.Map;

/**
 * 一键部署
 */
public class OneClickDeployTest {

    public static void main(String[]arg) throws Exception {
        //私钥,用来部署合约,将会耗用gas
        String privateKey="0x3962281158193372293187638892977826627782389871655162191585611137";
        //链地址,合约部署到的链
        String apiUrl="http://192.168.158.128:8545/";

        String tokenName="bman";
        String tokenSymbol="bman";
        String supply="10000000000000000";
                     //10000000000000000000000000000000000

        //部署token
        String tokenAddress = deployToken(apiUrl, privateKey, tokenName, tokenSymbol, supply);
    }

    /**
     * 部署token
     * @param apiUrl 链地址
     * @param privateKey 部署人私钥,要扣gas的
     * @param tokenName token的名称
     * @param tokenSymbol token符合
     * @param supply 发行总数,数字或者十六进制
     * @throws Exception
     */
    public static String deployToken(String apiUrl,String privateKey,String tokenName,String tokenSymbol,String supply) throws Exception {
        System.out.println("------------------------deployToken start------------------------------");
        Web3j web3j = Web3j.build(new HttpService(apiUrl));
        TransactionManager transactionManager = getRawTransactionManager(web3j, privateKey);
        ContractGasProvider contractGasProvider = getContractGasProvider();
        Token token = Token.deploy(web3j, transactionManager, contractGasProvider, tokenName, tokenSymbol,new BigInteger(supply)).send();
        //0xbbbdd7d07d194b9be54bbae21c892b138c8f6cfd
        System.out.println("token:contract::"+token.getContractAddress());
        BigInteger totalSupply = token.totalSupply().send();
        System.out.println("totalSupply:::"+totalSupply);
        System.out.println("------------------------deployToken done------------------------------");
        return token.getContractAddress();
    }

    private static Credentials getCredentials (String privateKey) {
        return Credentials.create(privateKey);
    }

    private static TransactionManager getRawTransactionManager(Web3j web3j,String privateKey) throws IOException {
        BigInteger chainId = web3j.ethChainId().send().getChainId();
        return new RawTransactionManager(web3j, getCredentials(privateKey),chainId.longValue());
    }

    private static ContractGasProvider getContractGasProvider(){
        return new StaticGasProvider(BigInteger.valueOf(22_000_000_000l),BigInteger.valueOf(6_700_000l));
    }
}

指定main方法,即可部署token