1

I am trying to run the below app on my private chain using Truffle.

I have created an AuctionFactory which would generate new Auction objects for new auctioneers. Auction factory looks like this:

pragma solidity ^0.4.11;

import '../contracts/Auction.sol';

contract AuctionFactory {

// contains the owner of an auction mapped to the address of that auction
mapping( address => address) public auctions;

function createAuction(uint32 pTicketPerPerson, uint256 pTotalTickets, uint256 pEndTime, uint32 pMinimumBid) returns (address auctionAddress) {

    address owner = msg.sender;
    Auction newAuction = new Auction(owner, pTicketPerPerson, pTotalTickets, pEndTime, pMinimumBid);
    auctions[owner] = newAuction;
    return newAuction;

}


function getAuctionAddress( address auctioneer) returns (address auctionAddress) {

    //do some sanity
    return auctions[auctioneer];
}
}

This is the Auction Contract

pragma solidity ^0.4.11;

  contract Auction {

    uint32 ticketId = 0;
    uint32 public balanceTikets;
    uint32 public ticketPerPerson;
    uint32 public minimumBid;
    uint32 public highestBid;
    uint256 public totalTickets;
    uint256 public endTime;
    uint256 public lastBid;

    address public lastBidder;
    address public auctioneer;

    mapping( address => Ticket[]) public allotedTicketsCount;

    mapping( address => uint256) public bids;

    event BidCreated(address bidder, uint32 bidAmount);
    event HighestBid(address bidder, uint32 bidAmount);

    struct Ticket {
       uint32 id;
       uint32 pricePaid;
    }


    /**
    * Constructor which will be called
    * for each new auction that is created. The requestor becomes the owner of new auction
    * 
    */
    function Auction(address owner, uint32 pTicketPerPerson, uint256 pTotalTickets, uint256 pEndTime, uint32 pMinimumBid ) {
      auctioneer = owner;
      ticketPerPerson = pTicketPerPerson;
      totalTickets = pTotalTickets;
      endTime = now + (pEndTime * 1 hours );
      minimumBid = pMinimumBid;
      highestBid = pMinimumBid; // start with 
    }


    function bidForTicket(address bidder, uint256 amount)  {

     bids[bidder] = amount;
     lastBidder = bidder;
     lastBid = amount;

   }

   function getBidFor(address bidAddress) returns (uint256 bidAmt) {
      return bids[bidAddress];
   }

  }

The Truffle migration script for the factory has this

    var AuctionFactory = artifacts.require("./AuctionFactory.sol");

    module.exports = function(deployer) {
      deployer.deploy(AuctionFactory, {gas: 1583814});
    };

I am using Truffle to create a sample UI to work with these. The app.js looks like this

    import { default as Web3} from 'web3';
    import { default as contract } from 'truffle-contract'
    // Import our contract artifacts and turn them into usable abstractions.
    import auctionFactory from '../../build/contracts/AuctionFactory.json'
    import auction from '../../build/contracts/Auction.json'

    var AuctionFactory = contract(auctionFactory);
    var Auction = contract(auction);

    window.bid = function() {
        let auctioneerId = $('#auctioneerId').val();
        let bidder = $('#bidder').val();
        let bidAmount = $('#bidAmount').val();
        console.log("Bid details " + bidAmount + " " + bidder);
        AuctionFactory.deployed().then(function(factoryInstance) {
                console.log("Factory address " + factoryInstance.address);
                factoryInstance.getAuctionAddress.call(auctioneerId).then(function(result) {
                    var myAuction = Auction.at(result);
                    console.warn("address of auction  "  + result);
                        //try to get some values from the auction

             myAuction.totalTickets.call().then(function(totalTickets) {
                console.warn("Total Tkts  " + totalTickets);
                $("#total_tkt").html(totalTickets+"");
            });


                    myAuction.bidForTicket(bidder, bidAmount, {gas:4000000, from:bidder}).then(function() {
                        console.warn("Bid created ");
                        myAuction.lastBidder.call().then(function(lastBidder) {
                            console.warn("Last bidder  " + lastBidder);

                        });

                        myAuction.lastBid.call().then(function(lastBid) {
                            console.warn("lastBid is " + lastBid);

                        });

                        myAuction.getBidFor.call(bidder).then(function(bidAmt) {
                            console.warn("Bid amt for " + bidder + " " + bidAmt);

                        });

                    });


                });                 
        });
    }

I am able to create an Auction. Get its address from the AuctionFactory based on the auction creator. If I read values that were set during initialization of the Auction like:

myAuction.totalTickets.call()

I am able to get correct data.

But when I try to add a bid to the Auction using

myAuction.bidForTicket(bidder, bidAmount, {gas:4000000, from:bidder})

That value never gets added to the Auction contract. I have checked the addresses and other configs.

I tried deploying the Auction contract separately as a single contract and am able to create bids and read updated values. But when I use the Auction created by the factory, it just doesn't write values. I mean when I read those fields I get 0 and address of last bidder as 0x0

Would really like to understand what am I missing here? Thanks in advance.

1 Answer 1

0

Just in case someone else is breaking their heads on the syntax: The change I had to make to get this working was in the app.js. Instead of

myAuction.bidForTicket(bidder, bidAmount, {gas:4000000, from:bidder}).then(function() 

I had to change it to:

var txnHash = bidProduct.bid.sendTransaction(bidAmount, {gas: 1400000, from:bidder}).then(function(txnHash) {.....

I mean the return value can still be ignored but the "sendTransaction" is what I think did the trick for me. As per docs just calling the method should have worked and I should have got the transaction hash but I coudn't quite figure out why it woudn't work using the previous code.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.