当前位置:   article > 正文

Java SDK部署和调用FISCO BCOS区块链智能合约_fisco bcos java sdk

fisco bcos java sdk

背景

使用WeBASE合约管理导出Java项目

在这里插入图片描述

前提

启动FISCO节点和webase-front,部署服务详情可参考官方文档: link

步骤

1.编写智能合约

一个简单的例子:

Table 合约:该合约负责维护候选人的信息。每个候选人都有一个唯一的标识符sign_key,以及与之相关联的其他属性,包括活动名称activity_name、参与者participant、公钥publicKey、签名数据sign_data。通过添加候选人和查询候选人信息等功能,Table合约提供了对候选人信息的管理和检索。

// SPDX-License-Identifier: MIT
pragma solidity ^0.6.0;

contract Table {
    struct Candidate {
        string activity_name;
        string participant;
        string publicKey;
        string sign_data;
        string sign_key;
    }

    mapping(string => Candidate) public candidates;

    function addCandidate(string memory activity_name, string memory participant, string memory publicKey, string memory sign_data, string memory sign_key) public {
        candidates[sign_key] = Candidate(activity_name, participant, publicKey, sign_data, sign_key);
    }

    function getCandidateInfo(string memory sign_key) public view returns (string memory, string memory, string memory, string memory, string memory) {
        Candidate memory candidate = candidates[sign_key];
        return (candidate.activity_name, candidate.participant, candidate.publicKey, candidate.sign_data, candidate.sign_key);
    }
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24

在这里插入图片描述

Vote 合约:该合约则负责实现投票功能。它引用了Table合约来管理候选人信息。通过调用Table合约中的函数来添加候选人,Vote合约提供了将候选人加入到投票系统中的功能。另外,它还提供了查询候选人信息的功能。

// SPDX-License-Identifier: MIT
pragma solidity ^0.6.0;

import "./Table.sol";

contract Vote {
    Table public electionTable;

    event CandidateAdded(string activity_name, string participant, string publicKey, string sign_data, string sign_key);

    constructor() public {
        electionTable = new Table();
    }

    function addCandidate(string memory activity_name, string memory participant, string memory publicKey, string memory sign_data, string memory sign_key) public {
        electionTable.addCandidate(activity_name, participant, publicKey, sign_data, sign_key);
        emit CandidateAdded(activity_name, participant, publicKey, sign_data, sign_key);
    }

    function getCandidateInfo(string memory sign_key) public view returns (string memory, string memory, string memory, string memory, string memory) {
        return electionTable.getCandidateInfo(sign_key);
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23

在这里插入图片描述

2.编译及部署智能合约

再编译和部署完智能合约后,可以得到合约的地址

3.导出Java项目

点击右上角导出Java项目后,即可导出一个自命名的Java项目
在这里插入图片描述

4.在IDEA中打开导出的Java项目

项目结构如图:
在这里插入图片描述
这是一个gradle构建的SpringBoot项目

5.编写Controller层来测试智能合约中的方法

TestController.java测试合约的add和get方法是否调用成功

package org.example.demo2.controller;


import org.example.demo2.model.bo.VoteAddCandidateInputBO;
import org.example.demo2.model.bo.VoteGetCandidateInfoInputBO;
import org.example.demo2.service.VoteService;
import org.fisco.bcos.sdk.v3.transaction.model.dto.CallResponse;
import org.fisco.bcos.sdk.v3.transaction.model.dto.TransactionResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class TestController {


    @Autowired
    private VoteService voteService;

    @PostMapping("/addCandidate")
    public TransactionResponse set(@RequestBody VoteAddCandidateInputBO vai) throws Exception {

        TransactionResponse result = voteService.addCandidate(vai);

        return result;
    }

    @GetMapping("/getCandidateInfo")
    public CallResponse get(@RequestBody VoteGetCandidateInfoInputBO vgi) throws Exception {
        CallResponse response = voteService.getCandidateInfo(vgi);

        return response;
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36

在这里插入图片描述

6.启动项目

项目启动成功如图所示:
在这里插入图片描述

7.Postman测试add和get方法

下图为add方法:
在这里插入图片描述
下图为get方法:
在这里插入图片描述
调用方法成功

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/天景科技苑/article/detail/906827
推荐阅读
相关标签
  

闽ICP备14008679号