by splintercell · 8y ago · 0 comments
``` const contract = new web3.eth.Contract(ABI, contractAddress, {from}) ```
in ReasonML easily, despite spending hours on trying to figure it out. At the end I finally reached to a feature request asking to be able to do 'new' on member functions, and it was in 'planning'.
On the other hand Elm's approach to JS interop seemed to be far superior for quirks like these. Javascript code lives on the other side of the border, and Elm code on this. You don't have to create a type for the whole class of the third party library, just the data you want Elm to manage.
module Web3 = {
module Eth = {
module Contract = {
type t;
[@bs.module "web3"] [@bs.scope "eth"] [@bs.new]
external make: (string, string, {. "from": string}) => t = "Contract";
};
};
};
let contract = Web3.Eth.Contract.make("ABI", "contactAddress", {"from": "from"});
You can try it out in the interactive playground.Also, regarding this:
> You don't have to create a type for the whole class of the third party library, just the data you want Elm to manage.
That's exactly how Reason JavaScript bindings work too.
I am going to try what you suggested, but keep in mind I did go to the slack channel and tried asking this, none of their solutions looked like yours. Either way, intuitively @bs.new and @bs.send should be able to work together.
For those not familiar with Elm it means you basically have to interact with all JS code through a pub-sub model and can't ever directly call JS code.
Elm's creator has his reasons for why he does this (https://guide.elm-lang.org/interop/ports.html), but it can make calling JS from Elm quite a pain.
I do the same thing in Elm, but all that code is living in a JS file, and it can be in Typescript/whatever.