From 69c107d29371d5d643d48fb600742a061576d498 Mon Sep 17 00:00:00 2001 From: Kyon <32325790+kyonRay@users.noreply.github.com> Date: Fri, 30 Aug 2024 16:14:41 +0800 Subject: [PATCH] (contract): add 0.4.25 and 0.5.2 example contracts. (#856) --- .../console/contract/ConsoleContractImpl.java | 6 +-- .../contract/solidity/0.4.25/BaseEvent.sol | 20 ++++++++ .../contract/solidity/0.4.25/Cast.sol | 22 +++++++++ .../contract/solidity/0.4.25/CastTest.sol | 40 ++++++++++++++++ .../contract/solidity/0.4.25/Crypto.sol | 14 ++++++ .../contract/solidity/0.4.25/EchoEvent.sol | 35 ++++++++++++++ .../contract/solidity/0.4.25/HelloWorld.sol | 17 +++++++ .../contract/solidity/0.4.25/ShaTest.sol | 41 +++++++++++++++++ .../contract/solidity/0.5.2/BaseEvent.sol | 20 ++++++++ .../contract/solidity/0.5.2/Cast.sol | 23 ++++++++++ .../contract/solidity/0.5.2/CastTest.sol | 40 ++++++++++++++++ .../contract/solidity/0.5.2/Crypto.sol | 14 ++++++ .../contract/solidity/0.5.2/EchoEvent.sol | 35 ++++++++++++++ .../contract/solidity/0.5.2/HelloWorld.sol | 17 +++++++ .../contract/solidity/0.5.2/ShaTest.sol | 41 +++++++++++++++++ .../resources/contract/solidity/EchoEvent.sol | 46 ++++++------------- 16 files changed, 397 insertions(+), 34 deletions(-) create mode 100644 src/main/resources/contract/solidity/0.4.25/BaseEvent.sol create mode 100644 src/main/resources/contract/solidity/0.4.25/Cast.sol create mode 100644 src/main/resources/contract/solidity/0.4.25/CastTest.sol create mode 100644 src/main/resources/contract/solidity/0.4.25/Crypto.sol create mode 100644 src/main/resources/contract/solidity/0.4.25/EchoEvent.sol create mode 100644 src/main/resources/contract/solidity/0.4.25/HelloWorld.sol create mode 100644 src/main/resources/contract/solidity/0.4.25/ShaTest.sol create mode 100644 src/main/resources/contract/solidity/0.5.2/BaseEvent.sol create mode 100644 src/main/resources/contract/solidity/0.5.2/Cast.sol create mode 100644 src/main/resources/contract/solidity/0.5.2/CastTest.sol create mode 100644 src/main/resources/contract/solidity/0.5.2/Crypto.sol create mode 100644 src/main/resources/contract/solidity/0.5.2/EchoEvent.sol create mode 100644 src/main/resources/contract/solidity/0.5.2/HelloWorld.sol create mode 100644 src/main/resources/contract/solidity/0.5.2/ShaTest.sol diff --git a/src/main/java/console/contract/ConsoleContractImpl.java b/src/main/java/console/contract/ConsoleContractImpl.java index 6a23e106..ef966af5 100644 --- a/src/main/java/console/contract/ConsoleContractImpl.java +++ b/src/main/java/console/contract/ConsoleContractImpl.java @@ -1,6 +1,5 @@ package console.contract; -import static console.contract.utils.ContractCompiler.mergeAbi; import static console.contract.utils.ContractCompiler.mergeSource; import static org.fisco.solc.compiler.SolidityCompiler.Options.ABI; import static org.fisco.solc.compiler.SolidityCompiler.Options.BIN; @@ -1115,9 +1114,10 @@ private String getSolidityAbi(String contractFileName) throws Exception { throw new CompileSolidityException( " Compile " + solFile.getName() + " error: " + res.getErrors()); } - + String contractName = solFile.getName().split("\\.")[0]; CompilationResult result = CompilationResult.parse(res.getOutput()); - return mergeAbi(result); + CompilationResult.ContractMetadata contractMetadata = result.getContract(contractName); + return contractMetadata.abi; } private String getWasmAbi(String groupId, String pwd, String contractFileName) diff --git a/src/main/resources/contract/solidity/0.4.25/BaseEvent.sol b/src/main/resources/contract/solidity/0.4.25/BaseEvent.sol new file mode 100644 index 00000000..aca82cdf --- /dev/null +++ b/src/main/resources/contract/solidity/0.4.25/BaseEvent.sol @@ -0,0 +1,20 @@ +// SPDX-License-Identifier: Apache-2.0 +pragma solidity ^0.4.25; + +contract BaseEvent { + + //--------------------------------------------------------------------------------------------------------------- + event Transfer(string indexed from_account, string indexed to_account, uint256 indexed amount); + event TransferAccount(string indexed from_account,string indexed to_account); + event TransferAmount(uint256 indexed amount); + + function transfer(string memory from_account, string memory to_account, uint256 amount) public { + + emit Transfer(from_account, to_account, amount); + + emit TransferAccount(from_account, to_account); + + emit TransferAmount(amount); + + } +} diff --git a/src/main/resources/contract/solidity/0.4.25/Cast.sol b/src/main/resources/contract/solidity/0.4.25/Cast.sol new file mode 100644 index 00000000..86d86f92 --- /dev/null +++ b/src/main/resources/contract/solidity/0.4.25/Cast.sol @@ -0,0 +1,22 @@ +// SPDX-License-Identifier: Apache-2.0 +pragma solidity ^0.4.25; + +pragma experimental ABIEncoderV2; + +contract Cast { + function stringToS256(string memory) public view returns (int256); + + function stringToS64(string memory) public view returns (int64); + + function stringToU256(string memory) public view returns (uint256); + + function stringToAddr(string memory) public view returns (address); + + function stringToBytes32(string memory) public view returns (bytes32); + + function s256ToString(int256) public view returns (string memory); + function s64ToString(int64) public view returns (string memory); + function u256ToString(uint256) public view returns (string memory); + function addrToString(address) public view returns (string memory); + function bytes32ToString(bytes32) public view returns (string memory); +} \ No newline at end of file diff --git a/src/main/resources/contract/solidity/0.4.25/CastTest.sol b/src/main/resources/contract/solidity/0.4.25/CastTest.sol new file mode 100644 index 00000000..25395da1 --- /dev/null +++ b/src/main/resources/contract/solidity/0.4.25/CastTest.sol @@ -0,0 +1,40 @@ +// SPDX-License-Identifier: Apache-2.0 +pragma solidity ^0.4.25; + +import "./Cast.sol"; + +contract CastTest { + Cast constant cast = Cast(address(0x100f)); + + function stringToS256(string memory _s) public view returns (int256){ + return cast.stringToS256(_s); + } + + function stringToS64(string memory _s) public view returns (int64){ + return cast.stringToS64(_s); + } + function stringToU256(string memory _s) public view returns (uint256){ + return cast.stringToU256(_s); + } + function stringToAddr(string memory _s) public view returns (address){ + return cast.stringToAddr(_s); + } + function stringToBytes32(string memory _s) public view returns (bytes32){ + return cast.stringToBytes32(_s); + } + function s256ToString(int256 _i) public view returns (string memory){ + return cast.s256ToString(_i); + } + function s64ToString(int64 _i) public view returns (string memory){ + return cast.s64ToString(_i); + } + function u256ToString(uint256 _u) public view returns (string memory){ + return cast.u256ToString(_u); + } + function addrToString(address _a) public view returns (string memory){ + return cast.addrToString(_a); + } + function bytes32ToString(bytes32 _b) public view returns (string memory){ + return cast.bytes32ToString(_b); + } +} diff --git a/src/main/resources/contract/solidity/0.4.25/Crypto.sol b/src/main/resources/contract/solidity/0.4.25/Crypto.sol new file mode 100644 index 00000000..dcecb006 --- /dev/null +++ b/src/main/resources/contract/solidity/0.4.25/Crypto.sol @@ -0,0 +1,14 @@ +pragma solidity ^0.4.25; + +pragma experimental ABIEncoderV2; + +contract Crypto +{ + function sm3(bytes memory data) public view returns (bytes32){} + + function keccak256Hash(bytes memory data) public view returns (bytes32){} + + function sm2Verify(bytes32 message, bytes memory publicKey, bytes32 r, bytes32 s) public view returns (bool, address){} + + function curve25519VRFVerify(bytes memory message, bytes memory publicKey, bytes memory proof) public view returns (bool, uint256){} +} diff --git a/src/main/resources/contract/solidity/0.4.25/EchoEvent.sol b/src/main/resources/contract/solidity/0.4.25/EchoEvent.sol new file mode 100644 index 00000000..beba602a --- /dev/null +++ b/src/main/resources/contract/solidity/0.4.25/EchoEvent.sol @@ -0,0 +1,35 @@ +// SPDX-License-Identifier: Apache-2.0 +pragma solidity ^0.4.25; + +import "./BaseEvent.sol"; + +contract EchoEvent { + + event Echo(uint256 indexed u); + event Echo(int256 indexed i); + event Echo(string indexed s); + event Echo(uint256 indexed u, int256 indexed i, string indexed s); + + function echo(uint256 u, int256 i, string memory s) public returns(uint256, int256, string memory) { + + emit Echo(u); + emit Echo(i); + emit Echo(s); + emit Echo(u, i ,s); + + return (u, i , s); + } + + event Echo(bytes32 indexed bsn); + event Echo(bytes indexed bs); + event Echo(bytes32 indexed bsn, bytes indexed bs); + + function echo(bytes32 bsn, bytes memory bs) public returns(bytes32, bytes memory) { + + emit Echo(bsn); + emit Echo(bs); + emit Echo(bsn, bs); + + return (bsn, bs); + } +} diff --git a/src/main/resources/contract/solidity/0.4.25/HelloWorld.sol b/src/main/resources/contract/solidity/0.4.25/HelloWorld.sol new file mode 100644 index 00000000..522f9c3f --- /dev/null +++ b/src/main/resources/contract/solidity/0.4.25/HelloWorld.sol @@ -0,0 +1,17 @@ +pragma solidity ^0.4.25; + +contract HelloWorld { + string name; + + constructor() public { + name = "Hello, World!"; + } + + function get() public view returns (string memory) { + return name; + } + + function set(string memory n) public { + name = n; + } +} \ No newline at end of file diff --git a/src/main/resources/contract/solidity/0.4.25/ShaTest.sol b/src/main/resources/contract/solidity/0.4.25/ShaTest.sol new file mode 100644 index 00000000..c460df08 --- /dev/null +++ b/src/main/resources/contract/solidity/0.4.25/ShaTest.sol @@ -0,0 +1,41 @@ +// SPDX-License-Identifier: Apache-2.0 +pragma solidity ^0.4.25; + +pragma experimental ABIEncoderV2; + +import "./Crypto.sol"; + +contract ShaTest{ + bytes _data = "Hello, ShaTest"; + Crypto crypto; + + constructor() public { + address cryptoAddr = address(0x100a); + crypto = Crypto(cryptoAddr); + } + + function getSha256(bytes memory _memory) public returns(bytes32 result) + { + return sha256(_memory); + } + + function getKeccak256(bytes memory _memory) public returns(bytes32 result) + { + return keccak256(_memory); + } + + function calculateSM3(bytes memory _memory) public returns(bytes32 result) + { + return crypto.sm3(_memory); + } + + function calculateKeccak256(bytes memory _memory) public returns(bytes32 result) + { + return crypto.keccak256Hash(_memory); + } + + function getData() public view returns(bytes memory) + { + return _data; + } +} diff --git a/src/main/resources/contract/solidity/0.5.2/BaseEvent.sol b/src/main/resources/contract/solidity/0.5.2/BaseEvent.sol new file mode 100644 index 00000000..597840b7 --- /dev/null +++ b/src/main/resources/contract/solidity/0.5.2/BaseEvent.sol @@ -0,0 +1,20 @@ +// SPDX-License-Identifier: Apache-2.0 +pragma solidity ^0.5.2; + +contract BaseEvent { + + //--------------------------------------------------------------------------------------------------------------- + event Transfer(string indexed from_account, string indexed to_account, uint256 indexed amount); + event TransferAccount(string indexed from_account,string indexed to_account); + event TransferAmount(uint256 indexed amount); + + function transfer(string memory from_account, string memory to_account, uint256 amount) public { + + emit Transfer(from_account, to_account, amount); + + emit TransferAccount(from_account, to_account); + + emit TransferAmount(amount); + + } +} diff --git a/src/main/resources/contract/solidity/0.5.2/Cast.sol b/src/main/resources/contract/solidity/0.5.2/Cast.sol new file mode 100644 index 00000000..1efe2869 --- /dev/null +++ b/src/main/resources/contract/solidity/0.5.2/Cast.sol @@ -0,0 +1,23 @@ +// SPDX-License-Identifier: Apache-2.0 +pragma solidity ^0.5.2; + + +pragma experimental ABIEncoderV2; + +contract Cast { + function stringToS256(string memory) public view returns (int256); + + function stringToS64(string memory) public view returns (int64); + + function stringToU256(string memory) public view returns (uint256); + + function stringToAddr(string memory) public view returns (address); + + function stringToBytes32(string memory) public view returns (bytes32); + + function s256ToString(int256) public view returns (string memory); + function s64ToString(int64) public view returns (string memory); + function u256ToString(uint256) public view returns (string memory); + function addrToString(address) public view returns (string memory); + function bytes32ToString(bytes32) public view returns (string memory); +} \ No newline at end of file diff --git a/src/main/resources/contract/solidity/0.5.2/CastTest.sol b/src/main/resources/contract/solidity/0.5.2/CastTest.sol new file mode 100644 index 00000000..f2f56314 --- /dev/null +++ b/src/main/resources/contract/solidity/0.5.2/CastTest.sol @@ -0,0 +1,40 @@ +// SPDX-License-Identifier: Apache-2.0 +pragma solidity ^0.5.2; + +import "./Cast.sol"; + +contract CastTest { + Cast constant cast = Cast(address(0x100f)); + + function stringToS256(string memory _s) public view returns (int256){ + return cast.stringToS256(_s); + } + + function stringToS64(string memory _s) public view returns (int64){ + return cast.stringToS64(_s); + } + function stringToU256(string memory _s) public view returns (uint256){ + return cast.stringToU256(_s); + } + function stringToAddr(string memory _s) public view returns (address){ + return cast.stringToAddr(_s); + } + function stringToBytes32(string memory _s) public view returns (bytes32){ + return cast.stringToBytes32(_s); + } + function s256ToString(int256 _i) public view returns (string memory){ + return cast.s256ToString(_i); + } + function s64ToString(int64 _i) public view returns (string memory){ + return cast.s64ToString(_i); + } + function u256ToString(uint256 _u) public view returns (string memory){ + return cast.u256ToString(_u); + } + function addrToString(address _a) public view returns (string memory){ + return cast.addrToString(_a); + } + function bytes32ToString(bytes32 _b) public view returns (string memory){ + return cast.bytes32ToString(_b); + } +} diff --git a/src/main/resources/contract/solidity/0.5.2/Crypto.sol b/src/main/resources/contract/solidity/0.5.2/Crypto.sol new file mode 100644 index 00000000..033f29f8 --- /dev/null +++ b/src/main/resources/contract/solidity/0.5.2/Crypto.sol @@ -0,0 +1,14 @@ +pragma solidity ^0.5.2; + +pragma experimental ABIEncoderV2; + +contract Crypto +{ + function sm3(bytes memory data) public view returns (bytes32){} + + function keccak256Hash(bytes memory data) public view returns (bytes32){} + + function sm2Verify(bytes32 message, bytes memory publicKey, bytes32 r, bytes32 s) public view returns (bool, address){} + + function curve25519VRFVerify(bytes memory message, bytes memory publicKey, bytes memory proof) public view returns (bool, uint256){} +} diff --git a/src/main/resources/contract/solidity/0.5.2/EchoEvent.sol b/src/main/resources/contract/solidity/0.5.2/EchoEvent.sol new file mode 100644 index 00000000..a655b449 --- /dev/null +++ b/src/main/resources/contract/solidity/0.5.2/EchoEvent.sol @@ -0,0 +1,35 @@ +// SPDX-License-Identifier: Apache-2.0 +pragma solidity ^0.5.2; + +import "./BaseEvent.sol"; + +contract EchoEvent { + + event Echo(uint256 indexed u); + event Echo(int256 indexed i); + event Echo(string indexed s); + event Echo(uint256 indexed u, int256 indexed i, string indexed s); + + function echo(uint256 u, int256 i, string memory s) public returns(uint256, int256, string memory) { + + emit Echo(u); + emit Echo(i); + emit Echo(s); + emit Echo(u, i ,s); + + return (u, i , s); + } + + event Echo(bytes32 indexed bsn); + event Echo(bytes indexed bs); + event Echo(bytes32 indexed bsn, bytes indexed bs); + + function echo(bytes32 bsn, bytes memory bs) public returns(bytes32, bytes memory) { + + emit Echo(bsn); + emit Echo(bs); + emit Echo(bsn, bs); + + return (bsn, bs); + } +} diff --git a/src/main/resources/contract/solidity/0.5.2/HelloWorld.sol b/src/main/resources/contract/solidity/0.5.2/HelloWorld.sol new file mode 100644 index 00000000..b8dbaf68 --- /dev/null +++ b/src/main/resources/contract/solidity/0.5.2/HelloWorld.sol @@ -0,0 +1,17 @@ +pragma solidity ^0.5.2; + +contract HelloWorld { + string name; + + constructor() public { + name = "Hello, World!"; + } + + function get() public view returns (string memory) { + return name; + } + + function set(string memory n) public { + name = n; + } +} \ No newline at end of file diff --git a/src/main/resources/contract/solidity/0.5.2/ShaTest.sol b/src/main/resources/contract/solidity/0.5.2/ShaTest.sol new file mode 100644 index 00000000..c54f4f10 --- /dev/null +++ b/src/main/resources/contract/solidity/0.5.2/ShaTest.sol @@ -0,0 +1,41 @@ +// SPDX-License-Identifier: Apache-2.0 +pragma solidity ^0.5.2; + +pragma experimental ABIEncoderV2; + +import "./Crypto.sol"; + +contract ShaTest{ + bytes _data = "Hello, ShaTest"; + Crypto crypto; + + constructor() public { + address cryptoAddr = address(0x100a); + crypto = Crypto(cryptoAddr); + } + + function getSha256(bytes memory _memory) public returns(bytes32 result) + { + return sha256(_memory); + } + + function getKeccak256(bytes memory _memory) public returns(bytes32 result) + { + return keccak256(_memory); + } + + function calculateSM3(bytes memory _memory) public returns(bytes32 result) + { + return crypto.sm3(_memory); + } + + function calculateKeccak256(bytes memory _memory) public returns(bytes32 result) + { + return crypto.keccak256Hash(_memory); + } + + function getData() public view returns(bytes memory) + { + return _data; + } +} diff --git a/src/main/resources/contract/solidity/EchoEvent.sol b/src/main/resources/contract/solidity/EchoEvent.sol index cdbfae12..19f7c04d 100644 --- a/src/main/resources/contract/solidity/EchoEvent.sol +++ b/src/main/resources/contract/solidity/EchoEvent.sol @@ -1,50 +1,34 @@ // SPDX-License-Identifier: Apache-2.0 pragma solidity >=0.6.10 <=0.8.26; -contract BaseEvent { - - //--------------------------------------------------------------------------------------------------------------- - event Transfer(string indexed from_account, string indexed to_account, uint256 indexed amount); - event TransferAccount(string indexed from_account,string indexed to_account); - event TransferAmount(uint256 indexed amount); - - function transfer(string memory from_account, string memory to_account, uint256 amount) public { - - emit Transfer(from_account, to_account, amount); - - emit TransferAccount(from_account, to_account); - - emit TransferAmount(amount); - - } -} +import "./BaseEvent.sol"; contract EchoEvent { - event Echo1(uint256 indexed u); - event Echo2(int256 indexed i); - event Echo3(string indexed s); - event Echo4(uint256 indexed u, int256 indexed i, string indexed s); + event Echo(uint256 indexed u); + event Echo(int256 indexed i); + event Echo(string indexed s); + event Echo(uint256 indexed u, int256 indexed i, string indexed s); function echo(uint256 u, int256 i, string memory s) public returns(uint256, int256, string memory) { - emit Echo1(u); - emit Echo2(i); - emit Echo3(s); - emit Echo4(u, i ,s); + emit Echo(u); + emit Echo(i); + emit Echo(s); + emit Echo(u, i ,s); return (u, i , s); } - event Echo5(bytes32 indexed bsn); - event Echo6(bytes indexed bs); - event Echo7(bytes32 indexed bsn, bytes indexed bs); + event Echo(bytes32 indexed bsn); + event Echo(bytes indexed bs); + event Echo(bytes32 indexed bsn, bytes indexed bs); function echo(bytes32 bsn, bytes memory bs) public returns(bytes32, bytes memory) { - emit Echo5(bsn); - emit Echo6(bs); - emit Echo7(bsn, bs); + emit Echo(bsn); + emit Echo(bs); + emit Echo(bsn, bs); return (bsn, bs); }