balances [owner]和balances [plot.owner]有什么区别?

时间:2019-02-28 09:34:01

标签: ethereum solidity

我正在通过此https://medium.com/coinmonks/ethereum-land-marketplace-dapp-tutorial-part-1-create-and-deploy-a-smart-contract-351bc0d62be2

学习Solidity

下面有一个函数,我不知道balances [owner]和balances [plot.owner]之间的区别。

function buyPlot(uint index) public payable {
    Plot storage plot = plots[index];

    require(msg.sender != plot.owner && plot.forSale && msg.value >= plot.price);

    if(plot.owner == 0x0) {
        balances[owner] += msg.value;
    }else {
        balances[plot.owner] += msg.value;
    }

    plot.owner = msg.sender;
    plot.forSale = false;

    emit PlotOwnerChanged(index);
}

这是否意味着,如果没有人拥有该地块(其中plot.owner == 0x0),然后将资金支付给智能合约的所有者(创建者)或土地的第一所有者?

这是完整的代码。

pragma solidity ^0.4.11;

contract LandContract {
address owner;
mapping (address => uint) public balances;

struct Plot {
    address owner;
    bool forSale;
    uint price;
}

Plot[12] public plots;

event PlotOwnerChanged(
    uint index
);

event PlotPriceChanged(
    uint index,
    uint price
);

event PlotAvailabilityChanged(
    uint index,
    uint price,
    bool forSale
);

constructor() public {
    owner = msg.sender;
    plots[0].price = 4000;
    plots[0].forSale = true;
    plots[1].price = 4000;
    plots[1].forSale = true;
    plots[2].price = 4000;
    plots[2].forSale = true;
    plots[3].price = 4000;
    plots[3].forSale = true;
    plots[4].price = 4000;
    plots[4].forSale = true;
    plots[5].price = 4000;
    plots[5].forSale = true;
    plots[6].price = 4000;
    plots[6].forSale = true;
    plots[7].price = 4000;
    plots[7].forSale = true;
    plots[8].price = 4000;
    plots[8].forSale = true;
    plots[9].price = 4000;
    plots[9].forSale = true;
    plots[10].price = 4000;
    plots[10].forSale = true;
    plots[11].price = 4000;
    plots[11].forSale = true;

}

function putPlotUpForSale(uint index, uint price) public {
    Plot storage plot = plots[index];

    require(msg.sender == plot.owner && price > 0);

    plot.forSale = true;
    plot.price = price;
    emit PlotAvailabilityChanged(index, price, true);
}

function takeOffMarket(uint index) public {
    Plot storage plot = plots[index];

    require(msg.sender == plot.owner);

    plot.forSale = false;
    emit PlotAvailabilityChanged(index, plot.price, false);
}

function getPlots() public view returns(address[], bool[], uint[]) {
    address[] memory addrs = new address[](12);
    bool[] memory available = new bool[](12);
    uint[] memory price = new uint[](12);

    for (uint i = 0; i < 12; i++) {
        Plot storage plot = plots[i];
        addrs[i] = plot.owner;
        price[i] = plot.price;
        available[i] = plot.forSale;
    }

    return (addrs, available, price);
}

function buyPlot(uint index) public payable {
    Plot storage plot = plots[index];

    require(msg.sender != plot.owner && plot.forSale && msg.value >= plot.price);

    if(plot.owner == 0x0) {
        balances[owner] += msg.value;
    }else {
        balances[plot.owner] += msg.value;
    }

    plot.owner = msg.sender;
    plot.forSale = false;

    emit PlotOwnerChanged(index);
}

function withdrawFunds() public {
    address payee = msg.sender;
      uint payment = balances[payee];

      require(payment > 0);

      balances[payee] = 0;
      require(payee.send(payment));
}


function destroy() payable public {
    require(msg.sender == owner);
    selfdestruct(owner);
}
}

1 个答案:

答案 0 :(得分:0)

是的,plot.owner是拥有地块的人。 owner是部署合同的人。您可能会认为,好像尚未购买的任何地块都属于owner,即部署合同的人(假设他没有将所有权转让给其他人),因此,当您购买以前没有任何人拥有的财产,那么这笔钱将归合同的所有者