Join 74,343 users and earn money for participation

electrum-cash: connecting to a server

4 139 exc boost
Avatar for JonathanSilverblood
Written by   236
1 year ago (Last updated: 11 months ago)

Introduction

Electrum-cash is a javascript/typescript library for communicating with electrum servers.

This example demonstrates how to create a connection to a server and how to cleanly shut down when you're done using it.

Including the client library in your project

When you are using a single server for your project, you should only include the client, either in CommonJS or as an ES2015 module depending on your project.

// Import the electrum client in CommonJS.
const { ElectrumClient } = require('electrum-cash');
// Import the electrum client as an ES2015 module.
import ElectrumClient from 'electrum-cash';

Set up the library and connect with a server

You will need to create an instance of the electrum client with the server connection electrum clientdetails for the server you want to use.

// Initialize an electrum client.
const electrum = new ElectrumClient('Your Electrum App Name', '1.4.3', 'bch.imaginary.cash');

Waiting for the connection to be established

Before you can start using the client, you need to connect to the server and wait for it to complete version negotiation.

// Wait for the client to connect and complete version negotiation.
await electrum.connect();

Shutting down when you're done

After you're done with the server, you should disconnect from it to shutdown gracefully.

// Close the connection.
await electrum.disconnect();

33
$ 1.23
$ 0.50 from @emergent_reasons
$ 0.28 from @TheRandomRewarder
$ 0.25 from @rosco
+ 2
Avatar for JonathanSilverblood
Written by   236
1 year ago (Last updated: 11 months ago)
Enjoyed this article?  Earn Bitcoin Cash by sharing it! Explain
...and you will also help the author collect more tips.

Comments

Hi I'm trying the sample code and get an error. TypeError: ElectrumClient is not a constructor in const electrum = new ElectrumClient('Your Electrum App Name', '1.4.3', 'bch.imaginary.cash'); I'm missing something

$ 0.00
6 months ago

Before you can use the library, you first need to add it to your project.

use "npm install electrum-cash", then in your source code add something like:

const { ElectrumClient } = require('electrum-cash');

$ 0.00
6 months ago

The nmp dependecies are up to date $ npm install electrum-cash

up to date, audited 10 packages in 2s

found 0 vulnerabilities -------------------- My code ------------ // Import the electrum client as an ES2015 module. import ElectrumClient from 'electrum-cash';

console.log(ElectrumClient.ClientState)

// Initialize an electrum client. const electrum = new ElectrumClient('Your Electrum App Name', '1.4.3', 'bch.imaginary.cash');

// Wait for the client to connect and complete version negotiation. await electrum.connect(); /* // The address we want to get balance for. const address = 'qqc9ccx0403cefp45n5knnauullel9y5xspyt56x4p';

// Request the balance from the server. const balance = await electrum.request('blockchain.address.get_balance', address);

// Print out the current balance, in satoshis. console.log(Balance for '${address}' is ${balance.confirmed} satoshis confirmed, and ${balance.unconfirmed} satoshis unconfirmed.); */ // Close the connection. await electrum.disconnect();

I'm able to log the clientState so the library is correctly loaded

$ node main.mjs { '0': 'UNAVAILABLE', '1': 'AVAILABLE', UNAVAILABLE: 0, AVAILABLE: 1 } file:///media/datos/work/yo/bchDemo/main.mjs:8 const electrum = new ElectrumClient('Your Electrum App Name', '1.4.3', 'bch.imaginary.cash');


It finally worked updating the client initialization as follows const electrum = new ElectrumClient.ElectrumClient('Your Electrum App Name', '1.4.3', 'bch.imaginary.cash');

$ 0.00
6 months ago

Try this:

import { ElectrumClient } from 'electrum-cash';

$ 0.00
6 months ago