去中心化商城业务合约代码

 

去中心化商城业务合约代码

商品信息介绍:

struct Product {

//商品基本信息

    uint id;//商品编号,全局递增

    string name;//商品名称

    string category;//商品类别

    string imageLink; //商品图片链接地址

    string descLink;  //商品描述链接地址

    //拍卖相关信息

    uint auctionStartTime; //拍卖开始时间   

    uint auctionEndTime;//拍卖截止时间

    uint startPrice; //起拍价格

    address highestBidder;   //出最高价者 

    uint highestBid; //最高出价

    uint secondHighestBid;//次高出价 

    uint totalBids;  //投标者人数

    //商品状态

    ProductStatus status; //商品销售状态:拍卖中、售出、未售

    ProductCondition condition; //品相:新品、二手  

  }

 

商品状态:

enum ProductStatus { Open, Sold, Unsold }

 

商品品相:

enum ProductCondition { New, Used }

 

卖家的商品信息:

mapping (address => mapping(uint => Product)) stores;

 

商品反查信息:

mapping (uint => address) productIdInStore;

 

商品编号计数器:

productIndex = 0;

 

商品添加到区块链函数:

function addProductToStore(string _name, string _category, string _imageLink, string _descLink, uint _auctionStartTime,uint _auctionEndTime, uint _startPrice, uint _productCondition) 

public {

  //拍卖截止时间应当晚于开始时间

  require (_auctionStartTime < _auctionEndTime);

 

  //商品编号计数器递增

  productIndex += 1;

 

  //构造Product结构变量

  Product memory product = Product(productIndex, _name, _category, _imageLink,_descLink, _auctionStartTime, _auctionEndTime,_startPrice, 0, 0, 0, 0, ProductStatus.Open,ProductCondition(_productCondition));

 

 //存入商品目录表                   

  stores[msg.sender][productIndex] = product;

 

  //保存商品反查表

  productIdInStore[productIndex] = msg.sender;

}

 

查看商品信息函数:

unction getProduct(uint _productId) view public returns (uint, string, string, string, string, uint,uint, uint, ProductStatus, ProductCondition) {//利用商品编号提取商品信息

 Product memory product = stores[productIdInStore[_productId]][_productId];

 

  //按照定义的先后顺序依次返回product结构各成员

 return (product.id, product.name, product.category, product.imageLink, 

    product.descLink, product.auctionStartTime,

    product.auctionEndTime, product.startPr