日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > java >内容正文

java

智能合约 web3j Java_Java使用web3j调用智能合约

發布時間:2024/1/1 java 28 豆豆
生活随笔 收集整理的這篇文章主要介紹了 智能合约 web3j Java_Java使用web3j调用智能合约 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

1.Java程序引入相關依賴,后面用于調用智能合約中的函數

org.web3j

core

5.0.0

org.web3j

codegen

5.0.0

org.fisco-bcos

solcJ

0.5.2.0

commons-io

commons-io

2.4

com.squareup.okhttp3

okhttp

4.4.0

com.squareup.okhttp3

okhttp-ws

3.4.2

2.將合約使用remix進行編譯

編譯后,復制abi、Bytecode,放入指定位置,生成abi和bin文件

@Test

void generateABIAndBIN() {

String abi = "abi復制放這里";

String bin = "bin復制過來放這里";

String abiFileName = "abi文件名.abi";

String binFileName = "bin文件名.bin";

File abiFile = new File("E:\\solidity\\xx\\xx\\"+ abiFileName);

File binFile = new File("E:\\solidity\\xx\\xx\\"+ binFileName);

if (!abiFile.getParentFile().exists()) {

boolean result = abiFile.getParentFile().mkdirs();

if (!result) {

System.out.println("創建失敗");

}

}

BufferedOutputStream abiBos = null;

BufferedOutputStream binBos = null;

try {

FileOutputStream abiFos = new FileOutputStream(abiFile);

FileOutputStream binFos = new FileOutputStream(binFile);

abiBos = new BufferedOutputStream(abiFos);

binBos = new BufferedOutputStream(binFos);

abiBos.write(abi.getBytes());

abiBos.flush();

binBos.write(bin.getBytes());

binBos.flush();

}catch (Exception e) {

throw new RuntimeException("寫入過程出現錯誤");

}finally {

if(abiBos != null) {

try {

abiBos.close();

} catch (IOException e) {

e.printStackTrace();

}

}

if(binBos != null) {

try {

binBos.close();

} catch (IOException e) {

e.printStackTrace();

}

}

}

}

@Test

public void generateClass() throws IOException, ClassNotFoundException {

String[] args = Arrays.asList(

"-a", "D:/solidity/xx/xx.abi",

"-b", "D:/solidity/xx/xx.bin",

"-p", "top.rhynie.xx.contract",

"-o", "D:/IDEA_Project/xx/src/main/java"

).toArray(new String[0]);

Stream.of(args).forEach(System.out::println);

SolidityFunctionWrapperGenerator.main(args);

}

4.注冊infura獲取免費節點

5.java代碼調用只能合約代碼

@Test

void deployContract() throws Exception {

Web3j web3 = Web3j.build(new HttpService("https://ropsten.infura.io/v3/xxxx"));

String ownAddress = "0x119Eb8E686423E56b7cfc6F211C8CD4a9F71E3Cc";

// Credentials c = WalletUtils.loadCredentials("密碼","keystore文件地址");

Credentials credentials = Credentials.create("私鑰");

AWToken awToken = AWToken.deploy(web3, credentials, web3.ethGasPrice().send().getGasPrice(), Contract.GAS_LIMIT).send();

System.out.println(awToken.getContractAddress());

// 調用合約的函數

awToken.transfer("0x0", value);

}

總結

以上是生活随笔為你收集整理的智能合约 web3j Java_Java使用web3j调用智能合约的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。