AssemblyScript CDK Alpha Examples

I’ve been developing an AssemblyScript CDK for the IC and have put together some examples for how to use it.

If anyone who is excited about DFINITY has interested in AssemblyScript I would appreciate checking out my repos and attempting to run my examples. I currently develop on a Mac so a lot of the instructions I have cater to Mac users. WABT is required, which can be brew installed, to make modifications to the WAT to fit DFINITY nuances.

You can view the source of cdk-as here

AssemblyScript has its limitations and I suggest checking out its quirks

I would like to point out that the Rust CDK and Motoko are light years ahead of the AS CDK, things like cross canister calls and a proper way of doing storage have not made its way into the current release.

Limitations

  • Int/Nat are arbitrary length integers and currently there is no library in assemblyscript, so you must use Int8/16/32/64
  • Cross canister calls have not been completed yet.
  • Recursive types or Cyclical types
  • Not all AS types can be Nullable
  • Web/Assets, technically it can support these, but I have not found a simple way to ship didc into the CLI

Complete list of supported ic0 calls and types

What examples would you like to see?

Feedback appreciated!

8 Likes

Nice work!

The didc binary is released here: Releases · dfinity/candid · GitHub. You can also build the Rust crate as a Wasm module, and call from JS.

2 Likes

Nice! I’ll come up with a technique to download it based off the current system, thanks!!

I build it locally, just not sure what dependencies are required, looks like its self contained.

Nice! How did you figure out how to use the IC’s System API?

Also, what you refer to as “DFINITY Types” is probably best called “Candid Type”, as this is the Candid interface language. I assume you saw candid/Candid.md at master · dfinity/candid · GitHub

2 Likes

My daily resources for figuring out the ICs System API:

cdk-rs/ic0.rs at next · dfinity/cdk-rs · GitHub for understanding what each IC API does

candid/tools/didc at master · dfinity/candid · GitHub Helps quite a bit in debugging as well as building a motoko project and making requests with --output raw.

wasm2wat demo for converting a motoko/rust/c/as built WASM back to WAT. This is where you discover how your WASM export/imports should look like and if yours is building correctly.

Candid Specification :: Internet Computer Mapping TypeTable IDs and general documentation

@dfinity/agent More encode/decode help with generating the TypeTable (which is a reversed version of the Motoko/Rust version, this is tricky when you have custom types and need to increment their ids (not sure what the correct term for these are) in the correct order)

GitHub - enzoh/counter: A Counter for the Internet Computer in C Enzos C example is the Hello World of DFINITY CDK development

AssemblyScript discord: dcode and MaxGraey respond within minutes (do they sleep?)

I still have things to consider.

  • Would I just include the Assets WASM in my CLI? Where does the assets.wasm actually come from when I build a project? (High priority)
  • How would do I create better unit tests? It would be interesting to have a DFINITY Node.JS that loads my DFX canister and can run a bunch of requests against it to test my canister. This could be a community project. (Medium priority)
  • Support recursive types. It just needs to be implemented, but I want to switch how the assemblyscript is transformed/generated first. (Medium priority)
  • BigInt in AS Reference Source (example Impl by Microsoft) Has yet to be implemented in AS and I believe it could support the AS community. (Medium priority)
  • For cross canister calls, I usually just hardcode the ID of the other canister, how would I discover this when building for a canister not in my project? (Low priority)
  • How do I get AS to support spaces in exports? This requires changes to AssemblyScript compiler (Low priority as WABT/Regex can fix this)

Lots more things to think about, but it’s a WIP.

Again if anyone is passionate about AS and DFINITY, feel free to create Issues on GitHub and we can start knocking them down.

5 Likes

That’s a simple Motoko-based canister implementing a key-value store, that is pre-compiled and shipped with the SDK. The source (which is boring) will hopefully be available soon (just like all the other source…)

I think that’s the best you can do right now: For canisters within your project, dfx can maintain a mapping from canister alias to installed-id. For external canisters, some kind of DNS-like registry does not exist yet, so hard-coding canister ids is the way to go.

Heh, yes. Wasm is quite liberal and allows spaces, but not unexpectedly, some tools don’t support that. Did you report that issue upstream?

1 Like

This is great thank you. It looks like I had some misconceptions.

  • dfx build automatically detects the “asset” type from the dfx.json and includes it in the build so I don’t actually need to do anything but have a properly configured dfx.json
  • I don’t actually need didc because it appears by having a properly define web asset it automatically generates my .did.js based off my .did

I was able to clone the Motoko phonebook example in AssemblyScript and leverage the same UI by just correctly configuring my dfx.json, cool stuff.

6 Likes

This is amazing! Great work. Can’t wait to use it.

1 Like