windows10 java 创建合约
a. 安装Nodejs 主要是方便使用npm 命令 并配置环境变量
b.使用 npm 可以便捷地安装Solidity编译器solcjs
npm install -g solc
c.找个目录 创建一个solidity文件 如
// SPDX-License-Identifier: GPL-3.0pragma solidity >=0.8.2 <0.9.0;/* @title Storage* @dev Store & retrieve value in a variable* @custom:dev-run-script ./scripts/deploy_with_ethers.ts*/
contract Storage {uint256 number;/* @dev Store value in variable* @param num value to store*/function store(uint256 num) public {number = num;}/* @dev Return value * @return value of 'number'*/function retrieve() public view returns (uint256){return number;}
}
- 在刚才的目录下,输入cmd回车,进入控制台 执行
-
solcjs --abi --bin Storage.sol
生成abi 和 bin文件
-
d.下载代码web3j生成器 地址是 web3j java生成器
-
解压以后 有lib 和 bin 两个目录
-
控制台今日到bin目录 执行命令
-
web3j generate solidity -b D:\\eth\\simpleStorege_sol_Storage.bin -a D:\\eth\\simpleStorege_sol_Storage.abi -o . -p org.web3j.generated.contracts其中 org.web3j.generated.contracts 是生成代码的目录
出现下图标识 代表生成成功 则可以进入查找
-
pom文件需要引入的依赖有】
-
<dependency><groupId>org.web3j</groupId><artifactId>core</artifactId><version>3.6.0</version></dependency><dependency><groupId>org.web3j</groupId><artifactId>utils</artifactId><version>4.1.1</version></dependency><dependency><groupId>org.web3j</groupId><artifactId>geth</artifactId><version>3.2.0</version></dependency><dependency><groupId>org.web3j</groupId><artifactId>core</artifactId><version>4.8.4</version><exclusions><exclusion><groupId>org.jetbrains.kotlin</groupId><artifactId>kotlin-stdlib</artifactId></exclusion><exclusion><groupId>com.squareup.okhttp3</groupId><artifactId>okhttp</artifactId></exclusion></exclusions></dependency><dependency><groupId>org.web3j</groupId><artifactId>codegen</artifactId><version>5.0.0</version></dependency>