11 、迷恋猫的交易
// 处理猫的拍卖,之前的拍卖合约,是可以处理所有的ERC721合约的,
// 这里是把猫
contract KittyAuction is KittyBreeding {
/// 设置买卖交易所的地址 ,只有ceo才能执行
/// @param _address - 合约地址
function setSaleAuctionAddress(address _address) external onlyCEO {
// 实例化合约
SaleClockAuction candidateContract = SaleClockAuction(_address);
// 验证是否是买卖的合约
require(candidateContract.isSaleClockAuction());
// 设置新的买卖交易所,saleAuction是在KittyBase中
saleAuction = candidateContract;
}
/// 设置新的交配交易所
/// @param _address - 交配交易所地址
function setSiringAuctionAddress(address _address) external onlyCEO {
SiringClockAuction candidateContract = SiringClockAuction(_address);
require(candidateContract.isSiringClockAuction());
// 设置新的交配交易所,siringAuction是在KittyBase中
siringAuction = candidateContract;
}
///创建一个新的买卖的拍卖,外部调用
/// Does some ownership trickery to create auctions in one tx.
function createSaleAuction(
uint256 _kittyId,
uint256 _startingPrice,
uint256 _endingPrice,
uint256 _duration
)
external
whenNotPaused
{
// 权限判断,合约的调用者必须是代币的拥有者
require(_owns(msg.sender, _kittyId));
// 判断是否怀孕
require(!isPregnant(_kittyId));
// 授权saleAuction 可以拍买 _kittyId
_approve(_kittyId, saleAuction);
// 调用买卖交易所的createAuction方法,创建拍卖
// msg.sender 为本合约地址
saleAuction.createAuction(
_kittyId,
_startingPrice,
_endingPrice,
_duration,
msg.sender
);
}
/// 创建新的交配的拍卖,外部调用
function createSiringAuction(
uint256 _kittyId,
uint256 _startingPrice,
uint256 _endingPrice,
uint256 _duration
)
external
whenNotPaused
{
// 权限判断,所有者
require(_owns(msg.sender, _kittyId));
// 冻结时间是否结束
require(isReadyToBreed(_kittyId));
// 授权给了 拍卖所
_approve(_kittyId, siringAuction);
// 交配交易所,创建交配
siringAuction.createAuction(
_kittyId,
_startingPrice,
_endingPrice,
_duration,
msg.sender
);
}
/// 完成交配的拍卖
function bidOnSiringAuction(
uint256 _sireId,
uint256 _matronId
)
external
payable
whenNotPaused
{
// 所有权
require(_owns(msg.sender, _matronId));
// 插件冻结期
require(isReadyToBreed(_matronId));
// 是否可以交易所拍卖
require(_canBreedWithViaAuction(_matronId, _sireId));
// 获取当前价格
uint256 currentPrice = siringAuction.getCurrentPrice(_sireId);
// 判断当前价格 是否 大于 怀孕费用
require(msg.value >= currentPrice + autoBirthFee);
// value(msg.value - autoBirthFee) 合约与合约之间,转移多余的以太币
siringAuction.bid.value(msg.value - autoBirthFee)(_sireId);
// 繁殖交配成功
_breedWith(uint32(_matronId), uint32(_sireId));
}
/// 转账,将拍卖所中的以太币转走,只有管理员才能操作
function withdrawAuctionBalances() external onlyCLevel {
saleAuction.withdrawBalance();
siringAuction.withdrawBalance();
}
}
网友评论