Module Overview#

Description#

The greenweb.xch sub-module allows interacting with the Chia blockchain. This includes, but is not limited to:

  • fetching an account's XCH balance
  • getting block details
  • subscribing to puzzle hash updates
  • sending Chia to an address
  • accepting an offer

Provider is the interface that exposes the methods.

LeafletProvider implements Provider and can be used to connect to leaflet nodes. GobyProvider tries to connect to a user's Goby Wallet extension. To use multiple providers, one can use MultiProvider. PrivateKeyProvider requires a private key to function and is mainly used for testing.

To see the functions implemented by each provider, please see this page.

Available functions#

Please see this page.

createProvider#

If you want to use multiple providers, you can use the createProvider helper function.

export type CreateProviderArgs = {
    leafletHost?: string,
    leafletAPIKey?: string,
    leafletPort?: number,
    useGoby?: boolean,
    gobyTryNonInteractiveConnect?: boolean,
    network?: Network,
    privateKey?: string,
};

// Function definition:
static createProvider({
  leafletHost = "leaflet.fireacademy.io",
  leafletAPIKey,
  leafletPort = 18444,
  useGoby = false,
  gobyTryNonInteractiveConnect = true,
  network = Network.mainnet,
  privateKey,
}: CreateProviderArgs): void {
// example usage:
greenweb.xch.createProvider({
  leafletAPIKey: 'TEST-API-KEY',
  useGoby: true,
});

Wrapper#

The sub-module is also provides a wrapper around Provider: simply call its setProvider function with the desired provider instance as an argument and you'll be able to call any function on greenweb.xch instead of the instance:

// works
const provider = new greenweb.xch.providers.LeafletProvider('leaflet.fireacademy.io', 'TEST-API-KEY');
provider.connect().then(() => {
  provider.getBalance({
    address: "xch1k6mv3caj73akwp0ygpqhjpat20mu3akc3f6xdrc5ahcqkynl7ejq2z74n3"
  }).then(balance => console.log(balance.toNumber()));
});
// also works
const provider = new greenweb.xch.providers.LeafletProvider('leaflet.fireacademy.io', 'TEST-API-KEY');
greenweb.xch.setProvider(provider);
greenweb.xch.connect().then(() => {
  greenweb.xch.getBalance({
    address: "xch1k6mv3caj73akwp0ygpqhjpat20mu3akc3f6xdrc5ahcqkynl7ejq2z74n3"
  }).then(balance => console.log(balance.toNumber()));
});
// does not work, unless .setProvider() was called previously
const provider = new greenweb.xch.providers.LeafletProvider('leaflet.fireacademy.io', 'TEST-API-KEY');
greenweb.xch.connect().then(() => {
  greenweb.xch.getBalance({
    address: "xch1k6mv3caj73akwp0ygpqhjpat20mu3akc3f6xdrc5ahcqkynl7ejq2z74n3"
  }).then(balance => console.log(balance.toNumber()));
});