Esempio n. 1
0
 def bid(self):
     missing_funds = self.auction_contract.call().missingFundsToEndAuction()
     if missing_funds == 0:
         return
     if missing_funds <= self.last_missing_funds:
         log.warning('missing funds <= last missing: %d < %d' %
                     (missing_funds, self.last_missing_funds))
     self.last_missing_funds = missing_funds
     balance = self.web3.eth.getBalance(self.address)
     unlocked = self.web3.personal.unlockAccount(self.address, passphrase)
     assert unlocked is True
     amount = self.get_random_bid(missing_funds, balance)
     log.info('BID bidder=%s, missing_funds=%.2e, balance=%d, amount=%s' %
              (self.address, missing_funds, balance,
               amount_format(self.web3, amount)))
     try:
         txhash = self.auction_contract.transact({
             'from': self.address,
             "value": amount
         }).bid()
         receipt, success = check_succesful_tx(self.web3, txhash)
     except ValueError as e:
         log.warn(str(e))
         if self.retries >= self.max_retries:
             raise e
         self.retries += 1
Esempio n. 2
0
def fund_bidders(web3, owner, kwargs):
    bidders = int(kwargs['bidders'])
    bid_start_price = int(kwargs['bid_price'] or 0)
    fund_bidders = kwargs['fund']
    bidder_addresses = web3.eth.accounts[1:(bidders + 1)]

    # come to daddy
    event_list = [
        gevent.spawn(returnFundsToOwner, web3, owner, bidder)
        for bidder in bidder_addresses
    ]
    gevent.joinall(event_list)

    log.info('Creating {0} bidder accounts: '.format(bidders -
                                                     len(bidder_addresses)))
    for i in range(len(bidder_addresses), bidders):
        address = web3.personal.newAccount(passphrase)
        bidder_addresses.append(address)

    log.info('Simulating {0} bidders: {1}'.format(len(bidder_addresses),
                                                  bidder_addresses))
    if bid_start_price:
        log.info('Bids will start at {0} WEI = {1} ETH  / TKN'.format(
            bid_start_price, web3.fromWei(bid_start_price, 'ether')))

    if fund_bidders:
        log.info('owner={owner} balance={balance}'.format(
            owner=owner,
            balance=amount_format(web3, web3.eth.getBalance(owner))))
        assignFundsToBidders(web3, owner, bidder_addresses,
                             kwargs['distribution_limit'])
    return bidder_addresses
Esempio n. 3
0
def auction_simulation(web3, token, auction, owner, bidders, kwargs):

    log.info('owner={owner} balance={balance}'.format(
        owner=owner, balance=amount_format(web3, web3.eth.getBalance(owner))))

    # Start the auction
    if kwargs['start_auction'] is True:
        log.info('Start auction owner balance %s' %
                 amount_format(web3, web3.eth.getBalance(owner)))
        txhash = auction.transact({'from': owner}).startAuction()
        receipt = check_succesful_tx(web3, txhash, tx_timeout)

    assert auction.call().stage() == 2  # AuctionStarted
    assert auction.call().price_start() > 0
    assert isinstance(auction.call().price_constant(), int)
    assert isinstance(auction.call().price_exponent(), int)
    assert token.call().decimals() > 0

    # deploy bidders
    # this will return when auction ends
    deploy_bidders(bidders, web3, auction, kwargs)

    # check if there are no funds remaining
    ret = auction.call({'from': owner}).missingFundsToEndAuction()
    assert ret == 0
    log.info('missing funds %s' % auction.call({
        'from': owner
    }).missingFundsToEndAuction())

    # Owner calls finalizeAuction
    txhash = auction.transact({'from': owner}).finalizeAuction()
    receipt = check_succesful_tx(web3, txhash)
    assert receipt is not None
    assert auction.call().stage() == 3  # AuctionEnded

    if kwargs['claim_tokens'] is True:
        event_lst = [
            gevent.spawn(claim_tokens, auction, x, web3) for x in bidders
        ]
        gevent.joinall(event_lst)
        event_lst = [gevent.spawn(get_balance, token, x) for x in bidders]
        gevent.joinall(event_lst)
        total_balance = sum([ev.value for ev in event_lst])
        assert auction.call().stage() == 4  # TokensDistributed
        return total_balance
Esempio n. 4
0
def start_auction(auction, owner, web3):
    if auction.call().stage() >= AUCTION_STARTED:
        log.info(
            'requested startAuction(), but auction has started already. skipping.'
        )
        return

    log.info('Start auction owner balance %s' %
             amount_format(web3, web3.eth.getBalance(owner)))
    txhash = auction.transact({'from': owner}).startAuction()
    receipt = check_succesful_tx(web3, txhash, tx_timeout)
    assert receipt is not None
Esempio n. 5
0
 def bid(self):
     missing_funds = self.auction_contract.call().missingFundsToEndAuction()
     balance = self.web3.eth.getBalance(self.address)
     max_bid = int(missing_funds * 0.6 * random.random())
     amount = int(max(0, min(balance - self.approx_bid_txn_cost, max_bid)))
     if amount == 0:
         amount = 1
     unlocked = self.web3.personal.unlockAccount(self.address, passphrase)
     assert unlocked is True
     log.info('bidder=%s, missing_funds=%d, balance=%d, amount=%s' %
              (self.address, missing_funds, balance, amount_format(self.web3, amount)))
     txhash = self.auction_contract.transact({'from': self.address, "value": amount}).bid()
     receipt = check_succesful_tx(self.web3, txhash)
     assert receipt is not None
Esempio n. 6
0
 def bid(self):
     missing_funds = self.auction_contract.call().missingFundsToEndAuction()
     if missing_funds == 0:
         return
     assert missing_funds <= self.last_missing_funds
     self.last_missing_funds = missing_funds
     balance = self.web3.eth.getBalance(self.address)
     unlocked = self.web3.personal.unlockAccount(self.address, passphrase)
     assert unlocked is True
     amount = self.get_random_bid(missing_funds, balance)
     log.info('BID bidder=%s, missing_funds=%.2e, balance=%d, amount=%s' %
              (self.address, missing_funds, balance,
               amount_format(self.web3, amount)))
     txhash = self.auction_contract.transact({
         'from': self.address,
         "value": amount
     }).bid()
     receipt = check_succesful_tx(self.web3, txhash)
     assert receipt is not None
Esempio n. 7
0
def auction_simulation(web3,
                       wallet,
                       token,
                       auction,
                       owner,
                       bidders,
                       bid_interval=None,
                       bid_start_price=None,
                       sim_claim_tokens=False):
    print_all_logs(token, auction)

    log.info('{owner} {balance}'.format(owner=owner,
                                        balance=amount_format(
                                            web3, web3.eth.getBalance(owner))))

    # Start the auction
    log.info('Start auction owner balance %s' %
             amount_format(web3, web3.eth.getBalance(owner)))
    txhash = auction.transact({'from': owner}).startAuction()
    receipt = check_succesful_tx(web3, txhash, tx_timeout)

    assert auction.call().stage() == 2  # AuctionStarted

    # Make the bids

    # Timeout until price is = bid_start_price
    price_start = auction.call().price_start()
    assert price_start > 0
    price_constant = auction.call().price_constant()
    assert isinstance(price_constant, int)
    price_exponent = auction.call().price_exponent()
    assert isinstance(price_exponent, int)
    decimals = token.call().decimals()
    multiplier = 10**decimals
    assert multiplier > 0

    '''
    # Delay in seconds if we want to start the first bid at a certain price
    if bid_start_price:
        initial_bid_delay = elapsedAtPrice(bid_start_price, price_factor, price_constant, multiplier)
        assert initial_bid_delay >= 0, 'Price for first bid was set too high'
        log.info('Elapsed time until the first bid is made', initial_bid_delay
    '''  # noqa

    log.info('Timeout between bids {0}'.format(bid_interval or ' is random.'))

    from deploy.bidder import Bidder
    import gevent
    bidder_objs = [Bidder(web3, auction, addr) for addr in bidders]
    bidder_gevents = [gevent.spawn(b.run) for b in bidder_objs]

    gevent.joinall(bidder_gevents)
    del bidder_gevents

    assert auction.call({'from': owner}).missingFundsToEndAuction() == 0
    log.info('missing funds from=%s' % auction.call({
        'from': owner
    }).missingFundsToEndAuction())

    # Owner calls finalizeAuction
    txhash = auction.transact({'from': owner}).finalizeAuction()
    receipt = check_succesful_tx(web3, txhash)
    assert receipt is not None
    assert auction.call().stage() == 3  # AuctionEnded

    # distribute tokens

    def claim_tokens(auction, bidder):
        txhash = auction.transact({'from': bidder}).claimTokens()
        check_succesful_tx(web3, txhash)

    def get_balance(token, bidder):
        token_balance = token.call().balanceOf(bidder)
        log.info('{bidder} {tokens}'.format(bidder=bidder,
                                            tokens=token_balance))
        return token_balance

    if sim_claim_tokens is True:
        event_lst = [gevent.spawn(claim_tokens, auction, x) for x in bidders]
        gevent.joinall(event_lst)
        event_lst = [gevent.spawn(get_balance, token, x) for x in bidders]
        gevent.joinall(event_lst)
        total_balance = sum([ev.value for ev in event_lst])
        assert auction.call().stage() == 4  # TokensDistributed
        return total_balance