You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The only advantage that this library offers over the much simpler solution is that the type hinting works in vim and VSCode and such.
I don't think that's it's worth the extra ONE THOUSAND lines.
There's probably some TypeScript trickery to overload the function type based on a constant as the first parameter that could do just as well as .d.ts or some such.
After years of submitting bugfixes and refactoring this library a little at a time, I finally found out what it actually does - which is so simple by comparison to how this C++ to JS port (I assume) turned out (I assume by the bitcoin team) that it's difficult to believe.
I'm not even sure if the people who use this regularly know what does due to all of the complex and abstract metaprogramming obscuring the functionality (otherwise I imagine they wouldn't use it).
It's just a really, really complicated way to do an http call that's actually this simple:
functioncreateRpcClient({ baseUrl, username, password }){letbasicAuth=btoa(`${username}:${password}`);asyncfunctionrequest(rpcname, ...args){rpcname=rpcname.toLowerCase();letid=getRandomId();letbody={id: id,method: rpcname,params: args};letpayload=JSON.stringify(body);letresp=awaitfetch(rpcBaseUrl,{method: 'POST',headers: {'Authorization': `Basic ${basicAuth}`,'Content-Type': 'application/json'},body: payload,});letdata=awaitresp.json();if(data.error){leterr=newError(data.error.message);Object.assign(err,data.error);throwerr;}letresult=data.result||data;returnresult;}return{
request,};}// not even sure if this range is requiredfunctiongetRandomId(){letf64=Math.random()*100000;leti32=Math.round(f64);returni32;}
Adding some flourish
init()
And if you wanted to make it convenient, you could add an init() method that loops until E_IN_WARMUP disappears:
The argument could be made that this provides some type hinting, but it doesn't even work with tsc or vim or VSCode.
It's done in such a bespoke way, that can't be auto-generated to keep up with the actual Dash RPCs, so it's worse to have it than to not having it at all.
If there were some machine-friendly JSON file for type hints, it could very simply be applied to each argument at the time each request is made:
functionrequest(rpcname, ...args){rpcname=rpcname.toLowerCase();assertTypes(rpcname,args);// ...}// the hundred+ different rpc names and types go here letallTypeHints={'getfoothing': [['number'],['number','string',null]]};functionassertTypes(rpcname,args){lettypeHints=allTypeHints[rpcname];for(leti=0;i<args.length;i+=1){letarg=args[i];lettypeHint=typeHints[i];assertType(typeHint,arg,i);}}functionassertType(typeHint,arg,i){if(!typeHint){// may be a new extra arg we don't know yetreturn;}letthisType=typeofarg;letisType=typeHint.includes(typeHint);if(isType){// is a known arg of a known typereturn;}letisNullish=!arg&&'boolean'!==thisType&&typeHint.includes(null);if(isNullish){// is an optional argreturn;}thrownewError(`expected params[${i}] to be one of [${typeHint}], but got '${thisType}'`);}
Update - Copied into DashTx
The simple rpc function has been moved to
DashTx.utils.rpc(method, ...params)
:It may also be copied into other projects as a little copying is better than a little dependency.
Original
See dashpay/dashd-rpc#77.
The only advantage that this library offers over the much simpler solution is that the type hinting works in vim and VSCode and such.
I don't think that's it's worth the extra ONE THOUSAND lines.
There's probably some TypeScript trickery to overload the function type based on a constant as the first parameter that could do just as well as
.d.ts
or some such.Update: Yep: dashpay/dashd-rpc#77 (comment)
Copied from the Linked Issue
After years of submitting bugfixes and refactoring this library a little at a time, I finally found out what it actually does - which is so simple by comparison to how this C++ to JS port (I assume) turned out (I assume by the bitcoin team) that it's difficult to believe.
I'm not even sure if the people who use this regularly know what does due to all of the complex and abstract metaprogramming obscuring the functionality (otherwise I imagine they wouldn't use it).
It's just a really, really complicated way to do an http call that's actually this simple:
DashRPC, in truth:
DashRPC, as a JS function:
Here's the library reimplemented in just a few lines:
Source: DashTx.js
DashRPC, as a JS Lib:
Here's the library reimplemented with all the JavaScript niceties you'd want:
Adding some flourish
init()
And if you wanted to make it convenient, you could add an
init()
method that loops untilE_IN_WARMUP
disappears:Type Hinting
The argument could be made that this provides some type hinting, but it doesn't even work with tsc or vim or VSCode.
It's done in such a bespoke way, that can't be auto-generated to keep up with the actual Dash RPCs, so it's worse to have it than to not having it at all.
If there were some machine-friendly JSON file for type hints, it could very simply be applied to each argument at the time each request is made:
Alternatively the type hinting could be generated as a build step... but it would result it thousands of extra lines of code (I know because I experimented with it already: https://github.com/dashhive/DashRPC.js/blob/v20.0.0/scripts/generate.js)
The text was updated successfully, but these errors were encountered: