wrap-cli icon indicating copy to clipboard operation
wrap-cli copied to clipboard

Modify Ethereum plugin config to use Connections store

Open krisbitney opened this issue 3 years ago • 1 comments

This PR modifies the Ethereum plugin's config to use a Connections store. The Connections class stores Connection instances. App developers can keep a reference to the Connections store and use it to update networks and providers without reconstructing the plugin or client.

Original spec: https://hackmd.io/@polywrap-dao/BkQdG7NAq

Implemented UX:

const connections = new Connections({
  networks: {
    mainnet: new Connection({ provider }),
    rinkeby: new Connection(...)
  },
  defaultNetowrk: "mainnet"
})

ethereumPlugin({
  connections
});

// assign a Connection to "ropsten" network, or replace existing connection if ropsten exists in store
connections.set("ropsten", new Connection({ provider: newProvider })); // pass a Connection
connections.set("ropsten", newProvider); // or pass a provider directly

// get a Connection and call setProvider to avoid constructing a new Connection instance
connections.get("rinkeby").setProvider(newProvider);

An alternative implementation might hide the Connection class. In that case, users would provide a config identical to the one we currently use but wrapped in a Connections constructor.

possible alternative:

const connections = new Connections({
  networks: {
    mainnet: { provider },
    rinkeby: { ... }
  },
  defaultNetowrk: "mainnet"
})

ethereumPlugin({
  connections
});

// set "mainnet" network to given ConnectionConfig. 
// This will call setProvider if "mainnet" is found in the store, or create a new Connection otherwise
connections.set("mainnet", { provider: newProvider });

Feedback is welcome!

krisbitney avatar Aug 16 '22 07:08 krisbitney

Added support for passing providers directly into connections.set, to match the original spec:

connections.set("ropsten", newProvider);

krisbitney avatar Aug 17 '22 16:08 krisbitney