第三章 通过java SDK 实现个性化智能合约的部署与测试

 想了解相关区块链开发,技术提问,请加QQ群:538327407(已满),群2:135019400

 

前提

已经部署好底层,外网可以正常请求访问。

 

正常流程

1、基础合约处理

https://fisco-bcos-documentation.readthedocs.io/zh_CN/latest/docs/tutorial/sdk_application.html#id2

 

将官方的Asset.sol 代码copy,使用vim Asset.sol 命令创建,copy 到里面。

 

上一小节,我们根据业务需求设计了合约Asset.sol的存储与接口,给出了完整实现,但是Java程序无法直接调用Solidity合约,需要先将Solidity合约文件编译为Java文件。

控制台提供了编译工具,可以将Asset.sol合约文件存放在console/contracts/solidity目录。利用console目录下提供的sol2java.sh脚本进行编译,操作如下:

# 切换到fisco/console/目录 $ cd ~/fisco/console/ # 编译合约,后面指定一个Java的包名参数,可以根据实际项目路径指定包名 $ ./sol2java.sh org.fisco.bcos.asset.contract

 

 2、合约转化

用官方的asset_app,自己生成的在sdk 那边用有问题

 

运行成功之后,将会在console/contracts/sdk目录生成javaabibin目录,如下所示。

 

java目录下生成了org/fisco/bcos/asset/contract/包路径目录,该目录下包含Asset.javaTable.java两个文件,其中Asset.javaJava应用调用Asset.sol合约需要的文件。

 

3、SDK配置

我们提供了一个Java工程项目供开发使用,首先获取Java工程项目:

# 获取Java工程项目压缩包 $ cd ~ $ curl -LO https://github.com/FISCO-BCOS/LargeFiles/raw/master/tools/asset-app.tar.gz # 解压得到Java工程项目asset-app目录 $ tar -zxf asset-app.tar.gz

asset-app项目的目录结构如下:

 

4、使用sdk进行开发

asset_app sol、指定的合约的javaabibin等文件copy 到项目中,使用winscp等软件copy

5、底层部署和测试

·         编译

# 切换到项目目录 $ cd ~/asset-app # 编译项目 $ ./gradlew build

编译成功之后,将在项目根目录下生成dist目录。dist目录下有一个asset_run.sh脚本,简化项目运行。现在开始一一验证本文开始定下的需求。

·         部署Asset.sol合约

# 进入dist目录 $ cd dist $ bash asset_run.sh deploy Deploy Asset succesfully, contract address is 0xd09ad04220e40bb8666e885730c8c460091a4775

·         注册资产

$ bash asset_run.sh register Alice 100000 Register account succesfully=> account: Alice, value: 100000 $ bash asset_run.sh register Bob 100000 Register account succesfully=> account: Bob, value: 100000

·         查询资产

$ bash asset_run.sh query Alice account Alice, value 100000 $ bash asset_run.sh query Bob account Bob, value 100000

·         资产转移

$ bash asset_run.sh transfer Alice Bob 50000 Transfer successfully=> from_account: Alice, to_account: Bob, amount: 50000 $ bash asset_run.sh query Alice account Alice, value 50000 $ bash asset_run.sh query Bob account Bob, value 150000

 

 

6、本地编写单元测试

 

  1 package customTest;
  2 
  3 import javafx.concurrent.Service;
  4 import org.fisco.bcos.Application;
  5 import org.fisco.bcos.solidity.Asset;
  6 import org.fisco.bcos.temp.HelloWorld;
  7 import org.fisco.bcos.web3j.crypto.Credentials;
  8 import org.fisco.bcos.web3j.crypto.gm.GenCredential;
  9 import org.fisco.bcos.web3j.protocol.Web3j;
 10 import org.fisco.bcos.web3j.protocol.core.methods.response.TransactionReceipt;
 11 import org.fisco.bcos.web3j.tuples.generated.Tuple2;
 12 import org.fisco.bcos.web3j.tx.gas.StaticGasProvider;
 13 import org.junit.After;
 14 import org.junit.Before;
 15 import org.junit.Test;
 16 import org.junit.runner.RunWith;
 17 import org.springframework.beans.factory.annotation.Autowired;
 18 import org.springframework.boot.test.context.SpringBootTest;
 19 import org.springframework.context.ApplicationContext;
 20 import org.springframework.context.support.ClassPathXmlApplicationContext;
 21 import org.springframework.test.context.junit4.SpringRunner;
 22 
 23 import java.math.BigInteger;
 24 
 25 import static org.junit.Assert.assertTrue;
 26 
 27 @RunWith(SpringRunner.class)
 28 @SpringBootTest(classes = Application.class)
 29 public class AssetTest {
 30     private Credentials credentials;
 31     private static BigInteger gasPrice = new BigInteger("300000000");
 32     private static BigInteger gasLimit = new BigInteger("300000000");
 33     @Autowired
 34     Web3j web3j;
 35 
 36     //这很重要,没有这个无法通过
 37     @Before
 38     public void setUp() throws Exception {
 39        /* credentials =
 40                 GenCredential.create(
 41                         "b83261efa42895c38c6c2364ca878f43e77f3cddbc922bf57d0d48070f79feb6");
 42         if (credentials == null) {
 43             throw new Exception("create Credentials failed");
 44         }*/
 45 
 46          credentials = GenCredential.create();
 47     }
 48 
 49     @After
 50     public void tearDown() {
 51     }
 52 
 53     @Test
 54     public void DoAsset() throws Exception {
 55         AssetRegisterAndQuery();
 56         //DeployAsset();
 57 
 58     }
 59     @Test
 60     //部署合约
 61     public void DeployAsset() throws Exception {
 62         // 部署合约
 63         Asset asset = Asset.deploy(web3j, credentials, new StaticGasProvider(gasPrice, gasLimit)).send();
 64 
 65         if (asset != null) {
 66             System.out.println("HelloWorld address is: " + asset.getContractAddress());
 67         }
 68 
 69     }
 70     @Test
 71     //用户注册 资产查询
 72     public void AssetRegisterAndQuery()throws Exception  {
 73         String contractAddress = "0xf9343346a8d80c3d2f2026bf72fff3aec48a4133";
 74         // 加载合约地址
 75         Asset asset = Asset.load(contractAddress, web3j, credentials, new StaticGasProvider(gasPrice, gasLimit));
 76 
 77         if (asset != null) {
 78             System.out.println("Asset address is: " + asset.getContractAddress());
 79             // call set function
 80             System.out.println("1、注册用户,并注册资产--------------------------------------");
 81             String assetAccount1="0x608153babb8b00f11523f6b1b2b225ea9e7dfd8b";
 82             String assetAccount2="0x86f17b879ce121e5d00351a120de0bd39867bf4c";
 83             // register接口调用
 84             TransactionReceipt receipt1 = asset.register(assetAccount1, new BigInteger("121210000") ).send();
 85             TransactionReceipt receipt2 = asset.register(assetAccount2, new BigInteger("121121213") ).send();
 86 
 87             System.out.println("receipt1="+receipt1.toString());
 88             System.out.println("receipt2="+receipt2.toString());
 89 
 90             System.out.println("2、查询用户资产----------------------------------------------");
 91             // select接口调用
 92             Tuple2<BigInteger, BigInteger> result1 = asset.select("abc").send();
 93             Tuple2<BigInteger, BigInteger> result2 = asset.select("ddd").send();
 94             System.out.println("Tuple2<BigInteger, BigInteger> result1="+result1.toString());
 95             System.out.println("Tuple2<BigInteger, BigInteger> result2="+result2.toString());
 96             //assertTrue("Hello, World!".equals(result));
 97         }
 98 
 99 
100     }
101 
102     // 资产交易
103     @Test
104     public  void AssetTransfer() throws  Exception{
105 
106         String contractAddress = "0xf9343346a8d80c3d2f2026bf72fff3aec48a4133";
107         // 加载合约地址
108         Asset asset = Asset.load(contractAddress, web3j, credentials, new StaticGasProvider(gasPrice, gasLimit));
109 
110         String fromAssetAccount="0x608153babb8b00f11523f6b1b2b225ea9e7dfd8b";
111         String  toAssetAccount="0x86f17b879ce121e5d00351a120de0bd39867bf4c";
112       BigInteger amount = new BigInteger("121210000");
113         if (asset != null) {
114             // transfer接口
115             TransactionReceipt receipt = asset.transfer( fromAssetAccount,toAssetAccount, amount).send();
116             System.out.println("AssetTest.AssetTransfer receipt="+receipt.toString());
117         }
118 
119     }
120 }

 

 

读后感觉不错,有收获可以微信请作者喝杯咖啡,读后有疑问请加微信,拉群研讨,注明来意

 

 

 

 

posted @ 2019-06-21 18:04  linbin524  阅读(3453)  评论(0编辑  收藏  举报