前言
最近在学区块链相关,想做点自己感兴趣的。网上关于这块部分的坑也比较多,最近也是问了很多行业从事者才慢慢填坑,因此记录下来分享一下。
一、准备工作
钱包:metemask、solidity编译器:remix 、java ide:idea。
二、智能合约的编写
智能合约编写的我选择在remix上方便,而且部署的时候不需要自定义gasPrice,remix会自动调整价格。附上完整solidity合约代码:
pragma solidity ^0.4.19;
contract SimpleStorage {
// 留言结构体
struct Message {
string word; // 留言
address from; // 留言者地址
string timestamp ; // 留言unix时间戳
}
Message[] private wordArr;
/**
* 写入留言的方法
*/
function setWord(string s, string t) public {
wordArr.push(Message({
word: s,
from: msg.sender,
timestamp: t
}));
}
/**
* 获取随机留言的方法
*/
function getRandomWord(uint random) public view returns (uint, string, address, string) {
if(wordArr.length==0){
return (0, "", msg.sender, "");
}else{
Message storage result = wordArr[random];
return (wordArr.length, result.word, result.from, result.timestamp);
}
}
}
- 这一部分填写在remix上后先编译
- 第二步来到部署运行界面,选择测试链的虚拟环境(Injected Web3),他会
自动连接你的小狐狸账户。如果你想后续切换账号,或者没连接上,请点击已连接,做出设置。 在这里可以先部署一次,然后获取部署后的合约地址 - 部署完,他会返回一个地址。把地址填入At Address即可获取地址上的合约,建议部署完就先测试一下,是否地址是否正确,部署是否成功。
三、用java完成web3j调用
在确保remix部署上去,以及可以在remix调用部署函数后进行下一步。
(一)、生成对应合约的bin 以及 abi 文件
直接上Java代码先:
public static void generateABIAndBIN(String abi,String bin,String abiFileName,String binFileName){
File abiFile = new File("src/main/resources/"+abiFileName);
File binFile = new File("src/main/resources/"+binFileName);
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){
e.printStackTrace();
throw new BlogException(201,"留言写入过程出现错误");
}finally {
if(abiBos != null){
try{
abiBos.close();;
}catch (IOException e){
e.printStackTrace();
}
}
if(binBos != null){
try {
binBos.close();
}catch (IOException e){
e.printStackTrace();
}
}
}
}
- 接着去之前部署好的remix上复制所需的bin ,abi复制。
将复制好的abi,bin填入函数内,以及自己即将要生成的abi,bin的文件名。如下是测试函数:
@Test
void genAbi(){
String abi = "[\n" +
"\t{\n" +
"\t\t\"constant\": false,\n" +
"\t\t\"inputs\": [\n" +
"\t\t\t{\n" +
"\t\t\t\t\"name\": \"s\",\n" +
"\t\t\t\t\"type\": \"string\"\n" +
"\t\t\t},\n" +
"\t\t\t{\n" +
"\t\t\t\t\"name\": \"t\",\n" +
"\t\t\t\t\"type\": \"string\"\n" +
"\t\t\t}\n" +
"\t\t],\n" +
"\t\t\"name\": \"setWord\",\n" +
"\t\t\"outputs\": [],\n" +
"\t\t\"payable\": false,\n" +
"\t\t\"stateMutability\": \"nonpayable\",\n" +
"\t\t\"type\": \"function\"\n" +
"\t},\n" +
"\t{\n" +
"\t\t\"constant\": true,\n" +
"\t\t\"inputs\": [\n" +
"\t\t\t{\n" +
"\t\t\t\t\"name\": \"random\",\n" +
"\t\t\t\t\"type\": \"uint256\"\n" +
"\t\t\t}\n" +
"\t\t],\n" +
"\t\t\"name\": \"getRandomWord\",\n" +
"\t\t\"outputs\": [\n" +
"\t\t\t{\n" +
"\t\t\t\t\"name\": \"\",\n" +
"\t\t\t\t\"type\": \"uint256\"\n" +
"\t\t\t},\n" +
"\t\t\t{\n" +
"\t\t\t\t\"name\": \"\",\n" +
"\t\t\t\t\"type\": \"string\"\n" +
"\t\t\t},\n" +
"\t\t\t{\n" +
"\t\t\t\t\"name\": \"\",\n" +
"\t\t\t\t\"type\": \"address\"\n" +
"\t\t\t},\n" +
"\t\t\t{\n" +
"\t\t\t\t\"name\": \"\",\n" +
"\t\t\t\t\"type\": \"string\"\n" +
"\t\t\t}\n" +
"\t\t],\n" +
"\t\t\"payable\": false,\n" +
"\t\t\"stateMutability\": \"view\",\n" +
"\t\t\"type\": \"function\"\n" +
"\t}\n" +
"]";
String bin = "{\n" +
"\t\"linkReferences\": {},\n" +
"\t\"object\": \"608060405234801561001057600080fd5b5061055e806100206000396000f30060806040526004361061004b5763ffffffff7c01000000000000000000000000000000000000000000000000000000006000350416638c595a958114610050578063cc76b911146100e9575b600080fd5b34801561005c57600080fd5b506040805160206004803580820135601f81018490048402850184019095528484526100e794369492936024939284019190819084018382808284375050604080516020601f89358b018035918201839004830284018301909452808352979a9998810197919650918201945092508291508401838280828437509497506102199650505050505050565b005b3480156100f557600080fd5b506101016004356102e0565b60405180858152602001806020018473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200180602001838103835286818151815260200191508051906020019080838360005b8381101561017a578181015183820152602001610162565b50505050905090810190601f1680156101a75780820380516001836020036101000a031916815260200191505b50838103825284518152845160209182019186019080838360005b838110156101da5781810151838201526020016101c2565b50505050905090810190601f1680156102075780820380516001836020036101000a031916815260200191505b50965050505050505060405180910390f35b6040805160608101825283815233602080830191909152918101839052600080546001810180835591805282518051929460039092027f290decd9548b62a8d60345a988386fc84ba6bc95484008f6362f93160ef3e563019261027f9284920190610497565b5060208281015160018301805473ffffffffffffffffffffffffffffffffffffffff191673ffffffffffffffffffffffffffffffffffffffff909216919091179055604083015180516102d89260028501920190610497565b505050505050565b60006060600060606000808054905060001415610321576040805160208181018352600080835283519182019093528281529196509450339350915061048f565b600080548790811061032f57fe5b6000918252602080832092546001600390930290930182810154815460408051601f6002600019988516156101000298909801909316879004928301869004860281018601909152818152929650869473ffffffffffffffffffffffffffffffffffffffff9092169391850192918591908301828280156103f15780601f106103c6576101008083540402835291602001916103f1565b820191906000526020600020905b8154815290600101906020018083116103d457829003601f168201915b5050845460408051602060026001851615610100026000190190941693909304601f81018490048402820184019092528181529598508694509250840190508282801561047f5780601f106104545761010080835404028352916020019161047f565b820191906000526020600020905b81548152906001019060200180831161046257829003601f168201915b5050505050905094509450945094505b509193509193565b828054600181600116156101000203166002900490600052602060002090601f016020900481019282601f106104d857805160ff1916838001178555610505565b82800160010185558215610505579182015b828111156105055782518255916020019190600101906104ea565b50610511929150610515565b5090565b61052f91905b80821115610511576000815560010161051b565b905600a165627a7a72305820030902f26f4ec7fc7f27262b5556ae47e4e6d2ca8bc95009b6be84f0dfb9326b0029\",\n" +
"\t\"opcodes\": \"PUSH1 0x80 PUSH1 0x40 MSTORE CALLVALUE DUP1 ISZERO PUSH2 0x10 JUMPI PUSH1 0x0 DUP1 REVERT JUMPDEST POP PUSH2 0x55E DUP1 PUSH2 0x20 PUSH1 0x0 CODECOPY PUSH1 0x0 RETURN STOP PUSH1 0x80 PUSH1 0x40 MSTORE PUSH1 0x4 CALLDATASIZE LT PUSH2 0x4B JUMPI PUSH4 0xFFFFFFFF PUSH29 0x100000000000000000000000000000000000000000000000000000000 PUSH1 0x0 CALLDATALOAD DIV AND PUSH4 0x8C595A95 DUP2 EQ PUSH2 0x50 JUMPI DUP1 PUSH4 0xCC76B911 EQ PUSH2 0xE9 JUMPI JUMPDEST PUSH1 0x0 DUP1 REVERT JUMPDEST CALLVALUE DUP1 ISZERO PUSH2 0x5C JUMPI PUSH1 0x0 DUP1 REVERT JUMPDEST POP PUSH1 0x40 DUP1 MLOAD PUSH1 0x20 PUSH1 0x4 DUP1 CALLDATALOAD DUP1 DUP3 ADD CALLDATALOAD PUSH1 0x1F DUP2 ADD DUP5 SWAP1 DIV DUP5 MUL DUP6 ADD DUP5 ADD SWAP1 SWAP6 MSTORE DUP5 DUP5 MSTORE PUSH2 0xE7 SWAP5 CALLDATASIZE SWAP5 SWAP3 SWAP4 PUSH1 0x24 SWAP4 SWAP3 DUP5 ADD SWAP2 SWAP1 DUP2 SWAP1 DUP5 ADD DUP4 DUP3 DUP1 DUP3 DUP5 CALLDATACOPY POP POP PUSH1 0x40 DUP1 MLOAD PUSH1 0x20 PUSH1 0x1F DUP10 CALLDATALOAD DUP12 ADD DUP1 CALLDATALOAD SWAP2 DUP3 ADD DUP4 SWAP1 DIV DUP4 MUL DUP5 ADD DUP4 ADD SWAP1 SWAP5 MSTORE DUP1 DUP4 MSTORE SWAP8 SWAP11 SWAP10 SWAP9 DUP2 ADD SWAP8 SWAP2 SWAP7 POP SWAP2 DUP3 ADD SWAP5 POP SWAP3 POP DUP3 SWAP2 POP DUP5 ADD DUP4 DUP3 DUP1 DUP3 DUP5 CALLDATACOPY POP SWAP5 SWAP8 POP PUSH2 0x219 SWAP7 POP POP POP POP POP POP POP JUMP JUMPDEST STOP JUMPDEST CALLVALUE DUP1 ISZERO PUSH2 0xF5 JUMPI PUSH1 0x0 DUP1 REVERT JUMPDEST POP PUSH2 0x101 PUSH1 0x4 CALLDATALOAD PUSH2 0x2E0 JUMP JUMPDEST PUSH1 0x40 MLOAD DUP1 DUP6 DUP2 MSTORE PUSH1 0x20 ADD DUP1 PUSH1 0x20 ADD DUP5 PUSH20 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF AND PUSH20 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF AND DUP2 MSTORE PUSH1 0x20 ADD DUP1 PUSH1 0x20 ADD DUP4 DUP2 SUB DUP4 MSTORE DUP7 DUP2 DUP2 MLOAD DUP2 MSTORE PUSH1 0x20 ADD SWAP2 POP DUP1 MLOAD SWAP1 PUSH1 0x20 ADD SWAP1 DUP1 DUP4 DUP4 PUSH1 0x0 JUMPDEST DUP4 DUP2 LT ISZERO PUSH2 0x17A JUMPI DUP2 DUP2 ADD MLOAD DUP4 DUP3 ADD MSTORE PUSH1 0x20 ADD PUSH2 0x162 JUMP JUMPDEST POP POP POP POP SWAP1 POP SWAP1 DUP2 ADD SWAP1 PUSH1 0x1F AND DUP1 ISZERO PUSH2 0x1A7 JUMPI DUP1 DUP3 SUB DUP1 MLOAD PUSH1 0x1 DUP4 PUSH1 0x20 SUB PUSH2 0x100 EXP SUB NOT AND DUP2 MSTORE PUSH1 0x20 ADD SWAP2 POP JUMPDEST POP DUP4 DUP2 SUB DUP3 MSTORE DUP5 MLOAD DUP2 MSTORE DUP5 MLOAD PUSH1 0x20 SWAP2 DUP3 ADD SWAP2 DUP7 ADD SWAP1 DUP1 DUP4 DUP4 PUSH1 0x0 JUMPDEST DUP4 DUP2 LT ISZERO PUSH2 0x1DA JUMPI DUP2 DUP2 ADD MLOAD DUP4 DUP3 ADD MSTORE PUSH1 0x20 ADD PUSH2 0x1C2 JUMP JUMPDEST POP POP POP POP SWAP1 POP SWAP1 DUP2 ADD SWAP1 PUSH1 0x1F AND DUP1 ISZERO PUSH2 0x207 JUMPI DUP1 DUP3 SUB DUP1 MLOAD PUSH1 0x1 DUP4 PUSH1 0x20 SUB PUSH2 0x100 EXP SUB NOT AND DUP2 MSTORE PUSH1 0x20 ADD SWAP2 POP JUMPDEST POP SWAP7 POP POP POP POP POP POP POP PUSH1 0x40 MLOAD DUP1 SWAP2 SUB SWAP1 RETURN JUMPDEST PUSH1 0x40 DUP1 MLOAD PUSH1 0x60 DUP2 ADD DUP3 MSTORE DUP4 DUP2 MSTORE CALLER PUSH1 0x20 DUP1 DUP4 ADD SWAP2 SWAP1 SWAP2 MSTORE SWAP2 DUP2 ADD DUP4 SWAP1 MSTORE PUSH1 0x0 DUP1 SLOAD PUSH1 0x1 DUP2 ADD DUP1 DUP4 SSTORE SWAP2 DUP1 MSTORE DUP3 MLOAD DUP1 MLOAD SWAP3 SWAP5 PUSH1 0x3 SWAP1 SWAP3 MUL PUSH32 0x290DECD9548B62A8D60345A988386FC84BA6BC95484008F6362F93160EF3E563 ADD SWAP3 PUSH2 0x27F SWAP3 DUP5 SWAP3 ADD SWAP1 PUSH2 0x497 JUMP JUMPDEST POP PUSH1 0x20 DUP3 DUP2 ADD MLOAD PUSH1 0x1 DUP4 ADD DUP1 SLOAD PUSH20 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF NOT AND PUSH20 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF SWAP1 SWAP3 AND SWAP2 SWAP1 SWAP2 OR SWAP1 SSTORE PUSH1 0x40 DUP4 ADD MLOAD DUP1 MLOAD PUSH2 0x2D8 SWAP3 PUSH1 0x2 DUP6 ADD SWAP3 ADD SWAP1 PUSH2 0x497 JUMP JUMPDEST POP POP POP POP POP POP JUMP JUMPDEST PUSH1 0x0 PUSH1 0x60 PUSH1 0x0 PUSH1 0x60 PUSH1 0x0 DUP1 DUP1 SLOAD SWAP1 POP PUSH1 0x0 EQ ISZERO PUSH2 0x321 JUMPI PUSH1 0x40 DUP1 MLOAD PUSH1 0x20 DUP2 DUP2 ADD DUP4 MSTORE PUSH1 0x0 DUP1 DUP4 MSTORE DUP4 MLOAD SWAP2 DUP3 ADD SWAP1 SWAP4 MSTORE DUP3 DUP2 MSTORE SWAP2 SWAP7 POP SWAP5 POP CALLER SWAP4 POP SWAP2 POP PUSH2 0x48F JUMP JUMPDEST PUSH1 0x0 DUP1 SLOAD DUP8 SWAP1 DUP2 LT PUSH2 0x32F JUMPI INVALID JUMPDEST PUSH1 0x0 SWAP2 DUP3 MSTORE PUSH1 0x20 DUP1 DUP4 KECCAK256 SWAP3 SLOAD PUSH1 0x1 PUSH1 0x3 SWAP1 SWAP4 MUL SWAP1 SWAP4 ADD DUP3 DUP2 ADD SLOAD DUP2 SLOAD PUSH1 0x40 DUP1 MLOAD PUSH1 0x1F PUSH1 0x2 PUSH1 0x0 NOT SWAP9 DUP6 AND ISZERO PUSH2 0x100 MUL SWAP9 SWAP1 SWAP9 ADD SWAP1 SWAP4 AND DUP8 SWAP1 DIV SWAP3 DUP4 ADD DUP7 SWAP1 DIV DUP7 MUL DUP2 ADD DUP7 ADD SWAP1 SWAP2 MSTORE DUP2 DUP2 MSTORE SWAP3 SWAP7 POP DUP7 SWAP5 PUSH20 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF SWAP1 SWAP3 AND SWAP4 SWAP2 DUP6 ADD SWAP3 SWAP2 DUP6 SWAP2 SWAP1 DUP4 ADD DUP3 DUP3 DUP1 ISZERO PUSH2 0x3F1 JUMPI DUP1 PUSH1 0x1F LT PUSH2 0x3C6 JUMPI PUSH2 0x100 DUP1 DUP4 SLOAD DIV MUL DUP4 MSTORE SWAP2 PUSH1 0x20 ADD SWAP2 PUSH2 0x3F1 JUMP JUMPDEST DUP3 ADD SWAP2 SWAP1 PUSH1 0x0 MSTORE PUSH1 0x20 PUSH1 0x0 KECCAK256 SWAP1 JUMPDEST DUP2 SLOAD DUP2 MSTORE SWAP1 PUSH1 0x1 ADD SWAP1 PUSH1 0x20 ADD DUP1 DUP4 GT PUSH2 0x3D4 JUMPI DUP3 SWAP1 SUB PUSH1 0x1F AND DUP3 ADD SWAP2 JUMPDEST POP POP DUP5 SLOAD PUSH1 0x40 DUP1 MLOAD PUSH1 0x20 PUSH1 0x2 PUSH1 0x1 DUP6 AND ISZERO PUSH2 0x100 MUL PUSH1 0x0 NOT ADD SWAP1 SWAP5 AND SWAP4 SWAP1 SWAP4 DIV PUSH1 0x1F DUP2 ADD DUP5 SWAP1 DIV DUP5 MUL DUP3 ADD DUP5 ADD SWAP1 SWAP3 MSTORE DUP2 DUP2 MSTORE SWAP6 SWAP9 POP DUP7 SWAP5 POP SWAP3 POP DUP5 ADD SWAP1 POP DUP3 DUP3 DUP1 ISZERO PUSH2 0x47F JUMPI DUP1 PUSH1 0x1F LT PUSH2 0x454 JUMPI PUSH2 0x100 DUP1 DUP4 SLOAD DIV MUL DUP4 MSTORE SWAP2 PUSH1 0x20 ADD SWAP2 PUSH2 0x47F JUMP JUMPDEST DUP3 ADD SWAP2 SWAP1 PUSH1 0x0 MSTORE PUSH1 0x20 PUSH1 0x0 KECCAK256 SWAP1 JUMPDEST DUP2 SLOAD DUP2 MSTORE SWAP1 PUSH1 0x1 ADD SWAP1 PUSH1 0x20 ADD DUP1 DUP4 GT PUSH2 0x462 JUMPI DUP3 SWAP1 SUB PUSH1 0x1F AND DUP3 ADD SWAP2 JUMPDEST POP POP POP POP POP SWAP1 POP SWAP5 POP SWAP5 POP SWAP5 POP SWAP5 POP JUMPDEST POP SWAP2 SWAP4 POP SWAP2 SWAP4 JUMP JUMPDEST DUP3 DUP1 SLOAD PUSH1 0x1 DUP2 PUSH1 0x1 AND ISZERO PUSH2 0x100 MUL SUB AND PUSH1 0x2 SWAP1 DIV SWAP1 PUSH1 0x0 MSTORE PUSH1 0x20 PUSH1 0x0 KECCAK256 SWAP1 PUSH1 0x1F ADD PUSH1 0x20 SWAP1 DIV DUP2 ADD SWAP3 DUP3 PUSH1 0x1F LT PUSH2 0x4D8 JUMPI DUP1 MLOAD PUSH1 0xFF NOT AND DUP4 DUP1 ADD OR DUP6 SSTORE PUSH2 0x505 JUMP JUMPDEST DUP3 DUP1 ADD PUSH1 0x1 ADD DUP6 SSTORE DUP3 ISZERO PUSH2 0x505 JUMPI SWAP2 DUP3 ADD JUMPDEST DUP3 DUP2 GT ISZERO PUSH2 0x505 JUMPI DUP3 MLOAD DUP3 SSTORE SWAP2 PUSH1 0x20 ADD SWAP2 SWAP1 PUSH1 0x1 ADD SWAP1 PUSH2 0x4EA JUMP JUMPDEST POP PUSH2 0x511 SWAP3 SWAP2 POP PUSH2 0x515 JUMP JUMPDEST POP SWAP1 JUMP JUMPDEST PUSH2 0x52F SWAP2 SWAP1 JUMPDEST DUP1 DUP3 GT ISZERO PUSH2 0x511 JUMPI PUSH1 0x0 DUP2 SSTORE PUSH1 0x1 ADD PUSH2 0x51B JUMP JUMPDEST SWAP1 JUMP STOP LOG1 PUSH6 0x627A7A723058 KECCAK256 SUB MULMOD MUL CALLCODE PUSH16 0x4EC7FC7F27262B5556AE47E4E6D2CA8B 0xc9 POP MULMOD 0xb6 0xbe DUP5 CREATE 0xdf 0xb9 ORIGIN PUSH12 0x2900000000000000000000 \",\n" +
"\t\"sourceMap\": \"28:871:0:-;;;;8:9:-1;5:2;;;30:1;27;20:12;5:2;28:871:0;;;;;;;\"\n" +
"}";
String abiFileName = "leaveMsg.abi";
String binFileName = "leaveMsg.bin";
SolidityUtils.generateABIAndBIN(abi,bin,abiFileName,binFileName);
}
接着就会生成如下abi,bin文件。
(二)、利用abi,bin文件生成solidity的java代码
public static void generateClass(String abiFile,String binFile,String generateFile){
String[] args = Arrays.asList(
"-a",abiFile,
"-b",binFile,
"-p","",
"-o",generateFile
).toArray(new String[0]);
Stream.of(args).forEach(System.out::println);
SolidityFunctionWrapperGenerator.main(args);
}
测试代码:
@Test
void generateJavaFile(){
String abiFile = "src/main/resources/leaveMsg.abi";
String binFile = "src/main/resources/leaveMsg.bin";
String generateFile = "src/main/java/com/geminicx/myblog/blog/solidity/";
SolidityUtils.generateClass(abiFile,binFile,generateFile);
}
- 然后就会生成与abi名字相同的java代码
(三)、(部署)调用合约上的函数
直接上代码
@Test
public void deplyContract() throws Exception{
Web3j web3 = Web3j.build(new HttpService("https://ropsten.infura.io/v3/9aa3d95b3bc440fa88ea12eaa4456161"));
Credentials credentials = Credentials.create("填入自己账户的私钥");
BigInteger gasPrice = web3.ethGasPrice().send().getGasPrice();
LeaveMsg contract = LeaveMsg.load("填入自己已部署的合约地址账",web3,credentials,
new StaticGasProvider(gasPrice,BigInteger.valueOf(3000000L)));
RemoteFunctionCall<TransactionReceipt> setWord = contract.setWord("这是一条java测试留言11111111", "8888");
TransactionReceipt transactionReceipt = setWord.sendAsync().get();
String transactionHash = transactionReceipt.getTransactionHash();
System.out.println(transactionHash);
如果不知道获取测试url的请看以下步骤: 打开小狐狸钱包添加网络:
在上图就可以获取各测试链url,有的时候可能需要科学上网。
最后附上一些官方文档: web3j官方文档 solidity官方文档
如果不足之处欢迎指正,觉得有帮助可以三连支持下!
|