From 84fba741266e1d788af49824c69d25c8009b1b8c Mon Sep 17 00:00:00 2001 From: llp Date: Mon, 13 Nov 2017 19:38:51 +0800 Subject: [PATCH] =?UTF-8?q?=E5=88=A0=E9=99=A4smartcontract=E7=9B=AE?= =?UTF-8?q?=E5=BD=95=EF=BC=8C=E5=A2=9E=E5=8A=A0.gitingore=E6=96=87?= =?UTF-8?q?=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 4 + smartcontract/.gitignore | 9 - smartcontract/contracts/Admin.sol | 136 --- smartcontract/contracts/ContractAddress.sol | 55 -- smartcontract/contracts/CreateID.sol | 31 - smartcontract/contracts/Login.sol | 44 - smartcontract/contracts/Market.sol | 330 ------- smartcontract/contracts/Migrations.sol | 23 - smartcontract/contracts/Proxy.sol | 151 --- smartcontract/contracts/User.sol | 682 ------------- smartcontract/contracts/UserList.sol | 63 -- smartcontract/contracts/lib/LibAddressMap.sol | 108 --- smartcontract/contracts/lib/LibArray.sol | 78 -- smartcontract/contracts/lib/LibFunds.sol | 60 -- smartcontract/contracts/lib/LibMarketMap.sol | 119 --- smartcontract/contracts/lib/LibSheetMap.sol | 148 --- smartcontract/contracts/lib/LibString.sol | 901 ------------------ smartcontract/contracts/lib/LibTradeMap.sol | 143 --- .../contracts/lib/LibUserAddrMap.sol | 114 --- .../contracts/lib/StructAddressInfo.sol | 7 - smartcontract/contracts/lib/StructFunds.sol | 12 - smartcontract/contracts/lib/StructMarket.sol | 23 - smartcontract/contracts/lib/StructSheet.sol | 16 - smartcontract/contracts/lib/StructTrade.sol | 20 - .../contracts/lib/StructUserAddr.sol | 10 - smartcontract/init.js | 105 -- .../migrations/1_initial_migration.js | 5 - .../migrations/2_deploy_contracts.js | 75 -- smartcontract/test/TestAdmin.sol | 176 ---- smartcontract/test/TestContractAddress.sol | 83 -- smartcontract/test/TestLogin.sol | 48 - smartcontract/test/TestMarket.sol | 165 ---- smartcontract/test/TestUser.sol | 402 -------- smartcontract/test/TestUserList.sol | 111 --- smartcontract/truffle.js | 12 - 35 files changed, 4 insertions(+), 4465 deletions(-) create mode 100644 .gitignore delete mode 100644 smartcontract/.gitignore delete mode 100644 smartcontract/contracts/Admin.sol delete mode 100644 smartcontract/contracts/ContractAddress.sol delete mode 100644 smartcontract/contracts/CreateID.sol delete mode 100644 smartcontract/contracts/Login.sol delete mode 100644 smartcontract/contracts/Market.sol delete mode 100644 smartcontract/contracts/Migrations.sol delete mode 100644 smartcontract/contracts/Proxy.sol delete mode 100644 smartcontract/contracts/User.sol delete mode 100644 smartcontract/contracts/UserList.sol delete mode 100644 smartcontract/contracts/lib/LibAddressMap.sol delete mode 100644 smartcontract/contracts/lib/LibArray.sol delete mode 100644 smartcontract/contracts/lib/LibFunds.sol delete mode 100644 smartcontract/contracts/lib/LibMarketMap.sol delete mode 100644 smartcontract/contracts/lib/LibSheetMap.sol delete mode 100644 smartcontract/contracts/lib/LibString.sol delete mode 100644 smartcontract/contracts/lib/LibTradeMap.sol delete mode 100644 smartcontract/contracts/lib/LibUserAddrMap.sol delete mode 100644 smartcontract/contracts/lib/StructAddressInfo.sol delete mode 100644 smartcontract/contracts/lib/StructFunds.sol delete mode 100644 smartcontract/contracts/lib/StructMarket.sol delete mode 100644 smartcontract/contracts/lib/StructSheet.sol delete mode 100644 smartcontract/contracts/lib/StructTrade.sol delete mode 100644 smartcontract/contracts/lib/StructUserAddr.sol delete mode 100644 smartcontract/init.js delete mode 100755 smartcontract/migrations/1_initial_migration.js delete mode 100755 smartcontract/migrations/2_deploy_contracts.js delete mode 100644 smartcontract/test/TestAdmin.sol delete mode 100644 smartcontract/test/TestContractAddress.sol delete mode 100644 smartcontract/test/TestLogin.sol delete mode 100644 smartcontract/test/TestMarket.sol delete mode 100644 smartcontract/test/TestUser.sol delete mode 100644 smartcontract/test/TestUserList.sol delete mode 100755 smartcontract/truffle.js diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..a05ee7c --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ +build +node_modules +yarn-error.log +smartcontract diff --git a/smartcontract/.gitignore b/smartcontract/.gitignore deleted file mode 100644 index cc19125..0000000 --- a/smartcontract/.gitignore +++ /dev/null @@ -1,9 +0,0 @@ -/build -/truffle.js -*.swp -*.swo -*~ -!.gitignore -!*.sol -!/migrations/*.js - diff --git a/smartcontract/contracts/Admin.sol b/smartcontract/contracts/Admin.sol deleted file mode 100644 index d851d37..0000000 --- a/smartcontract/contracts/Admin.sol +++ /dev/null @@ -1,136 +0,0 @@ -pragma solidity ^0.4.11; - -import "./lib/LibString.sol"; -import "./ContractAddress.sol"; -import "./UserList.sol"; -import "./User.sol"; - -contract Admin -{ - struct ConfirmListReq - { - bytes32 user_id_; - bytes32 user_sell_id_; - uint trade_id_; - bool status_; - } - struct ConfirmNegReq - { - bytes32 user_id_; - bytes32 user_sell_id_; - uint trade_id_; - bool status_; - } - - ContractAddress contract_address; - UserList user_list; - ConfirmListReq[] confirm_list_req; - ConfirmNegReq[] confirm_neg_req; - User user; - User user_sell; - - function init(address addr, string user_list_name) - { - contract_address = ContractAddress(addr); - user_list = UserList(contract_address.getContractAddress(user_list_name)); - } - - function insertConfirmListReq(bytes32 user_id, bytes32 user_sell_id,uint trade_id) - { - confirm_list_req.push( ConfirmListReq(user_id,user_sell_id,trade_id,false)); - } - - function insertConfirmNegReq(bytes32 user_id, bytes32 user_sell_id, uint trade_id) - { - confirm_neg_req.push( ConfirmNegReq(user_id,user_sell_id,trade_id,false)); - } - - //确认挂牌交易 - function confirmList(uint index) - { - user = User(user_list.getUserAgentAddr(confirm_list_req[index].user_id_)); - user_sell = User(user_list.getUserAgentAddr(confirm_list_req[index].user_sell_id_)); - user.confirmList(confirm_list_req[index].trade_id_); - user_sell.confirmList(confirm_list_req[index].trade_id_); - confirm_list_req[index].status_ = true; - } - - //确认协商交易 - function confirmNeg(uint index) - { - user = User(user_list.getUserAgentAddr(confirm_neg_req[index].user_id_)); - user_sell = User(user_list.getUserAgentAddr(confirm_neg_req[index].user_sell_id_)); - user.confirmNeg(confirm_neg_req[index].trade_id_); - user_sell.confirmNeg(confirm_neg_req[index].trade_id_); - confirm_neg_req[index].status_ = false; - } - - //添加用户 - /* - function addUser(bytes32 user_id, address external_addr) - { - user = new User(); - user.setContractAddress(contract_address); - user.setMarketName("Market"); - user.setCreateIDName("CreateID"); - user.setUserListName("UserList"); - user.setUserID(user_id); - user.setAdmin("Admin"); - user_list.addUser(external_addr,user,user_id,1); - } - */ - function addUser(address external_addr, bytes32 user_id, bytes32 class_id, bytes32 make_date, - bytes32 lev_id, bytes32 wh_id, bytes32 place_id, uint all_amount, - uint frozen_amount, uint available_amount, uint funds) - {//TODO 重复用户判断 - user = new User(); - user.setContractAddress(contract_address); - user.setMarketName("Market"); - user.setCreateIDName("CreateID"); - user.setUserListName("UserList"); - user.setUserID(user_id); - user.setAdmin("Admin"); - user_list.addUser(external_addr,user,user_id,1); - user.insertSheet(user_id, class_id, make_date, lev_id, wh_id, place_id, all_amount, frozen_amount, available_amount); - user.insertFunds(funds); - } - - //删除用户 - function delUser(bytes32 user_id) - { - user_list.delUserInfo(user_id); - } - //获取挂牌交易确认请求列表的长度 - function getConfirmListReqSize() returns(uint) - { - return confirm_list_req.length; - } - - //获取协商交易确认请求列表的长度 - function getConfirmNegReqSize() returns(uint) - { - return confirm_neg_req.length; - } - - //获取挂牌交易确认请求列表的元素 - /* - function getConfirmListReq(uint index) returns(string user_id,string user_sell_id,uint trade_id,bool status) - { - user_id = LibString.bytes32ToString(confirm_list_req[index].user_id_); - user_sell_id = LibString.bytes32ToString(confirm_list_req[index].user_sell_id_); - trade_id = confirm_list_req[index].trade_id_; - status = confirm_list_req[index].status_; - } - */ - - //获取协商交易确认请求列表的元素 - /* - function getConfirmNegReq(uint index) returns(string user_id,string user_sell_id,uint trade_id,bool status) - { - user_id = LibString.bytes32ToString(confirm_neg_req[index].user_id_); - user_sell_id = LibString.bytes32ToString(confirm_neg_req[index].user_sell_id_); - trade_id = confirm_neg_req[index].trade_id_; - status = confirm_neg_req[index].status_; - } - */ -} diff --git a/smartcontract/contracts/ContractAddress.sol b/smartcontract/contracts/ContractAddress.sol deleted file mode 100644 index 3612dad..0000000 --- a/smartcontract/contracts/ContractAddress.sol +++ /dev/null @@ -1,55 +0,0 @@ -pragma solidity ^0.4.11; -import "./lib/StructAddressInfo.sol"; -import "./lib/LibAddressMap.sol"; -contract ContractAddress -{ - using LibAddressMap for LibAddressMap.AddressMap; - LibAddressMap.AddressMap address_map; - - function setContractAddress(string contract_name, address addr) - { - address_map.insert(contract_name, StructAddressInfo.value(addr)); - } - function delContractAddress(string contract_name) - { - address_map.remove(contract_name); - } - function getContractNum() returns (uint) - { - return address_map.size(); - } - function getSize() returns (uint) - { - return getContractNum(); - } - /* - function getValueByIndex(uint index) returns (address addr) - { - if(index < getSize()) - { - addr = address_map.getValue(index); - } - return addr; - } - */ - function getAddressByIndex(uint index) returns (address addr) - { - if(index < getSize()) - { - addr = address_map.getValue(index).addr; - } - return addr; - } - function getKeyByIndex(uint index) returns (string key) //only use for *.js - { - if(index < getSize()) - { - key = address_map.getKey(index); - } - return key; - } - function getContractAddress(string contract_name) returns (address) - { - return address_map.getValue(contract_name); - } -} diff --git a/smartcontract/contracts/CreateID.sol b/smartcontract/contracts/CreateID.sol deleted file mode 100644 index 61ffc50..0000000 --- a/smartcontract/contracts/CreateID.sol +++ /dev/null @@ -1,31 +0,0 @@ -pragma solidity ^0.4.5; -contract CreateID -{ - uint sheet_id_; //仓单id - uint trade_id_; //合同id - uint neg_id_; //协商id - uint market_id_; - - //创建仓单id - function getSheetID() returns(uint) - { - return ++sheet_id_; - } - - //创建合同id - function getTradeID() returns(uint ) - { - return ++trade_id_; - } - - //创建协商交易编号 - function getNegID() returns(uint ) - { - return ++neg_id_; - } - function getMarketID() returns(uint) - { - return ++market_id_; - } -} - diff --git a/smartcontract/contracts/Login.sol b/smartcontract/contracts/Login.sol deleted file mode 100644 index b344e00..0000000 --- a/smartcontract/contracts/Login.sol +++ /dev/null @@ -1,44 +0,0 @@ -pragma solidity ^0.4.11; - -import "./ContractAddress.sol"; -import "./UserList.sol"; - -contract Login -{ - ContractAddress contract_address; - UserList user_list; - string user_list_name; - - function init(address addr, string user_list_name) - { - contract_address = ContractAddress(addr); - user_list = UserList(contract_address.getContractAddress(user_list_name)); - } - - function verfication(bytes32 user_id) returns(int ret, int user_auth, address user_addr) - { - if(!user_list.isExisted(user_id)) - { - ret = -1; - return; - } - else if(msg.sender == user_list.getExternal_addr(user_id)) - { - ret = 0; - var(un1,ret_user_addr,un2,ret_user_auth) = user_list.getUserInfo(user_id);//un1,un2代表未使用的变量 - user_auth = ret_user_auth; - user_addr = ret_user_addr; - return; - } - else - ret = -2; - } - function getUserListName() returns(string) - { - return user_list_name; - } - function getContractAddr() returns(address) - { - return contract_address; - } -} diff --git a/smartcontract/contracts/Market.sol b/smartcontract/contracts/Market.sol deleted file mode 100644 index 30f37cf..0000000 --- a/smartcontract/contracts/Market.sol +++ /dev/null @@ -1,330 +0,0 @@ -pragma solidity ^0.4.5; - -import "./User.sol"; -import "./UserList.sol"; -import "./CreateID.sol"; -import "./ContractAddress.sol"; -import "./lib/StructMarket.sol"; -import "./lib/LibMarketMap.sol"; -import "./lib/LibString.sol"; - -contract Market -{ - event getRet(uint indexed ret); - //更新行情事件 - event updateEvent(uint indexed seq, uint indexed market_id, uint indexed amount); - using LibMarketMap for LibMarketMap.MarketMap; - - LibMarketMap.MarketMap market_map; - - uint opt_seq; - StructMarket.value temp_market; //临时行情变量 - uint market_id; - - ContractAddress contract_address; - - string create_id_name; - string user_list_name; - //获取同步操作序号 - function get_opt_seq() returns(uint) - { - return opt_seq; - } - - //外部依赖 - function setContractAddress(address addr) - { - contract_address = ContractAddress(addr); - } - - function setCreateIDName(string name) - { - create_id_name = name; - } - - function setUserListName(string name) - { - user_list_name = name; - } - - //操作行情 - function getMarketID() returns (uint) - { - address empty_addr; - assert(contract_address != empty_addr); - assert(bytes(create_id_name).length != 0); - - CreateID create_id = CreateID(contract_address.getContractAddress(create_id_name)); - - assert(create_id != empty_addr); - - return create_id.getMarketID(); - } - - function getTradeID() returns (uint) - { - address empty_addr; - assert(contract_address != empty_addr); - assert(bytes(create_id_name).length != 0); - - CreateID create_id = CreateID(contract_address.getContractAddress(create_id_name)); - - assert(create_id != empty_addr); - - return create_id.getTradeID(); - } - - function insertMarket_1(uint sheet_id, - bytes32 class_id, bytes32 make_date, - bytes32 lev_id, bytes32 wh_id, bytes32 place_id) - { - market_id = getMarketID(); - temp_market.date_ = now; - temp_market.market_id_ = market_id; - temp_market.sheet_id_ = sheet_id; - temp_market.class_id_ = class_id; - temp_market.make_date_ = make_date; - temp_market.lev_id_ = lev_id; - temp_market.wh_id_ = wh_id; - temp_market.place_id_ = place_id; - temp_market.type_ = "一口价"; - } - - function insertMarket_2(uint price, uint list_qty, uint deal_qty, - uint rem_qty, bytes32 deadline, - uint dlv_unit, bytes32 user_id ) returns(uint,uint) - { - temp_market.price_ = price; - temp_market.list_qty_ = list_qty; - temp_market.deal_qty_ = deal_qty; - temp_market.rem_qty_ = rem_qty; - temp_market.deadline_ = deadline; - temp_market.dlv_unit_ = dlv_unit; - temp_market.user_id_ = user_id; - temp_market.seller_addr_= msg.sender; - market_map.insert(market_id, temp_market); - getRet(market_id); - return (market_id,temp_market.date_); - } - - //获取行情 - function getMarket_1(uint market_id) - returns ( - uint date, //挂牌日期 - uint ret_market_id, //挂牌编号 - uint sheet_id, //仓单编号 - bytes32 class_id, //品种代码 - bytes32 make_date, //产期 - bytes32 lev_id, //等级 - bytes32 wh_id, //仓库代码 - bytes32 place_id, //产地代码 - bytes32 price_type //报价类型 - ) - { - StructMarket.value memory temp_value = market_map.getValue(market_id); - date = temp_value.date_; - ret_market_id = temp_value.market_id_; - sheet_id = temp_value.sheet_id_; - class_id = temp_value.class_id_; - make_date = temp_value.make_date_; - lev_id = temp_value.lev_id_; - wh_id = temp_value.wh_id_; - place_id = temp_value.place_id_; - price_type = temp_value.type_; - } - - - function getMarket_2(uint market_id) - returns ( - uint price, //价格(代替浮点型) - uint list_qty, //挂牌量 - uint deal_qty, //成交量 - uint rem_qty, //剩余量 - bytes32 deadline, //挂牌截止日 - uint dlv_unit, //交割单位 - bytes32 user_id, //用户id - address seller_addr //卖方地址 - ) - - { - StructMarket.value memory temp_value = market_map.getValue(market_id); - price = temp_value.price_; - list_qty = temp_value.list_qty_; - deal_qty = temp_value.deal_qty_; - rem_qty = temp_value.rem_qty_; - deadline = temp_value.deadline_; - dlv_unit = temp_value.dlv_unit_; - user_id = temp_value.user_id_; - seller_addr= temp_value.seller_addr_; - } - - function getDynamicMarket(uint in_market_id) returns (bytes32 sell_user_id, uint deal_qty, uint rem_qty) //行情中,只有成交量和剩余量会发生变化 - { - StructMarket.value memory temp_value = market_map.getValue(in_market_id); - deal_qty = temp_value.deal_qty_; - rem_qty = temp_value.rem_qty_; - sell_user_id = temp_value.user_id_; - } - - function getMarketNum() returns(uint) - { - return market_map.size(); - } - - function getMarketIdByIndex(uint index) returns(uint) - { - return market_map.getKey(index); - } - - //更新行情 - function updateMarket(bytes32 buy_user_id, uint selected_market_id, uint confirm_qty) returns(int) - { - temp_market = market_map.getValue(selected_market_id); - if(temp_market.rem_qty_ != 0 && confirm_qty != 0 && confirm_qty <= temp_market.rem_qty_) - { - //获得操作序号 - opt_seq++; - market_map.update(selected_market_id, temp_market.deal_qty_ + confirm_qty, temp_market.rem_qty_ - confirm_qty); - if(confirm_qty == temp_market.rem_qty_) //确认量等于挂牌量,删除该条行情 - { - //market_map.remove(selected_market_id); - } - } - else - { - return -1; - } - - address empty_addr; - - UserList user_list = UserList(contract_address.getContractAddress(user_list_name)); - assert(user_list != empty_addr); - - User sell_user = User(user_list.getUserAgentAddr(temp_market.user_id_)); - User buy_user = User(user_list.getUserAgentAddr(buy_user_id)); - assert(sell_user != empty_addr); - assert(buy_user != empty_addr); - if (buy_user == empty_addr) - { - return -2; - } - - //更新卖方挂牌请求 - sell_user.updateListReq(selected_market_id, confirm_qty); - - //记录成交 - uint time = now; - uint trade_id = getTradeID(); - - sell_user.recordTrade(time, trade_id, buy_user_id, "卖", confirm_qty, selected_market_id); - buy_user.recordTrade(time, trade_id, temp_market.user_id_,"买", confirm_qty, selected_market_id); - - //确认量等于挂牌量,删除该条行情 - if(confirm_qty == temp_market.rem_qty_) - { - market_map.remove(selected_market_id); - } - //记录下更新事件 - updateEvent(opt_seq, selected_market_id, confirm_qty); - return 0; - } - - - //获取行情 - function getMarketStr_1(uint index) - returns ( - uint date, //挂牌日期 - uint ret_market_id, //挂牌编号 - uint sheet_id, //仓单编号 - string class_id, //品种代码 - string make_date, //产期 - string lev_id, //等级 - string wh_id, //仓库代码 - string place_id, //产地代码 - string price_type //报价类型 - ) - { - StructMarket.value memory temp_value = market_map.getValueByIndex(index); - date = temp_value.date_; - ret_market_id = temp_value.market_id_; - sheet_id = temp_value.sheet_id_; - class_id = LibString.bytes32ToString(temp_value.class_id_); - make_date = LibString.bytes32ToString(temp_value.make_date_); - lev_id = LibString.bytes32ToString(temp_value.lev_id_); - wh_id = LibString.bytes32ToString(temp_value.wh_id_); - place_id = LibString.bytes32ToString(temp_value.place_id_); - price_type = LibString.bytes32ToString(temp_value.type_); - } - function getMarketStr_2(uint index) - returns ( - uint price, //价格(代替浮点型) - uint list_qty, //挂牌量 - uint deal_qty, //成交量 - uint rem_qty, //剩余量 - string deadline, //挂牌截止日 - uint dlv_unit, //交割单位 - string user_id, //用户id - address seller_addr //卖方地址 - ) - - { - StructMarket.value memory temp_value = market_map.getValueByIndex(index); - price = temp_value.price_; - list_qty = temp_value.list_qty_; - deal_qty = temp_value.deal_qty_; - rem_qty = temp_value.rem_qty_; - deadline = LibString.bytes32ToString(temp_value.deadline_); - dlv_unit = temp_value.dlv_unit_; - user_id = LibString.bytes32ToString(temp_value.user_id_); - seller_addr= temp_value.seller_addr_; - } - - function getMarketStrByMarketID_1(uint id) - returns ( - uint date, //挂牌日期 - uint ret_market_id, //挂牌编号 - uint sheet_id, //仓单编号 - string class_id, //品种代码 - string make_date, //产期 - string lev_id, //等级 - string wh_id, //仓库代码 - string place_id, //产地代码 - string price_type //报价类型 - ) - { - StructMarket.value memory temp_value = market_map.getValue(id); - date = temp_value.date_; - ret_market_id = temp_value.market_id_; - sheet_id = temp_value.sheet_id_; - class_id = LibString.bytes32ToString(temp_value.class_id_); - make_date = LibString.bytes32ToString(temp_value.make_date_); - lev_id = LibString.bytes32ToString(temp_value.lev_id_); - wh_id = LibString.bytes32ToString(temp_value.wh_id_); - place_id = LibString.bytes32ToString(temp_value.place_id_); - price_type = LibString.bytes32ToString(temp_value.type_); - } - - function getMarketStrByMarketID_2(uint id) - returns ( - uint price, //价格(代替浮点型) - uint list_qty, //挂牌量 - uint deal_qty, //成交量 - uint rem_qty, //剩余量 - string deadline, //挂牌截止日 - uint dlv_unit, //交割单位 - string user_id, //用户id - address seller_addr //卖方地址 - ) - - { - StructMarket.value memory temp_value = market_map.getValue(id); - price = temp_value.price_; - list_qty = temp_value.list_qty_; - deal_qty = temp_value.deal_qty_; - rem_qty = temp_value.rem_qty_; - deadline = LibString.bytes32ToString(temp_value.deadline_); - dlv_unit = temp_value.dlv_unit_; - user_id = LibString.bytes32ToString(temp_value.user_id_); - seller_addr= temp_value.seller_addr_; - } -} diff --git a/smartcontract/contracts/Migrations.sol b/smartcontract/contracts/Migrations.sol deleted file mode 100644 index 7e7fe8d..0000000 --- a/smartcontract/contracts/Migrations.sol +++ /dev/null @@ -1,23 +0,0 @@ -pragma solidity ^0.4.4; - -contract Migrations { - address public owner; - uint public last_completed_migration; - - modifier restricted() { - if (msg.sender == owner) _; - } - - function Migrations() { - owner = msg.sender; - } - - function setCompleted(uint completed) restricted { - last_completed_migration = completed; - } - - function upgrade(address new_address) restricted { - Migrations upgraded = Migrations(new_address); - upgraded.setCompleted(last_completed_migration); - } -} diff --git a/smartcontract/contracts/Proxy.sol b/smartcontract/contracts/Proxy.sol deleted file mode 100644 index 24c701a..0000000 --- a/smartcontract/contracts/Proxy.sol +++ /dev/null @@ -1,151 +0,0 @@ -pragma solidity ^0.4.5; - -import "./User.sol"; -import "./UserList.sol"; -import "./ContractAddress.sol"; -import "./lib/LibString.sol"; - -contract Proxy -{ - string user_list_name; - - UserList user_list; - ContractAddress contract_address; - - function Proxy() - { - user_list_name = "UserList"; - } - function getName() returns(string) - { - return user_list_name; - } - //设置ContractAddress - function setContractAddress(address addr) - { - contract_address = ContractAddress(addr); - } - - //初始化 - function init(address addr) - { - setContractAddress(addr); - user_list = UserList(contract_address.getContractAddress(user_list_name)); - } - - //挂牌 - function insertSheet(bytes32 user_id, bytes32 class_id, bytes32 make_date,bytes32 lev_id, bytes32 wh_id, bytes32 place_id,uint all_amount,uint frozen_amount, uint available_amount) - { - User user = User(user_list.getUserAgentAddr(user_id)); - user.insertSheet(user_id, class_id, make_date, lev_id, wh_id, place_id, all_amount, frozen_amount, available_amount); - } - - function getSheetAmount(bytes32 user_id, uint sheet_id) - returns(uint all_amount, uint available_amount, uint frozen_amount) - { - User user = User(user_list.getUserAgentAddr(user_id)); - var(all_amount_,available_amount_, frozen_amount_) = user.getSheetAmount(sheet_id); - all_amount = all_amount_; - available_amount = available_amount_; - frozen_amount = frozen_amount_; - } - - function listRequest(bytes32 user_id, bytes32 seller_user_id, uint sheet_id, uint price, uint sell_qty) - returns(uint ret) - { - User user = User(user_list.getUserAgentAddr(user_id)); - ret = user.listRequest(seller_user_id, sheet_id, price, sell_qty); - } - - function delistRequest(bytes32 user_id, bytes32 buy_user_id, uint selected_market_id, uint confirm_qty) - returns(int ret) - { - User user = User(user_list.getUserAgentAddr(user_id)); - ret = user.delistRequest(buy_user_id, selected_market_id, confirm_qty); - } - - function getListReqNum(bytes32 user_id)returns(uint ret){ - User user = User(user_list.getUserAgentAddr(user_id)); - ret = user.getListReqNum(); - } - function getListReq_1(bytes32 user_id, uint i)external returns(uint sheet_id, uint market_id, uint date, string class_id, string make_date) - { - User user = User(user_list.getUserAgentAddr(user_id)); - if( i < user.getListReqNum()) - { - var(sheet_id_, market_id_, date_, class_id_, make_date_) = user.getListReq_1(i); - sheet_id = sheet_id_; - market_id = market_id_; - date = date_; - class_id = LibString.bytes32ToString(class_id_); - make_date = LibString.bytes32ToString(make_date_); - } - } - function getListReq_2(bytes32 user_id, uint i)external returns(string lev_id, uint price, uint list_qty, uint deal_qty, uint rem_qty) - { - User user = User(user_list.getUserAgentAddr(user_id)); - if( i < user.getListReqNum()) - { - var(lev_id_, price_, list_qty_, deal_qty_, rem_qty_) = user.getListReq_2(i); - lev_id = LibString.bytes32ToString(lev_id_); - price = price_; - list_qty = list_qty_; - deal_qty = deal_qty_; - rem_qty = rem_qty_; - } - } - - function getTradeNum(bytes32 user_id)returns (uint ret) - { - User user = User(user_list.getUserAgentAddr(user_id)); - ret = user.getTradeNum(); - } - function getTrade_1(bytes32 id, uint i)external returns(uint trade_date, uint trade_id, uint sheet_id , string bs) - { - User user = User(user_list.getUserAgentAddr(id)); - var (trade_date_, trade_id_, sheet_id_, bs_) = user.getTrade_1(i); - trade_date = trade_date_; - trade_id = trade_id_; - sheet_id = sheet_id_; - bs = LibString.bytes32ToString(bs_); - } - function getTrade_2(bytes32 id, uint i)external returns(uint price, uint trade_qty ,string user_id, string opp_id) - { - User user = User(user_list.getUserAgentAddr(id)); - var(price_, trade_qty_, user_id_, opp_id_) = user.getTrade_2(i); - price = price_; - trade_qty = trade_qty_; - user_id = LibString.bytes32ToString(user_id_); - opp_id = LibString.bytes32ToString(opp_id_); - } - function getSheetMapNum(bytes32 user_id) returns(uint ret) - { - User user = User(user_list.getUserAgentAddr(user_id)); - ret = user.getSheetMapNum(); - } - function getSheetMap_1(bytes32 id, uint index)external returns(string user_id, uint sheet_id,string class_id, string make_date, string level_id) - { - User user = User(user_list.getUserAgentAddr(id)); - var(user_id_, sheet_id_, class_id_, make_date_, lev_id_)=user.getSheetMap_1(index); - user_id = LibString.bytes32ToString(user_id_); - sheet_id = sheet_id_; - class_id = LibString.bytes32ToString(class_id_); - make_date = LibString.bytes32ToString(make_date_); - level_id = LibString.bytes32ToString(lev_id_); - } - function getSheetMap_2(bytes32 user_id, uint index)external returns(string wh_id, string place_id, uint all_amount, uint avail_amount, uint frozen_amount) - { - User user = User(user_list.getUserAgentAddr(user_id)); - var(wh_id_, place_id_,all_amount_, avail_amount_, frozen_amount_) = user.getSheetMap_2(index); - wh_id = LibString.bytes32ToString(wh_id_); - place_id = LibString.bytes32ToString(place_id_); - all_amount = all_amount_; - avail_amount = avail_amount_; - frozen_amount = frozen_amount_; - } - function insertFunds(bytes32 user_id, uint qty) - { - User user = User(user_list.getUserAgentAddr(user_id)); - user.insertFunds(qty); - } -} diff --git a/smartcontract/contracts/User.sol b/smartcontract/contracts/User.sol deleted file mode 100644 index aaeecf1..0000000 --- a/smartcontract/contracts/User.sol +++ /dev/null @@ -1,682 +0,0 @@ -pragma solidity ^0.4.5; - -import "./CreateID.sol"; -import "./Market.sol"; -import "./UserList.sol"; -import "./ContractAddress.sol"; -import "./lib/LibSheetMap.sol"; -import "./lib/StructSheet.sol"; -import "./lib/LibTradeMap.sol"; -import "./lib/StructTrade.sol"; -import "./lib/StructMarket.sol"; -import "./lib/StructFunds.sol"; -import "./lib/LibFunds.sol"; -import "./lib/LibString.sol"; -import "./Admin.sol"; - - -contract User -{ - event getRet(uint indexed ret); - //挂牌请求数据结构 - struct ListRequest - { - uint sheet_id_; //仓单序号 - uint market_id_; //挂单编号 - uint date_; //挂牌日期 - bytes32 class_id_; //品种代码 - bytes32 make_date_; //产期 - bytes32 lev_id_; //等级 - uint price_; //价格(代替浮点型) - uint list_qty_; //挂牌量 - uint rem_qty_; //剩余量 - uint deal_qty_; //成交量 - } - - //协商交易请求数据结构 发送 - struct NegSendRequest - { - uint sheet_id_; //仓单序号 - uint neg_qty_; //交易数量 - uint price_; //价格 - uint neg_id_; //协商编号 - bytes32 opp_id_;//对手方id - bytes32 trade_state_; //成交状态 - } - - //协商交易请求数据结构 接收 - struct NegReceiveRequest - { - uint sheet_id_; //仓单序号 - uint neg_qty_; //交易数量 - uint price_; //价格 - uint neg_id_; //协商编号 - bytes32 opp_id_; //对手方id - bytes32 trade_state_; //成交状态 - } - - using LibSheetMap for LibSheetMap.SheetMap; - using LibTradeMap for LibTradeMap.TradeMap; - using LibFunds for LibFunds.Funds; - - LibSheetMap.SheetMap sheet_map; //仓单 - LibTradeMap.TradeMap trade_map; //合同 - LibFunds.Funds funds; //资金 - uint fee; //手续费 - - ContractAddress contract_address; - Market market; - CreateID create_id; - UserList user_list; - Admin admin; - - string market_name; - string create_id_name; - string user_list_name; - string admin_name; - bytes32 my_user_id; - bytes32 tmp_id; //临时id变量 - StructMarket.value temp_market; //临时行情变量 - StructTrade.value tmp_trade; //临时合同变量 - StructSheet.value tmp_sheet; //临时仓单变量 - - ListRequest[] list_req; //协商交易请求列表 - NegSendRequest[] neg_req_send_array; - NegReceiveRequest[] neg_req_receive_array; - - - - function setContractAddress(address addr) - { - contract_address = ContractAddress(addr); - } - function setMarketName(string name) - { - market_name = name; - } - function setCreateIDName(string name) - { - create_id_name = name; - } - function setUserListName(string name) - { - user_list_name = name; - } - function setUserID(bytes32 id) - { - my_user_id = id; - } - function setAdmin(string admin_name) - { - admin = Admin(contract_address.getContractAddress(admin_name)); - } - - //初始化CreateID合约变量 - function setCreateID() - { - create_id = CreateID(contract_address.getContractAddress(create_id_name)); - } - - //初始化仓单 - function insertSheet(bytes32 user_id, bytes32 class_id, bytes32 make_date,bytes32 lev_id, bytes32 wh_id, bytes32 place_id, uint all_amount,uint frozen_amount, uint available_amount) - { - create_id = CreateID(contract_address.getContractAddress(create_id_name)); - uint sheet_id = create_id.getSheetID(); - sheet_map.insert(sheet_id, StructSheet.value(user_id, sheet_id, class_id, make_date, lev_id, wh_id, place_id, all_amount, - available_amount,frozen_amount)); - } - - //获取持有者的仓单数量 - function getSheetAmount(uint sheet_id) returns (uint all_amount, uint available_amount, uint frozen_amount) - { - StructSheet.value memory sheet = sheet_map.getValue(sheet_id); - all_amount = sheet.all_amount_; - available_amount = sheet.available_amount_; - frozen_amount = sheet.frozen_amount_; - } - - //获取持有者的仓单总数量 - function getSheetAllAmount(uint sheet_id) returns (uint all_amount) - { - StructSheet.value memory sheet = sheet_map.getValue(sheet_id); - all_amount = sheet.all_amount_; - } - - //获取持有者的仓单可用数量 - function getSheetAvailableAmount(uint sheet_id) returns (uint available_amount) - { - StructSheet.value memory sheet = sheet_map.getValue(sheet_id); - available_amount = sheet.available_amount_; - } - - //获取持有者的仓单冻结数量 - function getSheetFrozenAmount(uint sheet_id) returns (uint frozen_amount) - { - StructSheet.value memory sheet = sheet_map.getValue(sheet_id); - frozen_amount = sheet.frozen_amount_; - } - - //获取仓单信息 - function getSheetAttribute(uint sheet_id) returns(bytes32 class_id,bytes32 make_date,bytes32 lev_id,bytes32 wh_id,bytes32 place_id) - { - tmp_sheet = sheet_map.getValue(sheet_id); - class_id = tmp_sheet.class_id_; - make_date = tmp_sheet.make_date_; - lev_id = tmp_sheet.lev_id_; - wh_id = tmp_sheet.wh_id_; - place_id = tmp_sheet.place_id_; - } - - //初始化资金 资金数扩大100倍 - function insertFunds(uint qty) - { - funds.insert(qty); - } - - function setFee(uint n) - { - fee = n; - } - - function getTotalFunds() returns(uint) - { - return funds.getTotalFunds(); - } - - function getAvaFunds() returns(uint) - { - return funds.getAvaFunds(); - } - - function getFrozenFunds() returns(uint) - { - return funds.getFrozenFunds(); - } - - - - //挂牌请求 "zhang",0,10,20 - function listRequest(bytes32 seller_user_id, uint sheet_id, uint price, uint sell_qty) returns( uint) - { - var sheet = sheet_map.getValue(sheet_id); - if(sheet.available_amount_ == 0) - { - getRet(uint(-1)); - return uint(-1); - } - market = Market(contract_address.getContractAddress(market_name)); - market.insertMarket_1(sheet.sheet_id_,sheet.class_id_, sheet.make_date_,sheet.lev_id_, sheet.wh_id_, sheet.place_id_); - //TODO modify deadline、dlv_unit - var(ret_market_id,date) = market.insertMarket_2(price, sell_qty, 0, sell_qty, "deadline", 5, sheet.user_id_ ); - if(ret_market_id >0) - { - freeze(sheet_id, sell_qty); - - //将挂牌数据保存到挂牌请求列表中 - //仓单序号,挂牌编号,挂牌日期,类别,产期,等级,价格,挂牌量,剩余量,成交量, - list_req.push(ListRequest(sheet_id, ret_market_id, date, sheet.class_id_, sheet.make_date_, sheet.lev_id_, price, sell_qty, sell_qty,0)); - getRet(ret_market_id); - } - return ret_market_id; - } - - - //摘牌请求 "li",1,10 - function delistRequest(bytes32 buy_user_id, uint selected_market_id, uint confirm_qty) returns (int) - { - getMarketTemp_1(selected_market_id); - getMarketTemp_2(selected_market_id); - uint payment = confirm_qty * temp_market.price_; - - if( payment > funds.getAvaFunds() ) - return -1; - - market = Market(contract_address.getContractAddress(market_name)); - int ret = market.updateMarket(buy_user_id, selected_market_id, confirm_qty); - - //判断是否摘牌成功 - if(ret != 0) - return -2; - - return 0; - } - - //获取行情数据 - function getMarketTemp_1(uint market_id) - { - market = Market(contract_address.getContractAddress(market_name)); - var( - date, //挂牌日期 - ret_market_id, //挂牌编号 - sheet_id, //仓单编号 - class_id, //品种代码 - make_date, //产期 - lev_id, //等级 - wh_id, //仓库代码 - place_id, //产地代码 - price_type //报价类型 - ) = market.getMarket_1(market_id); - temp_market.date_ = date; - temp_market.market_id_ = ret_market_id; - temp_market.sheet_id_ = sheet_id; - temp_market.class_id_ = class_id; - temp_market.make_date_ = make_date; - temp_market.lev_id_ = lev_id; - temp_market.wh_id_ = wh_id; - temp_market.place_id_ = place_id; - temp_market.type_ = price_type; - } - function getMarketTemp_2(uint market_id) - { - var( - price, //价格(代替浮点型) - list_qty, //挂牌量 - deal_qty, //成交量 - rem_qty, //剩余量 - deadline, //挂牌截止日 - dlv_unit, //交割单位 - user_id, //用户id - seller_addr //卖方地址 - ) = market.getMarket_2(market_id); - temp_market.price_ = price; - temp_market.list_qty_ = list_qty; - temp_market.deal_qty_ = deal_qty; - temp_market.rem_qty_ = rem_qty; - temp_market.deadline_ = deadline; - temp_market.dlv_unit_ = dlv_unit; - temp_market.user_id_ = user_id; - temp_market.seller_addr_= seller_addr; - } - - //构建合同 - function recordTrade(uint trade_date, uint trade_id, bytes32 opp_user_id, bytes32 bs, uint confirm_qty, uint market_id) returns(int) - { - int ret = -1; - getMarketTemp_1(market_id); - getMarketTemp_2(market_id); - if (bs == "买") - trade_map.insert(trade_id,trade_date, opp_user_id,my_user_id, bs, confirm_qty,temp_market); - if (bs == "卖") - trade_map.insert(trade_id,trade_date, my_user_id, opp_user_id, bs, confirm_qty,temp_market); - - - if(bs == "买") - { - funds.freeze(confirm_qty * temp_market.price_); - admin.insertConfirmListReq(my_user_id, opp_user_id,trade_id); - } - - ret = 0; - } - - function getTradeNum() returns (uint) - { - return trade_map.size(); - } - - //管理员确认挂牌交易 - function confirmList(uint trade_id) returns(int) - { - uint sheet_id = trade_map.data[trade_id].sheet_id_; - uint qty = trade_map.data[trade_id].trade_qty_; - uint price = trade_map.data[trade_id].price_; - bytes32 user_id = trade_map.data[trade_id].user_id_; - bytes32 opp_id = trade_map.data[trade_id].opp_id_; - uint ret = 0; - - //判断该合同是否存在 - if(!trade_map.isExisted(trade_id)) - return -1; - - if(trade_map.data[trade_id].bs_ == "卖") - { - sheet_map.reduce(sheet_id,qty); - funds.insert(qty * price); - funds.deductFee(qty*price*fee); - } - else - { - //初始化user_list - user_list = UserList(contract_address.getContractAddress(user_list_name)); - - User user_sell = User(user_list.getUserAgentAddr(opp_id)); - var(class_id,make_date,lev_id,wh_id,place_id)= user_sell.getSheetAttribute(sheet_id); - - if( (ret = sheet_map.isExisted(class_id,make_date,lev_id,wh_id,place_id)) != 0) - //var(existed,ret) = sheet_map.isExisted(class_id,make_date,lev_id,wh_id,place_id); - //if( sheet_map.isExisted(class_id,make_date,lev_id,wh_id,place_id) ) - sheet_map.add(ret,qty); - else - { - sheet_id = create_id.getSheetID(); - sheet_map.insert(sheet_id, StructSheet.value(user_id, sheet_id, class_id, make_date, lev_id, wh_id, place_id,qty,0,qty)); - } - - funds.reduce(qty * price); - } - return 0; - } - - //更新卖方挂牌请求 - function updateListReq(uint market_id, uint deal_qty) - { - var list_num = list_req.length; - for(uint i = 0; i available_amount) - return false; - - sheet_map.freeze(sheet_id, amount); - return true; - } - - - - //发送协商交易请求 卖方调用 - function sendNegReq(uint sheet_id, uint qty, uint price, bytes32 opp_id) returns(int ret) - { - //冻结仓单 - if( ! freeze(sheet_id, qty)) - { - //TODO event - ret = -1; - return ret; - } - - create_id = CreateID(contract_address.getContractAddress(create_id_name)); - uint neg_id = create_id.getNegID();//协商交易编号 - - //更新协商交易请求列表(发送) - neg_req_send_array.push( NegSendRequest(sheet_id,qty,price,neg_id,opp_id,"未成交") ); - - //初始化user_list - user_list = UserList(contract_address.getContractAddress(user_list_name)); - //调用对手方协商交易请求的接收方法 - User buy_user = User( user_list.getUserAgentAddr(opp_id) ); - //TODO assert 判断buy_user不为空 - buy_user.recNegReq(sheet_id, qty, price,neg_id, my_user_id); - - ret = 0; - } - - //接收协商交易请求 卖方调用买方 - function recNegReq(uint sheet_id, uint qty, uint price, uint neg_id,bytes32 user_sell_id) - { - neg_req_receive_array.push( NegReceiveRequest(sheet_id,qty,price, neg_id,user_sell_id, "未成交") ); - } - - //获取接收的协商交易请求数据 - function getNegReqRec(uint k) returns(uint length, uint sheet_id, uint price, uint neg_id, bytes32 user_sell_id_) - { - length = neg_req_receive_array.length; - sheet_id = neg_req_receive_array[k].sheet_id_; - price = neg_req_receive_array[k].price_; - neg_id = neg_req_receive_array[k].neg_id_; - user_sell_id_ = neg_req_receive_array[k].opp_id_; - - return (length,sheet_id,price,neg_id,user_sell_id_); - } - - //确认协商交易 买方调用此函数 - function agreeNeg(bytes32 buy_user_id, uint neg_id) returns(int ret) - { - //判断数组是否为空 - if(neg_req_receive_array.length ==0) - { - ret = -1; - return; - } - - for(uint i= 0; i funds.getAvaFunds() ) - { - ret = -2; - return; - } - - //初始化user_list - user_list = UserList(contract_address.getContractAddress(user_list_name)); - - //构建成交 - bytes32 sell_user_id = neg_req_receive_array[i].opp_id_; - User sell_user = User( user_list.getUserAgentAddr(sell_user_id) ); - - //获取合同编号 - CreateID create_id = CreateID(contract_address.getContractAddress(create_id_name)); - uint trade_id = create_id.getTradeID(); - uint date = now; - - ret = recordNegTrade(trade_id, date, buy_user_id, sell_user_id, "买", neg_id); - if(ret != 0) - { - ret = -3; - return ; - } - - ret = sell_user.recordNegTrade(trade_id,date, buy_user_id, sell_user_id, "卖", neg_id); - if(ret != 0) - { - ret = -4; - return ; - } - - ret = 0; - } - - //构建合同 - function recordNegTrade(uint trade_id, uint date, bytes32 buy_user_id,bytes32 sell_user_id, bytes32 bs, uint neg_id) returns(int ) - { - if(bs == "卖") - { - //判断发送请求数组是否为空 - if(neg_req_send_array.length ==0) - return -1; - - for(uint i= 0; i < neg_req_send_array.length; i++ ) - { - if(neg_req_send_array[i].neg_id_ == neg_id) - break; - } - - trade_map.insert(trade_id, StructTrade.value(date,trade_id,neg_req_send_array[i].sheet_id_,bs,neg_req_send_array[i].price_,neg_req_send_array[i].neg_qty_,sell_user_id,buy_user_id)); - - //funds.insert(neg_req_send_array[i].qty_ * neg_req_send_array[i].price_); - } - else - { - //判断接收请求数组是否为空 - if(neg_req_receive_array.length ==0) - return -2; - - for(uint k= 0; k < neg_req_receive_array.length; k++ ) - { - if(neg_req_receive_array[k].neg_id_ == neg_id) - break; - } - - trade_map.insert(trade_id, StructTrade.value(date,trade_id,neg_req_receive_array[k].sheet_id_,bs,neg_req_receive_array[k].price_,neg_req_receive_array[k].neg_qty_,buy_user_id,sell_user_id)); - - //funds.reduce(neg_req_receive_array[k].qty_ * neg_req_receive_array[k].price_); - funds.freeze(neg_req_receive_array[k].neg_qty_ * neg_req_receive_array[k].price_); - - admin.insertConfirmNegReq(my_user_id, sell_user_id, trade_id); - } - return 0; - } - - //获取合同数据 - function getTradeMap(uint k) returns(uint length, uint trade_id, uint sheet_id, bytes32 bs, bytes32 opp_id) - { - tmp_trade = trade_map.getValue(k); - - length = trade_map.length(); - trade_id = tmp_trade.trade_id_; - sheet_id = tmp_trade.sheet_id_; - bs = tmp_trade.bs_; - opp_id = tmp_trade.opp_id_; - } - - //管理员确认协商交易 - function confirmNeg(uint trade_id) returns(int) - { - uint sheet_id = trade_map.data[trade_id].sheet_id_; - uint qty = trade_map.data[trade_id].trade_qty_; - uint price = trade_map.data[trade_id].price_; - bytes32 user_id = trade_map.data[trade_id].user_id_; - bytes32 opp_id = trade_map.data[trade_id].opp_id_; - uint ret = 0; - - //判断该合同是否存在 - if(!trade_map.isExisted(trade_id)) - return -1; - - if(trade_map.data[trade_id].bs_ == "卖") - { - sheet_map.reduce(sheet_id,qty); - funds.insert(qty * price); - funds.deductFee(qty*price*fee); - } - else - { - //初始化user_list - user_list = UserList(contract_address.getContractAddress(user_list_name)); - - User user_sell = User(user_list.getUserAgentAddr(opp_id)); - var(class_id,make_date,lev_id,wh_id,place_id)= user_sell.getSheetAttribute(sheet_id); - - if( (ret = sheet_map.isExisted(class_id,make_date,lev_id,wh_id,place_id)) != 0) - // var(existed,ret) = sheet_map.isExisted(class_id,make_date,lev_id,wh_id,place_id); - //if( sheet_map.isExisted(class_id,make_date,lev_id,wh_id,place_id)) - sheet_map.add(ret,qty); - else - { - sheet_id = create_id.getSheetID(); - sheet_map.insert(sheet_id, StructSheet.value(user_id, sheet_id, class_id, make_date, lev_id, wh_id, place_id,qty,0,qty)); - } - - funds.reduce(qty * price); - } - return 0; - } - - - //获取sheetMap元素个数 - function getSheetMapNum() returns(uint) - { - return sheet_map.size(); - } - //获取sheetMap元素信息 - function getSheetMap_1(uint index) external returns(bytes32 user_id, uint sheet_id, bytes32 class_id, bytes32 make_date, bytes32 level_id) - { - tmp_sheet = sheet_map.getValueByIndex(index); - user_id = tmp_sheet.user_id_; - sheet_id = tmp_sheet.sheet_id_; - class_id = tmp_sheet.class_id_; - make_date = tmp_sheet.make_date_; - level_id = tmp_sheet.lev_id_; - } - function getSheetMap_2(uint index) external returns(bytes32 wh_id, bytes32 place_id, uint all_amount, uint avail_amount, uint frozen_amount) - { - tmp_sheet = sheet_map.getValueByIndex(index); - wh_id = tmp_sheet.wh_id_; - place_id = tmp_sheet.place_id_; - all_amount = tmp_sheet.all_amount_; - avail_amount = tmp_sheet.available_amount_; - frozen_amount = tmp_sheet.frozen_amount_; - } - - //获取挂牌请求列表的长度 - function getListReqNum() returns(uint) - { - return list_req.length; - } - //获取挂牌请求列表数据 - function getListReq_1(uint i) external returns(uint sheet_id, uint market_id, uint date, bytes32 class_id, bytes32 make_date) - { - if (i < list_req.length) - { - sheet_id = list_req[i].sheet_id_; - market_id = list_req[i].market_id_; - date = list_req[i].date_; - class_id = list_req[i].class_id_; - make_date = list_req[i].make_date_; - } - } - function getListReq_2(uint i) external returns(bytes32 lev_id, uint price, uint list_qty, uint deal_qty, uint rem_qty) - { - if (i < list_req.length) - { - lev_id = list_req[i].lev_id_; - price = list_req[i].price_; - list_qty = list_req[i].list_qty_; - deal_qty = list_req[i].deal_qty_; - rem_qty = list_req[i].rem_qty_; - } - } - - //获取协商请发送求列表数据 - function getNegReqSend(uint i) external returns(uint sheet_id, uint neg_id, uint price, uint neg_qty, bytes32 opp_id, bytes32 trade_state) - { - if (i < neg_req_send_array.length) - { - sheet_id = neg_req_send_array[i].sheet_id_; - neg_id = neg_req_send_array[i].neg_id_; - price = neg_req_send_array[i].price_; - neg_qty = neg_req_send_array[i].neg_qty_; - opp_id = neg_req_send_array[i].opp_id_; - trade_state = neg_req_send_array[i].trade_state_; - } - } - - //获取协商请接收求列表数据 - function getNegReqReceive(uint i) external returns(uint sheet_id, uint neg_id, uint price, uint neg_qty, string opp_id, string trade_state) - { - if (i < neg_req_receive_array.length) - { - sheet_id = neg_req_receive_array[i].sheet_id_; - neg_id = neg_req_receive_array[i].neg_id_; - price = neg_req_receive_array[i].price_; - neg_qty = neg_req_receive_array[i].neg_qty_; - opp_id = LibString.bytes32ToString(neg_req_receive_array[i].opp_id_); - trade_state = LibString.bytes32ToString(neg_req_receive_array[i].trade_state_); - } - } - //根据索引获取合同数据 - function getTrade_1(uint it) external returns(uint trade_date, uint trade_id, uint sheet_id, bytes32 bs) - { - tmp_trade = trade_map.getValueByIndex(it); - - trade_date = tmp_trade.trade_date_; - trade_id = tmp_trade.trade_id_; - sheet_id = tmp_trade.sheet_id_; - bs = tmp_trade.bs_; - } - function getTrade_2(uint it) external returns(uint price, uint trade_qty,bytes32 user_id, bytes32 opp_id) - { - tmp_trade = trade_map.getValueByIndex(it); - price = tmp_trade.price_; - trade_qty = tmp_trade.trade_qty_; - user_id = tmp_trade.user_id_; - opp_id = tmp_trade.opp_id_; - } -} - diff --git a/smartcontract/contracts/UserList.sol b/smartcontract/contracts/UserList.sol deleted file mode 100644 index ead400f..0000000 --- a/smartcontract/contracts/UserList.sol +++ /dev/null @@ -1,63 +0,0 @@ -pragma solidity ^0.4.11; - -import "./lib/StructUserAddr.sol"; -import "./lib/LibUserAddrMap.sol"; - -contract UserList -{ - using LibUserAddrMap for LibUserAddrMap.UserAddrMap; - LibUserAddrMap.UserAddrMap user_map; - StructUserAddr.value user_addr; - - function addUser(address external_addr, address agent_addr, bytes32 user_id, int user_auth) - { - user_map.insert(user_id, StructUserAddr.value(external_addr, agent_addr, user_id, user_auth)); - } - - function getUserInfo(bytes32 user_id) returns(address external_addr, address agent_addr, bytes32 ret_user_id, int user_auth) - { - user_addr = user_map.getValue(user_id); - external_addr = user_addr.extern_addr_; - agent_addr = user_addr.agent_addr_; - ret_user_id = user_addr.user_id_; - user_auth = user_addr.user_auth_; - } - - function getUserAgentAddr(bytes32 user_id) returns(address agent_addr) - { - user_addr = user_map.getValue(user_id); - agent_addr = user_addr.agent_addr_; - } - function getUserNum() returns (uint) - { - return user_map.size(); - } - function getUserInfoByIndex(uint index) returns(address external_addr, address agent_addr, bytes32 ret_user_id, int user_auth) - { - user_addr = user_map.getValue(index); - external_addr = user_addr.extern_addr_; - agent_addr = user_addr.agent_addr_; - ret_user_id = user_addr.user_id_; - user_auth = user_addr.user_auth_; - } - - //获取外部账户地址 - function getExternal_addr(bytes32 user_id) returns(address external_addr) - { - user_addr = user_map.getValue(user_id); - external_addr = user_addr.extern_addr_; - } - - //查询user_id 是否存在 - function isExisted(bytes32 user_id) returns(bool ret) - { - ret = user_map.isExisted(user_id); - } - - - function delUserInfo(bytes32 user_id) - { - user_map.remove(user_id); - } - -} diff --git a/smartcontract/contracts/lib/LibAddressMap.sol b/smartcontract/contracts/lib/LibAddressMap.sol deleted file mode 100644 index 718379b..0000000 --- a/smartcontract/contracts/lib/LibAddressMap.sol +++ /dev/null @@ -1,108 +0,0 @@ -pragma solidity ^0.4.5; -import "./LibString.sol"; -import "./LibArray.sol"; -import {StructAddressInfo as Data} from "./StructAddressInfo.sol"; -library LibAddressMap -{ - using LibString for *; - struct AddressMap - { - mapping(string => Data.value) data; - string[] keyIndex; - } - - function insert(AddressMap storage self, string k, Data.value v) internal returns (bool replaced) - { - replaced = true; - if(self.data[k].addr == 0x0) - { - self.keyIndex.push(k); - replaced = false; - } - self.data[k] = v; - } - function remove(AddressMap storage self, string k) internal returns (bool existed) - { - if(self.data[k].addr == 0x0) - { - return false; - } - else - { - delete self.data[k]; - LibArray.deleteElement(self.keyIndex, k); - return true; - } - } - function getValue(AddressMap storage self, string k) internal returns (address) - { - return self.data[k].addr; - } - function isExisted(AddressMap storage self, string k) internal returns (bool existed) - { - var (ret, ) = LibString.inArray(k, self.keyIndex); - return ret; - } - function empty(AddressMap storage self) internal returns (bool) - { - return (self.keyIndex.length == 0); - } - function it_start(AddressMap storage self) internal returns (uint) - { - if(empty(self)) - return uint(-1); - return 0; - } - function it_next(AddressMap storage self, uint it) internal returns (uint) - { - it++; - if(it < self.keyIndex.length) - return it; - else - return uint(-1); - } - - function it_valid(uint it) internal returns (bool) - { - if( (uint(-1) != it) ) - { - return true; - } - else - { - return false; - } - } - function it_valid(AddressMap storage, uint it) internal returns (bool) - { - if( (uint(-1) != it) ) - { - return true; - } - else - { - return false; - } - } - function getValue(AddressMap storage self, uint it) internal returns (Data.value) - { - if(it + 1 > self.keyIndex.length) - { - Data.value empty_value; - return empty_value; - } - return self.data[ self.keyIndex[it] ]; - } - function getKey(AddressMap storage self, uint it) internal returns (string) - { - return self.keyIndex[it]; - } - function size(AddressMap storage self) internal returns(uint) - { - return self.keyIndex.length; - } - function length(AddressMap storage self) internal returns(uint) - { - return self.keyIndex.length; - } -} diff --git a/smartcontract/contracts/lib/LibArray.sol b/smartcontract/contracts/lib/LibArray.sol deleted file mode 100644 index 3a422d9..0000000 --- a/smartcontract/contracts/lib/LibArray.sol +++ /dev/null @@ -1,78 +0,0 @@ -pragma solidity ^0.4.11; -import "./LibString.sol"; -library LibArray -{ - function deleteElement(string[] storage self, string element) - { - uint j = 0; - for (uint i = 0 ; i < self.length; ++i) - { - if (LibString.equals(self[i], element)) - { - j++; // - continue; - } - if(j != 0) - { - self[i-j] = self[i]; - } - } - self.length = self.length - j; - } - function deleteElement(bytes32[] storage self, bytes32 element) - { - uint j = 0; - for (uint i = 0 ; i < self.length; ++i) - { - if (self[i] == element) - { - j++; // - continue; - } - if(j != 0) - { - self[i-j] = self[i]; - } - } - self.length = self.length - j; - } - function deleteElement(uint[] storage self, uint element) - { - uint j = 0; - for (uint i = 0 ; i < self.length; ++i) - { - if (self[i] == element) - { - j++; // - continue; - } - if(j != 0) - { - self[i-j] = self[i]; - } - } - self.length = self.length - j; - } - - function isExisted(bytes32[] storage self, bytes32 element) internal returns(bool) - { - for(uint i = 0; i < self.length; ++i) - { - if(self[i] == element) - return true; - } - return false; - } - - function isExisted(uint[] storage self, uint element) internal returns(bool) - { - for(uint i = 0; i < self.length; ++i) - { - if(self[i] == element) - return true; - } - return false; - } - -} - diff --git a/smartcontract/contracts/lib/LibFunds.sol b/smartcontract/contracts/lib/LibFunds.sol deleted file mode 100644 index 7218242..0000000 --- a/smartcontract/contracts/lib/LibFunds.sol +++ /dev/null @@ -1,60 +0,0 @@ -pragma solidity^0.4.11; -import "./StructFunds.sol"; - -library LibFunds -{ - struct Funds - { - StructFunds.value data; - } - - - function insert(Funds storage self, uint qty) internal - { - self.data.total_funds_ += qty * 100; - self.data.ava_funds_ += qty * 100; - } - - function deductFee(Funds storage self, uint qty) internal - { - self.data.total_funds_ -= qty; - self.data.ava_funds_ -= qty; - } - - function reduce(Funds storage self, uint qty) internal - { - - self.data.total_funds_ -= qty * 100; - self.data.frozen_funds_ -= qty * 100; - } - - function freeze(Funds storage self, uint qty) internal returns (bool) - { - if(self.data.ava_funds_ < (qty * 100)) - return false; - - self.data.ava_funds_ -= qty * 100; - self.data.frozen_funds_ += qty * 100; - return true; - } - - function getTotalFunds(Funds storage self) internal returns (uint) - { - return self.data.total_funds_; - } - - - function getAvaFunds(Funds storage self) internal returns (uint) - { - return self.data.ava_funds_; - } - - function getFrozenFunds(Funds storage self) internal returns (uint) - { - return self.data.frozen_funds_; - } - -} - - - diff --git a/smartcontract/contracts/lib/LibMarketMap.sol b/smartcontract/contracts/lib/LibMarketMap.sol deleted file mode 100644 index 25cabb0..0000000 --- a/smartcontract/contracts/lib/LibMarketMap.sol +++ /dev/null @@ -1,119 +0,0 @@ -pragma solidity ^0.4.11; -import "./LibString.sol"; -import "./LibArray.sol"; -import "./StructMarket.sol"; -library LibMarketMap -{ - using LibString for *; - struct MarketMap - { - mapping(uint => StructMarket.value) data; - uint[] keyIndex; - } - - function insert(MarketMap storage self, uint k, StructMarket.value v) internal returns (bool replaced) - { - replaced = true; - if(self.data[k].dlv_unit_ == 0) - { - self.keyIndex.push(k); - replaced = false; - } - self.data[k] = v; - } - function update(MarketMap storage self, uint k, uint deal_qty, uint rem_qty) internal - { - self.data[k].deal_qty_ = deal_qty; - self.data[k].rem_qty_ = rem_qty; - } - function remove(MarketMap storage self, uint k) internal returns (bool existed) - { - if(self.data[k].dlv_unit_ == 0) - { - return false; - } - else - { - delete self.data[k]; - LibArray.deleteElement(self.keyIndex, k); - return true; - } - } - function getValue(MarketMap storage self, uint k) internal returns (StructMarket.value) - { - return self.data[k]; - } - /* - function isExisted(MarketMap storage self, uint k) internal returns (bool existed) - { - var (ret, ) = LibString.inArray(k, self.keyIndex); - return ret; - } - */ - function empty(MarketMap storage self) internal returns (bool) - { - return (self.keyIndex.length == 0); - } - function it_start(MarketMap storage self) internal returns (uint) - { - if(empty(self)) - return uint(-1); - return 0; - } - function it_next(MarketMap storage self, uint it) internal returns (uint) - { - it++; - if(it < self.keyIndex.length) - return it; - else - return uint(-1); - } - - function it_valid(uint it) internal returns (bool) - { - if( (uint(-1) != it) ) - { - return true; - } - else - { - return false; - } - } - function it_valid(MarketMap storage, uint it) internal returns (bool) - { - if( (uint(-1) != it) ) - { - return true; - } - else - { - return false; - } - } - function getValueByIndex(MarketMap storage self, uint it) internal returns (StructMarket.value) - { - if(it + 1 > self.keyIndex.length) - { - StructMarket.value empty_value; - return empty_value; - } - return self.data[ self.keyIndex[it] ]; - } - function getKey(MarketMap storage self, uint it) internal returns (uint) - { - if(it + 1 > self.keyIndex.length) - { - return 0; - } - return self.keyIndex[it]; - } - function size(MarketMap storage self) internal returns(uint) - { - return self.keyIndex.length; - } - function length(MarketMap storage self) internal returns(uint) - { - return self.keyIndex.length; - } -} diff --git a/smartcontract/contracts/lib/LibSheetMap.sol b/smartcontract/contracts/lib/LibSheetMap.sol deleted file mode 100644 index 35b674a..0000000 --- a/smartcontract/contracts/lib/LibSheetMap.sol +++ /dev/null @@ -1,148 +0,0 @@ -pragma solidity ^0.4.11; -import "./LibString.sol"; -import "./LibArray.sol"; -import "./StructSheet.sol"; -library LibSheetMap -{ - using LibString for *; - struct SheetMap - { - mapping(uint => StructSheet.value) data; - uint[] keyIndex; - } - - //创建仓单 - function insert(SheetMap storage self, uint k, StructSheet.value v) internal returns (bool replaced) - { - replaced = true; - if(self.data[k].user_id_ == "") - { - self.keyIndex.push(k); - replaced = false; - } - self.data[k] = v; - } - - //更新仓单数量 - function update(SheetMap storage self, uint k, uint all_amount, uint available_amount, uint frozen_amount) internal - { - self.data[k].all_amount_ = all_amount; - self.data[k].available_amount_ = available_amount; - self.data[k].frozen_amount_ = frozen_amount; - } - - //增加仓单 - function add(SheetMap storage self,uint k, uint qty) - { - self.data[k].all_amount_ += qty; - self.data[k].available_amount_ += qty; - } - - //减少仓单 - function reduce(SheetMap storage self, uint k, uint qty) - { - self.data[k].all_amount_ -= qty; - } - - //冻结仓单 - function freeze(SheetMap storage self, uint k, uint qty) - { - self.data[k].available_amount_ -= qty; - self.data[k].frozen_amount_ += qty; - } - - //移除仓单 - function remove(SheetMap storage self, uint k) internal returns (bool existed) - { - if(self.data[k].user_id_ == "") - { - return false; - } - else - { - delete self.data[k]; - LibArray.deleteElement(self.keyIndex, k); - return true; - } - } - function getValue(SheetMap storage self, uint k) internal returns (StructSheet.value) - { - return self.data[k]; - } - - //判断该种仓单是否存在 - function isExisted(SheetMap storage self, bytes32 class_id, bytes32 make_date, bytes32 lev_id, bytes32 wh_id, bytes32 place_id) internal returns (uint) - { - for(uint i = 0; i < self.keyIndex.length; i++) - { - if( self.data[self.keyIndex[i]].class_id_ == class_id && self.data[self.keyIndex[i]].make_date_ == make_date && self.data[self.keyIndex[i]].lev_id_ == lev_id && self.data[self.keyIndex[i]].wh_id_ == wh_id && self.data[self.keyIndex[i]].place_id_ == place_id) - return self.data[self.keyIndex[i]].sheet_id_; - } - return 0; - } - - function empty(SheetMap storage self) internal returns (bool) - { - return (self.keyIndex.length == 0); - } - function it_start(SheetMap storage self) internal returns (uint) - { - if(empty(self)) - return uint(-1); - return 0; - } - function it_next(SheetMap storage self, uint it) internal returns (uint) - { - it++; - if(it < self.keyIndex.length) - return it; - else - return uint(-1); - } - - function it_valid(uint it) internal returns (bool) - { - if( (uint(-1) != it) ) - { - return true; - } - else - { - return false; - } - } - function it_valid(SheetMap storage, uint it) internal returns (bool) - { - if( (uint(-1) != it) ) - { - return true; - } - else - { - return false; - } - } - function getValueByIndex(SheetMap storage self, uint it) internal returns (StructSheet.value) - { - if(it + 1 > self.keyIndex.length) - { - StructSheet.value empty_value; - return empty_value; - } - return self.data[ self.keyIndex[it] ]; - } - /* - function getKey(SheetMap storage self, uint it) internal returns (bytes32) - { - return self.keyIndex[it]; - } - */ - function size(SheetMap storage self) internal returns(uint) - { - return self.keyIndex.length; - } - function length(SheetMap storage self) internal returns(uint) - { - return self.keyIndex.length; - } -} diff --git a/smartcontract/contracts/lib/LibString.sol b/smartcontract/contracts/lib/LibString.sol deleted file mode 100644 index 219a0a3..0000000 --- a/smartcontract/contracts/lib/LibString.sol +++ /dev/null @@ -1,901 +0,0 @@ -pragma solidity ^0.4.2; -library LibString { - - using LibString for *; - - function memcpy(uint dest, uint src, uint len) private { - // Copy word-length chunks while possible - for(; len >= 32; len -= 32) { - assembly { - mstore(dest, mload(src)) - } - dest += 32; - src += 32; - } - - // Copy remaining bytes - uint mask = 256 ** (32 - len) - 1; - assembly { - let srcpart := and(mload(src), not(mask)) - let destpart := and(mload(dest), mask) - mstore(dest, or(destpart, srcpart)) - } - } - - // Returns the memory address of the first byte of the first occurrence of - // `needle` in `self`, or the first byte after `self` if not found. - function findPtr(uint selflen, uint selfptr, uint needlelen, uint needleptr) private returns (uint) { - uint ptr; - uint idx; - - if (needlelen <= selflen) { - if (needlelen <= 32) { - // Optimized assembly for 68 gas per byte on short strings - assembly { - let mask := not(sub(exp(2, mul(8, sub(32, needlelen))), 1)) - let needledata := and(mload(needleptr), mask) - let end := add(selfptr, sub(selflen, needlelen)) - ptr := selfptr - loop: - jumpi(exit, eq(and(mload(ptr), mask), needledata)) - ptr := add(ptr, 1) - jumpi(loop, lt(sub(ptr, 1), end)) - ptr := add(selfptr, selflen) - exit: - } - return ptr; - } else { - // For long needles, use hashing - bytes32 hash; - assembly { hash := sha3(needleptr, needlelen) } - ptr = selfptr; - for (idx = 0; idx <= selflen - needlelen; idx++) { - bytes32 testHash; - assembly { testHash := sha3(ptr, needlelen) } - if (hash == testHash) - return ptr; - ptr += 1; - } - } - } - return selfptr + selflen; - } - - /* - function length(string _self) internal returns (uint _ret) { - _ret = bytes(_self).length; -} -*/ - - function compare(string _self, string _str) internal returns (int8 _ret) { - for (uint i=0; i bytes(_str)[i]) { - return 1; - } else if (bytes(_self)[i] < bytes(_str)[i]) { - return -1; - } - } - - if (bytes(_self).length > bytes(_str).length) { - return 1; - } if (bytes(_self).length < bytes(_str).length) { - return -1; - } else { - return 0; - } - } - - function compareNoCase(string _self, string _str) internal returns (int8 _ret) { - for (uint i=0; i= 'a' && ch1 <='z' && ch2 >= 'a' && ch2 <='z') { - if (ch1 > ch2) { - return 1; - } else if (ch1 < ch2) { - return -1; - } - } else { - if (bytes(_self)[i] > bytes(_str)[i]) { - return 1; - } else if (bytes(_self)[i] < bytes(_str)[i]) { - return -1; - } - } - } - - if (bytes(_self).length > bytes(_str).length) { - return 1; - } if (bytes(_self).length < bytes(_str).length) { - return -1; - } else { - return 0; - } - } - - function equals(string _self, string _str) internal returns (bool _ret) { - if (bytes(_self).length != bytes(_str).length) { - return false; - } - - for (uint i=0; i= 'a' && ch1 <='z' && ch2 >= 'a' && ch2 <='z') { - if (ch1 != ch2) { - return false; - } - } else { - if (bytes(_self)[i] != bytes(_str)[i]) { - return false; - } - } - } - - return true; - } - - function substr(string _self, uint _start, uint _len) internal returns (string _ret) { - if (_len > bytes(_self).length-_start) { - _len = bytes(_self).length-_start; - } - - if (_len <= 0) { - _ret = ""; - return; - } - - _ret = new string(_len); - - uint selfptr; - uint retptr; - assembly { - selfptr := add(_self, 0x20) - retptr := add(_ret, 0x20) - } - - memcpy(retptr, selfptr+_start, _len); - } - - function concat(string _self, string _str) internal returns (string _ret) { - _ret = new string(bytes(_self).length + bytes(_str).length); - - uint selfptr; - uint strptr; - uint retptr; - assembly { - selfptr := add(_self, 0x20) - strptr := add(_str, 0x20) - retptr := add(_ret, 0x20) - } - - memcpy(retptr, selfptr, bytes(_self).length); - memcpy(retptr+bytes(_self).length, strptr, bytes(_str).length); - } - - function concat(string _self, string _str1, string _str2) - internal returns (string _ret) { - _ret = new string(bytes(_self).length + bytes(_str1).length + bytes(_str2).length); - - uint selfptr; - uint str1ptr; - uint str2ptr; - uint retptr; - assembly { - selfptr := add(_self, 0x20) - str1ptr := add(_str1, 0x20) - str2ptr := add(_str2, 0x20) - retptr := add(_ret, 0x20) - } - - uint pos = 0; - memcpy(retptr+pos, selfptr, bytes(_self).length); - pos += bytes(_self).length; - memcpy(retptr+pos, str1ptr, bytes(_str1).length); - pos += bytes(_str1).length; - memcpy(retptr+pos, str2ptr, bytes(_str2).length); - pos += bytes(_str2).length; - } - - function concat(string _self, string _str1, string _str2, string _str3) - internal returns (string _ret) { - _ret = new string(bytes(_self).length + bytes(_str1).length + bytes(_str2).length - + bytes(_str3).length); - - uint selfptr; - uint str1ptr; - uint str2ptr; - uint str3ptr; - uint retptr; - assembly { - selfptr := add(_self, 0x20) - str1ptr := add(_str1, 0x20) - str2ptr := add(_str2, 0x20) - str3ptr := add(_str3, 0x20) - retptr := add(_ret, 0x20) - } - - uint pos = 0; - memcpy(retptr+pos, selfptr, bytes(_self).length); - pos += bytes(_self).length; - memcpy(retptr+pos, str1ptr, bytes(_str1).length); - pos += bytes(_str1).length; - memcpy(retptr+pos, str2ptr, bytes(_str2).length); - pos += bytes(_str2).length; - memcpy(retptr+pos, str3ptr, bytes(_str3).length); - pos += bytes(_str3).length; - } - - function trim(string _self) internal returns (string _ret) { - uint i; - uint8 ch; - for (i=0; i0; --i) { - ch = uint8(bytes(_self)[i-1]); - if (!(ch == 0x20 || ch == 0x09 || ch == 0x0D || ch == 0x0A)) { - break; - } - } - uint end = i; - - _ret = new string(end-start); - - uint selfptr; - uint retptr; - assembly { - selfptr := add(_self, 0x20) - retptr := add(_ret, 0x20) - } - - memcpy(retptr, selfptr+start, end-start); - } - - function trim(string _self, string _chars) internal returns (string _ret) { - uint16 i; - uint16 j; - bool matched; - for (i=0; i0; --i) { - matched = false; - for (j=0; j 0) { - ptr = findPtr(bytes(_self).length-pos, selfptr+pos, bytes(_delim).length, delimptr) - selfptr; - - if (ptr < bytes(_self).length) { - found = true; - } else { - ptr = bytes(_self).length; - } - } else { - ptr = bytes(_self).length; - } - - string memory elem = new string(ptr-pos); - uint elemptr; - assembly { - elemptr := add(elem, 0x20) - } - memcpy(elemptr, selfptr+pos, ptr-pos); - pos = ptr + bytes(_delim).length; - _array.push(elem); - - if (!found) { - break; - } - } - } - - function indexOf(string _self, string _str) internal returns (int _ret) { - uint selfptr; - uint strptr; - assembly { - selfptr := add(_self, 0x20) - strptr := add(_str, 0x20) - } - - uint ptr = findPtr(bytes(_self).length, selfptr, bytes(_str).length, strptr) - selfptr; - if (ptr < bytes(_self).length) { - _ret = int(ptr); - } else { - _ret = -1; - } - } - - function indexOf(string _self, string _str, uint pos) internal returns (int _ret) { - uint selfptr; - uint strptr; - assembly { - selfptr := add(_self, 0x20) - strptr := add(_str, 0x20) - } - - uint ptr = findPtr(bytes(_self).length-pos, selfptr+pos, bytes(_str).length, strptr) - selfptr; - if (ptr < bytes(_self).length) { - _ret = int(ptr); - } else { - _ret = -1; - } - } - - function toInt(string _self) internal returns (int _ret) { - _ret = 0; - if (bytes(_self).length == 0) { - return; - } - - uint16 i; - uint8 digit; - for (i=0; i= 0x30 && digit <= 0x39)) { - return; - } - _ret = _ret*10 + int(digit-0x30); - } - - if (!positive) { - _ret = -_ret; - } - } - - function toAddress(string _self) internal returns (address _ret) { - uint16 i; - uint8 digit; - for (i=0; i= 0x30 && digit <= 0x39) //'0'-'9' - digit -= 0x30; - else if (digit|0x20 >= 0x61 && digit|0x20 <= 0x66) //'a'-'f' - digit = digit-0x61+10; - else - return address(0); - - addr = addr*16+digit; - } - - return address(addr); - } - - function toKeyValue(string _self, string _key) internal returns (string _ret) { - _ret = new string(bytes(_self).length + bytes(_key).length + 5); - - uint selfptr; - uint keyptr; - uint retptr; - assembly { - selfptr := add(_self, 0x20) - keyptr := add(_key, 0x20) - retptr := add(_ret, 0x20) - } - - uint pos = 0; - - bytes(_ret)[pos++] = '"'; - memcpy(retptr+pos, keyptr, bytes(_key).length); - pos += bytes(_key).length; - bytes(_ret)[pos++] = '"'; - - bytes(_ret)[pos++] = ':'; - - bytes(_ret)[pos++] = '"'; - memcpy(retptr+pos, selfptr, bytes(_self).length); - pos += bytes(_self).length; - bytes(_ret)[pos++] = '"'; - } - - function toKeyValue(string[] storage _self, string _key) internal returns (string _ret) { - uint len = bytes(_key).length+5; - for (uint i=0; i<_self.length; ++i) { - if (i < _self.length-1) - len += bytes(_self[i]).length+3; - else - len += bytes(_self[i]).length+2; - } - - _ret = new string(len); - - uint pos = 0; - - bytes(_ret)[pos++] = '"'; - for (uint j=0; j= 0x30 && digit <= 0x39)) { - if (!positive) { - _ret = -_ret; - } - return; - } - _ret = _ret*10 + int(digit-0x30); - } - - if (!positive) { - _ret = -_ret; - } -} - -function getArrayValueByKey(string _self, string _key) internal returns (string _ret) { - int pos = -1; - uint searchStart = 0; - while (true) { - pos = _self.indexOf("\"".concat(_key, "\""), searchStart); - if (pos == -1) { - pos = _self.indexOf("'".concat(_key, "'"), searchStart); - if (pos == -1) { - return; - } - } - - pos += int(bytes(_key).length+2); - - bool colon = false; - while (uint(pos) < bytes(_self).length) { - if (bytes(_self)[uint(pos)] == ' ' || bytes(_self)[uint(pos)] == '\t' - || bytes(_self)[uint(pos)] == '\r' || bytes(_self)[uint(pos)] == '\n') { - pos++; - } else if (bytes(_self)[uint(pos)] == ':') { - pos++; - colon = true; - break; - } else { - break; - } - } - - if(uint(pos) == bytes(_self).length) { - return; - } - - if (colon) { - break; - } else { - searchStart = uint(pos); - } - } - - int start = _self.indexOf("[", uint(pos)); - if (start == -1) { - return; - } - start += 1; - - int end = _self.indexOf("]", uint(pos)); - if (end == -1) { - return; - } - - _ret = _self.substr(uint(start), uint(end-start)); -} - -function getArrayValueByKey(string _self, string _key, string[] storage _array) internal { - //Why can not use delete _array? - for (uint i=0; i<_array.length; ++i) { - delete _array[i]; - } - _array.length = 0; - - int pos = -1; - uint searchStart = 0; - while (true) { - pos = _self.indexOf("\"".concat(_key, "\""), searchStart); - if (pos == -1) { - pos = _self.indexOf("'".concat(_key, "'"), searchStart); - if (pos == -1) { - return; - } - } - - pos += int(bytes(_key).length+2); - - bool colon = false; - while (uint(pos) < bytes(_self).length) { - if (bytes(_self)[uint(pos)] == ' ' || bytes(_self)[uint(pos)] == '\t' - || bytes(_self)[uint(pos)] == '\r' || bytes(_self)[uint(pos)] == '\n') { - pos++; - } else if (bytes(_self)[uint(pos)] == ':') { - pos++; - colon = true; - break; - } else { - break; - } - } - - if(uint(pos) == bytes(_self).length) { - return; - } - - if (colon) { - break; - } else { - searchStart = uint(pos); - } - } - - int start = _self.indexOf("[", uint(pos)); - if (start == -1) { - return; - } - start += 1; - - int end = _self.indexOf("]", uint(pos)); - if (end == -1) { - return; - } - - string memory vals = _self.substr(uint(start), uint(end-start)).trim(" \t\r\n"); - - if (bytes(vals).length == 0) { - return; - } - - vals.split(",", _array); - - for (i=0; i<_array.length; ++i) { - _array[i] = _array[i].trim(" \t\r\n"); - _array[i] = _array[i].trim("'\""); - } -} - -function keyExists(string _self, string _key) internal returns (bool _ret) { - int pos = _self.indexOf("\"".concat(_key, "\"")); - if (pos == -1) { - pos = _self.indexOf("'".concat(_key, "'")); - if (pos == -1) { - return false; - } - } - - return true; -} - -function storageToUint(string _self) internal returns (uint _ret) { - uint len = bytes(_self).length; - if (len > 32) { - len = 32; - } - - _ret = 0; - for(uint i=0; i= 'a' && bytes(_self)[i] <= 'z') { - bytes(_self)[i] &= ~0x20; - } - } - - _ret = _self; -} - -function toLower(string _self) internal returns (string _ret) { - for (uint i=0; i= 'A' && bytes(_self)[i] <= 'Z') { - bytes(_self)[i] |= 0x20; - } - } - - _ret = _self; -} - - function bytes32ToString(bytes32 x) internal constant returns (string) - { - bytes memory bytesString = new bytes(32); - uint charCount = 0; - for (uint j = 0; j < 32; j++) - { - - byte char = byte(bytes32(uint(x) * 2 ** (8 * j))); - if (char != 0) - { - - bytesString[charCount] = char; - charCount++; - } - } - bytes memory bytesStringTrimmed = new bytes(charCount); - for (j = 0; j < charCount; j++) - { - - bytesStringTrimmed[j] = bytesString[j]; - } - return string(bytesStringTrimmed); - } - - } diff --git a/smartcontract/contracts/lib/LibTradeMap.sol b/smartcontract/contracts/lib/LibTradeMap.sol deleted file mode 100644 index 7ea0309..0000000 --- a/smartcontract/contracts/lib/LibTradeMap.sol +++ /dev/null @@ -1,143 +0,0 @@ -pragma solidity ^0.4.11; -import "./LibString.sol"; -import "./LibArray.sol"; -import "./StructTrade.sol"; -import "./StructMarket.sol"; -library LibTradeMap -{ - using LibString for *; - struct TradeMap - { - mapping(uint => StructTrade.value) data; - uint[] keyIndex; - } - - function insert(TradeMap storage self, uint k, StructTrade.value v) internal returns (bool replaced) - { - replaced = true; - if(self.data[k].user_id_ == "") - { - self.keyIndex.push(k); - replaced = false; - } - self.data[k] = v; - } - function insert(TradeMap storage self, uint k, uint trade_date, bytes32 opp_user_id, bytes32 user_id,bytes32 bs, uint confirm_qty, StructMarket.value storage market_value) internal returns (bool replaced) - { - replaced = true; - if(self.data[k].user_id_ == "") - { - self.keyIndex.push(k); - replaced = false; - } - self.data[k].trade_date_ = trade_date; - self.data[k].trade_id_ = k; - self.data[k].sheet_id_ = market_value.sheet_id_; - self.data[k].bs_ = bs; - self.data[k].price_ = market_value.price_; - self.data[k].trade_qty_ = confirm_qty; - //uint fee_; //手续费 - //uint transfer_money_; //已划货款 - //uint remainder_money_; //剩余货款 - self.data[k].opp_id_ = opp_user_id; - self.data[k].user_id_ = user_id;//TODO 根据买卖标志取自己的id - //string trade_state_; //交收状态 - //string trade_type_ //交易方式 - } - - /* - function update(TradeMap storage self, uint k, uint all_amount, uint available_amount, uint frozen_amount) internal - { - self.data[k].all_amount_ = all_amount; - self.data[k].available_amount_ = available_amount; - self.data[k].frozen_amount_ = frozen_amount; - } - */ - function remove(TradeMap storage self, uint k) internal returns (bool existed) - { - if(self.data[k].user_id_ == "") - { - return false; - } - else - { - delete self.data[k]; - LibArray.deleteElement(self.keyIndex, k); - return true; - } - } - function getValue(TradeMap storage self, uint k) internal returns (StructTrade.value) - { - return self.data[k]; - } - - function isExisted(TradeMap storage self, uint k) internal returns (bool existed) - { - existed = LibArray.isExisted(self.keyIndex, k); - } - - function empty(TradeMap storage self) internal returns (bool) - { - return (self.keyIndex.length == 0); - } - function it_start(TradeMap storage self) internal returns (uint) - { - if(empty(self)) - return uint(-1); - return 0; - } - function it_next(TradeMap storage self, uint it) internal returns (uint) - { - it++; - if(it < self.keyIndex.length) - return it; - else - return uint(-1); - } - - function it_valid(uint it) internal returns (bool) - { - if( (uint(-1) != it) ) - { - return true; - } - else - { - return false; - } - } - function it_valid(TradeMap storage, uint it) internal returns (bool) - { - if( (uint(-1) != it) ) - { - return true; - } - else - { - return false; - } - } - function getValueByIndex(TradeMap storage self, uint it) internal returns (StructTrade.value) - { - if(it + 1 > self.keyIndex.length) - { - StructTrade.value memory empty_value; - return empty_value; - } - return self.data[ self.keyIndex[it] ]; - } - /* - function getKey(TradeMap storage self, uint it) internal returns (bytes32) - { - return self.keyIndex[it]; - } - */ - function size(TradeMap storage self) internal returns(uint) - { - return self.keyIndex.length; - } - function length(TradeMap storage self) internal returns(uint) - { - return self.keyIndex.length; - } -} diff --git a/smartcontract/contracts/lib/LibUserAddrMap.sol b/smartcontract/contracts/lib/LibUserAddrMap.sol deleted file mode 100644 index 14e562a..0000000 --- a/smartcontract/contracts/lib/LibUserAddrMap.sol +++ /dev/null @@ -1,114 +0,0 @@ -pragma solidity ^0.4.5; -import "./LibString.sol"; -import "./LibArray.sol"; -import "./StructUserAddr.sol"; -library LibUserAddrMap -{ - using LibString for *; - struct UserAddrMap - { - mapping(bytes32 => StructUserAddr.value) data; - bytes32[] keyIndex; - } - - function insert(UserAddrMap storage self, bytes32 k, StructUserAddr.value v) internal returns (bool replaced) - { - replaced = true; - if(self.data[k].user_auth_ == 0) - { - self.keyIndex.push(k); - replaced = false; - } - self.data[k] = v; - } - function remove(UserAddrMap storage self, bytes32 k) internal returns (bool existed) - { - if(self.data[k].user_auth_ == 0) - { - return false; - } - else - { - delete self.data[k]; - LibArray.deleteElement(self.keyIndex, k); - return true; - } - } - function getValue(UserAddrMap storage self, bytes32 k) internal returns (StructUserAddr.value) - { - return self.data[k]; - } - - //user_id 是否存在 - function isExisted(UserAddrMap storage self, bytes32 k) internal returns (bool existed) - { - existed = LibArray.isExisted(self.keyIndex, k); - } - - function empty(UserAddrMap storage self) internal returns (bool) - { - return (self.keyIndex.length == 0); - } - function it_start(UserAddrMap storage self) internal returns (uint) - { - if(empty(self)) - return uint(-1); - return 0; - } - function it_next(UserAddrMap storage self, uint it) internal returns (uint) - { - it++; - if(it < self.keyIndex.length) - return it; - else - return uint(-1); - } - - function it_valid(uint it) internal returns (bool) - { - if( (uint(-1) != it) ) - { - return true; - } - else - { - return false; - } - } - function it_valid(UserAddrMap storage, uint it) internal returns (bool) - { - if( (uint(-1) != it) ) - { - return true; - } - else - { - return false; - } - } - function getValue(UserAddrMap storage self, uint it) internal returns (StructUserAddr.value) - { - if(it + 1 > self.keyIndex.length) - { - StructUserAddr.value empty_value; - return empty_value; - } - return self.data[ self.keyIndex[it] ]; - } - function getKey(UserAddrMap storage self, uint it) internal returns (bytes32) - { - if(it + 1 > self.keyIndex.length) - { - return ""; - } - return self.keyIndex[it]; - } - function size(UserAddrMap storage self) internal returns(uint) - { - return self.keyIndex.length; - } - function length(UserAddrMap storage self) internal returns(uint) - { - return self.keyIndex.length; - } -} diff --git a/smartcontract/contracts/lib/StructAddressInfo.sol b/smartcontract/contracts/lib/StructAddressInfo.sol deleted file mode 100644 index 94bea70..0000000 --- a/smartcontract/contracts/lib/StructAddressInfo.sol +++ /dev/null @@ -1,7 +0,0 @@ -pragma solidity ^0.4.11; -library StructAddressInfo { - struct value - { - address addr; - } -} diff --git a/smartcontract/contracts/lib/StructFunds.sol b/smartcontract/contracts/lib/StructFunds.sol deleted file mode 100644 index 6c0eef0..0000000 --- a/smartcontract/contracts/lib/StructFunds.sol +++ /dev/null @@ -1,12 +0,0 @@ -pragma solidity^0.4.11; - -library StructFunds -{ - struct value - { - uint total_funds_; - uint ava_funds_; - uint frozen_funds_; - } -} - diff --git a/smartcontract/contracts/lib/StructMarket.sol b/smartcontract/contracts/lib/StructMarket.sol deleted file mode 100644 index 729e128..0000000 --- a/smartcontract/contracts/lib/StructMarket.sol +++ /dev/null @@ -1,23 +0,0 @@ -pragma solidity ^0.4.11; -library StructMarket { - struct value - { - uint date_; //挂牌日期 - uint market_id_; //挂牌编号 - uint sheet_id_; //仓单编号 - bytes32 class_id_; //品种代码 - bytes32 make_date_; //产期 - bytes32 lev_id_; //等级 - bytes32 wh_id_; //仓库代码 - bytes32 place_id_; //产地代码 - bytes32 type_; //报价类型 - uint price_; //价格(代替浮点型) - uint list_qty_; //挂牌量 - uint deal_qty_; //成交量 - uint rem_qty_; //剩余量 - bytes32 deadline_; //挂牌截止日 - uint dlv_unit_; //交割单位 - bytes32 user_id_; //用户id - address seller_addr_; //卖方地址 - } -} diff --git a/smartcontract/contracts/lib/StructSheet.sol b/smartcontract/contracts/lib/StructSheet.sol deleted file mode 100644 index 9935a06..0000000 --- a/smartcontract/contracts/lib/StructSheet.sol +++ /dev/null @@ -1,16 +0,0 @@ -pragma solidity ^0.4.11; -library StructSheet{ - struct value - { - bytes32 user_id_; //客户id - uint sheet_id_; //仓单序号 - bytes32 class_id_; //品种id - bytes32 make_date_; //产期 - bytes32 lev_id_; //等级 - bytes32 wh_id_; //仓库代码 - bytes32 place_id_; //产地代码 - uint all_amount_; //仓单总量 - uint available_amount_;//可用数量 - uint frozen_amount_; //冻结数量 - } -} diff --git a/smartcontract/contracts/lib/StructTrade.sol b/smartcontract/contracts/lib/StructTrade.sol deleted file mode 100644 index eacd16f..0000000 --- a/smartcontract/contracts/lib/StructTrade.sol +++ /dev/null @@ -1,20 +0,0 @@ -pragma solidity ^0.4.11; -library StructTrade { - //合同数据结构 - struct value - { - uint trade_date_; //合同日期 - uint trade_id_; //合同编号 - uint sheet_id_; //仓单编号 - bytes32 bs_; //买卖 - uint price_; //价格 - uint trade_qty_; //合同量 - //uint fee_; //手续费 - //uint transfer_money_; //已划货款 - //uint remainder_money_; //剩余货款 - bytes32 user_id_; //己方id - bytes32 opp_id_; //对手方id - //string trade_state_; //交收状态 - //string trade_type_ //交易方式 - } -} diff --git a/smartcontract/contracts/lib/StructUserAddr.sol b/smartcontract/contracts/lib/StructUserAddr.sol deleted file mode 100644 index 4d8dade..0000000 --- a/smartcontract/contracts/lib/StructUserAddr.sol +++ /dev/null @@ -1,10 +0,0 @@ -pragma solidity ^0.4.11; -library StructUserAddr{ - struct value - { - address extern_addr_; - address agent_addr_; - bytes32 user_id_; - int user_auth_; - } -} diff --git a/smartcontract/init.js b/smartcontract/init.js deleted file mode 100644 index 0897936..0000000 --- a/smartcontract/init.js +++ /dev/null @@ -1,105 +0,0 @@ -//可配置 -var god_account= "0x4c34655804601ee05028726a7e0b5669b52b25a3"; //root -var admin_extern_addr = "0x4c34655804601ee05028726a7e0b5669b52b25a3"; - -//不要改动 -var market_name ="Market"; -var create_id_name ="CreateID"; -var user_list_name ="UserList"; -var admin_name ="Admin"; - -// Step 1: Get a contract into my application -var UserList_json = require("./build/contracts/UserList.json"); -var ContractAddress_json = require("./build/contracts/ContractAddress.json"); -var Market_json = require("./build/contracts/Market.json"); -var CreateID_json = require("./build/contracts/CreateID.json"); -var Login_json = require("./build/contracts/Login.json"); -var Admin_json = require("./build/contracts/Admin.json"); -var Proxy_json = require("./build/contracts/Proxy.json"); - -var Web3 = require('web3'); - -// Step 2: Turn that contract into an abstraction I can use -var contract = require("truffle-contract"); -var UserList = contract(UserList_json); -var ContractAddress = contract(ContractAddress_json); -var Market = contract(Market_json); -var CreateID = contract(CreateID_json); -var Login = contract(Login_json); -var Admin = contract(Admin_json); -var Proxy = contract(Proxy_json); - -// Step 3: Provision the contract with a web3 provider -var web3 = new Web3.providers.HttpProvider("http://192.168.22.123:8545"); -UserList.setProvider(web3); -ContractAddress.setProvider(web3); -Market.setProvider(web3); -CreateID.setProvider(web3); -Login.setProvider(web3); -Admin.setProvider(web3); -Proxy.setProvider(web3); - -// Step 4: Use the contract! -let UserList_instance; -let ContractAddress_instance; -let Market_instance; -let CreateID_instance; -let Login_instance; -let Admin_instance; -let Proxy_instance; - -async function initAddress() -{ - console.log("Start:获取系统合约的地址...."); - UserList_instance = await UserList.deployed(); - console.log("\tUserList Address:"+UserList_instance.address); - ContractAddress_instance = await ContractAddress.deployed(); - console.log("\tContractAddress Address:"+ContractAddress_instance.address); - Market_instance = await Market.deployed(); - console.log("\tMarket Address:"+Market_instance.address); - CreateID_instance = await CreateID.deployed(); - console.log("\tCreateID Address:"+CreateID_instance.address); - Login_instance = await Login.deployed(); - console.log("\tLogin Address:"+Login_instance.address); - Admin_instance = await Admin.deployed(); - console.log("\tAdmin Address:"+Admin_instance.address); - Proxy_instance = await Proxy.deployed(); - console.log("\tProxy Address:"+Proxy_instance.address); - console.log("End:...获取系统合约地址结束"); - -} -async function setDep() -{ - console.log("Start:设置合约依赖关系...."); - console.log("\t设置ContractAddress合约"); - await ContractAddress_instance.setContractAddress.sendTransaction(market_name,Market_instance.address,{from:god_account}); - await ContractAddress_instance.setContractAddress.sendTransaction(create_id_name,CreateID_instance.address,{from:god_account}); - await ContractAddress_instance.setContractAddress.sendTransaction(user_list_name,UserList_instance.address,{from:god_account}); - await ContractAddress_instance.setContractAddress.sendTransaction(admin_name,Admin_instance.address,{from:god_account}); - - console.log("\t设置Market合约"); - await Market_instance.setContractAddress.sendTransaction(ContractAddress_instance.address,{from:god_account}); - await Market_instance.setCreateIDName.sendTransaction(create_id_name,{from:god_account}); - await Market_instance.setUserListName.sendTransaction(user_list_name,{from:god_account}); - - console.log("\t设置Login合约"); - await Login_instance.init.sendTransaction(ContractAddress_instance.address,user_list_name,{from:god_account}); - - console.log("\t设置Admin合约"); - await Admin_instance.init.sendTransaction(ContractAddress_instance.address,user_list_name,{from:god_account,gas:9999999}); - - console.log("\t设置Proxy合约"); - await Proxy_instance.init.sendTransaction(ContractAddress_instance.address,{from:god_account,gas:9999999}); - console.log("End:...设置合约依赖关系完毕"); -} -async function setAdminExternAddr() -{ - console.log("Start:设置Admin合约外部账户地址...."); - await UserList_instance.addUser.sendTransaction(admin_extern_addr,Admin_instance.address,admin_name,"5",{from:god_account,gas:9999999}); - console.log("\tAdmin合约的外部账户地址:"+admin_extern_addr); - console.log("End:...设置Admin合约外部账户地址完毕"); -} -initAddress().then( async function(){ - await setDep(); - await setAdminExternAddr(); -}); diff --git a/smartcontract/migrations/1_initial_migration.js b/smartcontract/migrations/1_initial_migration.js deleted file mode 100755 index 4d5f3f9..0000000 --- a/smartcontract/migrations/1_initial_migration.js +++ /dev/null @@ -1,5 +0,0 @@ -var Migrations = artifacts.require("./Migrations.sol"); - -module.exports = function(deployer) { - deployer.deploy(Migrations); -}; diff --git a/smartcontract/migrations/2_deploy_contracts.js b/smartcontract/migrations/2_deploy_contracts.js deleted file mode 100755 index 7bca44f..0000000 --- a/smartcontract/migrations/2_deploy_contracts.js +++ /dev/null @@ -1,75 +0,0 @@ -//independ -var CreateID = artifacts.require("CreateID"); - -//struct -var StructAddressInfo = artifacts.require("StructAddressInfo"); -var StructMarket = artifacts.require("StructMarket"); -var StructUserAddr = artifacts.require("StructUserAddr"); -var StructSheet = artifacts.require("StructSheet"); -var StructTrade = artifacts.require("StructTrade"); - -//lib -var LibArray = artifacts.require("LibArray"); -var LibString = artifacts.require("LibString"); -var LibAddressMap = artifacts.require("LibAddressMap"); -var LibMarketMap = artifacts.require("LibMarketMap"); -var LibUserAddrMap = artifacts.require("LibUserAddrMap"); -var LibSheetMap = artifacts.require("LibSheetMap"); -var LibTradeMap = artifacts.require("LibTradeMap"); - -//contract -var ContractAddress = artifacts.require("ContractAddress"); -var User = artifacts.require("User"); -var UserList = artifacts.require("UserList"); -var Market = artifacts.require("Market"); -var Proxy = artifacts.require("Proxy"); -var Login = artifacts.require("Login"); -var Admin = artifacts.require("Admin"); - -module.exports = function(deployer) { - - //independ - deployer.deploy(CreateID); - - //struct - deployer.deploy(StructAddressInfo); - deployer.deploy(StructMarket); - deployer.deploy(StructUserAddr); - deployer.deploy(StructSheet); - deployer.deploy(StructTrade); - - //lib - deployer.deploy(LibString); - deployer.deploy(LibArray); - deployer.deploy(LibAddressMap); - deployer.deploy(LibMarketMap); - deployer.deploy(LibUserAddrMap); - deployer.deploy(LibSheetMap); - deployer.deploy(LibTradeMap); - - //befor deploy contract. link lib. - deployer.link(StructAddressInfo, [ContractAddress]); - deployer.link(LibArray, [ContractAddress, Market, UserList]); - deployer.link(LibString, [ContractAddress,Admin]); - deployer.link(LibAddressMap, [ContractAddress]); - - deployer.link(StructMarket, [Market]); - deployer.link(LibMarketMap, [Market]); - - deployer.link(StructUserAddr, [UserList]); - deployer.link(LibUserAddrMap, [UserList]); - - deployer.link(StructSheet, [User]); - deployer.link(LibSheetMap, [User,Admin]); - deployer.link(StructTrade, [User]); - deployer.link(LibTradeMap, [User]); - - //contract - deployer.deploy(ContractAddress); - deployer.deploy(Market); - deployer.deploy(UserList); - deployer.deploy(Admin); - deployer.deploy(CreateID); - deployer.deploy(Proxy); - deployer.deploy(Login); -}; diff --git a/smartcontract/test/TestAdmin.sol b/smartcontract/test/TestAdmin.sol deleted file mode 100644 index f324128..0000000 --- a/smartcontract/test/TestAdmin.sol +++ /dev/null @@ -1,176 +0,0 @@ -pragma solidity ^0.4.11; - -import "truffle/Assert.sol"; -import "../contracts/Admin.sol"; -import "../contracts/User.sol"; - -contract TestAdmin -{ - ContractAddress contract_addr; - UserList user_list; - Market market; - CreateID create_id; - Admin admin; - User user_a; - User user_b; - string market_name; - string create_id_name; - string user_list_name; - string admin_name; - - bytes32 user_a_id; - bytes32 user_b_id; - - uint sheet_id; - uint all_amount; - uint available_amount; - uint frozen_amount; - - - - function beforeEach() - { - sheet_id = 1; - all_amount = 60; - available_amount = 40; - frozen_amount = 20; - - - user_a = new User(); - user_b = new User(); - user_list = new UserList(); - contract_addr = new ContractAddress(); - market = new Market(); - create_id = new CreateID(); - admin = new Admin(); - - market_name = "market"; - create_id_name = "create_id"; - user_list_name = "user_list"; - admin_name = "admin"; - user_a_id = "I am user a"; - user_b_id = "I am user b"; - - contract_addr.setContractAddress(market_name, market); - contract_addr.setContractAddress(create_id_name, create_id); - contract_addr.setContractAddress(user_list_name, user_list); - contract_addr.setContractAddress(admin_name, admin); - - market.setContractAddress(contract_addr); - market.setCreateIDName(create_id_name); - market.setUserListName(user_list_name); - - user_list.addUser(user_a,user_a,user_a_id,1); - user_list.addUser(user_b,user_b,user_b_id,1); - - admin.init(contract_addr, user_list_name); - - user_a.setContractAddress(contract_addr); - user_a.setMarketName(market_name); - user_a.setCreateIDName(create_id_name); - user_a.setUserListName(user_list_name); - user_a.setUserID(user_a_id); - user_a.setCreateID(); - user_a.setAdmin(admin_name); - - user_b.setContractAddress(contract_addr); - user_b.setMarketName(market_name); - user_b.setCreateIDName(create_id_name); - user_b.setUserListName(user_list_name); - user_b.setUserID(user_b_id); - user_b.setCreateID(); - user_b.setAdmin(admin_name); - - } - - //测试摘牌 - function testDelistRequest() - { - - //user_a 挂牌 - uint sell_price = 100; - uint sell_qty = 10; - user_a.insertSheet(user_a_id,"SR","make_date","level_id","wh_id","产地",all_amount,frozen_amount,available_amount); - - user_a.insertFunds(1000); //初始化资金 - user_a.setFee(5); - - var ret_market_id = user_a.listRequest(user_a_id,sheet_id,sell_price,sell_qty); - - //user_b 摘牌 - user_b.insertFunds(1000); //初始化资金 - user_a.setFee(5); - uint buy_qty = 2; - var ret_delist = user_b.delistRequest(user_b_id, ret_market_id, buy_qty); - user_b.delistRequest(user_b_id, ret_market_id, buy_qty); - user_b.delistRequest(user_b_id, ret_market_id, buy_qty); - admin.confirmList(0); - admin.confirmList(1); - admin.confirmList(2); - var ret_b_funds = user_b.getAvaFunds(); - - Assert.equal(ret_market_id, 1, ""); - Assert.equal(ret_delist, 0, ""); - Assert.equal(market.getMarketNum(), 1, ""); - Assert.equal(user_a.getTradeNum(), 3, ""); - Assert.equal(user_b.getTradeNum(), 3, ""); - - Assert.equal(admin.getConfirmListReqSize(), 3, ""); - Assert.equal(user_a.getSheetAllAmount(sheet_id),54,""); - Assert.equal(user_a.getTotalFunds(), 157000, ""); - Assert.equal(user_a.getAvaFunds(), 157000, ""); - Assert.equal(user_b.getSheetAllAmount(2),6,""); - Assert.equal(user_b.getTotalFunds(), 40000, ""); - Assert.equal(user_b.getAvaFunds(), 40000, ""); - } - - //测试协商交易的管理员确认函数 - function testConfirmNeg() - { - int ret = 0; - uint sell_price = 100; - uint sell_qty = 6; - - //创建仓单 - user_a.insertSheet(user_a_id,"SR","make_date","level_id","wh_id","产地",all_amount,frozen_amount,available_amount); - user_a.insertFunds(1000); //初始化资金 - user_a.setFee(3); //设置手续费比率 - - //发送协商交易请求 - user_a.sendNegReq(sheet_id,sell_qty,sell_price,user_b_id); - - user_b.insertFunds(1000); //初始化资金 - user_b.setFee(3); //设置手续费比率 - - //同意协商交易 - ret = user_b.agreeNeg(user_b_id, 1); - //管理员确认 - admin.confirmNeg(0); - - //获取双方的合同数据 - var(a_length,a_ret_trade_id, a_ret_sheet_id, a_ret_bs, a_ret_opp_id) = user_a.getTradeMap(1); - var(b_length,b_ret_trade_id, b_ret_sheet_id, b_ret_bs, b_ret_opp_id) = user_b.getTradeMap(1); - - Assert.equal(ret, 0, "user_b.agreeNeg ret = 0"); - Assert.equal(a_length, 1, "a_length = 1"); - Assert.equal(a_ret_trade_id, 1, "a_ret_trade_id = 1"); - Assert.equal(a_ret_sheet_id, sheet_id, ""); - Assert.equal(a_ret_bs, "卖", ""); - Assert.equal(a_ret_opp_id, user_b_id,""); - Assert.equal(user_a.getTotalFunds(), 158200, ""); - Assert.equal(user_a.getSheetAllAmount(sheet_id), 54, ""); - - - Assert.equal(b_length, 1, "b_length = 1"); - Assert.equal(b_ret_trade_id, 1, "b_ret_trade_id = 1"); - Assert.equal(b_ret_sheet_id, sheet_id, ""); - Assert.equal(b_ret_bs, "买", " b_ret_bs "); - Assert.equal(b_ret_opp_id, user_a_id," b_ret_opp_id = I am user b "); - Assert.equal(user_b.getTotalFunds(), 40000, ""); - Assert.equal(user_b.getAvaFunds(), 40000, ""); - Assert.equal(user_b.getFrozenFunds(), 0, ""); - Assert.equal(user_b.getSheetAllAmount(2), 6, ""); - } - -} - diff --git a/smartcontract/test/TestContractAddress.sol b/smartcontract/test/TestContractAddress.sol deleted file mode 100644 index a730f84..0000000 --- a/smartcontract/test/TestContractAddress.sol +++ /dev/null @@ -1,83 +0,0 @@ -pragma solidity ^0.4.11; -import "truffle/Assert.sol"; -import "truffle/DeployedAddresses.sol"; -import "../contracts/ContractAddress.sol"; -import "../contracts/User.sol"; -contract TestContractAddress -{ - ContractAddress contract_address; - address addr; - address addr_1; - address addr_2; - string contract_name; - string contract_name_1; - string contract_name_2; - - function beforeEach() - { - contract_address = new ContractAddress(); - addr = new User(); - addr_1 = new User(); - addr_2 = new User(); - contract_name = "test"; - contract_name_1 = "test1"; - contract_name_2 = "test2"; - } - function testSetGet_normal() - { - contract_address.setContractAddress(contract_name, addr); - Assert.equal(contract_address.getContractAddress(contract_name), addr, ""); - } - function testGet_no_existed_key() - { - Assert.equal(contract_address.getContractAddress("no_existed_key"), 0x0, ""); - } - function testGetSize_empty_map() - { - Assert.equal(contract_address.getSize(), 0, ""); - } - function testGetSize_with_add_del_element() - { - contract_address.setContractAddress(contract_name, addr); //add - Assert.equal(contract_address.getSize(), 1, ""); - contract_address.delContractAddress(contract_name); //del - Assert.equal(contract_address.getSize(), 0, ""); - contract_address.delContractAddress(contract_name); //repeat del - Assert.equal(contract_address.getSize(), 0, ""); - } - function testTraverse_null_map() - { - uint size = contract_address.getSize(); - for(uint index = 0; index < size; ++index) - { - Assert.equal(true, false, "Impossible to trigger"); - } - } - function testTraverse_normal_map() - { - contract_address.setContractAddress(contract_name, addr); - contract_address.setContractAddress(contract_name_1, addr_1); - contract_address.setContractAddress(contract_name_2, addr_2); - uint size = contract_address.getSize(); - for(uint index = 0; index < size; ++index) - { - if(index == 0) - { - Assert.equal(contract_address.getAddressByIndex(index), addr, ""); - } - else if(index == 1) - { - Assert.equal(contract_address.getAddressByIndex(index), addr_1, ""); - } - else if(index == 2) - { - Assert.equal(contract_address.getAddressByIndex(index), addr_2, ""); - } - else - { - Assert.equal(true, false, "Impossible to trigger"); - } - } - Assert.equal(index, 3, ""); - } -} diff --git a/smartcontract/test/TestLogin.sol b/smartcontract/test/TestLogin.sol deleted file mode 100644 index 73de0e1..0000000 --- a/smartcontract/test/TestLogin.sol +++ /dev/null @@ -1,48 +0,0 @@ -pragma solidity ^0.4.11; - -import "truffle/Assert.sol"; -import "../contracts/Admin.sol"; -import "../contracts/Login.sol"; - - -contract TestLogin -{ - ContractAddress contract_addr; - UserList user_list; - Admin admin; - Login login; - string user_list_name; - - function beforeEach() - { - contract_addr = new ContractAddress(); - user_list = new UserList(); - admin = new Admin(); - login = new Login(); - - contract_addr.setContractAddress(user_list_name, user_list); - - admin.init(contract_addr, user_list_name); - login.init(contract_addr, user_list_name); - } - - function testVerfication() - { - admin.addUser("A", 0x123); - admin.addUser("B", 0x456); - admin.addUser("C", 0x789); - - var(ret,user_auth,user_addr_1) = login.verfication("B"); - var(ret_1,user_auth_1,user_addr_2) = login.verfication("B"); - var(ret_2,user_auth_2,user_addr_3) = login.verfication("D"); - - //Assert.equal(ret,0,""); - //Assert.equal(user_auth,1,""); - //公钥不正确 - Assert.equal(ret_1,-2,""); - Assert.equal(user_auth_1,0,""); - //use_id不存在 - Assert.equal(ret_2,-1,""); - Assert.equal(user_auth_2,0,""); - } -} diff --git a/smartcontract/test/TestMarket.sol b/smartcontract/test/TestMarket.sol deleted file mode 100644 index 1955792..0000000 --- a/smartcontract/test/TestMarket.sol +++ /dev/null @@ -1,165 +0,0 @@ -pragma solidity ^0.4.11; -import "truffle/Assert.sol"; -import "truffle/DeployedAddresses.sol"; -import "../contracts/ContractAddress.sol"; -import "../contracts/Market.sol"; -import "../contracts/CreateID.sol"; -import "../contracts/lib/StructMarket.sol"; -contract TestMarket -{ - Market market; - ContractAddress contract_address; - string create_id_name = "id_name"; - - // market element start - uint sheet_id = 5; - uint market_date = now; - bytes32 make_date = "2017XXXX"; - bytes32 class_id = "SR"; - bytes32 lev_id = "super"; - bytes32 wh_id = "ss"; - bytes32 place_id = "henan"; - bytes32 market_type = "a price"; - uint price = 1999; - uint list_qty = 10; - uint deal_qty = 5; - uint rem_qty = 0; - bytes32 deadline = "2018999"; - uint dlv_unit = 100; - bytes32 user_id = "userA"; - address seller_addr; - // market element end - StructMarket.value market_value; - - function beforeEach() - { - market = new Market(); - contract_address = new ContractAddress(); - CreateID create_id = new CreateID(); - - contract_address.setContractAddress(create_id_name, create_id); - - market.setContractAddress(contract_address); - market.setCreateIDName(create_id_name); - } - function testGetMarketID_one_time() - { - Assert.equal(market.getMarketID(), 1, ""); - } - function testGetMarketID_two_time() - { - Assert.equal(market.getMarketID(), 1, ""); - Assert.equal(market.getMarketID(), 2, ""); - } - function testInsertMarket() - { - market.insertMarket_1( - sheet_id, - class_id, - make_date, - lev_id, - wh_id, - place_id); - var (id,date) = market.insertMarket_2( - price, - list_qty, - deal_qty, - rem_qty, - deadline, - dlv_unit, - user_id); - Assert.equal(id, 1, ""); - Assert.equal(market.getMarketNum(), 1, ""); - } - function testGetMarket1_by_non_existed_market_id() - { - market.insertMarket_1( - sheet_id, - class_id, - make_date, - lev_id, - wh_id, - place_id); - var (id,date) = market.insertMarket_2( - price, - list_qty, - deal_qty, - rem_qty, - deadline, - dlv_unit, - user_id); - var(ret_date,ret_market_id,ret_sheet_id,ret_class_id,ret_make_date,ret_lev_id,ret_wh_id,ret_place_id,ret_price_type) - = market.getMarket_1(3333); //3333 is a non existed id - Assert.equal(ret_date, 0 , ""); - } - function testGetMarket2_by_non_existed_market_id() - { - market.insertMarket_1( - sheet_id, - class_id, - make_date, - lev_id, - wh_id, - place_id); - var (id,date) = market.insertMarket_2( - price, - list_qty, - deal_qty, - rem_qty, - deadline, - dlv_unit, - user_id); - var(ret_price,ret_list_qty,ret_deal_qty,ret_rem_qty,ret_deadline,ret_dlv_unit,ret_user_id,ret_sell_addr) - = market.getMarket_2(3334); //3334 is a non existed - Assert.equal(ret_price, 0 , ""); - } - function testGetMarketIdByIndex_by_non_existed_index() - { - var id = market.getMarketIdByIndex(3322); - Assert.equal(id, 0, ""); - } - function testGetMarketIdByIndex_by_existed_index() - { - market.insertMarket_1( - sheet_id, - class_id, - make_date, - lev_id, - wh_id, - place_id); - var (id,date) = market.insertMarket_2( - price, - list_qty, - deal_qty, - rem_qty, - deadline, - dlv_unit, - user_id); - Assert.equal(id, 1, ""); - var ret_id = market.getMarketIdByIndex(0); //first element - Assert.equal(id, ret_id, ""); - } - function testGetDynamicMarket_by_existed_market_id() - { - market.insertMarket_1( - sheet_id, - class_id, - make_date, - lev_id, - wh_id, - place_id); - var (id,date) = market.insertMarket_2( - price, - list_qty, - deal_qty, - rem_qty, - deadline, - dlv_unit, - user_id); - Assert.equal(id, 1, ""); - var(sell_user_id, ret_deal_qty, ret_rem_qty) = market.getDynamicMarket(id); //first element - Assert.equal(user_id, sell_user_id, ""); - Assert.equal(deal_qty, ret_deal_qty, ""); - Assert.equal(rem_qty, ret_rem_qty, ""); - } -} diff --git a/smartcontract/test/TestUser.sol b/smartcontract/test/TestUser.sol deleted file mode 100644 index b5b0787..0000000 --- a/smartcontract/test/TestUser.sol +++ /dev/null @@ -1,402 +0,0 @@ -pragma solidity ^0.4.11; -import "truffle/Assert.sol"; -import "truffle/DeployedAddresses.sol"; -import "../contracts/User.sol"; -import "../contracts/ContractAddress.sol"; -import "../contracts/Market.sol"; -import "../contracts/UserList.sol"; -import "../contracts/CreateID.sol"; -import "../contracts/Admin.sol"; - -contract TestUser -{ - User user;// {{{ - UserList user_list; - Market market; - Admin admin; - CreateID create_id; - ContractAddress contract_addr;// }}} - string market_name; - string create_id_name; - string user_list_name; - string admin_name; - bytes32 user_id; - uint sheet_id; - uint all_amount; - uint available_amount; - uint frozen_amount; - - User user_a; - User user_b; - bytes32 user_a_id; - bytes32 user_b_id; - - - function beforeEach() - { - - sheet_id = 1; - all_amount = 60; - available_amount = 40; - frozen_amount = 20; - - user = new User(); - user_a = new User(); - user_b = new User(); - user_list = new UserList(); - contract_addr = new ContractAddress(); - market = new Market(); - create_id = new CreateID(); - admin = new Admin(); - - market_name = "market"; - create_id_name = "create_id"; - user_list_name = "user_list"; - admin_name = "admin"; - user_id = "user"; - user_a_id = "I am user a"; - user_b_id = "I am user b"; - - contract_addr.setContractAddress(market_name, market); - contract_addr.setContractAddress(create_id_name, create_id); - contract_addr.setContractAddress(user_list_name, user_list); - contract_addr.setContractAddress(admin_name, admin); - - market.setContractAddress(contract_addr); - market.setCreateIDName(create_id_name); - market.setUserListName(user_list_name); - - user_list.addUser(user,user,user_id,1); - user_list.addUser(user_a,user_a,user_a_id,1); - user_list.addUser(user_b,user_b,user_b_id,1); - - user.setContractAddress(contract_addr); - user.setMarketName(market_name); - user.setCreateIDName(create_id_name); - user.setUserListName(user_list_name); - user.setUserID(user_id); - user.setAdmin(admin_name); - - user_a.setContractAddress(contract_addr); - user_a.setMarketName(market_name); - user_a.setCreateIDName(create_id_name); - user_a.setUserListName(user_list_name); - user_a.setUserID(user_a_id); - user_a.setCreateID(); - user_a.setAdmin(admin_name); - - user_b.setContractAddress(contract_addr); - user_b.setMarketName(market_name); - user_b.setCreateIDName(create_id_name); - user_b.setUserListName(user_list_name); - user_b.setUserID(user_b_id); - user_b.setCreateID(); - user_b.setAdmin(admin_name); - } - - function testInsertsheet_normal() - { - user.insertSheet(user_id,"SR","make_date","level_id","wh_id","产地",all_amount, frozen_amount, available_amount); - var(ret_all_amount, ret_available_amount, ret_frozen_amount) = user.getSheetAmount(sheet_id); - Assert.equal(all_amount, ret_all_amount, ""); - Assert.equal(available_amount, ret_available_amount, ""); - Assert.equal(frozen_amount, ret_frozen_amount, ""); - } - - function testFreeze_exceed_owned_sheet() - { - user.insertSheet(user_id,"SR","make_date","level_id","wh_id","产地",all_amount,frozen_amount,available_amount); - user.freeze(1, available_amount + 1); - var(ret_all_amount, ret_available_amount, ret_frozen_amount) = user.getSheetAmount(sheet_id); - Assert.equal(all_amount, ret_all_amount, ""); - Assert.equal(available_amount, ret_available_amount, ""); - Assert.equal(frozen_amount, ret_frozen_amount, ""); - } - function testGetMarketAddr() - { - address market_addr = contract_addr.getContractAddress(market_name); - Assert.equal(market_addr, market, ""); - } - - //测试挂牌 - function testListRequest_one_time() - { - uint sell_price = 100; - uint sell_qty = 6; - user.insertSheet(user_id,"SR","make_date","level_id","wh_id","产地",all_amount,frozen_amount,available_amount); - var ret_market_id = user.listRequest(user_id,sheet_id,sell_price,sell_qty); - var(ret_all_amount, ret_available_amount, ret_frozen_amount) = user.getSheetAmount(sheet_id); - - Assert.equal(ret_available_amount, available_amount - sell_qty, ""); - Assert.equal(ret_frozen_amount, frozen_amount + sell_qty, ""); - Assert.equal(market.getMarketNum(), 1, ""); - Assert.equal(user.getListReqNum(), 1, ""); - - } - function testListRequest_two_time() - { - uint sell_price = 100; - uint sell_qty = 6; - user.insertSheet(user_id,"SR","make_date","level_id","wh_id","产地",all_amount,frozen_amount,available_amount); - var ret_market_id = user.listRequest(user_id,sheet_id,sell_price,sell_qty); //one time - ret_market_id = user.listRequest(user_id,sheet_id,sell_price,sell_qty); //two time - var(ret_all_amount, ret_available_amount, ret_frozen_amount) = user.getSheetAmount(sheet_id); - Assert.equal(ret_available_amount, available_amount - sell_qty*2, ""); - Assert.equal(ret_frozen_amount, frozen_amount + sell_qty*2, ""); - Assert.equal(market.getMarketNum(), 2, ""); - Assert.equal(user.getListReqNum(), 2, ""); - } - - //测试摘牌 - function testDelistRequest_listqty_greater_delistqty() - { - //user_a 挂牌 - uint sell_price = 100; - uint sell_qty = 6; - user_a.insertSheet(user_a_id,"SR","make_date","level_id","wh_id","产地",all_amount,frozen_amount,available_amount); - user_a.insertFunds(1000); //初始化资金 - - var ret_market_id = user_a.listRequest(user_a_id,sheet_id,sell_price,sell_qty); - - //user_b 摘牌 - user_b.insertFunds(1000); //初始化资金 - - uint buy_qty = 2; - var ret_delist = user_b.delistRequest(user_b_id, ret_market_id, buy_qty); - var ret_a_funds = user_a.getTotalFunds(); - var ret_b_funds = user_b.getAvaFunds(); - - //Assert - Assert.equal(ret_market_id, 1, ""); - Assert.equal(ret_delist, 0, ""); - Assert.equal(market.getMarketNum(), 1, ""); - Assert.equal(user_a.getTradeNum(), 1, ""); - Assert.equal(user_b.getTradeNum(), 1, ""); - Assert.equal(ret_a_funds, 100000, ""); - Assert.equal(ret_b_funds, 80000, ""); - - } - - function testDelistRequest_listqty_equal_delistqty() - { - //user_a 挂牌 - uint sell_price = 100; - uint sell_qty = 6; - user_a.insertSheet(user_a_id,"SR","make_date","level_id","wh_id","产地",all_amount,frozen_amount,available_amount); - user_a.insertFunds(1000); //初始化资金 - - var ret_market_id = user_a.listRequest(user_a_id,sheet_id,sell_price,sell_qty); - - //user_b 摘牌 - uint buy_qty = 6; - user_b.insertFunds(1000); //初始化资金 - - var ret_delist = user_b.delistRequest(user_b_id, ret_market_id, buy_qty); - var ret_a_funds = user_a.getTotalFunds(); - var ret_b_funds = user_b.getAvaFunds(); - - //Assert - Assert.equal(ret_market_id, 1, ""); - Assert.equal(ret_delist, 0, ""); - Assert.equal(market.getMarketNum(), 0, ""); - Assert.equal(user_a.getTradeNum(), 1, ""); - Assert.equal(user_b.getTradeNum(), 1, ""); - Assert.equal(ret_a_funds, 100000, ""); - Assert.equal(ret_b_funds, 40000, ""); - } - - //测试挂牌管理员确认函数 挂牌量为6,摘牌量为2 - function testConfirmListGreater() - { - //user_a 挂牌 - uint sell_price = 100; - uint sell_qty = 6; - user_a.insertSheet(user_a_id,"SR","make_date","level_id","wh_id","产地",all_amount,frozen_amount,available_amount); - user_a.insertFunds(1000); //初始化资金 - user_a.setFee(3); //设置手续费比率 - - var ret_market_id = user_a.listRequest(user_a_id,sheet_id,sell_price,sell_qty); - - //user_b 摘牌 - uint buy_qty = 2; - user_b.insertFunds(1000); //初始化资金 - user_b.setFee(3); //设置手续费比率 - - var ret_delist = user_b.delistRequest(user_b_id, ret_market_id, buy_qty);//摘牌 - user_a.confirmList(1); //确认 - user_b.confirmList(1); //确认 - var ret_confirm = user_a.confirmList(2); - var ret_a_funds = user_a.getTotalFunds(); - var ret_b_funds = user_b.getTotalFunds(); - var ret_a_sheet = user_a.getSheetAllAmount(sheet_id); - var ret_b_sheet = user_b.getSheetAllAmount(2); - - //Assert - Assert.equal(ret_market_id, 1, ""); - Assert.equal(ret_delist, 0, ""); - Assert.equal(market.getMarketNum(), 1, ""); - Assert.equal(user_a.getTradeNum(), 1, ""); - Assert.equal(user_b.getTradeNum(), 1, ""); - Assert.equal(ret_a_funds, 119400, ""); - Assert.equal(ret_b_funds, 80000, ""); - Assert.equal(ret_a_sheet,58, ""); - Assert.equal(ret_b_sheet,2, ""); - Assert.equal(ret_confirm,-1, ""); - - } - - //测试挂牌管理员确认函数 摘牌量与挂牌量相等 - function testConfirmListEqual() - { - //user_a 挂牌 - uint sell_price = 100; - uint sell_qty = 6; - user_a.insertSheet(user_a_id,"SR","make_date","level_id","wh_id","产地",all_amount,frozen_amount,available_amount); - user_a.insertFunds(1000); //初始化资金 - user_a.setFee(3); //设置手续费比率 - - var ret_market_id = user_a.listRequest(user_a_id,sheet_id,sell_price,sell_qty); - - //user_b 摘牌 - uint buy_qty = 6; - user_b.insertFunds(1000); //初始化资金 - user_b.setFee(3); //设置手续费比率 - - var ret_delist = user_b.delistRequest(user_b_id, ret_market_id, buy_qty);//摘牌 - user_a.confirmList(1); //确认 - user_b.confirmList(1); //确认 - var ret_a_funds = user_a.getTotalFunds(); - var ret_b_funds = user_b.getTotalFunds(); - var ret_a_sheet = user_a.getSheetAllAmount(sheet_id); - var ret_b_sheet = user_b.getSheetAllAmount(2); - - //Assert - Assert.equal(ret_market_id, 1, ""); - Assert.equal(ret_delist, 0, ""); - Assert.equal(market.getMarketNum(), 0, ""); - Assert.equal(user_a.getTradeNum(), 1, ""); - Assert.equal(user_b.getTradeNum(), 1, ""); - Assert.equal(ret_a_funds, 158200, ""); - Assert.equal(ret_b_funds, 40000, ""); - Assert.equal(ret_a_sheet,54, ""); - Assert.equal(ret_b_sheet,6, ""); - } - - //测试协商交易 - function testSendNegReq() - { - uint sell_price = 100; - uint sell_qty = 6; - - //创建仓单 - user_a.insertSheet(user_a_id,"SR","make_date","level_id","wh_id","产地",all_amount,frozen_amount,available_amount); - //发送协商交易请求 - user_a.sendNegReq(sheet_id,sell_qty,sell_price,user_b_id); - - var(ret_all_amount, ret_available_amount, ret_frozen_amount) = user_a.getSheetAmount(sheet_id); - var(ret_length, ret_sheet_id, ret_price, ret_neg_id, ret_user_sell_id) = user_b.getNegReqRec(0); - - Assert.equal(ret_length, 1, ""); - Assert.equal(ret_available_amount, available_amount - sell_qty, ""); - Assert.equal(ret_frozen_amount, frozen_amount + sell_qty, ""); - Assert.equal(market.getMarketNum(), 0, ""); - - Assert.equal(ret_sheet_id, sheet_id, ""); - Assert.equal(ret_price, sell_price, "ret_price = 100"); - Assert.equal(ret_neg_id, 1, "ret_neg_id = 1"); - Assert.equal(ret_user_sell_id, user_a_id, "ret_user_sell_id = I am user_a"); - } - - //测试协商交易的同意函数 - function testAgreeNeg() - { - int ret = 0; - uint sell_price = 100; - uint sell_qty = 6; - - //创建仓单 - user_a.insertSheet(user_a_id,"SR","make_date","level_id","wh_id","产地",all_amount,frozen_amount,available_amount); - //初始化资金 - user_a.insertFunds(1000); - - //发送协商交易请求 - user_a.sendNegReq(sheet_id,sell_qty,sell_price,user_b_id); - - //初始化资金 - user_b.insertFunds(1000); - - //同意协商交易 - ret = user_b.agreeNeg(user_b_id, 1); - - //获取双方的合同数据 - var(a_length,a_ret_trade_id, a_ret_sheet_id, a_ret_bs, a_ret_opp_id) = user_a.getTradeMap(1); - var(b_length,b_ret_trade_id, b_ret_sheet_id, b_ret_bs, b_ret_opp_id) = user_b.getTradeMap(1); - - Assert.equal(ret, 0, "user_b.agreeNeg ret = 0"); - Assert.equal(a_length, 1, "a_length = 1"); - Assert.equal(a_ret_trade_id, 1, "a_ret_trade_id = 1"); - Assert.equal(a_ret_sheet_id, sheet_id, ""); - Assert.equal(a_ret_bs, "卖", ""); - Assert.equal(a_ret_opp_id, user_b_id,""); - Assert.equal(user_a.getTotalFunds(), 100000, ""); - - - Assert.equal(b_length, 1, "b_length = 1"); - Assert.equal(b_ret_trade_id, 1, "b_ret_trade_id = 1"); - Assert.equal(b_ret_sheet_id, sheet_id, ""); - Assert.equal(b_ret_bs, "买", " b_ret_bs "); - Assert.equal(b_ret_opp_id, user_a_id," b_ret_opp_id = I am user b "); - Assert.equal(user_b.getAvaFunds(), 40000, ""); - - } - - //测试协商交易的管理员确认函数 - function testConfirmNeg() - { - int ret = 0; - uint sell_price = 100; - uint sell_qty = 6; - - //创建仓单 - user_a.insertSheet(user_a_id,"SR","make_date","level_id","wh_id","产地",all_amount,frozen_amount,available_amount); - user_a.insertFunds(1000); //初始化资金 - user_a.setFee(3); //设置手续费比率 - - //发送协商交易请求 - user_a.sendNegReq(sheet_id,sell_qty,sell_price,user_b_id); - - user_b.insertFunds(1000); //初始化资金 - user_b.setFee(3); //设置手续费比率 - - //同意协商交易 - ret = user_b.agreeNeg(user_b_id, 1); - - //管理员确认 - user_a.confirmNeg(1); - user_b.confirmNeg(1); - - //获取双方的合同数据 - var(a_length,a_ret_trade_id, a_ret_sheet_id, a_ret_bs, a_ret_opp_id) = user_a.getTradeMap(1); - var(b_length,b_ret_trade_id, b_ret_sheet_id, b_ret_bs, b_ret_opp_id) = user_b.getTradeMap(1); - - Assert.equal(ret, 0, "user_b.agreeNeg ret = 0"); - Assert.equal(a_length, 1, "a_length = 1"); - Assert.equal(a_ret_trade_id, 1, "a_ret_trade_id = 1"); - Assert.equal(a_ret_sheet_id, sheet_id, ""); - Assert.equal(a_ret_bs, "卖", ""); - Assert.equal(a_ret_opp_id, user_b_id,""); - Assert.equal(user_a.getTotalFunds(), 158200, ""); - Assert.equal(user_a.getSheetAllAmount(sheet_id), 54, ""); - - - Assert.equal(b_length, 1, "b_length = 1"); - Assert.equal(b_ret_trade_id, 1, "b_ret_trade_id = 1"); - Assert.equal(b_ret_sheet_id, sheet_id, ""); - Assert.equal(b_ret_bs, "买", " b_ret_bs "); - Assert.equal(b_ret_opp_id, user_a_id," b_ret_opp_id = I am user b "); - Assert.equal(user_b.getTotalFunds(), 40000, ""); - Assert.equal(user_b.getAvaFunds(), 40000, ""); - Assert.equal(user_b.getFrozenFunds(), 0, ""); - Assert.equal(user_b.getSheetAllAmount(2), 6, ""); - } -} diff --git a/smartcontract/test/TestUserList.sol b/smartcontract/test/TestUserList.sol deleted file mode 100644 index c53f997..0000000 --- a/smartcontract/test/TestUserList.sol +++ /dev/null @@ -1,111 +0,0 @@ -pragma solidity ^0.4.11; -import "truffle/Assert.sol"; -import "truffle/DeployedAddresses.sol"; -import "../contracts/lib/StructUserAddr.sol"; -import "../contracts/UserList.sol"; -import "../contracts/User.sol"; -contract TestUserList -{ - address extern_addr; - address agent_addr; - bytes32 user_a; - bytes32 user_b; - int user_auth; - UserList user_list; - - function beforeEach() - { - extern_addr = new User(); //只是测试,生成一个地址,真实的外部地址,不是User合约的地址 - agent_addr = new User(); - user_a = "user_a"; - user_b = "user_b"; - user_auth = 1; - user_list = new UserList(); - } - function testAddUser_only_add_one() - { - user_list.addUser(extern_addr, agent_addr, user_a, user_auth); - Assert.equal(user_list.getUserNum(), 1, ""); - } - - function testAddUser_add_two_same_value() - { - user_list.addUser(extern_addr, agent_addr, user_a, user_auth); - Assert.equal(user_list.getUserNum(), 1, ""); - - user_list.addUser(extern_addr, agent_addr, user_a, user_auth); //repeate add some value - Assert.equal(user_list.getUserNum(), 1, ""); - } - - function testAddUser_add_two_differ_value() - { - user_list.addUser(extern_addr, agent_addr, user_a, user_auth); - Assert.equal(user_list.getUserNum(), 1, ""); - - user_list.addUser(extern_addr, agent_addr, user_b, user_auth); //repeate add some value - Assert.equal(user_list.getUserNum(), 2, ""); - } - - function testGetUserInfo_by_non_existed_user_id() - { - var(ret_extern_addr , ret_agent_addr, ret_user_id, ret_user_auth) = user_list.getUserInfo("non_exist_user"); - Assert.equal(ret_user_auth, 0, ""); - Assert.equal(ret_user_id, "", ""); - } - function testGetUserInfo_by_right_user_id() - { - user_list.addUser(extern_addr, agent_addr, user_a, user_auth); - user_list.addUser(extern_addr, agent_addr, user_b, user_auth); //repeate add some value - var(ret_extern_addr , ret_agent_addr, ret_user_id, ret_user_auth) = user_list.getUserInfo(user_a); - Assert.equal(ret_user_id, user_a, ""); - } - - function testGetUserInfo_by_non_exist_index() - { - user_list.addUser(extern_addr, agent_addr, user_a, user_auth); - user_list.addUser(extern_addr, agent_addr, user_b, user_auth); //repeate add some value - var(ret_extern_addr , ret_agent_addr, ret_user_id, ret_user_auth) = user_list.getUserInfoByIndex(3); //3 exceed index.length - Assert.equal(ret_user_id, "", ""); - Assert.equal(ret_user_auth, 0, ""); - } - function testGetUserInfo_by_right_index() - { - user_list.addUser(extern_addr, agent_addr, user_a, user_auth); - user_list.addUser(extern_addr, agent_addr, user_b, user_auth); //repeate add some value - uint user_num = user_list.getUserNum(); - Assert.equal(user_num, 2, ""); - for(uint index = 0; index < user_num; ++index) - { - var(ret_extern_addr , ret_agent_addr, ret_user_id, ret_user_auth) = user_list.getUserInfoByIndex(index); - if(index == 0) - { - Assert.equal(ret_user_id, user_a, ""); - } - else if(index == 1) - { - Assert.equal(ret_user_id, user_b, ""); - } - else - { - Assert.equal(true, false, "Impossible to trigger"); - } - } - } - function testDelUserInfo_by_non_existed_user_id() - { - user_list.addUser(extern_addr, agent_addr, user_a, user_auth); - user_list.addUser(extern_addr, agent_addr, user_b, user_auth); //repeate add some value - uint user_num = user_list.getUserNum(); - Assert.equal(user_num, 2, ""); - user_list.delUserInfo("non_existed_user_id"); - Assert.equal(user_num, 2, ""); - } - function testDelUserInfo_by_existed_user_id() - { - user_list.addUser(extern_addr, agent_addr, user_a, user_auth); - user_list.addUser(extern_addr, agent_addr, user_b, user_auth); //repeate add some value - Assert.equal(user_list.getUserNum(), 2, ""); - user_list.delUserInfo(user_a); - Assert.equal(user_list.getUserNum(), 1, ""); - } -} diff --git a/smartcontract/truffle.js b/smartcontract/truffle.js deleted file mode 100755 index 149fcd0..0000000 --- a/smartcontract/truffle.js +++ /dev/null @@ -1,12 +0,0 @@ -require('babel-register') -module.exports = { - networks: { - development: { - host: '192.168.22.123', - port: 8545, - network_id: "*", //match any networkID - gas:70000000, -// gasprice:1 - } - } -};