eosjs icon indicating copy to clipboard operation
eosjs copied to clipboard

How to set eosio.token or any contract using eosjs & scatter from front end application at run time

Open amolmaid opened this issue 8 years ago • 27 comments

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 ?

amolmaid avatar Aug 03 '18 14:08 amolmaid

See setCode and setAbi .. If you get the data into an Array it will work.

https://github.com/eosio/eosjs#deploy

jcalfee avatar Aug 04 '18 22:08 jcalfee

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.

amolmaid avatar Aug 05 '18 09:08 amolmaid

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

jcalfee avatar Aug 06 '18 17:08 jcalfee

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 ~

wcy1231 avatar Aug 27 '18 05:08 wcy1231

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.

working setcode

You can refer another related issue for this I created - https://github.com/EOSIO/eosjs/issues/300

Hope this will help.

amolmaid avatar Aug 27 '18 11:08 amolmaid

@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 avatar Aug 27 '18 11:08 wcy1231

@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.

amolmaid avatar Aug 27 '18 12:08 amolmaid

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)

wcy1231 avatar Aug 27 '18 12:08 wcy1231

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 :)

amolmaid avatar Aug 27 '18 13:08 amolmaid

@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.

zlnvch avatar Sep 23 '18 09:09 zlnvch

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

jcalfee avatar Sep 23 '18 13:09 jcalfee

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 ...

zlnvch avatar Sep 23 '18 13:09 zlnvch

Does the data start off like this:

00 61 73 6d ..

In text:

\u0000asm

jcalfee avatar Sep 23 '18 15:09 jcalfee

Maybe that patch did not work. Try this where asm is your Array:

eos.setcode('myaccount', 0, 0, eos.fc.types.bytes().fromObject(asm))

jcalfee avatar Sep 23 '18 15:09 jcalfee

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

zlnvch avatar Sep 23 '18 15:09 zlnvch

It takes a major change in the c++ to change the wasm.. Wasm is a sub-set of c++...

jcalfee avatar Sep 23 '18 15:09 jcalfee

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.

zlnvch avatar Sep 23 '18 16:09 zlnvch

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.

jcalfee avatar Sep 23 '18 16:09 jcalfee

@znebby

Did you have any luck with this?

lachlangreenbank avatar Nov 25 '18 09:11 lachlangreenbank

Sorry, have not tried since then. Perhaps it can be done in eosjs2?

zlnvch avatar Nov 25 '18 10:11 zlnvch