以太坊从重新部署合约到把生产相关文件引入web3j到解析input data

  1. 重新在Remix上测试合约成功后,将dapp目录下的contracts下对应的sol文件替换(以webpack生成的包为例子);
  2. 一般我会选择删除测试私链的数据,然后重新初始化、启动geth、新建账户、挖矿等;
    rm -rf data/* nohup.out output.log
    geth --datadir data/ init genesis.json
    nohup geth --datadir data/ --networkid 15 --rpc --rpcapi db,eth,net,web3,personal,miner --rpcport 8545 --rpcaddr 0.0.0.0 --allow-insecure-unlock --nodiscover --rpccorsdomain "*" 2>output.log &

    贴上相关的配置文件:
    genesis.json

    {
      "config": {
        "chainId": 15,
        "homesteadBlock": 0,
        "eip150Block": 0,
        "eip155Block": 0,
        "eip158Block": 0,
        "byzantiumBlock": 0,
        "constantinopleBlock": 0,
        "petersburgBlock": 0,
        "istanbulBlock": 0
      },
      "alloc": {},
      "coinbase": "0x0000000000000000000000000000000000000000",
      "difficulty": "0x100",
      "extraData": "",
      "gasLimit": "0xffffffff",
      "nonce": "0x0000000000000042",
      "mixhash": "0x0000000000000000000000000000000000000000000000000000000000000000",
      "parentHash": "0x0000000000000000000000000000000000000000000000000000000000000000",
      "timestamp": "0x00"
    }

    truffle-config.json

    require('babel-register')
    module.exports = {
      networks: {
        development: {
          port: 8545,      
          host: 'localhost',
          network_id: '*',
          gas: 6700000
        }
      },
      compilers: {
        solc: {
          version: "0.6.4"
        }
     }
    }
    

    2_deploy_contracts.js

    const SiLuan = artifacts.require("SiLuan");
    
    module.exports = function(deployer) {
      deployer.deploy(SiLuan);
    };


     

  3. 生成合约的java文件以便导入idea
     

    ../relatedEnv/web3j-4.5.5/bin/web3j truffle generate build/contracts/SiLuan.json -o . -p .

    前面这个是web3j的包的位置 -o 输出路径 -p 输出包

  4. truffle migrate 部署合约

  5. 将合约地址、私钥、账户地址等参数导入项目
     

    web3j:
        client-address: http://192.168.123.76:8545
        password=123456
        path=E:\\winshare\\UTC--2020-04-17T07-11-08.390910900Z--0e10b3a6c88909925401e7aa64efddf44670caa5
        contract_address=0x36Bd220e5c71bBdf9c1aF0B64bC9918b7014ac7f
        user1_address=0x0e10b3a6c88909925401e7aa64efddf44670caa5
        abi=[{"inputs":[],"stateMutability":"nonpayable","type":"constructor"},{"inputs":[{"internalType":"string",等等}]
    


     

  6. input data的解析
    导入包:
    相关帮助:GitHub - prettymuchbryce/abidecoder: Kotlin and Java library for decoding data params and events from ethereum transactions

            <dependency>
                <groupId>org.ethereum</groupId>
                <artifactId>ethereumj-core</artifactId>
                <version>1.7.2-RELEASE</version>
            </dependency>
            <dependency>
                <groupId>org.jetbrains.kotlin</groupId>
                <artifactId>kotlin-reflect</artifactId>
                <version>1.3.71</version>
                <scope>runtime</scope>
            </dependency>
            <dependency>
                <groupId>org.jetbrains.kotlin</groupId>
                <artifactId>kotlin-stdlib</artifactId>
                <version>1.3.71</version>
            </dependency>
            <dependency>
                <groupId>com.github.prettymuchbryce</groupId>
                <artifactId>abidecoder</artifactId>
                <version>3ae24ecb55</version>
            </dependency>

    最后一个包需要基于前三个包,前三个包可以直接下,最后一个包需要添加repository后再下:
     

    <repositories>
    	<repository>
    		<id>jitpack.io</id>
    		<url>https://jitpack.io</url>
    	</repository>
    </repositories>
    	
    <dependency>
    	<groupId>org.github.prettymuchbryce</groupId>
    	<artifactId>abidecoder</artifactId>
    	<version>master-SNAPSHOT</version>
    </dependency>
    使用方法
    
    import com.prettymuchbryce.abidecoder.Decoder;
    
    public class Main {
    	public static void main(String[] args) {
    		Decoder d = new Decoder();
    		d.addAbi(myAbiJsonString);
    		Decoder.DecodedMethod result = d.decodeMethod(methodData);
    		System.out.print(result);
    	}
    }

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值