Logo
Section 1 / 8

创建拍卖

学习如何在 Solidity 中实现智能合约交互。

导入

创建拍卖

学习如何在 Solidity 中实现智能合约交互。

// SPDX-License-Identifier: MIT pragma solidity ^0.8.13;

contract Ownable { address public owner; string public itemName;

1constructor(string memory itemName_) { 2 owner = msg.sender; 3 itemName = itemName_; 4} 5 6// 将所有权转移给另一个地址 7function transferOwnership(address newOwner) public { 8 require(isOwner(), "不是所有者"); 9 require(newOwner != address(0x0), "无效的所有者地址"); 10 11 owner = newOwner; 12} 13 14// 检查调用者是否为所有者 15function isOwner() public view returns(bool) { 16 return owner == msg.sender; 17}

}

// SPDX-License-Identifier: MIT pragma solidity ^0.8.13;

import "Ownable.sol";

contract Auction { enum AuctionState { INITIALIZED, ONGOING, FINISHED }

1address public beneficiary; 2uint public deadline; 3Ownable public ownable; 4AuctionState public state = AuctionState.INITIALIZED;

}

constructor(address ownableAddress) { beneficiary = msg.sender;

1ownable = Ownable(ownableAddress); 2require(ownable.owner() == address(beneficiary), "应由调用者拥有");

}

function startAuction(uint duration) public { require(ownable.isOwner(), "拍卖应由所有者发起"); require(state == AuctionState.INITIALIZED, "状态无效");

1// 将拍卖的持续时间加到当前区块的时间戳上 2deadline = block.timestamp + duration; 3 4state = AuctionState.ONGOING;

}

Ownable ownable = new Ownable("ticket");

try new Ownable("ticket") returns (Ownable ownable) { // 如果 "Ownable" 成功部署,则执行此代码 return ownable.isOwner(); } catch Error(string memory reason) { // 如果调用了 revert() 或 require() return false; } catch Panic(uint errorCode) { // 如果调用了 assert() return false; } catch (bytes memory reason) { // 如果发生任何其他错误 return false; }

try new Ownable("ticket") returns (Ownable ownable) { // 如果 "Ownable" 成功部署,则执行此代码 return ownable.isOwner(); } catch { // 如果发生任何错误 return false; }

以太坊 API

在 Remix 中设置拍卖