-
Notifications
You must be signed in to change notification settings - Fork 70
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Borsh #75
Conversation
Interesting, it behaves as if it was not finding the exports of borsh... could it be because of how the borsh.js was compiled/created? |
@gagdiez is right. the problem is the rollup configuration, it can only import ES modules, we need either package borsh in that format or tweak the rollup config. Enable non ES module imports in rollup, is possible, but that is without tree-shaking, and resulting bytecode would be larger |
I made an attempt to fix rollup configuration to use borsh, now import serialize/deserialize from borsh works. Now it fails for a different issue: borsh uses
|
Could we implement a Buffer object as a wrapper of uint8array and import it globally? Or use something like: https://github.com/feross/buffer Edit: I used "browserify" on
Now I am getting a |
@gagdiez Cool! A good step towards success! Let me further explore on your approach |
I'm able to make it compile too, by using https://github.com/feross/buffer, update cli.js to config rollup, use more gas since compiled contracts become bigger. And mutate the
The borsh test now fail of this error:
Looking at build/status-message-borsh.js, it correctly included https://github.com/feross/buffer, but it still doesn't work. When trying to import and log the buffer module directly:
It prints:
Which seems work. So I guess, import Buffer in borsh-js should also work, but I'm having trouble to modify and build from source: near/borsh-js#57 |
test-template is working locally, strange that it doesn't work on CI 🤔 |
|
// commonjs(), | ||
babel({ babelHelpers: 'bundled' }) | ||
babel({ babelHelpers: 'bundled' }), | ||
commonjs() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Note: Not sure why are we doing this change. Let's discuss it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
commonjs is used for import non-es packages, if I didn't remember it wrong, import commonjs module doesn't work without it.
import { NearContract, NearBindgen, call, view, near } from 'near-sdk-js' | ||
import { serialize, deserialize } from 'borsh'; | ||
import { panic } from '../../../src/api'; | ||
import { Buffer } from 'buffer/' |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not the best practice. Can we add it as a dependency?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, should do so
@@ -24,8 +24,8 @@ test.beforeEach(async t => { | |||
// Deploy and init the counter JS contract | |||
const counter = await root.createSubAccount('counter'); | |||
let contract_base64 = (await readFile('build/contract.base64')).toString(); | |||
await counter.call(jsvm, 'deploy_js_contract', Buffer.from(contract_base64, 'base64'), {attachedDeposit: '400000000000000000000000'}); | |||
await counter.call(jsvm, 'call_js_contract', encodeCall(counter.accountId, 'init', []), {attachedDeposit: '400000000000000000000000'}); | |||
await counter.call(jsvm, 'deploy_js_contract', Buffer.from(contract_base64, 'base64'), {attachedDeposit: '800000000000000000000000'}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The contract is 37! times bigger now :)
I think it's better not to support Borsh at all. But I believe we will be able to find the solution.
Update: ok, this is because we are packaging all the borsh code to the contract. That is not cool :/
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes this is how commonjs module worse than esm, rollup cannot handle just importing the used functions. And you found it's even worse that it also packaging all borsh code :(
"test": "ava" | ||
}, | ||
"author": "Near Inc <hello@nearprotocol.com>", | ||
"license": "Apache-2.0", | ||
"dependencies": { | ||
"near-sdk-js": "file:../../" | ||
"borsh": "https://github.com/near/borsh-js/tarball/import-buffer", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I do not understand how this branch is different and why are we using it here. Can we use fix-build
branch instead? (or maybe master if we will merge it).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It has a import {Buffer} from
buffer/` in borsh.ts, and borsh.js built from that. Without this line it doesn't work.
"@rollup/plugin-babel": "^5.3.1", | ||
"@rollup/plugin-commonjs": "^21.0.1", | ||
"@rollup/plugin-commonjs": "^22.0.0", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This change helps to fix status-message-collections
, but I have no idea why :/
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe some bug fixes happened in 22.0?
Perhaps due to commonjs increase template.js size (it depends on lodash). Ah i figure out why it work on my side - i didn't
Good to hear. Yes.
Got it. Is the problem only related to template and status-message-borsh? |
Status summary: we made borsh work by a series of hacks in this PR. But enabling commonjs modules enlarge the contract size significantly. We plan to ship borsh also in esm format to resolve |
Closed as completed by #361 |
It is throwing errors for now. Trying to find the reason.
The difference between JSON and borsh serialization is 1 line of code + schema. It will be great to have this logic in NearContract in the future.