Skip to content

Commit

Permalink
Merge pull request #61 from WeBankBlockchain/dev
Browse files Browse the repository at this point in the history
智能合约代码贡献第二期
  • Loading branch information
dalaocu authored Aug 30, 2021
2 parents 1420c93 + a8a05a1 commit 50946aa
Show file tree
Hide file tree
Showing 153 changed files with 6,398 additions and 88 deletions.
Empty file modified .gitignore
100644 → 100755
Empty file.
Empty file modified LICENSE
100644 → 100755
Empty file.
2 changes: 1 addition & 1 deletion README.md
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

| 依赖软件 | 说明 |备注|
| --- | --- | --- |
| Solidity | 0.4.25 | |
| Solidity | 0.4.25-0.6.10 | |
| Git | 下载需要使用Git | |

## 文档
Expand Down
Empty file modified contracts/base_type/BasicDemo.sol
100644 → 100755
Empty file.
Empty file modified contracts/base_type/LibAddress.sol
100644 → 100755
Empty file.
Empty file modified contracts/base_type/LibConverter.sol
100644 → 100755
Empty file.
94 changes: 94 additions & 0 deletions contracts/base_type/LibSafeMathForFloatUtils.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
pragma solidity^0.4.25;
import "./LibSafeMathForUint256Utils.sol";

library LibSafeMathForFloatUtils {
using LibSafeMathForUint256Utils for uint256;

/*
fmul:浮点数乘法
a:被乘数
dA:被乘数a的精度,若a = 1234,dA=2,实际表示浮点型数为12.34
b:乘数
dB:乘数的精度
返回值:乘法后的结果,精度值(以被乘数精度为准)
100.01 * 100.01 = 10000.0001 => 10000.00
*/
function fmul(uint256 a, uint8 dA, uint256 b, uint8 dB) internal pure returns (uint256 c, uint8 decimals) {
decimals = dA;
c = a.mul(b).div(10 ** uint256(dB));
}

/*
fdiv:浮点数除法
a:被除数
dA:被除数a的精度,若a = 1234,decimalsA=2,实际表示浮点型数为12.34
b:除数
dB:除数的精度
返回值:除法后的结果,精度值(以被除数精度为准)
10000.00 / 100.00 = 100.00
*/
function fdiv(uint256 a, uint8 dA, uint256 b, uint8 dB) internal pure returns (uint256 c, uint8 decimals) {
decimals = dA;
if(dA == dB) {
c = a.mul(10 ** uint256(dA)).div(b);
}
else if(dA > dB) {
//第一个参数精度更大
b = b.mul(10 **uint256(dA - dB) );
c = a.mul(10 ** uint256(dA)).div(b);
} else {
//第2个参数精度更大
b = b.div(10 ** uint256(dB - dA) );
c = a.mul(10 ** uint256(dA)).div(b);
}
}

/*
fadd:浮点数加法
a:加数a
dA:加数a的精度,若a = 1234,decimalsA=2,实际表示浮点型数为12.34
b:加数b
dB:加数b的精度
返回值:加法后的结果,精度值(以第1个参数精度为准)
*/
function fadd(uint256 a, uint8 dA, uint256 b, uint8 dB) internal pure returns (uint256 c, uint8 decimals) {
decimals = dA;
if(dA == dB) {
c = a.add(b);
}
else if(dA > dB) {
//第一个参数精度更大
b = b.mul(10 **uint256(dA - dB) );
c = a.add(b);
} else {
//第2个参数精度更大
b = b.div(10 ** uint256(dB - dA) );
c = a.add(b);
}
}

/*
fsub:浮点数减法
a:被减数
dA:被减数a的精度,若a = 1234,decimalsA=2,实际表示浮点型数为12.34
b:减数
dB:减数b的精度
返回值:减法后的结果,精度值(以第1个参数精度为准)
*/
function fsub(uint256 a, uint8 dA, uint256 b, uint8 dB) internal pure returns (uint256 c, uint8 decimals) {
decimals = dA;
if(dA == dB) {
c = a.sub(b);
} else if (dA > dB) {
c = a.sub(b.mul(10 ** uint256(dA - dB)));
} else {
c = a.sub(b.div(10 ** uint256(dB - dA)));
}

}


}



Empty file modified contracts/base_type/LibSafeMathForUint256Utils.sol
100644 → 100755
Empty file.
File renamed without changes.
115 changes: 115 additions & 0 deletions contracts/base_type/array/Lib2DArrayForUint256.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
pragma solidity ^0.4.25;
/**
* @author wpzczbyqy <[email protected]>
* @title uint256类型 二维数组操作
* 提供二维数组的操作,增加新元素,删除元素,修改值,查找值,合并扩展数组等
**/

library Lib2DArrayForUint256 {
/**
*@dev 二维数组中增加一个数组元素
*@param array uint256类型二维数组
*@param value uint256类型一维数组
**/
function addValue(uint256[][] storage array,uint256[] value) internal{
require(value.length > 0, "Empty Array: can not add empty array");
array.push(value);
}

/**
*@dev 查找二维数组中指定位置的值
*@param array uint256类型二维数组
*@param row 值所在的行
*@param col 值所在的列
*@return uint256 返回查找的值
**/
function getValue(uint256[][] storage array, uint256 row, uint256 col) internal returns (uint256) {
if(array.length == 0){
return 0;
}
require(row < array.length,"Row: index out of bounds");
require(col < array[row].length, "Col: index out of bounds");
return array[row][col];
}

/**
*@dev 修改二维数组中指定位置的值
*@param array uint256类型二维数组
*@param row 值所在的行
*@param col 值所在的列
*@param val 修改指定位置的值为val
*@return uint256[][] 返回修改后的数组
**/
function setValue(uint256[][] storage array, uint256 row, uint256 col, uint256 val) internal returns (uint256[][]) {
if(array.length == 0){
return;
}

require(row < array.length,"Row: index out of bounds");
require(col < array[row].length, "Col: index out of bounds");
array[row][col] = val;
return array;
}

/**
*@dev 修改二维数组中指定位置的值
*@param array uint256类型二维数组
*@param row 值所在的行
*@param col 值所在的列
*@param val 修改指定位置的值为val
*@return uint256[][] 返回修改后的数组
**/
function firstIndexOf(uint256[][] storage array, uint256 val) internal returns (bool, uint256, uint256) {
uint256 row;
uint256 col;
if (array.length == 0) {
return (false, 0, 0);
}
for(uint256 i = 0; i < array.length; i++) {
for(uint256 j = 0; j < array[i].length; j++) {
if(array[i][j] == val){
row = i;
col = j;
return (true, row, col);
}
}
}
return (false, 0, 0);
}

/**
*@dev 删除二维数组中的某个数组元素
*@param array uint256类型二维数组
*@param index 删除第index个数组元素
*@return uint256[][] 返回修改后的数组
**/
function removeByIndex(uint256[][] storage array, uint256 index) internal returns(uint256[][]) {
require(index < array.length, "Index: index out of bounds");
delete array[index];

while(index < array.length -1) {
delete array[index];
for(uint256 i = 0; i < array[index + 1].length; i++){
array[index].push(array[index + 1][i]);
}
index++;
}
array.length--;
return array;
}

/**
*@dev 合并两个二维数组
*@param array1 uint256类型二维数组
*@param array2 uint256类型二维数组
*@return uint256[][] 返回合并后的数组
**/
function extend(uint256[][] storage array1, uint256[][] storage array2) internal returns(uint256[][]){
require(array2.length > 0, "Extend: can not extend empty array");

for(uint256 i = 0; i < array2.length; i++){
array1.push(array2[i]);
}
return array1;
}
}
File renamed without changes.
57 changes: 57 additions & 0 deletions contracts/base_type/array/TwoDArrayDemo.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
pragma solidity ^0.4.25;
pragma experimental ABIEncoderV2;

import "./Lib2DArrayForUint256.sol";

contract TwoDArrayDemo {

uint256[][] private array;
uint256[][] private array2;
uint256[] val = [1,2];
uint256[] val2 = [3, 4, 5];
uint256[] val3 = [6, 7, 8, 9];

function addAndGetValue(uint256 row, uint256 col) public view returns (uint256) {
array = new uint256[][](0);
Lib2DArrayForUint256.addValue(array,val);
Lib2DArrayForUint256.addValue(array,val2);
// (1,1) => expected Return values:(4)
return Lib2DArrayForUint256.getValue(array,row,col);
}

function firstIndexOf(uint256 key) public view returns (bool, uint256, uint256) {
array = new uint256[][](0);
Lib2DArrayForUint256.addValue(array,val);
// (2) => expected Return values:(true, 0, 0)
return Lib2DArrayForUint256.firstIndexOf(array,key);
}


function setValue(uint256 row, uint256 col, uint256 key) public view returns (uint256[][]) {
array = new uint256[][](0);
Lib2DArrayForUint256.addValue(array,val);
Lib2DArrayForUint256.addValue(array,val2);
// (1, 0, 9) => expected Return values:([[1, 2] [9, 4, 5] ] )
return Lib2DArrayForUint256.setValue(array,row,col,key);
}

function removeByIndex(uint256 index) public view returns (uint256[][]) {
array = new uint256[][](0);
Lib2DArrayForUint256.addValue(array,val);
Lib2DArrayForUint256.addValue(array,val2);
Lib2DArrayForUint256.addValue(array,val3);
// 1 => expected Return values:([[1, 2] [6, 7, 8, 9] ] )
return Lib2DArrayForUint256.removeByIndex(array,index);

}

function extend() public view returns (uint256[][]) {
array = new uint256[][](0);
array2 = new uint256[][](0);
Lib2DArrayForUint256.addValue(array,val);
Lib2DArrayForUint256.addValue(array,val2);
Lib2DArrayForUint256.addValue(array2,val3);
// expected Return values:([[1, 2] [3, 4, 5] [6, 7, 8, 9] ] )
return Lib2DArrayForUint256.extend(array,array2);
}
}
107 changes: 107 additions & 0 deletions contracts/base_type/bits/LibBitOperationForByte.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
pragma solidity ^0.4.25;

/**
* @author wpzczbyqy <[email protected]>
* @description byte类型位操作
* 提供solidity内置函数不包括的位操作方法,例如按位非、移位、取前/后n位等方法
**/

library LibBitOperationForByte {

/**
*按位非
*@param a byte类型参数
*@return byte
**/
function invert(byte a) internal pure returns (byte) {
return a ^ 0xff;
}

/**
*向左移动n位
*@param a byte类型参数
*@param n 向左移动的位数
*@return byte
* 例如:0xa1向左移动2位,为0x84
**/
function leftShift(byte a, uint n) internal pure returns (byte) {
uint shifted = uint(a) * 2 ** n;
return byte(shifted);
}

/**
*向右移动n位
*@param a byte类型参数
*@param n 向右移动的位数
*@return byte
* 例如:0xa1向右移动2位,为0x28
**/
function rightShift(byte a, uint n) internal pure returns (byte) {
uint shifted = uint(a) / 2 ** n;
return byte(shifted);
}

/**
*获取前n位
*@param a byte类型参数
*@param n 获取的位数长度
*@return byte
* 例如:0xa1获取前2位,为0x80
**/
function getFirstN(byte a, uint8 n) internal pure isValidLength(n) returns (byte) {
byte nbits = byte(2 ** n - 1);
byte mask = leftShift(nbits, 8 - n);//constrcut the mask,e.g when n == 4, mask is 1111 0000
return a & mask;
}

/**
*获取后n位
*@param a byte类型参数
*@param n 获取的位数长度
*@return byte
* 例如:0xa1获取后2位,为0x01
**/
function getLastN(byte a, uint8 n) internal pure isValidLength(n) returns (byte) {
if(n == 8) {
return a;
}
uint8 lastN = uint8(a) % (2 ** n);
return byte(lastN);
}

/**
*获取第n个位置上的bit
*@param a byte类型参数
*@param n 第n位
*@return uint8,第n位的值0/1
**/
function getBitAtPositionN(byte a, uint8 n) internal pure isValidPosition(n) returns (uint8) {
bool val = a & leftShift(0x01, n) != 0;
if(val == true){
return 1;
}
return 0;
}

/**
*将第n个位置上的bit取反
*@param a byte类型参数
*@param n 第n位
*@return byte
**/
function invertBitAtPositionN(byte a, uint8 n) internal pure isValidPosition(n) returns (byte) {
return a ^ leftShift(0x01, n);
}

/*校验长度*/
modifier isValidLength(uint8 n) {
require(n < 9, "Invalid Length: byte is 8 bits");
_;
}

/*校验位置*/
modifier isValidPosition(uint8 n) {
require(n < 8, "Invalid Position: n start with 0, n < 8");
_;
}
}
Loading

0 comments on commit 50946aa

Please sign in to comment.