Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add files via upload #52

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
107 changes: 107 additions & 0 deletions 2021-Shenzhen-FinTechathon3/Contracts/solidity/Blacklist.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
// SPDX-License-Identifier: SimPL-2.0
pragma solidity>=0.4.24 <0.6.11;
pragma experimental ABIEncoderV2;

import "./Table.sol";
import "./DataFormat.sol";

contract Blacklist is DataFormat{
event insertResult(int256 count);
event updateResult(int256 count);
event removeResult(int256 count);


TableFactory tableFactory;

string constant BLACK_LIST_TABLE_NAME = "blacklist";

string[] allUserId;

constructor() public {
// Set tableFactory address
tableFactory = TableFactory(0x1001);

//
tableFactory.createTable(BLACK_LIST_TABLE_NAME, "userId", "userState");
}


// 初始化用户账户状态表; 用户状态默认值 1 : 正常账户
// 参数数据格式(userId) 返回值(插入数据条数)
function initUserState(string memory userId)
public
returns(int256)
{
Table table = tableFactory.openTable(BLACK_LIST_TABLE_NAME);
Entry entry = table.newEntry();

// Set Value
entry.set("userId", userId);
entry.set("userState", "1");

int256 count = table.insert(userId, entry);
emit insertResult(count);

return count;
}

// 更新用户账户状态, 正常 -> 拉黑
// 参数数据格式(userId) 返回值(更新数据条数)
function changeUserState(string memory userId)
public
returns(int256)
{
Table table = tableFactory.openTable(BLACK_LIST_TABLE_NAME);
Entry entry = table.newEntry();

// 记录黑名单账户
allUserId.push(userId);

//已完成状态 0
entry.set("userState", "0");

Condition condition = table.newCondition();
condition.EQ("userId", userId);

int256 count = table.update(userId, entry, condition);
emit updateResult(count);
return count;
}


// 获得用户账户状态
// 参数数据格式(userId) 返回值("1" -> 正常;"0" -> 拉黑)
function getUserState(string memory userId)
public
view
returns(string memory)
{
Table table = tableFactory.openTable(BLACK_LIST_TABLE_NAME);
string memory userState;

Condition condition = table.newCondition();
condition.EQ("userId", userId);

Entries entries = table.select(userId, condition);
if(0 == entries.size())
{
return("该ID不存在");
}

Entry entry = entries.get(0);

userState= entry.getString("userState");

return(userState);
}

// 获取所有黑名单账户
function getAllBlacklistUser()
public
view
returns(string[] memory, uint256)
{
uint256 blacklistNum = allUserId.length;
return(allUserId, blacklistNum);
}
}
53 changes: 53 additions & 0 deletions 2021-Shenzhen-FinTechathon3/Contracts/solidity/DataFormat.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
// SPDX-License-Identifier: SimPL-2.0
pragma solidity>=0.4.24 <0.6.11;

contract DataFormat{
// string matching
function strMatching(string memory v1, string memory v2)
internal
pure
returns(bool)
{
return keccak256(bytes(v1)) == keccak256(bytes(v2));
}

// string catenate
function strCat(string memory v1, string memory v2)
internal
pure
returns (string memory){
bytes memory v1Bytes = bytes(v1);
bytes memory v2Bytes = bytes(v2);

string memory result = new string(v1Bytes.length + v2Bytes.length);
bytes memory resultBytes = bytes(result);

uint k = 0;
uint i = 0;
for (i = 0; i < v1Bytes.length; i++){
resultBytes[k++] = v1Bytes[i];
}
for (i = 0; i < v2Bytes.length; i++) {
resultBytes[k++] = v2Bytes[i];
}

return string(resultBytes);

}


function strCatWithSymbol(string memory v1, string memory v2)
internal
pure
returns (string memory)
{
if(true == strMatching("", v1)){
v1 = v2;
}
else{
v1 = strCat(v1, ",");
v1 = strCat(v1, v2);
}
return v1;
}
}
117 changes: 117 additions & 0 deletions 2021-Shenzhen-FinTechathon3/Contracts/solidity/FundingUser.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
// SPDX-License-Identifier: SimPL-2.0
pragma solidity>=0.4.24 <0.6.11;
pragma experimental ABIEncoderV2;

import "./Table.sol";
import "./DataFormat.sol";

contract FundingUser is DataFormat{
event insertResult(int256 count);
event updateResult(int256 count);
event removeResult(int256 count);

TableFactory tableFactory;

string constant FUNDING_USER_TABLE_NAME = "fundingUser";

constructor() public {
// Set tableFactory address
tableFactory = TableFactory(0x1001);

//资助用户(allFundingNumStr, 对应allFundingUser的金额)
tableFactory.createTable(FUNDING_USER_TABLE_NAME, "userId", "allFundingUser, allFundingNumStr, fundingTime, score");
}

// 初始化用户资助表
// 参数数据格式(userId) 返回值(插入数据条数)
function initFundingInfo(string memory userId)
public
returns(int256)
{
Table table = tableFactory.openTable(FUNDING_USER_TABLE_NAME);
Entry entry = table.newEntry();

// Set Value
entry.set("userId", userId);
entry.set("allFundingUser", "");
entry.set("allFundingNumStr", "");
entry.set("fundingTime", "");
entry.set("score", int256(0));

int256 count = table.insert(userId, entry);
emit insertResult(count);

return count;
}

// 用户进行资助后,更新资助信息
// 参数数据格式(userId,资助金额int,资助对象Id,资助金额string,资助时间) 返回值(更新数据条数)
// 测试数据 1."2" "200" "伍羽放" "200" "2020-10-12" 2."2" "500" "赵双" "500" "2021-10-12"
function updateFundingInfo(string memory userId, int256 fundsNumInt, string memory fundingId, string memory fundsNumStr, string memory fundingTime)
public
returns(int256)
{
Table table = tableFactory.openTable(FUNDING_USER_TABLE_NAME);

Condition condition = table.newCondition();
condition.EQ("userId", userId);

// Get current fundsSum
Entry entry = table.select(userId, condition).get(0);

//资助者对象Id拼接
string memory allFundingUser = entry.getString("allFundingUser");
allFundingUser = strCatWithSymbol(allFundingUser, fundingId);

// 每名资助者的金额
// 资助金额拼接
string memory allFundingNumStr = entry.getString("allFundingNumStr");
allFundingNumStr = strCatWithSymbol(allFundingNumStr, fundsNumStr);

//资助者时间拼接
string memory allFundingTime = entry.getString("fundingTime");
allFundingTime = strCatWithSymbol(allFundingTime, fundingTime);

int256 score = entry.getInt("score");
score = score + (fundsNumInt * 10);

entry.set("allFundingUser", allFundingUser);
entry.set("allFundingNumStr", allFundingNumStr);
entry.set("fundingTime", allFundingTime);
entry.set("score", score);

int256 count = table.update(userId, entry, condition);
emit updateResult(count);

return count;
}

// 获得资助者当前资助数据
// 参数数据格式(userId) 返回值(所有资助对象Id,所有资助对象金额,对应资助时间,总积分)
function getUserFundingInfo(string memory userId)
public
view
returns(string memory, string memory, string memory, int256)
{
Table table = tableFactory.openTable(FUNDING_USER_TABLE_NAME);

Condition condition = table.newCondition();
condition.EQ("userId", userId);

Entries entries = table.select(userId, condition);
if(0 == entries.size())
{
return("","","",-1);
}

Entry entry = entries.get(0);
//allFundingUser, allFundingNumStr, fundingTime, score
string memory allFundingUser = entry.getString("allFundingUser");
string memory allFundingNumStr = entry.getString("allFundingNumStr");
string memory fundingTime = entry.getString("fundingTime");
int256 score = entry.getInt("score");

return(allFundingUser, allFundingNumStr, fundingTime, score);
}

}
Loading