0

So I have a factory contract "RestaurantFactory" which i migrate, this contract creates another contract "Restaurant", after deploying the RestaurantFactory and calling createRestaurant() I receive an address for the Restaurant contract. I then use this and the ABI stored in my build/contracts folder to get an instance of the contract in my truffle console like so:

fact = await RestaurantFactory.deployed();
pizza = await fact.createRestaurant.call(web3.utils.utf8ToHex('bobs pizza shop'),'23 Cardiff road, Manchester, LL244HT',web3.utils.utf8ToHex('077998546987'))

const fs = require('fs');
const contract = JSON.parse(fs.readFileSync('./build/contracts/Restaurant.json', 'utf8'));
console.log(JSON.stringify(contract.abi));

restaurant = new web3.eth.Contract(contract.abi,pizza)

after this point, I have tried a variety of different ways of calling a function such as a name() from this contract, all result in "TypeError: restaurant.name is not a function", some of the ways I've tried include:

name = restaurant.name();
name = restaurant.name().call();
name = restaurant.methods.name().call();

Other Info, entering "restaurant" into the console returns:

Contract { 
  currentProvider: [Getter/Setter],
  _requestManager:
   RequestManager {
     provider:
      HttpProvider {
        host: 'http://127.0.0.1:7545',
        httpAgent: [Agent],
        timeout: 0,
        headers: undefined,
        connected: true,
        send: [Function],
        _alreadyWrapped: true },
     providers:
      { WebsocketProvider: [Function: WebsocketProvider],
        HttpProvider: [Function: HttpProvider],
        IpcProvider: [Function: IpcProvider] },
     subscriptions: {} },
  givenProvider: null,
  providers:
   { WebsocketProvider: [Function: WebsocketProvider],
     HttpProvider: [Function: HttpProvider],
     IpcProvider: [Function: IpcProvider] },
  _provider:
   HttpProvider {
     host: 'http://127.0.0.1:7545',
     httpAgent:
      Agent {
        domain: null,
        _events: [Object],
        _eventsCount: 1,
        _maxListeners: undefined,
        defaultPort: 80,
        protocol: 'http:',
        options: [Object],
        requests: {},
        sockets: {},
        freeSockets: {},
        keepAliveMsecs: 1000,
        keepAlive: false,
        maxSockets: Infinity,
        maxFreeSockets: 256 },
     timeout: 0,
     headers: undefined,
     connected: true,
     send: [Function],
     _alreadyWrapped: true },
  setProvider: [Function],
  BatchRequest: [Function: bound Batch],
  extend:
   { [Function: ex]
     formatters:
      { inputDefaultBlockNumberFormatter: [Function: inputDefaultBlockNumberFormat
        inputBlockNumberFormatter: [Function: inputBlockNumberFormatter],
        inputCallFormatter: [Function: inputCallFormatter],
        inputTransactionFormatter: [Function: inputTransactionFormatter],
        inputAddressFormatter: [Function: inputAddressFormatter],
        inputPostFormatter: [Function: inputPostFormatter],
        inputLogFormatter: [Function: inputLogFormatter],
        inputSignFormatter: [Function: inputSignFormatter],
        outputBigNumberFormatter: [Function: outputBigNumberFormatter],
        outputTransactionFormatter: [Function: outputTransactionFormatter],
        outputTransactionReceiptFormatter: [Function: outputTransactionReceiptForm
        outputBlockFormatter: [Function: outputBlockFormatter],
        outputLogFormatter: [Function: outputLogFormatter],
        outputPostFormatter: [Function: outputPostFormatter],
        outputSyncingFormatter: [Function: outputSyncingFormatter] },
     utils:
      { _fireError: [Function: _fireError],
        _jsonInterfaceMethodToString: [Function: _jsonInterfaceMethodToString],
        _flattenTypes: [Function: _flattenTypes],
        randomHex: [Function: randomHex],
        _: [Function],
        BN: [Function],
        isBN: [Function: isBN],
        isBigNumber: [Function: isBigNumber],
        isHex: [Function: isHex],
        isHexStrict: [Function: isHexStrict],
        sha3: [Function],
        keccak256: [Function],
        soliditySha3: [Function: soliditySha3],
        isAddress: [Function: isAddress],
        checkAddressChecksum: [Function: checkAddressChecksum],
        toChecksumAddress: [Function: toChecksumAddress],
        toHex: [Function: toHex],
        toBN: [Function: toBN],
        bytesToHex: [Function: bytesToHex],
        hexToBytes: [Function: hexToBytes],
        hexToNumberString: [Function: hexToNumberString],
        hexToNumber: [Function: hexToNumber],
        toDecimal: [Function: hexToNumber],
        numberToHex: [Function: numberToHex],
        fromDecimal: [Function: numberToHex],
        hexToUtf8: [Function: hexToUtf8],
        hexToString: [Function: hexToUtf8],
        toUtf8: [Function: hexToUtf8],
        utf8ToHex: [Function: utf8ToHex],
        stringToHex: [Function: utf8ToHex],
        fromUtf8: [Function: utf8ToHex],
        hexToAscii: [Function: hexToAscii],
        toAscii: [Function: hexToAscii],
        asciiToHex: [Function: asciiToHex],
        fromAscii: [Function: asciiToHex],
        unitMap: [Object],
        toWei: [Function: toWei],
        fromWei: [Function: fromWei],
        padLeft: [Function: leftPad],
        leftPad: [Function: leftPad],
        padRight: [Function: rightPad],
        rightPad: [Function: rightPad],
        toTwosComplement: [Function: toTwosComplement] },
     Method: [Function: Method] },
  clearSubscriptions: [Function],
  options: { address: [Getter/Setter], jsonInterface: [Getter/Setter] },
  defaultAccount: [Getter/Setter],
  defaultBlock: [Getter/Setter],
  methods:
   { name: [Function: bound _createTxObject],
     '0x06fdde03': [Function: bound _createTxObject],
     'name()': [Function: bound _createTxObject],
     location: [Function: bound _createTxObject],
     '0x516f279e': [Function: bound _createTxObject],
     'location()': [Function: bound _createTxObject],
     contactNumber: [Function: bound _createTxObject],
     '0x76af633c': [Function: bound _createTxObject],
     'contactNumber()': [Function: bound _createTxObject],
     getName: [Function: bound _createTxObject],
     '0x17d7de7c': [Function: bound _createTxObject],
     'getName()': [Function: bound _createTxObject] },
  events: { allEvents: [Function: bound ] },
  _address: '0x11AAac9A11839887956904Dc3b7a9a88E6A52a4D',
  _jsonInterface:
   [ { constant: true,
       inputs: [],
       name: 'name',
       outputs: [Array],
       payable: false,
       stateMutability: 'view',
       type: 'function',
       signature: '0x06fdde03' },
     { constant: true,
       inputs: [],
       name: 'location',
       outputs: [Array],
       payable: false,
       stateMutability: 'view',
       type: 'function',
       signature: '0x516f279e' },
     { constant: true,
       inputs: [],
       name: 'contactNumber',
       outputs: [Array],
       payable: false,
       stateMutability: 'view',
       type: 'function',
       signature: '0x76af633c' },
     { inputs: [Array],
       payable: false,
       stateMutability: 'nonpayable',
       type: 'constructor',
       constant: undefined },
     { constant: false,
       inputs: [],
       name: 'getName',
       outputs: [Array],
       payable: false,
       stateMutability: 'nonpayable',
       type: 'function',
       signature: '0x17d7de7c' } ],
  address: '0x11AAac9A11839887956904Dc3b7a9a88E6A52a4D',
  jasonInterface:
   [ { constant: true,
       inputs: [],
       name: 'name',
       outputs: [Array],
       payable: false,
       stateMutability: 'view',
       type: 'function',
       signature: '0x06fdde03' },
     { constant: true,
       inputs: [],
       name: 'location',
       outputs: [Array],
       payable: false,
       stateMutability: 'view',
       type: 'function',
       signature: '0x516f279e' },
     { constant: true,
       inputs: [],
       name: 'contactNumber',
       outputs: [Array],
       payable: false,
       stateMutability: 'view',
       type: 'function',
       signature: '0x76af633c' },
     { inputs: [Array],
       payable: false,
       stateMutability: 'nonpayable',
       type: 'constructor',
       constant: undefined },
     { constant: false,
       inputs: [],
       name: 'getName',
       outputs: [Array],
       payable: false,
       stateMutability: 'nonpayable',
       type: 'function',
       signature: '0x17d7de7c' } ] }

also, my two smart contracts

pragma solidity >=0.4.21 <0.6.0;

contract Restaurant {

    uint id;
    bytes32 public name;
    string public location;
    bytes16 public contactNumber;
    address owner;

    uint totalOrders;
    constructor(uint _id, bytes32 _name,string memory _address,bytes16 _contactNumber) public {
        id = _id;
        name = _name;
        location = _address;
        contactNumber = _contactNumber;
        totalOrders = 0;
        owner = tx.origin; // assign owner of the order to the account that called the restaurant factory 
    }

    function getName() public returns(bytes32 name) {
        return name;
    }
}



pragma solidity >=0.4.21 <0.6.0;

import "./Restaurant.sol";

contract RestaurantFactory {
    address public owner;
    mapping(uint => Restaurant) public restaurants;
    uint public restaurantCount = 0;

    event LOG_NEWRestaurantContract(address indexed theNewRestaurant, address indexed theRestaurantCreater);

    constructor() public {
        owner = msg.sender;
    }

    // factory function creates a new restaurant
    function createRestaurant(bytes32 name, string calldata _address, bytes16 contactNumber) external returns(Restaurant newRestaurant){
        // require deposit to create a restaurant
        restaurantCount ++;
        Restaurant theNewRestaurant = new Restaurant(restaurantCount, name, _address, contactNumber);
        restaurants[restaurantCount] = Restaurant(theNewRestaurant);
        return restaurants[restaurantCount];

    }

    function test() public returns(bytes32 name){
        return restaurants[0].name();
    }
}

can anyone help me get an instance of this contract that I can call a function from?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.