vechain-sdk-js
vechain-sdk-js copied to clipboard
💡 [REQUEST] - Improve serverless (especially CloudFlare) support
Summary
My personal setup involves CloudFlare Workers a lot to create APIs or Vechain interaction with a very low effort.
To enable transaction building with connex I have a stripped version of the thor-devkit
that runs in node_compat
(NodeJS compatibility) mode.
I would like to use the SDK within CloudFlare Workers in compatibility mode as well without creating a stripped version to:
- Build transaction objects
- Sign transactions
- Interact with the Node-APIs
Ideas to create the compatibility are:
- Use global fetch or allow injection of fetch function
- Dynamically import modules, like
events
on demand instead
Basic Example
- Create a new CloudFlare Worker using
npx wrangler generate sdk-cf
- Add
@vechain/sdk-network
to it usingnpm install --save @vechain/sdk-network @vechain/sdk-core
- Create a thor client in the worker using
ThorClient.fromUrl('https://testnet.vechain.org')
- Test the Worker by using
wrangler dev
or if you have a CloudFlare account usewrangler deploy
- It will fail to start.
- Optimal Results would be a CloudFlare Worker that can use the SDK instantly without enable Node Compatibility in the next step.
- Add
node_compat = true
to thewrangler.toml
to enable NodeJS compatibility - Test the Worker by using
wrangler dev
or if you have a CloudFlare account usewrangler deploy
Using this simplified index.ts
:
import { ThorClient } from "@vechain/sdk-network";
export default {
async fetch(
): Promise<Response> {
const thor = ThorClient.fromUrl('https://testnet.vechain.org')
const block = await thor.blocks.getBestBlockRef()
return new Response(block);
},
};