---
# System prepended metadata

title: 手遊詐騙程式碼

---

# 手遊詐騙程式碼

```solidity=
// SPDX-License-Identifier: MIT
pragma solidity >= 0.5.0 <0.7.0;
pragma experimental ABIEncoderV2;

contract lab{
    address payable middle_address; // 中間商地址
    uint index = 0; //商品編碼
    uint order_id = 0; //訂單編號
    
    //帳戶結構
    struct Member{
        address member_address;    //賣家地址
        string member_name;        //賣家名稱
        // string[] record;           //交易紀錄
        bool isCreated;            //帳戶是否建立 
    }

    //購買資訊
    struct BuyInfo{
        uint buyid;          //訂單編號
        address buyaddress;  //買家地址
        address selladdress; //賣家地址
        string buyname;      //購買商品名稱
        uint buynumber;      //購買商品數量
        uint buyprice;       //購買商品總價
        uint buyTime;        //訂單成立時間
        bool isSet;          //訂單是否成立
        bool isFinish;       //訂單是否完成
    }

    // 商品資訊
    struct GoodInfo{
        uint good_id;           //商品編號
        address seller_address; //賣家地址
        string good_name;       //商品名稱
        string good_detail;     //商品細節
        uint good_price;        //商品價格
        bool isExisted;         //商品是否存在
    }

    struct Account{
        uint orderacc_id;    //對應訂單編號
        string account;      //帳戶
        string password;     //密碼
    }

    struct Treasure{
        uint ordertre_id;    //對應訂單編號
        string vt_name;      //虛寶名稱
    }

    modifier OnlyOwner {
        require(msg.sender == middle_address, "你沒有此項權限");
        _;
    }

    mapping(address => BuyInfo) records; //紀錄買賣紀錄
    mapping(address => Member) seller;   //賣家資訊
    mapping(address => Member) buyer;    //買家資訊
    mapping(uint => BuyInfo) orders;     //訂單資訊
    mapping(uint => GoodInfo) goods;     //商品資訊
    mapping(uint => Account) com_acc;    //交貨物(帳號)
    mapping(uint => Treasure) com_tre;   //交貨物(虛寶)

    constructor() public{
        middle_address = msg.sender; //部屬合約的地址為中間商
    }

    //賣家註冊
    function CreateSeller(string memory sellname) public returns (string memory,bool){
        require(seller[msg.sender].isCreated == false, "此賣家已經建立");

        seller[msg.sender].member_address = msg.sender;
        seller[msg.sender].member_name = sellname;
        seller[msg.sender].isCreated = true; 
        return ("賣家註冊成功",seller[msg.sender].isCreated);
    }

    //買家註冊
    function CreateBuyer(string memory buyname) public returns (string memory,bool){
        require(buyer[msg.sender].isCreated == false, "此買家已經建立");

        buyer[msg.sender].member_address = msg.sender;
        buyer[msg.sender].member_name = buyname;
        buyer[msg.sender].isCreated = true; 
        return ("買家註冊成功", buyer[msg.sender].isCreated);
    }

    //賣家添加商品
    function AddGoods(string memory gname, string memory gdetail, uint gprice) public returns (uint) {
        require(msg.sender == seller[msg.sender].member_address, "你不是賣家");
        require(seller[msg.sender].isCreated == true, "此商家不存在");
        index += 1;
        
        goods[index].good_id = index;
        goods[index].seller_address = msg.sender;
        goods[index].good_name = gname;
        goods[index].good_detail = gdetail;
        goods[index].good_price = gprice * 1 ether;
        goods[index].isExisted = true;
        return goods[index].good_id;
    }

    // 查看商品
    function LookGood(uint number) public view returns (GoodInfo memory){
        return goods[number];
    }

    // 查看賣家
    function LookSeller(address selleradd) public view returns (Member memory){
        return seller[selleradd];
    }

    // 查看買家
    function LookBuyer(address buyeradd) public view returns (Member memory){
        return buyer[buyeradd];
    }

    //買家新增訂單
    function CreateOrder(uint id, uint number) public returns (BuyInfo memory) {
        require(goods[id].isExisted == true, "此商品不存在");
        
        order_id += 1;
        uint current = 0;
        current = goods[id].good_price * number;

        orders[order_id].buyid = order_id;
        orders[order_id].buyaddress = msg.sender;
        orders[order_id].selladdress = goods[id].seller_address;
        orders[order_id].buyname = goods[id].good_name;
        orders[order_id].buynumber = number;
        orders[order_id].buyprice = current;
        orders[order_id].buyTime = block.timestamp;
        orders[order_id].isSet = true;
        orders[order_id].isFinish = false;
        
    }

    //賣家查詢訂單
    function CheckOrder(uint oid) public view returns (BuyInfo memory){
        return orders[oid];
    }

    //賣家給中間商商品(帳號)
    function SendAcc(uint aid,string memory _account, string memory _password) public {
        require(msg.sender == seller[msg.sender].member_address, "你不是賣家");
        require(msg.sender == orders[aid].selladdress, "你不是這個訂單的賣家");

        com_acc[aid].orderacc_id = aid;
        com_acc[aid].account = _account;
        com_acc[aid].password = _password;
    }

    //賣家給中間商商品(虛寶)
    function SendTre(uint tid,string memory tre_name) public {
        require(msg.sender == seller[msg.sender].member_address, "你不是賣家");
        require(msg.sender == orders[tid].selladdress, "你不是這個訂單的賣家");

        com_tre[tid].ordertre_id = tid;
        com_tre[tid].vt_name = tre_name;

    }

    //買家給中間商錢
    function Pay(uint oid) public payable {
        require(msg.sender == buyer[msg.sender].member_address, "你不是買家");
        require(msg.sender == orders[oid].buyaddress, "你不是這個訂單的買家");
        require(orders[oid].buyprice == msg.value, "金額不符合");

        uint total = address(this).balance;
        middle_address.transfer(total);
    }

    //中間商給買家商品(帳號)
    function Delivery_Account(uint oid) public view OnlyOwner returns (Account memory){
        return com_acc[oid];

    }

    //中間商給買家商品(虛寶)
    function Delivery_Tresure(uint oid) public view OnlyOwner returns (Treasure memory){
        return com_tre[oid];
    
    }

    //中間商給賣家錢
    function Delivery_Money(uint oid) public payable OnlyOwner {
        uint money = address(this).balance;
        address payable sell = payable(orders[oid].selladdress);
        sell.transfer(money);
    }

    //中間商設定合約狀態
    function SetOrder(uint oid) public OnlyOwner returns (BuyInfo memory) {
        orders[oid].isFinish = true;
        return orders[oid];
    }

}
```