2

Backstory: I'm creating a UI and I'd like an option for advanced users to be able to execute any function in the API.

I've successfully imported the eosio_system.json file which contains all the API commands that can be issued in underscore format ex: "account_name": "name"

I iterate over every command in the json and then provide the appropriate fields to the user for them to fill out. Once they hit send is when I get problems.

Send will return 2 things, the name of the function as a string such as: account_Name and fields provided as an object or string depending on the function fields, in this case a string account name like "altshiftdev1".

I then want to execute the named function but the eos javascript api uses camelcase, not underscore so I convert it and this is as far as I get. I can't seem to execute the now correctly formatted string as a function. I've tried a few methods like eval() but nothing seems to work.

Here's my current method:

processAction (label, action) {
  // label === eos api function name like 'account_name'
  // action === the params for that function
  const eos = Eos(eosConfig) // eos and eosConfig are imported elsewhere globally, I just put this line in for clarity
  let camelCase = label.replace(/_([a-z])/gi, function ($0, $1) { return $1.toUpperCase() })
  this.test = eos[camelCase](action)
  console.log(this.test)
}

Update: To anyone who wants to replicate what I'm trying I was using the wrong file, the schema isn't a 1:1 match with the API functions, things like eos.getAccount are not listed in the schema.

However if you import the modules, you'll be able to find all the available functions to iterate over, use this:

const {format, api, ecc, json, Fcbuffer} = Eos.modules

// Then the functions will be found in json.api.chain

Then the updated answer from @TeeAttack42 will work.

1 Answer 1

2

I guess the problem lies in the this.test allocation, because the result of your eos call is this.test, which is likely to be a promise.

If you have Babel installed or ES6 support you can use an arrow function:

 eos[camelCase](action)
   .then(response => {
     console.log(response)
   })
   .catch(error => console.log(error))

Otherwise you can try define it as a normal function.

4
  • thanks for your response but I realized I made another mistake that prevents me from going further. The eosio_system.json is actually useless because it's using custom names for apis which are in no way related to the real names used. Ex: account_name which is listed in that file is not the actual call which is getAccount(account_name). So even if I get your code working, and I think your right regarding the promise, the function won't run. Do you know if there's a document out there that fits the proper scheme I'm looking for? Aug 2, 2018 at 16:30
  • I don't know of any file that fits, but maybe you can iterate over the the eos object it self and get the available functions. I will edit the answer to add some source code.
    – tmm
    Aug 2, 2018 at 16:51
  • 1
    Found it! I'll update your answer with the code, you were very close! Aug 2, 2018 at 17:00
  • Nice! Sorry the edits got messed up. Hope this fits your solution
    – tmm
    Aug 2, 2018 at 17:11

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.