Solidity
將數據寫入通過 Factory 創建的 Contract Instance
我正在嘗試使用 Truffle 在我的私有鏈上執行以下應用程序。
我創建了一個 AuctionFactory,它將為新的拍賣師生成新的 Auction 對象。拍賣工廠是這樣的:
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]; } }
這是拍賣契約
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]; } }
工廠的 Truffle 遷移腳本有這個
var AuctionFactory = artifacts.require("./AuctionFactory.sol"); module.exports = function(deployer) { deployer.deploy(AuctionFactory, {gas: 1583814}); };
我正在使用 Truffle 創建一個範例 UI 來處理這些。app.js 看起來像這樣
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); }); }); }); }); }
我能夠創建拍賣。根據拍賣創建者從 AuctionFactory 獲取其地址。如果我讀取在拍賣初始化期間設置的值,例如:
myAuction.totalTickets.call()
我能夠得到正確的數據。
但是當我嘗試使用
myAuction.bidForTicket(bidder, bidAmount, {gas:4000000, from:bidder})
該價值永遠不會添加到拍賣契約中。我檢查了地址和其他配置。
我嘗試將拍賣契約單獨部署為單個契約,並且能夠創建出價並讀取更新的值。但是當我使用工廠創建的拍賣時,它只是不寫值。我的意思是,當我閱讀這些欄位時,我得到 0,最後一個投標人的地址為 0x0
真的想了解我在這裡缺少什麼嗎?提前致謝。
以防萬一其他人在語法上大吃一驚:我必須在 app.js 中進行更改才能使其正常工作。代替
myAuction.bidForTicket(bidder, bidAmount, {gas:4000000, from:bidder}).then(function()
我不得不將其更改為:
var txnHash = bidProduct.bid.sendTransaction(bidAmount, {gas: 1400000, from:bidder}).then(function(txnHash) {.....
我的意思是返回值仍然可以忽略,但“sendTransaction”是我認為對我有用的。根據文件,只是呼叫該方法應該可以工作,我應該得到事務雜湊,但我不太明白為什麼它不能使用以前的程式碼工作。