Skip to content

Commit cf744f4

Browse files
dalaocuarc0035
andauthored
release V1.0.0 (#2)
* init * read fix structure * fix dir * make it compile for 0.6.10 * add badge of downloads * fix readme Co-authored-by: aoronchu <[email protected]>
1 parent 728df81 commit cf744f4

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

48 files changed

+5699
-1
lines changed

README.md

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,38 @@
1-
# sol-lib
1+
[![GitHub All Releases](https://img.shields.io/github/downloads/WeBankBlockchain/SmartDev-Contract/total.svg)](https://github.com/WeBankBlockchain/SmartDev-Contract)
2+
3+
4+
# 组件介绍
5+
6+
智能合约库模板,涵盖了从基础类型到上层业务的常见代码,用户可根据实际需要进行参考、复用。
7+
8+
[](https://toolkit-doc.readthedocs.io/zh_CN/latest/_images/wescott.png)
9+
10+
## 环境要求
11+
12+
| 依赖软件 | 说明 |备注|
13+
| --- | --- | --- |
14+
| Solidity | >= 0.6.10 | 需使用0.6.10版本编译|
15+
| Git | 下载需要使用Git | |
16+
17+
## 文档
18+
- [**中文**](https://toolkit-doc.readthedocs.io/zh_CN/latest/docs/WeBankBlockchain-SmartDev-Contract/index.html)
19+
- [**快速开始**](https://toolkit-doc.readthedocs.io/zh_CN/latest/docs/WeBankBlockchain-SmartDev-Contract/quick_start.html)
20+
## 贡献代码
21+
欢迎参与本项目的社区建设:
22+
- 如项目对您有帮助,欢迎点亮我们的小星星(点击项目左上方Star按钮)。
23+
- 欢迎提交代码(Pull requests)。
24+
- [提问和提交BUG](https://github.com/WeBankBlockchain/SmartDev-Contract/issues)
25+
- 如果发现代码存在安全漏洞,请在[这里](https://security.webank.com)上报。
26+
27+
28+
![](https://media.githubusercontent.com/media/FISCO-BCOS/LargeFiles/master/images/QR_image.png)
29+
30+
31+
## License
32+
![license](http://img.shields.io/badge/license-Apache%20v2-blue.svg)
33+
34+
开源协议为[Apache License 2.0](http://www.apache.org/licenses/). 详情参考[LICENSE](../LICENSE)
35+
36+
37+
38+

contracts/base_type/LibAddress.sol

Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
/*
2+
* Copyright 2014-2019 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
* */
16+
17+
pragma solidity >=0.4.24 <0.6.11;
18+
19+
library LibAddress{
20+
21+
/*
22+
*@dev
23+
*@param
24+
*@return
25+
*/
26+
function isContract(address account) internal view returns(bool) {
27+
uint256 size;
28+
assembly { size := extcodesize(account) }
29+
return size > 0;
30+
}
31+
32+
function isEmptyAddress(address addr) internal pure returns(bool){
33+
return addr == address(0);
34+
}
35+
36+
37+
function addressToBytes(address addr) internal pure returns (bytes memory){
38+
bytes20 addrBytes = bytes20(uint160(addr));
39+
bytes memory rtn = new bytes(20);
40+
for(uint8 i=0;i<20;i++){
41+
rtn[i] = addrBytes[i];
42+
}
43+
return rtn;
44+
}
45+
46+
47+
function bytesToAddress(bytes memory addrBytes) internal pure returns (address){
48+
require(addrBytes.length == 20);
49+
//Convert binary to uint160
50+
uint160 intVal = 0;
51+
52+
for(uint8 i=0;i<20;i++){
53+
intVal <<= 8;
54+
intVal += uint8(addrBytes[i]);
55+
}
56+
return address(intVal);
57+
}
58+
59+
60+
function addressToString(address addr) internal pure returns(string memory){
61+
//Convert addr to bytes
62+
bytes20 value = bytes20(uint160(addr));
63+
bytes memory strBytes = new bytes(42);
64+
//Encode hex prefix
65+
strBytes[0] = '0';
66+
strBytes[1] = 'x';
67+
//Encode bytes usig hex encoding
68+
for(uint i=0;i<20;i++){
69+
uint8 byteValue = uint8(value[i]);
70+
strBytes[2 + (i<<1)] = encode((byteValue >> 4) & 0x0f);
71+
strBytes[3 + (i<<1)] = encode(byteValue & 0x0f);
72+
}
73+
return string(strBytes);
74+
}
75+
76+
function stringToAddress(string memory data) internal returns(address){
77+
bytes memory strBytes = bytes(data);
78+
require(strBytes.length >= 39 && strBytes.length <= 42, "Not hex string");
79+
//Skip prefix
80+
uint start = 0;
81+
uint bytesBegin = 0;
82+
if(strBytes[1] == 'x' || strBytes[1] == 'X'){
83+
start = 2;
84+
}
85+
//Special case: 0xabc. should be 0x0abc
86+
uint160 addrValue = 0;
87+
uint effectPayloadLen = strBytes.length - start;
88+
if(effectPayloadLen == 39){
89+
addrValue += decode(strBytes[start++]);
90+
bytesBegin++;
91+
}
92+
//Main loop
93+
for(uint i=bytesBegin;i < 20; i++){
94+
addrValue <<= 8;
95+
uint8 tmp1 = decode(strBytes[start]);
96+
uint8 tmp2 = decode(strBytes[start+1]);
97+
uint8 combined = (tmp1 << 4) + tmp2;
98+
addrValue += combined;
99+
start+=2;
100+
}
101+
102+
return address(addrValue);
103+
}
104+
105+
106+
//-----------HELPER METHOD--------------//
107+
108+
//num represents a number from 0-15 and returns ascii representing [0-9A-Fa-f]
109+
function encode(uint8 num) private pure returns(byte){
110+
//0-9 -> 0-9
111+
if(num >= 0 && num <= 9){
112+
return byte(num + 48);
113+
}
114+
//10-15 -> a-f
115+
return byte(num + 87);
116+
}
117+
118+
//asc represents one of the char:[0-9A-Fa-f] and returns consperronding value from 0-15
119+
function decode(byte asc) private pure returns(uint8){
120+
uint8 val = uint8(asc);
121+
//0-9
122+
if(val >= 48 && val <= 57){
123+
return val - 48;
124+
}
125+
//A-F
126+
if(val >= 65 && val <= 70){
127+
return val - 55;
128+
}
129+
//a-f
130+
return val - 87;
131+
}
132+
133+
}
Lines changed: 198 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,198 @@
1+
/*
2+
* Copyright 2014-2019 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
* */
16+
17+
pragma solidity >=0.4.24 <0.6.11;
18+
19+
import "./LibSafeMathForUint256Utils.sol";
20+
21+
library LibArrayForUint256Utils {
22+
23+
/**
24+
* @dev Searches a sortd uint256 array and returns the first element index that
25+
* match the key value, Time complexity O(log n)
26+
*
27+
* @param array is expected to be sorted in ascending order
28+
* @param key is element
29+
*
30+
* @return if matches key in the array return true,else return false
31+
* @return the first element index that match the key value,if not exist,return 0
32+
*/
33+
function binarySearch(uint256[] storage array, uint256 key) internal view returns (bool, uint) {
34+
if(array.length == 0){
35+
return (false, 0);
36+
}
37+
38+
uint256 low = 0;
39+
uint256 high = array.length-1;
40+
41+
while(low <= high){
42+
uint256 mid = LibSafeMathForUint256Utils.average(low, high);
43+
if(array[mid] == key){
44+
return (true, mid);
45+
}else if (array[mid] > key) {
46+
high = mid - 1;
47+
} else {
48+
low = mid + 1;
49+
}
50+
}
51+
52+
return (false, 0);
53+
}
54+
55+
function firstIndexOf(uint256[] storage array, uint256 key) internal view returns (bool, uint256) {
56+
57+
if(array.length == 0){
58+
return (false, 0);
59+
}
60+
61+
for(uint256 i = 0; i < array.length; i++){
62+
if(array[i] == key){
63+
return (true, i);
64+
}
65+
}
66+
return (false, 0);
67+
}
68+
69+
function reverse(uint256[] storage array) internal {
70+
uint256 temp;
71+
for (uint i = 0; i < array.length / 2; i++) {
72+
temp = array[i];
73+
array[i] = array[array.length - 1 - i];
74+
array[array.length - 1 - i] = temp;
75+
}
76+
}
77+
78+
function equals(uint256[] storage a, uint256[] storage b) internal view returns (bool){
79+
if(a.length != b.length){
80+
return false;
81+
}
82+
for(uint256 i = 0; i < a.length; i++){
83+
if(a[i] != b[i]){
84+
return false;
85+
}
86+
}
87+
return true;
88+
}
89+
90+
function removeByIndex(uint256[] storage array, uint index) internal{
91+
require(index < array.length, "ArrayForUint256: index out of bounds");
92+
93+
while (index < array.length - 1) {
94+
array[index] = array[index + 1];
95+
index++;
96+
}
97+
array.pop();
98+
}
99+
100+
function removeByValue(uint256[] storage array, uint256 value) internal{
101+
uint index;
102+
bool isIn;
103+
(isIn, index) = firstIndexOf(array, value);
104+
if(isIn){
105+
removeByIndex(array, index);
106+
}
107+
}
108+
109+
function addValue(uint256[] storage array, uint256 value) internal{
110+
uint index;
111+
bool isIn;
112+
(isIn, index) = firstIndexOf(array, value);
113+
if(!isIn){
114+
array.push(value);
115+
}
116+
}
117+
118+
function extend(uint256[] storage a, uint256[] storage b) internal {
119+
if(b.length != 0){
120+
for(uint i = 0; i < b.length; i++){
121+
a.push(b[i]);
122+
}
123+
}
124+
}
125+
126+
function distinct(uint256[] storage array) internal returns (uint256 length) {
127+
bool contains;
128+
uint index;
129+
for (uint i = 0; i < array.length; i++) {
130+
bool contains = false;
131+
uint256 index = 0;
132+
for(uint j = i+1;j < array.length; j++){
133+
if(array[j] == array[i]){
134+
contains =true;
135+
index = i;
136+
break;
137+
}
138+
}
139+
if (contains) {
140+
for (uint j = index; j < array.length - 1; j++){
141+
array[j] = array[j + 1];
142+
}
143+
array.pop();
144+
i--;
145+
}
146+
}
147+
length = array.length;
148+
}
149+
150+
function qsort(uint256[] storage array) internal {
151+
qsort(array, 0, array.length-1);
152+
}
153+
154+
function qsort(uint256[] storage array, uint256 begin, uint256 end) private {
155+
if(begin >= end || end == uint256(-1)) return;
156+
uint256 pivot = array[end];
157+
158+
uint256 store = begin;
159+
uint256 i = begin;
160+
for(;i<end;i++){
161+
if(array[i] < pivot){
162+
uint256 tmp = array[i];
163+
array[i] = array[store];
164+
array[store] = tmp;
165+
store++;
166+
}
167+
}
168+
169+
array[end] = array[store];
170+
array[store] = pivot;
171+
172+
qsort(array, begin, store-1);
173+
qsort(array, store+1, end);
174+
}
175+
176+
function max(uint256[] storage array) internal view returns (uint256 maxValue, uint256 maxIndex) {
177+
maxValue = array[0];
178+
maxIndex = 0;
179+
for(uint256 i = 0;i < array.length;i++){
180+
if(array[i] > maxValue){
181+
maxValue = array[i];
182+
maxIndex = i;
183+
}
184+
}
185+
}
186+
187+
function min(uint256[] storage array) internal view returns (uint256 minValue, uint256 minIndex) {
188+
minValue = array[0];
189+
minIndex = 0;
190+
for(uint256 i = 0;i < array.length;i++){
191+
if(array[i] < minValue){
192+
minValue = array[i];
193+
minIndex = i;
194+
}
195+
}
196+
}
197+
198+
}

0 commit comments

Comments
 (0)