eosjs icon indicating copy to clipboard operation
eosjs copied to clipboard

Helloworld contract - ReferenceError: headers is not defined

Open Joelone opened this issue 8 years ago • 28 comments

When I use "7aefccc 6.1.11" of eosjs with hello world smart contract, there is an error as following:

initialised helloworld (node:20693) UnhandledPromiseRejectionWarning: ReferenceError: headers is not defined at transactionHeaders (/home/joel/eosjs/app/hello.js:13:29) at transaction (/home/joel/eosjs/src/write-api.js:416:5) at Object.transaction (/home/joel/eosjs/src/write-api.js:147:14) at at process._tickCallback (internal/process/next_tick.js:160:7) (node:20693) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 2)

Joelone avatar Apr 02 '18 02:04 Joelone

Here is source code of hello.js

Eos = require('../src')
const binaryen = require('binaryen')
const fs = require('fs')

// Optional configuration..
config = {
  keyProvider: ['5KQwr......'], // WIF string or array of keys..
  httpEndpoint: 'http://127.0.0.1:8888',
  mockTransactions: () => 'pass', // or 'fail'
  transactionHeaders: (expireInSeconds, callback) => {
    callback(null/*error*/, headers)
  },
  expireInSeconds: 60,
  broadcast: true,
  debug: false,
  sign: true
}

eos = Eos.Localnet(config, binaryen)

contractDir = `contracts`
wast = fs.readFileSync(`${contractDir}/hello/hello.wast`)
abi = fs.readFileSync(`${contractDir}/hello/hello.abi`)

// Publish contract to the blockchain
eos.setcode('helloworld', 0, 0, wast, abi)

const options = {
  broadcast: true,
  sign: true,
  scope: [ "helloworld" ],
  authorization: [{
      "account": "helloworld",
      "permission": "active"
    } ]
}
eos.contract('helloworld', options)
  .then((helloworld) => {
    console.log(helloworld)
    console.log("initialised helloworld")
})

//eosc push message helloworld sayhello '{"sayto":"inita"}' -S helloworld
try {
  eos.transaction({
    scope: ['helloworld'],
    messages: [{
      code: 'helloworld',
      type: 'sayhello',
      authorization: [{
        "account": "helloworld",
        "permission": "active"
      }],
      data: {
        "sayto": "inita"
      },
    }]
  })
} catch(e) {
  console.error(e)
}

Joelone avatar Apr 02 '18 02:04 Joelone

I guess I may miss some config in transactionHeaders. Any hints? Thanks!

Joelone avatar Apr 02 '18 02:04 Joelone

Delete everything in config that you don't absolutely need.

jcalfee avatar Apr 02 '18 12:04 jcalfee

Same with options .. eosjs is designed to have defaults for all that..

jcalfee avatar Apr 02 '18 12:04 jcalfee

Thanks jcalfee, I got different error after removed some items in config and options

Eos = require('../src')
const binaryen = require('binaryen')
const fs = require('fs')

// Optional configuration..
config = {
  keyProvider: ['5KQwrPb...'], // WIF string or array of keys..
  httpEndpoint: 'http://127.0.0.1:8888',
}

eos = Eos.Localnet(config)

contractDir = `contracts`
wast = fs.readFileSync(`${contractDir}/hello/hello.wast`)
abi = fs.readFileSync(`${contractDir}/hello/hello.abi`)

// Publish contract to the blockchain
eos.setcode('helloworld', 0, 0, wast, abi)

const options = {
}
console.log("before eos.contract")
eos.contract('helloworld',options)
  .then((helloworld) => {
    // console.log(helloworld)
    console.log("initialised helloworld")
})

console.log("before eos.transaction")
try {
  eos.transaction({
    scope: ['helloworld'],
    messages: [{
      code: 'helloworld',
      type: 'sayhello',
      authorization: [{
        "account": "helloworld",
        "permission": "active"
      }],
      data: {
        "sayto": "inita"
      }
    }]
  })
} catch(e) {
  console.error(e)
}

Joelone avatar Apr 03 '18 02:04 Joelone

Here is the new message with cleaner version of config and options:

before eos.contract
before eos.transaction
initialised helloworld
{ AssertionError [ERR_ASSERTION]: required: config.binaryen = require("binaryen") setcode.code message.data transaction.messages
    at setcode.code.fromObject (/home/joel/eosjs/src/structs.js:275:5)
    at Object.fromObject (/home/joel/eosjs/node_modules/fcbuffer/lib/struct.js:141:15)
    at message.data.fromObject (/home/joel/eosjs/src/structs.js:333:27)
    at Object.fromObject (/home/joel/eosjs/node_modules/fcbuffer/lib/struct.js:141:15)
    at Object.fromObject (/home/joel/eosjs/node_modules/fcbuffer/lib/types.js:320:28)
    at Object.fromObject (/home/joel/eosjs/node_modules/fcbuffer/lib/struct.js:144:34)
    at rawTx (/home/joel/eosjs/src/write-api.js:434:36)
    at /home/joel/eosjs/src/write-api.js:573:5
    at /home/joel/eosjs/node_modules/eosjs-api/lib/exported-helpers.js:65:7
    at /home/joel/eosjs/node_modules/eosjs-api/lib/exported-helpers.js:75:7
  generatedMessage: false,
  name: 'AssertionError [ERR_ASSERTION]',
  code: 'ERR_ASSERTION',
  actual: false,
  expected: true,
  operator: '==' }
{ Error: {"code":400,"message":"Bad Request","details":"irrelevant authority included (3030003)\nMessage declared authorities it did not need: [{\"account\":\"helloworld\",\"permission\":\"active\"}]\n\n\n\n"}
    at /home/joel/eosjs/node_modules/eosjs-api/lib/apigen.js:98:23
    at <anonymous>
    at process._tickCallback (internal/process/next_tick.js:160:7) status: 400, statusText: 'Bad Request' }
[push_transaction error] '{"code":400,"message":"Bad Request","details":"irrelevant authority included (3030003)\nMessage declared authorities it did not need: [{\"account\":\"helloworld\",\"permission\":\"active\"}]\n\n\n\n"}', digest '45c0db7828eb8eecc25a0100408a97721aa36a000100408a97721aa36a0000003446d5bcc10100408a97721aa36a00000000a8ed323208000000000093dd74'
{"code":400,"message":"Bad Request","details":"irrelevant authority included (3030003)\nMessage declared authorities it did not need: [{\"account\":\"helloworld\",\"permission\":\"active\"}]\n\n\n\n"}
(node:5959) UnhandledPromiseRejectionWarning: {"code":400,"message":"Bad Request","details":"irrelevant authority included (3030003)\nMessage declared authorities it did not need: [{\"account\":\"helloworld\",\"permission\":\"active\"}]\n\n\n\n"}
(node:5959) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1)
(node:5959) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

Joelone avatar Apr 03 '18 02:04 Joelone

According to the error message, "irrelevant authority included", I remove the authority from message and modify the last part of code as:

try {
  eos.transaction({
    scope: ['helloworld'],
    messages: [{
      code: 'helloworld',
      type: 'sayhello',
      data: {
        "sayto": "inita"
      }
    }]
  })
} catch(e) {
  console.error(e)
}

Joelone avatar Apr 03 '18 03:04 Joelone

Then it said "Expecting message.authorization array".

before eos.contract
before eos.transaction
initialised helloworld
(node:6923) UnhandledPromiseRejectionWarning: TypeError: Expecting message.authorization array
    at arg.messages.forEach.message (/home/joel/eosjs/src/write-api.js:404:15)
    at Array.forEach (<anonymous>)
    at transaction (/home/joel/eosjs/src/write-api.js:402:18)
    at Object.transaction (/home/joel/eosjs/src/write-api.js:147:14)
    at <anonymous>
    at process._tickCallback (internal/process/next_tick.js:160:7)
(node:6923) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1)
(node:6923) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
{ AssertionError [ERR_ASSERTION]: required: config.binaryen = require("binaryen") setcode.code message.data transaction.messages
    at setcode.code.fromObject (/home/joel/eosjs/src/structs.js:275:5)
    at Object.fromObject (/home/joel/eosjs/node_modules/fcbuffer/lib/struct.js:141:15)
    at message.data.fromObject (/home/joel/eosjs/src/structs.js:333:27)
    at Object.fromObject (/home/joel/eosjs/node_modules/fcbuffer/lib/struct.js:141:15)
    at Object.fromObject (/home/joel/eosjs/node_modules/fcbuffer/lib/types.js:320:28)
    at Object.fromObject (/home/joel/eosjs/node_modules/fcbuffer/lib/struct.js:144:34)
    at rawTx (/home/joel/eosjs/src/write-api.js:434:36)
    at /home/joel/eosjs/src/write-api.js:573:5
    at /home/joel/eosjs/node_modules/eosjs-api/lib/exported-helpers.js:65:7
    at /home/joel/eosjs/node_modules/eosjs-api/lib/exported-helpers.js:75:7
  generatedMessage: false,
  name: 'AssertionError [ERR_ASSERTION]',
  code: 'ERR_ASSERTION',
  actual: false,
  expected: true,
  operator: '==' }

Joelone avatar Apr 03 '18 03:04 Joelone

Use something like this:

eos.contract('helloworld', helloworld => { // or .then(
    helloworld.sayhello('inita')
})

jcalfee avatar Apr 03 '18 15:04 jcalfee

Because your deploying a contract you need this:

config.binaryen = require("binaryen")
Eos.Localnet(config)

jcalfee avatar Apr 03 '18 15:04 jcalfee

Thank you very much, the code is much cleaner now.

Eos = require('../src')
const fs = require('fs')

// Optional configuration..
config = {
  keyProvider: ['5KQwrPb...'], // WIF string or array of keys..
  httpEndpoint: 'http://127.0.0.1:8888',
}

config.binaryen = require("binaryen")
eos = Eos.Localnet(config)

contractDir = `contracts`
wast = fs.readFileSync(`${contractDir}/hello/hello.wast`)
abi = fs.readFileSync(`${contractDir}/hello/hello.abi`)

// Publish contract to the blockchain
eos.setcode('helloworld', 0, 0, wast, abi)

//eosc push message helloworld sayhello '{"sayto":"inita"}' -S helloworld
eos.contract('helloworld', helloworld => { // or .then(
    helloworld.sayhello('inita')
})

Joelone avatar Apr 04 '18 02:04 Joelone

I feel like I'm very close to make it work. From the last line of message,it looks like the sayhello be called before the contract be set.

(node:21847) UnhandledPromiseRejectionWarning: TypeError: helloworld.sayhello is not a function
    at eos.contract.helloworld (/home/joel/eosjs/app/hello.js:33:16)
    at callback (/home/joel/eosjs/node_modules/eosjs-api/lib/process-args.js:68:9)
    at writeApi.genContractActions.then.catch.r (/home/joel/eosjs/src/write-api.js:60:20)
    at <anonymous>
    at process._tickCallback (internal/process/next_tick.js:160:7)
(node:21847) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1)
(node:21847) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
Assembling WASM...

Joelone avatar Apr 04 '18 03:04 Joelone

sayhello needs to be in the actions section of your ABI..

jcalfee avatar Apr 04 '18 15:04 jcalfee

It is in actions section of hello.abi, and I verify it with command line.

{
  "types": [],
  "structs": [{
      "name": "hello",
      "base": "",
      "fields": {
        "from": "account_name",
        "to": "account_name"
      }
    }
  ],
  "actions": [{
      "action_name": "sayhello",
      "type": "hello"
    }
  ],
  "tables": [{
      "table_name": "hello",
      "index_type": "i64",
      "key_names": [
        "from"
      ],
      "key_types": [
        "account_name"
      ],
      "type": "hello"
    }
  ]
}

Joelone avatar Apr 04 '18 15:04 Joelone

Fyi: action_name is for dawn2 but that is renamed to name in dawn3..

This was a problem here quite involved so there could still be an issue, I didn't have actions implemented until version 6.1.4: https://github.com/EOSIO/eosjs/issues/18 .. Looks like your using 6.1.11 though..

Can you tell me what this prints: console.log(Object.keys(helloworld))

In any case, I may need a test case for this.. A test case like this is possible now using the ./docker environment.

jcalfee avatar Apr 04 '18 16:04 jcalfee

I don't have this docker environment in dawn2 ..

Can you try this as a work-around? Rename your action or the struct so it has the same name like this:

  "actions": [{
      "action_name": "transfer",
      "type": "transfer"
}

jcalfee avatar Apr 04 '18 16:04 jcalfee

Rename the action/table are all good until I push message action to it. I guess the keyword transfer is reserve for EOS system.

$ ./eosc push message helloworld tansfer '{"from":"helloworld","to":"inita"}' -S helloworld,inita
267194ms            main.cpp:1128                 operator()           ] Converting argument to binary...
267204ms            main.cpp:1195                 main                 ] Failed with error: Assert Exception (10)
status_code == 200: Error code 500
: {"code":500,"message":"Internal Service Error","details":"7 bad_cast_exception: Bad Cast\nInvalid cast from object_type to string\n    {\"type\":\"object_type\"}\n      variant.cpp:476 as_string\n\n    {\"code\":\"helloworld\",\"type\":\"tansfer\",\"obj\":{\"from\":\"helloworld\",\"to\":\"inita\"}}\n      chain_controller.cpp:1566 message_to_binary"}

Joelone avatar Apr 04 '18 20:04 Joelone

I update my local eosjs to fd51ca2, just a few commits after tag v7.1.2. And modify the code like

//eosc push message helloworld sayhello '{"sayto":"inita"}' -S helloworld
eos.contract('helloworld', helloworld => { // or .then(
    helloworld.sayhello('inita')
    console.log(Object.keys(helloworld))
})

Beside the function not found error, it also said "Error: missing required keys for"

(node:9466) UnhandledPromiseRejectionWarning: TypeError: helloworld.sayhello is not a function
    at eos.contract.helloworld (/home/joel/eosjs/app/hello.js:24:16)
    at callback (/home/joel/eosjs/node_modules/eosjs-api/lib/process-args.js:68:9)
    at writeApi.genContractActions.then.catch.r (/home/joel/eosjs/src/write-api.js:58:20)
    at <anonymous>
    at process._tickCallback (internal/process/next_tick.js:188:7)
(node:9466) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1)
(node:9466) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
Assembling WASM...
Error: missing required keys for {"expiration":"2018-04-04T20:15:45","region":0,"ref_block_num":845,"ref_block_prefix":2333810170,"packed_bandwidth_words":97,"context_free_cpu_bandwidth":0,"context_free_actions":[],"actions":[{"account":"eosio","name":"setcode","authorization":[{"actor":"helloworld","permission":"active"}],"data":"00408a97721aa36a0000c5050061736d0100000001..."}]}
    at eos.getRequiredKeys.then (/home/joel/eosjs/src/index.js:196:13)
    at <anonymous>
    at process._tickCallback (internal/process/next_tick.js:188:7)
Error: missing required keys for {"expiration":"2018-04-04T20:15:45","region":0,"ref_block_num":845,"ref_block_prefix":2333810170,"packed_bandwidth_words":97,"context_free_cpu_bandwidth":0,"context_free_actions":[],"actions":[{"account":"eosio","name":"setcode","authorization":[{"actor":"helloworld","permission":"active"}],"data":"00408a97721aa36a0000c5050061736d010..."}]}
    at eos.getRequiredKeys.then (/home/joel/eosjs/src/index.js:196:13)
    at <anonymous>
    at process._tickCallback (internal/process/next_tick.js:188:7)
(node:9466) UnhandledPromiseRejectionWarning: Error: missing required keys for {"expiration":"2018-04-04T20:15:45","region":0,"ref_block_num":845,"ref_block_prefix":2333810170,"packed_bandwidth_words":97,"context_free_cpu_bandwidth":0,"context_free_actions":[],"actions":[{"account":"eosio","name":"setcode","authorization":[{"actor":"helloworld","permission":"active"}],"data":"00408a97721..."}]}
    at eos.getRequiredKeys.then (/home/joel/eosjs/src/index.js:196:13)
    at <anonymous>
    at process._tickCallback (internal/process/next_tick.js:188:7)
(node:9466) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 2)

Joelone avatar Apr 04 '18 20:04 Joelone

The smart contract code are compiled with tag DAWN-2018-02-14 on EOS. Here is the eosc command line result. If you think the previous error is caused by older EOS, I'm going to move a more recent version of EOS.

$ eosc push message helloworld sayhello '{"sayto":"inita"}' -S helloworld
1062248ms            main.cpp:1128                 operator()           ] Converting argument to binary...
{
  "transaction_id": "c816f67cba4c7b7d75bc2c6b67ace22013d0ee05b1ed4dcbcd000eb93f744501",
  "processed": {
    "ref_block_num": 873,
    "ref_block_prefix": 4256484897,
    "expiration": "2018-04-04T20:18:11",
    "scope": [
      "helloworld"
    ],
    "signatures": [],
    "messages": [{
        "code": "helloworld",
        "type": "sayhello",
        "authorization": [],
        "data": {
          "sayto": "inita"
        },
        "hex_data": "000000000093dd74"
      }
    ],
    "output": [{
        "notify": [],
        "deferred_trxs": []
      }
    ]
  }
}

Joelone avatar Apr 04 '18 20:04 Joelone

Right now you can use npm i eosjs@dawn3 with Eos.Localnet(..) and if you have docker, run up.sh in ./docker [master]branch .. you'll see a function in up.sh that you can use for the Eos command-line interface. Use that to deploy your smart contract in dawn3 ..

jcalfee avatar Apr 04 '18 23:04 jcalfee