3.使用SDK快速体验星火链

本章带大家使用Java SDK 在星火链网体验网上分别部署、调用、 查询一个JavascriptSolidity智能合约,需要完成如下工作:

  1. 获取星火令来激活账户

  2. Java sdk的初始化

  3. 通过体验网部署合约

3.1 SDK使用

通过maven引入星火链Java SDK.

<dependency>
	<groupId>cn.bitfactory</groupId>
	<artifactId>bif-chain-sdk</artifactId>
	<version>1.1.0</version>
</dependency>

3.2 账号创建

调用JavaSDK接口离线创建一个账户。

import cn.bif.model.crypto.KeyPairEntity;
KeyPairEntity entity = KeyPairEntity.getBidAndKeyPair();                //离线创建一个新账号
System.out.printf("BID address %s\n", entity.getEncAddress());          //账户地址, 可以公开
System.out.printf("privatekey %s\n", entity.getEncPrivateKey());      //账户私钥, 请妥善保管

3.3 获取星火令

账户需要拥有星火令才能正常使用星火链功能, 体验网星火令可以通过如下方式获取:

  • 通过星火插件钱包申请可信凭证获取100星火令(需要等待2-3天工作人员审批,着急的话可以在技术交流群找工作人员加急审核,技术交流群见第一章二维码)

星火插件钱包下载地址:https://bitfactory.cn/szsf.html

3.4 初始化SDK

通过配置星火链RPC地址连接SDK到星火链, 本次演示的demo里链接到星火链体验网。

import cn.bif.api.BIFSDK;

public static final String NODE_URL = "http://test.bifcore.bitfactory.cn";  //星火链体验网RPC地址

BIFSDK sdk = BIFSDK.getInstance(NODE_URL);

3.5 查看账户状态

import cn.bif.model.request.BIFAccountGetInfoRequest;
import cn.bif.model.response.BIFAccountGetInfoResponse;

//构建查看账户请求
BIFAccountGetInfoRequest infoReq = new BIFAccountGetInfoRequest();
//要查看账户的地址
infoReq.setAddress(address);

//发出查询请求
BIFAccountGetInfoResponse infoRsp = sdk.getBIFAccountService().getAccount(infoReq);

if (infoRsp.getErrorCode() == 0) {
    //查询成功
    System.out.println(JsonUtils.toJSONString(infoRsp.getResult()));
} else {
    //查询失败
    System.out.println(infoRsp.getErrorDesc());
}
.. ::node
注意, 新创建的空白账户查询会失败, 需要转入星火令激活才能正常使用。

正常账户查询返回示例:

{
    "address":"did:bid:efKkF5uKsopAishxkYja4ULRJhrhrJQU",    //账户地址
    "balance":10000000000,                                   //账户余额
    "nonce":0                                                //账户发出的交易计数
}

3.6 部署调用一个Js合约

3.6.1 智能合约代码

  • Javascript智能合约代码如下:

    "use strict";
    
    
    function init(input)
    {
        const {key, value} = JSON.parse(input);
        Chain.store(key, String(value));
        Chain.tlog('Log', key, String(value));
        return;
    }
    function store({key, value}) {
        Chain.store(key, String(value));
        Chain.tlog('Log', key, String(value));
    }
    
    function del({key}) {
        Chain.del(key);
    }
    
    function main(input)
    {
        let {method, params} = JSON.parse(input);
        if (method === 'store')
      {
            store(params);
        } else if (method === 'del') {
            del(params);
        }
    }
    
    function get({key}) {
        return Chain.load(key);
    }
    function query(input)
    {
        let {method, params} = JSON.parse(input);
        if (method === 'get') {
            return get(params);
        }
    }
    

    该合约实现了一个简单的存储功能, 用户可以调用main接口存储自定义Key-Value信息, 然后通过查询接口查询已经存入的Key-Value信息。

3.6.2 部署合约

合约编写完毕后, 需要将合约部署到链上, 注意这里需要账户内有足够的XHT, 部署代码如下:

	//发起交易的账户,使用上面生成的账户地址
  String senderAddress = "did:bid:efzE8AcDgWUeNbgujA5hK3oUeuG9k19b";
	//发起交易的账户的私钥,用上面生成的私钥
  String senderPrivateKey = "priSPKiwJR7PwjN3bGPRvXRoC6Dny1H5H3kASHgTEugjtUDknW";
	//合约代码,注意双引号转义
  String   payload =  "\"use strict\"; function init(input) {     const {key, value} = JSON.parse(input);     Chain.store(key, String(value));     Chain.tlog('Log', key, String(value));     return; } function store({key, value}) {     Chain.store(key, String(value));     Chain.tlog('Log', key, String(value)); }  function del({key}) {     Chain.del(key); }  function main(input) {     let {method, params} = JSON.parse(input);     if (method === 'store')     {         store(params);     } else if (method === 'del') {         del(params);     } }  function get({key}) {     return Chain.load(key); } function query(input) {     let {method, params} = JSON.parse(input);     if (method === 'get') {         return get(params);     } }";

  //给合约账户的初始化金额,如无需求可以设置为0
  Long initBalance = ToBaseUnit.ToUGas("0");

  BIFContractCreateRequest request = new BIFContractCreateRequest();
  request.setSenderAddress(senderAddress);
  
  //SDK本地内部
  request.setPrivateKey(senderPrivateKey);
  request.setInitBalance(initBalance);
  request.setPayload(payload);
  request.setInitInput("{\"key\":\"test\",\"value\":2}");
  //备注,选填
	request.setRemarks("create contract");
	//type,javascript合约type为0,solidity合约type为1
	request.setType(0);
  //feelimit设置要小于账户的balance
	request.setFeeLimit(100060000L);
  request.setNonceType(1);

	// 调用contractCreate接口
	BIFContractCreateResponse response = sdk.getBIFContractService().contractCreate(request);
	if (response.getErrorCode() == 0) {
  	System.out.println(JsonUtils.toJSONString(response.getResult()));
	} else {
  	System.out.println(JsonUtils.toJSONString(response));
	}

如果部署成功, 调用返回里会拿到这个交易的hash:

{
      "hash":"92313f8fb30b042a4d3f4d276ee2d4ce59f21994deed53e6cc6e2a2dcbe61f8f"
}
  • 交易信息和合约地址查询

    用SDK查询部署合约的交易详细信息, 可以从中获取到创建的合约地址。

    //部署返回hash
    String hash = "92313f8fb30b042a4d3f4d276ee2d4ce59f21994deed53e6cc6e2a2dcbe61f8f";
    BIFContractGetAddressRequest request = new BIFContractGetAddressRequest();
    request.setHash(hash);
    
    // 调用接口
    BIFContractGetAddressResponse response = sdk.getBIFContractService().getContractAddress(request);
    if (response.getErrorCode() == 0) {
        System.out.println(JsonUtils.toJSONString(response.getResult()));
    } else {
        System.out.println(JsonUtils.toJSONString(response));
    }
    

    合约部署信息示例如下:

    {
        "contract_address_infos":[
            {
                "contract_address":"did:bid:ef6ZAWV315UracvhKARdHz5CbQ6dULPp",
                "operation_index":0
            }
        ]
    }
    

    did:bid:ef6ZAWV315UracvhKARdHz5CbQ6dULPp即为刚刚创建的合约链上地址。

  • 交易信息和合约地址查询 用SDK根据合约地址查询合约账户的内容:

    BIFContractGetInfoRequest request = new BIFContractGetInfoRequest();
    //上面返回的合约地址
    request.setContractAddress("did:bid:efXkBsC2nQN6PJLjT9nv3Ah7S3zJt2WW");
     BIFContractGetInfoResponse response = sdk.getBIFContractService().getContractInfo(request);
    if (response.getErrorCode() == 0) {
        System.out.println(JsonUtils.toJSONString(response.getResult()));
    } else {
       System.out.println(JsonUtils.toJSONString(response));
    }
    

    查询内容结果如下:

    {
    	"contract":{
    	"type":0,
    "payload":"\"use strict\"; function init(input) {     const {key, value} = JSON.parse(input);     Chain.store(key, String(value));     Chain.tlog('Log', key, String(value));     return; } function store({key, value}) {     Chain.store(key, String(value));     Chain.tlog('Log', key, String(value)); }  function del({key}) {     Chain.del(key); }  function main(input) {     let {method, params} = JSON.parse(input);     if (method === 'store')     {         store(params);     } else if (method === 'del') {         del(params);     } }  function get({key}) {     return Chain.load(key); } function query(input) {     let {method, params} = JSON.parse(input);     if (method === 'get') {         return get(params);     } }"
    }
    }
    

3.6.3 合约调用

合约成功部署并且获取到合约地址后, 就可以通过SDK发送交易调用合约接口, 我们存储一个Key-Value对到合约里:

调用合约input如下

{
    "key":"test",
    "value": 123
}

调用合约代码如下:

// 初始化参数
String senderAddress = "did:bid:efzE8AcDgWUeNbgujA5hK3oUeuG9k19b";
String contractAddress = "did:bid:ef6ZAWV315UracvhKARdHz5CbQ6dULPp";
String senderPrivateKey = "priSPKiwJR7PwjN3bGPRvXRoC6Dny1H5H3kASHgTEugjtUDknW";
Long amount = 0L;
String input = "{\"method\":\"store\",\"params\":{\"key\":\"test\",\"value\":123}}";
BIFContractInvokeRequest request = new BIFContractInvokeRequest();
request.setSenderAddress(senderAddress);
request.setPrivateKey(senderPrivateKey);
request.setContractAddress(contractAddress);
request.setBIFAmount(amount);
request.setRemarks("contract invoke");
request.setInput(input);
request.setNonceType(1);

// 调用contractInvoke 接口
BIFContractInvokeResponse response = sdk.getBIFContractService().contractInvoke(request);
if (response.getErrorCode() == 0) {
    System.out.println(JsonUtils.toJSONString(response.getResult()));
} else {
    System.out.println(JsonUtils.toJSONString(response));
}

调用成功后,我们会得到调用交易的hash:

{
    "hash":"2fcb88fcab04af78e4d06ec263d462b2aacd00d9405159725918d9ca8144ea5d"
}

利用交易哈希查询调用结果:

BIFTransactionGetInfoRequest request = new BIFTransactionGetInfoRequest();
//上面返回的哈希值
request.setHash("2fcb88fcab04af78e4d06ec263d462b2aacd00d9405159725918d9ca8144ea5d");
BIFTransactionGetInfoResponse response =  sdk.getBIFTransactionService().getTransactionInfo(request);
if (response.getErrorCode() == 0) {
	System.out.println(JsonUtils.toJSONString(response.getResult()));
} else {
	System.out.println(JsonUtils.toJSONString(response));
}

打印结果为:

{"total_count":1,"transactions":[{"actual_fee":"290","close_time":1711676430063002,"error_code":0,"error_desc":"","hash":"5d1a11f0ab56b3780c784ca57f2815872395cc0a813c0f64b45b98167b770972","ledger_seq":3800392,"signatures":[{"sign_data":"b9135e2c402b05a66e4a23dec493e12eebe43b5a58205b0bca87ee78dd43b649a6cc97a3d7f3c16f486a7320125a3a00b16ad6d6dde8e15436acf0b786757301","public_key":"b06566ef31937a34b42d2ed56c8fd132920709b92aeca7dc96c22e38d270822d9e1318"}],"transaction":{"source_address":"did:bid:ef4w8cwj7W1C27cZc46SCpsAi3nASBwB","fee_limit":1000000,"gas_price":1,"nonce":141,"metadata":"636f6e747261637420696e766f6b65","operations":[{"type":7,"pay_coin":{"dest_address":"did:bid:efiqdeCKdn1b3wX4oWC25KckyqKLG33j","input":"{\"key\":\"test\", \"value\": 123}"}}]},"tx_size":290}]}

3.6.4 查询合约

不同于调用合约, 查询合约为只读操作, 因此不需要发出上链交易和耗费gas, 这里我们查询刚刚设置的key, 查询input为:

{
    "key":"test"
}

Java查询代码如下:

// 初始化参数
String contractAddress = "did:bid:ef6ZAWV315UracvhKARdHz5CbQ6dULPp";
String callInput = "{\"method\":\"get\",\"params\":{\"key\":\"test\"}}";
BIFContractCallRequest request = new BIFContractCallRequest();
request.setContractAddress(contractAddress);
request.setInput(callInput);

// 调用contractQuery接口
BIFContractCallResponse response = sdk.getBIFContractService().contractQuery(request);
if (response.getErrorCode() == 0) {
    BIFContractCallResult result = response.getResult();
    System.out.println(JsonUtils.toJSONString(result));
} else {
    System.out.println(JsonUtils.toJSONString(response));
}

查询的返回如下:

{
    "query_rets":[
        {
            "result":
                {
                    "type":"string",//查询结果字段的类型
                    "value":"123"//查询结果字段的值
                }
        }
    ]
}

3.7部署调用一个 Solidity合约

3.7.1 智能合约代码

  • Solidity智能合约代码如下:

    pragma solidity ^0.8.20;
    
    contract Storage  {
      mapping(uint256 => string) private _datas;
      mapping(uint256=>bool) private _exists;
      event Log(uint256 , string);
    
      constructor(uint256 id, string memory data) public {
        _datas[id] = data;
        _exists[id] = true;
        emit Log(id, data);
      }
    
      function queryById(uint256 id) public view returns (string memory) {
        require(!_exists[id], "data not exists");
        return _datas[id];
      }
      function setById(uint256 id, string memory data) public payable {
        require(_exists[id], "data has exists");
        _datas[id] = data;
      }
    }
    

    该合约实现了一个简单的存储功能, 用户可以调用setById接口存储自定义Key-Value信息, 然后通过queryById接口查询已经存入的Key-Value信息。

3.7.2 智能合约编译

星火链对的EVM虚拟机进行了定制化的修改,因此提供了专门的编译器来编译星火链Solidity合约. 下载地址及使用方式见星火链solidity编译器

编译后的bytecode 为:

608060405234801562000010575f80fd5b5060405162000f5038038062000f5083398181016040528101906200003691906200027e565b805f808481526020019081526020015f20908162000055919062000510565b506001805f8481526020019081526020015f205f6101000a81548160ff0219169083151502179055507f48197b8eaf01bc9d46384d798a981a4537fc58dcd35ece37054dddbba8418edf8282604051620000b192919062000655565b60405180910390a1505062000687565b5f604051905090565b5f80fd5b5f80fd5b5f819050919050565b620000e681620000d2565b8114620000f1575f80fd5b50565b5f815190506200010481620000db565b92915050565b5f80fd5b5f80fd5b5f601f19601f8301169050919050565b7f4e487b71000000000000000000000000000000000000000000000000000000005f52604160045260245ffd5b6200015a8262000112565b810181811067ffffffffffffffff821117156200017c576200017b62000122565b5b80604052505050565b5f62000190620000c1565b90506200019e82826200014f565b919050565b5f67ffffffffffffffff821115620001c057620001bf62000122565b5b620001cb8262000112565b9050602081019050919050565b5f5b83811015620001f7578082015181840152602081019050620001da565b5f8484015250505050565b5f620002186200021284620001a3565b62000185565b9050828152602081018484840111156200023757620002366200010e565b5b62000244848285620001d8565b509392505050565b5f82601f8301126200026357620002626200010a565b5b81516200027584826020860162000202565b91505092915050565b5f8060408385031215620002975762000296620000ca565b5b5f620002a685828601620000f4565b925050602083015167ffffffffffffffff811115620002ca57620002c9620000ce565b5b620002d8858286016200024c565b9150509250929050565b5f81519050919050565b7f4e487b71000000000000000000000000000000000000000000000000000000005f52602260045260245ffd5b5f60028204905060018216806200033157607f821691505b602082108103620003475762000346620002ec565b5b50919050565b5f819050815f5260205f209050919050565b5f6020601f8301049050919050565b5f82821b905092915050565b5f60088302620003ab7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff826200036e565b620003b786836200036e565b95508019841693508086168417925050509392505050565b5f819050919050565b5f620003f8620003f2620003ec84620000d2565b620003cf565b620000d2565b9050919050565b5f819050919050565b6200041383620003d8565b6200042b6200042282620003ff565b8484546200037a565b825550505050565b5f90565b6200044162000433565b6200044e81848462000408565b505050565b5b818110156200047557620004695f8262000437565b60018101905062000454565b5050565b601f821115620004c4576200048e816200034d565b62000499846200035f565b81016020851015620004a9578190505b620004c1620004b8856200035f565b83018262000453565b50505b505050565b5f82821c905092915050565b5f620004e65f1984600802620004c9565b1980831691505092915050565b5f620005008383620004d5565b9150826002028217905092915050565b6200051b82620002e2565b67ffffffffffffffff81111562000537576200053662000122565b5b62000543825462000319565b6200055082828562000479565b5f60209050601f83116001811462000586575f841562000571578287015190505b6200057d8582620004f3565b865550620005ec565b601f19841662000596866200034d565b5f5b82811015620005bf5784890151825560018201915060208501945060208101905062000598565b86831015620005df5784890151620005db601f891682620004d5565b8355505b6001600288020188555050505b505050505050565b620005ff81620000d2565b82525050565b5f82825260208201905092915050565b5f6200062182620002e2565b6200062d818562000605565b93506200063f818560208601620001d8565b6200064a8162000112565b840191505092915050565b5f6040820190506200066a5f830185620005f4565b81810360208301526200067e818462000615565b90509392505050565b6108bb80620006955f395ff3fe608060405260043610610028575f3560e01c8063794bde481461002c578063baf8baab14610048575b5f80fd5b610046600480360381019061004191906103a9565b610084565b005b348015610053575f80fd5b5061006e60048036038101906100699190610403565b61012c565b60405161007b91906104a8565b60405180910390f35b60015f8381526020019081526020015f205f9054906101000a900460ff16156100e2576040517f08c379a00000000000000000000000000000000000000000000000000000000081526004016100d990610512565b60405180910390fd5b805f808481526020019081526020015f2090816100ff919061072a565b506001805f8481526020019081526020015f205f6101000a81548160ff0219169083151502179055505050565b606060015f8381526020019081526020015f205f9054906101000a900460ff1661018b576040517f08c379a000000000000000000000000000000000000000000000000000000000815260040161018290610843565b60405180910390fd5b5f808381526020019081526020015f2080546101a69061055d565b80601f01602080910402602001604051908101604052809291908181526020018280546101d29061055d565b801561021d5780601f106101f45761010080835404028352916020019161021d565b820191905f5260205f20905b81548152906001019060200180831161020057829003601f168201915b50505050509050919050565b5f604051905090565b5f80fd5b5f80fd5b5f819050919050565b61024c8161023a565b8114610256575f80fd5b50565b5f8135905061026781610243565b92915050565b5f80fd5b5f80fd5b5f601f19601f8301169050919050565b7f4e487b71000000000000000000000000000000000000000000000000000000005f52604160045260245ffd5b6102bb82610275565b810181811067ffffffffffffffff821117156102da576102d9610285565b5b80604052505050565b5f6102ec610229565b90506102f882826102b2565b919050565b5f67ffffffffffffffff82111561031757610316610285565b5b61032082610275565b9050602081019050919050565b828183375f83830152505050565b5f61034d610348846102fd565b6102e3565b90508281526020810184848401111561036957610368610271565b5b61037484828561032d565b509392505050565b5f82601f8301126103905761038f61026d565b5b81356103a084826020860161033b565b91505092915050565b5f80604083850312156103bf576103be610232565b5b5f6103cc85828601610259565b925050602083013567ffffffffffffffff8111156103ed576103ec610236565b5b6103f98582860161037c565b9150509250929050565b5f6020828403121561041857610417610232565b5b5f61042584828501610259565b91505092915050565b5f81519050919050565b5f82825260208201905092915050565b5f5b8381101561046557808201518184015260208101905061044a565b5f8484015250505050565b5f61047a8261042e565b6104848185610438565b9350610494818560208601610448565b61049d81610275565b840191505092915050565b5f6020820190508181035f8301526104c08184610470565b905092915050565b7f64617461206861732065786973747300000000000000000000000000000000005f82015250565b5f6104fc600f83610438565b9150610507826104c8565b602082019050919050565b5f6020820190508181035f830152610529816104f0565b9050919050565b7f4e487b71000000000000000000000000000000000000000000000000000000005f52602260045260245ffd5b5f600282049050600182168061057457607f821691505b60208210810361058757610586610530565b5b50919050565b5f819050815f5260205f209050919050565b5f6020601f8301049050919050565b5f82821b905092915050565b5f600883026105e97fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff826105ae565b6105f386836105ae565b95508019841693508086168417925050509392505050565b5f819050919050565b5f61062e6106296106248461023a565b61060b565b61023a565b9050919050565b5f819050919050565b61064783610614565b61065b61065382610635565b8484546105ba565b825550505050565b5f90565b61066f610663565b61067a81848461063e565b505050565b5b8181101561069d576106925f82610667565b600181019050610680565b5050565b601f8211156106e2576106b38161058d565b6106bc8461059f565b810160208510156106cb578190505b6106df6106d78561059f565b83018261067f565b50505b505050565b5f82821c905092915050565b5f6107025f19846008026106e7565b1980831691505092915050565b5f61071a83836106f3565b9150826002028217905092915050565b6107338261042e565b67ffffffffffffffff81111561074c5761074b610285565b5b610756825461055d565b6107618282856106a1565b5f60209050601f831160018114610792575f8415610780578287015190505b61078a858261070f565b8655506107f1565b601f1984166107a08661058d565b5f5b828110156107c7578489015182556001820191506020850194506020810190506107a2565b868310156107e457848901516107e0601f8916826106f3565b8355505b6001600288020188555050505b505050505050565b7f64617461206e6f742065786973747300000000000000000000000000000000005f82015250565b5f61082d600f83610438565b9150610838826107f9565b602082019050919050565b5f6020820190508181035f83015261085a81610821565b905091905056fea2646970667358221220b94128b3605c967628357841386396d9f6591952a812e74f61fa3260b4d50a9964736f6c637826302e382e32312d63692e323032342e332e342b636f6d6d69742e31333434653233632e6d6f640057

3.7.3 部署合约

合约编写完毕后, 需要将合约部署到链上, 注意这里需要账户内有足够的XHT, 部署代码如下:

// 初始化参数
String senderAddress = "did:bid:efzE8AcDgWUeNbgujA5hK3oUeuG9k19b";
String senderPrivateKey = "priSPKiwJR7PwjN3bGPRvXRoC6Dny1H5H3kASHgTEugjtUDknW";
//上面编译后的合约代码
String payload = "608060405234801562000010575f80fd5b5060405162000f5038038062000f5083398181016040528101906200003691906200027e565b805f808481526020019081526020015f20908162000055919062000510565b506001805f8481526020019081526020015f205f6101000a81548160ff0219169083151502179055507f48197b8eaf01bc9d46384d798a981a4537fc58dcd35ece37054dddbba8418edf8282604051620000b192919062000655565b60405180910390a1505062000687565b5f604051905090565b5f80fd5b5f80fd5b5f819050919050565b620000e681620000d2565b8114620000f1575f80fd5b50565b5f815190506200010481620000db565b92915050565b5f80fd5b5f80fd5b5f601f19601f8301169050919050565b7f4e487b71000000000000000000000000000000000000000000000000000000005f52604160045260245ffd5b6200015a8262000112565b810181811067ffffffffffffffff821117156200017c576200017b62000122565b5b80604052505050565b5f62000190620000c1565b90506200019e82826200014f565b919050565b5f67ffffffffffffffff821115620001c057620001bf62000122565b5b620001cb8262000112565b9050602081019050919050565b5f5b83811015620001f7578082015181840152602081019050620001da565b5f8484015250505050565b5f620002186200021284620001a3565b62000185565b9050828152602081018484840111156200023757620002366200010e565b5b62000244848285620001d8565b509392505050565b5f82601f8301126200026357620002626200010a565b5b81516200027584826020860162000202565b91505092915050565b5f8060408385031215620002975762000296620000ca565b5b5f620002a685828601620000f4565b925050602083015167ffffffffffffffff811115620002ca57620002c9620000ce565b5b620002d8858286016200024c565b9150509250929050565b5f81519050919050565b7f4e487b71000000000000000000000000000000000000000000000000000000005f52602260045260245ffd5b5f60028204905060018216806200033157607f821691505b602082108103620003475762000346620002ec565b5b50919050565b5f819050815f5260205f209050919050565b5f6020601f8301049050919050565b5f82821b905092915050565b5f60088302620003ab7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff826200036e565b620003b786836200036e565b95508019841693508086168417925050509392505050565b5f819050919050565b5f620003f8620003f2620003ec84620000d2565b620003cf565b620000d2565b9050919050565b5f819050919050565b6200041383620003d8565b6200042b6200042282620003ff565b8484546200037a565b825550505050565b5f90565b6200044162000433565b6200044e81848462000408565b505050565b5b818110156200047557620004695f8262000437565b60018101905062000454565b5050565b601f821115620004c4576200048e816200034d565b62000499846200035f565b81016020851015620004a9578190505b620004c1620004b8856200035f565b83018262000453565b50505b505050565b5f82821c905092915050565b5f620004e65f1984600802620004c9565b1980831691505092915050565b5f620005008383620004d5565b9150826002028217905092915050565b6200051b82620002e2565b67ffffffffffffffff81111562000537576200053662000122565b5b62000543825462000319565b6200055082828562000479565b5f60209050601f83116001811462000586575f841562000571578287015190505b6200057d8582620004f3565b865550620005ec565b601f19841662000596866200034d565b5f5b82811015620005bf5784890151825560018201915060208501945060208101905062000598565b86831015620005df5784890151620005db601f891682620004d5565b8355505b6001600288020188555050505b505050505050565b620005ff81620000d2565b82525050565b5f82825260208201905092915050565b5f6200062182620002e2565b6200062d818562000605565b93506200063f818560208601620001d8565b6200064a8162000112565b840191505092915050565b5f6040820190506200066a5f830185620005f4565b81810360208301526200067e818462000615565b90509392505050565b6108bb80620006955f395ff3fe608060405260043610610028575f3560e01c8063794bde481461002c578063baf8baab14610048575b5f80fd5b610046600480360381019061004191906103a9565b610084565b005b348015610053575f80fd5b5061006e60048036038101906100699190610403565b61012c565b60405161007b91906104a8565b60405180910390f35b60015f8381526020019081526020015f205f9054906101000a900460ff16156100e2576040517f08c379a00000000000000000000000000000000000000000000000000000000081526004016100d990610512565b60405180910390fd5b805f808481526020019081526020015f2090816100ff919061072a565b506001805f8481526020019081526020015f205f6101000a81548160ff0219169083151502179055505050565b606060015f8381526020019081526020015f205f9054906101000a900460ff1661018b576040517f08c379a000000000000000000000000000000000000000000000000000000000815260040161018290610843565b60405180910390fd5b5f808381526020019081526020015f2080546101a69061055d565b80601f01602080910402602001604051908101604052809291908181526020018280546101d29061055d565b801561021d5780601f106101f45761010080835404028352916020019161021d565b820191905f5260205f20905b81548152906001019060200180831161020057829003601f168201915b50505050509050919050565b5f604051905090565b5f80fd5b5f80fd5b5f819050919050565b61024c8161023a565b8114610256575f80fd5b50565b5f8135905061026781610243565b92915050565b5f80fd5b5f80fd5b5f601f19601f8301169050919050565b7f4e487b71000000000000000000000000000000000000000000000000000000005f52604160045260245ffd5b6102bb82610275565b810181811067ffffffffffffffff821117156102da576102d9610285565b5b80604052505050565b5f6102ec610229565b90506102f882826102b2565b919050565b5f67ffffffffffffffff82111561031757610316610285565b5b61032082610275565b9050602081019050919050565b828183375f83830152505050565b5f61034d610348846102fd565b6102e3565b90508281526020810184848401111561036957610368610271565b5b61037484828561032d565b509392505050565b5f82601f8301126103905761038f61026d565b5b81356103a084826020860161033b565b91505092915050565b5f80604083850312156103bf576103be610232565b5b5f6103cc85828601610259565b925050602083013567ffffffffffffffff8111156103ed576103ec610236565b5b6103f98582860161037c565b9150509250929050565b5f6020828403121561041857610417610232565b5b5f61042584828501610259565b91505092915050565b5f81519050919050565b5f82825260208201905092915050565b5f5b8381101561046557808201518184015260208101905061044a565b5f8484015250505050565b5f61047a8261042e565b6104848185610438565b9350610494818560208601610448565b61049d81610275565b840191505092915050565b5f6020820190508181035f8301526104c08184610470565b905092915050565b7f64617461206861732065786973747300000000000000000000000000000000005f82015250565b5f6104fc600f83610438565b9150610507826104c8565b602082019050919050565b5f6020820190508181035f830152610529816104f0565b9050919050565b7f4e487b71000000000000000000000000000000000000000000000000000000005f52602260045260245ffd5b5f600282049050600182168061057457607f821691505b60208210810361058757610586610530565b5b50919050565b5f819050815f5260205f209050919050565b5f6020601f8301049050919050565b5f82821b905092915050565b5f600883026105e97fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff826105ae565b6105f386836105ae565b95508019841693508086168417925050509392505050565b5f819050919050565b5f61062e6106296106248461023a565b61060b565b61023a565b9050919050565b5f819050919050565b61064783610614565b61065b61065382610635565b8484546105ba565b825550505050565b5f90565b61066f610663565b61067a81848461063e565b505050565b5b8181101561069d576106925f82610667565b600181019050610680565b5050565b601f8211156106e2576106b38161058d565b6106bc8461059f565b810160208510156106cb578190505b6106df6106d78561059f565b83018261067f565b50505b505050565b5f82821c905092915050565b5f6107025f19846008026106e7565b1980831691505092915050565b5f61071a83836106f3565b9150826002028217905092915050565b6107338261042e565b67ffffffffffffffff81111561074c5761074b610285565b5b610756825461055d565b6107618282856106a1565b5f60209050601f831160018114610792575f8415610780578287015190505b61078a858261070f565b8655506107f1565b601f1984166107a08661058d565b5f5b828110156107c7578489015182556001820191506020850194506020810190506107a2565b868310156107e457848901516107e0601f8916826106f3565b8355505b6001600288020188555050505b505050505050565b7f64617461206e6f742065786973747300000000000000000000000000000000005f82015250565b5f61082d600f83610438565b9150610838826107f9565b602082019050919050565b5f6020820190508181035f83015261085a81610821565b905091905056fea2646970667358221220b94128b3605c967628357841386396d9f6591952a812e74f61fa3260b4d50a9964736f6c637826302e382e32312d63692e323032342e332e342b636f6d6d69742e31333434653233632e6d6f640057";



Long initBalance = ToBaseUnit.ToUGas("0");

BIFContractCreateRequest request = new BIFContractCreateRequest();
request.setSenderAddress(senderAddress);
request.setPrivateKey(senderPrivateKey);
request.setInitBalance(initBalance);
request.setPayload(payload);
request.setRemarks("create contract");
//给构造函数传参
String input = "{\"function\":\"Storage(uint256,string)\", \"args\":\"123,'abc'\"}";
request.setInitInput(input);
//type,javascript合约type为0,solidity合约type为1
request.setType(1);
request.setFeeLimit(100189100L);//根据实际情况调整
request.setNonceType(1);
     
// 调用contractCreate接口
BIFContractCreateResponse response = sdk.getBIFContractService().contractCreate(request);
if (response.getErrorCode() == 0) {
    System.out.println(JsonUtils.toJSONString(response.getResult()));
} else {
    System.out.println(JsonUtils.toJSONString(response));
}

如果部署成功, 调用返回里会拿到这个交易的hash:

{
    "hash":"d525a576173e2606acb33103eca5e3cc5c0106c67833a2b75d67d3164cec6b12"
}
  • 交易信息和合约地址查询

//部署返回hash
String hash = "d525a576173e2606acb33103eca5e3cc5c0106c67833a2b75d67d3164cec6b12";
BIFContractGetAddressRequest request = new BIFContractGetAddressRequest();
request.setHash(hash);

BIFContractGetAddressResponse response = sdk.getBIFContractService().getContractAddress(request);
if (response.getErrorCode() == 0) {
    System.out.println(JsonUtils.toJSONString(response.getResult()));
} else {
    System.out.println(JsonUtils.toJSONString(response));
}

可以查询到刚刚创建的合约链上地址: did:bid:ef91sQXEXVfPFQXRZpgA3Rs1pTNbZndq

3.7.4 合约调用

合约成功部署并且获取到合约地址后, 就可以通过SDK发送交易调用合约接口, 我们存储一个Key-Value对到合约里:

调用合约input如下

{
    "key":123,
    "value": "abc"
}

调用合约代码如下:

// 初始化参数
String senderAddress = "did:bid:efzE8AcDgWUeNbgujA5hK3oUeuG9k19b";
String contractAddress = "did:bid:ef91sQXEXVfPFQXRZpgA3Rs1pTNbZndq";
String senderPrivateKey = "priSPKiwJR7PwjN3bGPRvXRoC6Dny1H5H3kASHgTEugjtUDknW";
Long amount = 0L;
String input = "{\"function\":\"setById(uint256,string)\", \"args\":\"123,'abc'\"}";
BIFContractInvokeRequest request = new BIFContractInvokeRequest();
request.setSenderAddress(senderAddress);
request.setPrivateKey(senderPrivateKey);
request.setContractAddress(contractAddress);
request.setBIFAmount(amount);
request.setRemarks("contract invoke");
request.setInput(input);
request.setNonceType(1);

// 调用contractInvoke 接口
BIFContractInvokeResponse response = sdk.getBIFContractService().contractInvoke(request);
if (response.getErrorCode() == 0) {
    System.out.println(JsonUtils.toJSONString(response.getResult()));
} else {
    System.out.println(JsonUtils.toJSONString(response));
}

调用成功后,我们会得到调用交易的hash:

{
    "hash":"b7b0a8f8415ce8cf17627eb3da75f565e0e7cf9b54217121afce4fab2df52080"
}

3.7.5 查询合约

不同于调用合约, 查询合约为只读操作, 因此不需要发出上链交易和耗费gas, 这里我们查询刚刚设置的key, 查询input为:

{
    "id":123
}

Java查询代码如下:

//合约调用
String contractAddress = "did:bid:ef91sQXEXVfPFQXRZpgA3Rs1pTNbZndq";
String callInput = "{\"function\":\"queryById(uint256)\",\"args\":123,\"return\":\"returns(string)\"}";
BIFContractCallRequest request = new BIFContractCallRequest();
request.setContractAddress(contractAddress);
request.setInput(callInput);

BIFContractCallResponse response = sdk.getBIFContractService().contractQuery(request);
if (response.getErrorCode() == 0) {
    BIFContractCallResult result = response.getResult();
    System.out.println(JsonUtils.toJSONString(result));
} else {
    System.out.println(JsonUtils.toJSONString(response));
}

查询的返回如下:

{
	"query_rets": [{
		"error": {},
		"result": {
			"code": 0,
			"data": "[abc]",
			"desc": "",
			"evmcode": "000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000036162630000000000000000000000000000000000000000000000000000000000",
			"gasused": 9985
		}
	}]
}