How to set eosio.token or any contract using eosjs & scatter from front end application at run time
I am currently working on a dAPP and it's front end we are using ReactJS. I wish to add a feature that on button click user could create a new token; For that I need to publish a eosio.token contract which internally needs the permission from user's account (so as scatter). So could anyone help me with that, as there is no way to use FS i.e file system library at front end. So just using eosjs how could I publish a contract ?
See setCode and setAbi .. If you get the data into an Array it will work.
https://github.com/eosio/eosjs#deploy
Thanks for the response @jcalfee ,
But I am kinda confuse here, how can I add a file data into an array, I was able to read .abi file as its a json file, but for .wasm file its ~14k lines of code so how can I add it into an array? And where exactly I should keep the file to read its data ( as we are talking about front end where we cannot access file system) ? It would be really great if you could enlarge a little.
Sorry, array buffer.. Probably a Uint8Array or an ArrayBuffer..
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Typed_arrays#Buffers_and_views_typed_array_architecture
https://stackoverflow.com/questions/8880571/convert-nodejs-buffer-to-browsers-javascript#16548689
I meet the same problem with setcode at front end. And I use "new Uint8Array(wasm.data)", but it still not work. Do you have sample code. Thanks a lot ~
Hi @wcy1231 , I was able to solve this problem after spending quite a long time. Basically I used my own back-end which simply returns the .wasm and .abi files in buffer format and I am inputting it to the setcode() and setabi() methods on front end by taking user's consent via scatter.

You can refer another related issue for this I created - https://github.com/EOSIO/eosjs/issues/300
Hope this will help.
@amolmaid Thank you. I fix this just now. Also after a long time. My method is change the wasm to ASCII string. It can work at front end
@wcy1231 , It would be really great if you could share your code snippet to read the .wasm file from front end directly; and also where exactly to store them ? It could save my backend call ...
So thanks in advance.
I also read .wasm from backend
backend: nodedjs wasm= fs.readFileSync('s.wasm'); res.send(hex2ascii(wasm.toString('Hex')))
frontend: eos.setcode('myaccount',0,0,wasm)
Ohh okay ! but nice alternative.
I read directly in backend,
const wasm = fs.readFileSync(`/home/ranjit/eos_v105/eos/build/contracts/eosio.token/eosio.token.wasm`)
const abi = fs.readFileSync(`/home/ranjit/eos_v105/eos/build/contracts/eosio.token/eosio.token.abi`)
const result = {wasm: wasm, abi: abi}
return res.json(JSON.stringify(result));
Thanks for the response :)
@wanderingbort and I tried for a long time to do this only in the browser at the London Hackathon yesterday. ABI works fine, but it was just not possible to set the wasm file. It does not seem to get sent in the expected format from the browser.
It was limited expecting a Buffer (that does not help you) or hex (difficult to convert). So I released [email protected] that adds support for the Array and Uint8Array.
https://github.com/EOSIO/eosjs-fcbuffer/commit/259e50c9381d86272503b3c888ce148f0f61bc03#diff-3a699a0719518f967e9b2df3f5549e02
Thanks but I am still getting the same error message. I pull down the .wasm file using XmlHttpRequest, setting response type as "arraybuffer". Then I convert to Uint8Array.
Same error as with 16.0.8: magic number mismatch. Serialization Error Processing WASM. Magic number loaded ... but was expecting ...
Does the data start off like this:
00 61 73 6d ..
In text:
\u0000asm
Maybe that patch did not work. Try this where asm is your Array:
eos.setcode('myaccount', 0, 0, eos.fc.types.bytes().fromObject(asm))
Now I get the error: Contract is already running this version of code (it is most certainly not).
We were also getting this error yesterday, along with the magic number error, depending on which exact format was used
It takes a major change in the c++ to change the wasm.. Wasm is a sub-set of c++...
This contract is not currently pushed on that account. We confirmed last night that even if no contact is pushed or a completely different one, that error message would (wrongly) appear. Yesterday it happened when I tried setting the XmlHttpRequest response type to blob.
This message Contract is already running this version of code could potentially include a invalid deployment. This comes up from time to time while I'm re-deploying changes even though I made changes but it did not functionally affect the wasm.
@znebby
Did you have any luck with this?
Sorry, have not tried since then. Perhaps it can be done in eosjs2?