【问题标题】:How can I create clock auction with javascript from smart contracts?如何使用智能合约中的 javascript 创建时钟拍卖?
【发布时间】:2021-08-19 23:15:09
【问题描述】:

我对这个领域很陌生,但我想创建一个具有拍卖功能的 dapp。有什么帮助我可以开始吗?我开始创建 NFT 市场,但我现在卡在这个级别,我无法从我的智能合约到 javascript 实现。

这是我的合同

pragma solidity >=0.7.0 <0.9.0;

contract SimpleAuction{
address payable public beneficiary;
uint public auctionEndTime;

address public highestBidder;
uint public highestBid;

mapping(address => uint) public pendingReturns;

bool ended = false;

event HighestBidIncrease(address bidder, uint amount);
event AuctionEnded(address winner, uint amount);

constructor(uint _biddingTime, address payable _beneficiary){
    beneficiary = _beneficiary;
    auctionEndTime = block.timestamp + _biddingTime;
}

function bid() public payable{
    if (block.timestamp > auctionEndTime){
        revert("The auction has already ended");
    }
    
    if (msg.value <= highestBid){
        revert("There is alreay a higher or equal bid");
    }
    
    if (highestBid != 0){
        pendingReturns[highestBidder] += highestBid;
    }
    
    highestBidder = msg.sender;
    highestBid = msg.value;
    emit HighestBidIncrease(msg.sender, msg.value);
}

function withdraw() public returns (bool){
    uint amount = pendingReturns[msg.sender];
    if(amount > 0){
        pendingReturns[msg.sender] = 0;
        
        if(!payable(msg.sender).send(amount)){
            pendingReturns[msg.sender] = amount;
            return false;
        }
    }
    return true;
}

function auctionEnd() public{
    if (block.timestamp < auctionEndTime){
        revert ("The auction has not ended yet");
    }
    
    if (ended){
        revert("the function auctionEnded has already been called");
    }
    
    ended = true;
    emit AuctionEnded(highestBidder, highestBid);
    
    beneficiary.transfer(highestBid);
}

}

【问题讨论】:

  • 你的意思是你想利用这个合约并调用它的函数?您在使用松露吗?
  • 没错,我使用 truffle 并且我已经创建了市场合约并且它运行良好,我使用 BSC 测试网部署了它,直到现在都没有问题。我只需要为我的 dapp 添加拍卖功能。
  • 到目前为止一切顺利。但是要调用合约,您需要 Web3.js。我不知道您对合约 ABI、地址、字节码等概念的熟悉程度。然后您还需要进行交易,然后使用未锁定的帐户发送该交易。这不是我只能在一个答案中提供的东西。如果您之前没有创建过交易,您将需要进行一些探索。例如看看这个教程:youtube.com/…

标签: javascript ethereum solidity truffle


【解决方案1】:

我看到您从moralis 拍卖教程中获取了此代码,

1- 这个代码对你的市场没有用,因为它在合约部署时开始拍卖(这意味着当你运行 truffle migrate 时)

2- 你的智能合约无论如何都需要部署 2 个参数,即受益人地址和拍卖时间,你能正确部署它吗?如果不是,您必须将此代码添加到您的迁移中:

const Auction = artifacts.require("Auction");

module.exports = function (deployer) {
  deployer.deploy(Auction,"120","0xF17A576C88EE43D30b67Eb412e2D588e2866102F");
};

其中 120 是以秒为单位的时间,地址是收款人地址,ofc 出于示例目的,这是硬编码的,您可以从某些变量中输入您喜欢的值

3- 这个智能合约有利于学习拍卖的逻辑,但对你的实际项目无用

【讨论】:

    猜你喜欢
    • 2022-07-21
    • 1970-01-01
    • 2022-06-25
    • 2019-01-19
    • 2019-07-16
    • 2017-10-11
    • 2020-06-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多