0.2.22 • Published 4 days ago

@electra.finance/sdk v0.2.22

Weekly downloads
-
License
ISC
Repository
github
Last release
4 days ago

npm version npm bundle size (version) Downloads

Overview

Electra Software Developer Kit is a futures trading library for JavaScript and TypeScript. It provides a set of tools for building trading bots, price feeds, and other applications on top of Electra Finance.

API Key

Electra’s SDK is free to use and does not require an API key or registration. Refer to integration examples for more detailed information.

Install

npm i @electra.finance/sdk

Usage

Initialization

:warning: Ethers ^5.6.0 required

// Node.js
import { Unit, Electra } from "@electra.finance/sdk";
import { Wallet } from "ethers";

const electra = new Electra();
const unit = electra.getUnit("bsc"); // eth, bsc, ftm, polygon, okc available
const wallet = new Wallet("0x...", unit.provider);
// Unit is chain-in-environment abstraction
// Metamask
import { Unit } from "@electra.finance/sdk";
import detectEthereumProvider from "@metamask/detect-provider";
import { BaseProvider } from "@metamask/providers";
import { providers } from "ethers";

const startApp = async (provider: BaseProvider) => {
  const web3Provider = new providers.Web3Provider(provider);
  await web3Provider.ready;
  const signer = web3Provider.getSigner(); // ready to go
  const electra = new Electra();
  const unit = electra.getUnit("eth"); // ready to go
};

detectEthereumProvider().then((provider) => {
  if (provider) {
    startApp(provider as BaseProvider);
  } else {
    console.log("Please install MetaMask!");
  }
});

Crosschain methods

Get pairs

const pairs = await electra.getPairs("futures"); // 'futures'

// Response example:
// {
//   'BTCUSDF': [ '1', '56' ],
//   'ETHUSDF': [ '1', '56', '137' ],
// }

Chain-specific methods

Deposit and withdraw

To make deposits and withdrawals, you need to use package @electra.finance/contracts.

import { IsolatedMarginCFD__factory } from "@electra.finance/contracts";

const cfdContractAddress = "0x0000000000000000000000000000000000000000";
const cfdContract = IsolatedMarginCFD__factory.connect(cfdContractAddress, signer);

const deposit = await cfdContract.depositAsset("12423450000"); // Deposit
const withdraw = await cfdContract.withdrawAsset("12423450000"); // Withdraw

Get aggregated orderbook

import { simpleFetch } from "simple-typed-fetch";

const orderbook = await simpleFetch(unit.aggregator.getAggregatedOrderbook)(
  "BTCUSDF",
  20 // Depth
);

Get historical price

import { simpleFetch } from "simple-typed-fetch";

const candles = await simpleFetch(unit.priceFeed.getCandles)(
  "BTCUSDF",
  1650287678, // interval start, unix timestamp
  1650374078, // interval end, unix timestamp
  "5m" // '5m' or '30m' or '1h' or '1d',
);

Get tradable pairs

import { simpleFetch } from "simple-typed-fetch";
const pairsList = await simpleFetch(unit.aggregator.getPairsList)("futures");
console.log(pairsList); // ['ETHUSDF, 'BTCUSDF']

Get deposits and withdrawals

import { simpleFetch } from "simple-typed-fetch";
const depositsAndWithdrawals = await simpleFetch(unit.blockchainService.getCFDHistory)(
  "0x0000000000000000000000000000000000000000", // Some wallet address
);
console.log(depositsAndWithdrawals);

Get available contracts

import { simpleFetch } from "simple-typed-fetch";
const contracts = await simpleFetch(unit.blockchainService.getCFDContracts)();
console.log(contracts);

Place order

import { simpleFetch } from "simple-typed-fetch";
import { signCFDOrder } from "@electra.finance/sdk";

const {
  matcherAddress, // The address that will transfer funds to you during the exchange process
} = await simpleFetch(unit.blockchainService.getInfo)();

const signedOrder = signCFDOrder(
  "0x0000000000000000000000000000000000000000", // instrumentAddress, you can retrieve list of available instruments from blockchainService.getCFDContracts()
  'BUY', // side: 'BUY' | 'SELL'
  "0.34543", // price
  "245234.234", // amount
  "matcherFee", // matcherFee
  "0xfffffffffffffffffffffffffffffffffffffff", // senderAddress
  matcherAddress,
  false, // usePersonalSign
  signer, // pass here ethers.Signer instance
  chainId: SupportedChainId.BSC,
  "0.34", // optional stopPrice
  false // isFromDelegate — if true, then the order will be placed on behalf of the delegate
);

const { orderId } =  = await simpleFetch(unit.aggregator.placeCFDOrder)(signedOrder);
console.log(`Order placed: ${orderId}`);

Aggregator WebSocket

Available subscriptions:

CFD_ADDRESS_UPDATES_SUBSCRIBE = 'ausf', // Orders history, positions info, balances info
AGGREGATED_ORDER_BOOK_UPDATES_SUBSCRIBE = 'aobus', // Bids and asks

Balances and order history stream

unit.aggregator.ws.subscribe(
  "ausf", // CFD_ADDRESS_UPDATES_SUBSCRIBE — orders, positions, balances
  {
    payload: "0x0000000000000000000000000000000000000000", // Some wallet address
    callback: (data) => {
      switch (data.kind) {
        case "initial":
          if (data.orders) console.log(data.orders); // All orders. "orders" is undefined if you don't have any orders yet
          if (data.balances) console.log(data.balances); // All balances
          break;
        case "update": {
          if (data.orders) console.log("Order update", data.orders); // Since this is an update message, the orders only contain the changed orders
          if (data.balances) console.log("Balance update", data.balances); // Since this is an update message, the balances only contain the changed assets
        }
      }
    },
  }
);

unit.aggregator.ws.unsubscribe("0x0000000000000000000000000000000000000000");

Orderbook stream

unit.aggregator.ws.subscribe("aobus", {
  payload: "ETHUSDF", // Some trading pair
  callback: (asks, bids, pairName) => {
    console.log(`${pairName} orderbook asks`, asks);
    console.log(`${pairName} orderbook bids`, bids);
  },
});

unit.aggregator.ws.unsubscribe("ETHUSDF");

Aggregator WS Stream Unsubscribing

// Asset pairs config updates unsubscribe
unit.aggregator.ws.unsubscribe("apcu");

Price Feed Websocket Stream

const allTickersSubscription = unit.priceFeed.ws.subscribe("allTickers", {
  callback: (tickers) => {
    console.log(tickers);
  },
});
allTickersSubscription.unsubscribe();
unit.priceFeed.ws.unsubscribe("allTickers", allTickersSubscription.id); // Also you can unsubscribe like this

const tickerSubscription = unit.priceFeed.ws.subscribe("ticker", {
  callback: (ticker) => {
    console.log(tricker);
  },
  payload: "ETHUSDF",
});
tickerSubscription.subscription();
unit.priceFeed.ws.unsubscribe("ticker", tickerSubscription.id);

const lastPriceSubscription = unit.priceFeed.ws.subscribe("lastPrice", {
  callback: ({ pair, price }) => {
    console.log(`Price: ${price}`);
  },
  payload: "ETHUSDF",
});
lastPriceSubscription.unsubscribe();
unit.priceFeed.ws.unsubscribe("lastPrice", lastPriceSubscription.id);

Data fetching

// Verbose way example

const getCandlesResult = await unit.priceFeed.getCandles(
  "ETHUSDF",
  1650287678,
  1650374078,
  "5m"
);
if (getCandlesResult.isErr()) {
  // You can handle fetching errors here
  // You can access error text, statuses
  const { error } = placeOrderFetchResult;
  switch (error.type) {
    case "fetchError": // Instance of Error
      console.error(error.message);
      break;
    case "unknownFetchError":
      console.error(`URL: ${error.url}, Error: ${error.message}`);
      break;
    case "unknownFetchThrow":
      console.error("Something wrong happened during fetching", error.error);
      break;
    // ... more error types see in src/fetchWithValidation.ts
  }
} else {
  // Success result
  const { candles, timeStart, timeEnd } = getCandlesResult.value;
  // Here we can handle response data
}
// Simple Fetch

const { candles, timeStart, timeEnd } = await simpleFetch(
  unit.priceFeed.getCandles
)("ETHUSDF", 1650287678, 1650374078, "5m");

// Here we can handle response data

Using contracts

Use package @electra.finance/contracts

0.2.22-rc0

4 days ago

0.1.57-rc4

6 days ago

0.1.57-rc5

6 days ago

0.2.22

6 days ago

0.2.21

1 month ago

0.2.20

1 month ago

0.2.19

2 months ago

0.2.18

2 months ago

0.2.17

2 months ago

0.1.57-rc0

2 months ago

0.1.57-rc1

2 months ago

0.1.57-rc2

2 months ago

0.1.57-rc3

2 months ago

0.2.14-rc100

2 months ago

0.2.16

2 months ago

0.2.15

2 months ago

0.2.14

2 months ago

0.2.14-rc0

2 months ago

0.2.13

2 months ago

0.2.12

2 months ago

0.2.11-rc1

2 months ago

0.2.11-rc0

2 months ago

0.2.11

2 months ago

0.2.10

2 months ago

0.2.12-rc100

2 months ago

0.2.12-rc0

2 months ago

0.2.9

2 months ago

0.2.7-rc6

2 months ago

0.2.7-rc7

2 months ago

0.2.7

2 months ago

0.2.6

2 months ago

0.2.8

2 months ago

0.2.7-rc5

2 months ago

0.2.7-rc2

2 months ago

0.2.7-rc4

2 months ago

0.2.7-rc3

2 months ago

0.2.7-rc0

2 months ago

0.2.5-rc6

2 months ago

0.2.7-rc1

2 months ago

0.2.5

2 months ago

0.2.4-rc6

2 months ago

0.2.4-rc5

2 months ago

0.2.4-rc4

2 months ago

0.2.4

2 months ago

0.2.5-rc4

2 months ago

0.2.5-rc3

2 months ago

0.2.5-rc5

2 months ago

0.2.5-rc1

2 months ago

0.2.6-rc1

2 months ago

0.2.6-rc0

2 months ago

0.2.5-rc0

2 months ago

0.2.4-rc3

3 months ago

0.2.4-rc2

3 months ago

0.2.4-rc1

3 months ago

0.2.4-rc0

3 months ago

0.1.52

9 months ago

0.1.53

9 months ago

0.1.54

9 months ago

0.1.50-rc1

10 months ago

0.1.55

9 months ago

0.1.56

8 months ago

0.1.57

8 months ago

0.1.50

10 months ago

0.1.51

9 months ago

0.1.49

10 months ago

0.1.46

10 months ago

0.1.48

10 months ago

0.1.35-rc43

9 months ago

0.1.35-rc44

9 months ago

0.1.52-rc1

9 months ago

0.2.1-rc1

7 months ago

0.1.35-rc30

10 months ago

0.1.35-rc31

10 months ago

0.1.58-rc0

7 months ago

0.1.58-rc1

7 months ago

0.1.35-rc29

10 months ago

0.1.35-rc41

9 months ago

0.1.35-rc42

9 months ago

0.1.35-rc40

9 months ago

0.1.46-transport2

10 months ago

0.1.46-transport1

10 months ago

0.1.46-transport4

10 months ago

0.1.46-transport3

10 months ago

0.1.46-transport7

10 months ago

0.1.56-rc0

8 months ago

0.1.35-rc38

9 months ago

0.1.35-rc39

9 months ago

0.1.35-rc36

9 months ago

0.1.35-rc37

9 months ago

0.1.35-rc34

10 months ago

0.1.35-rc35

9 months ago

0.1.35-rc32

10 months ago

0.1.35-rc33

10 months ago

0.1.53-rc0

9 months ago

0.1.53-rc1

9 months ago

0.1.53-rc2

9 months ago

0.1.53-rc3

9 months ago

0.1.51-rc4

9 months ago

0.1.51-rc3

9 months ago

0.2.1

7 months ago

0.2.0

7 months ago

0.1.51-rc2

9 months ago

0.1.51-rc1

10 months ago

0.2.3

6 months ago

0.2.2

6 months ago

0.1.46-prerelease

10 months ago

0.1.46-prerelease6

10 months ago

0.1.46-prerelease7

10 months ago

0.1.46-prerelease4

10 months ago

0.1.46-prerelease5

10 months ago

0.1.46-prerelease2

10 months ago

0.1.46-prerelease3

10 months ago

0.1.46-rc1

10 months ago

0.1.35-rc28

10 months ago

0.1.35-rc27

10 months ago

0.1.45

10 months ago

0.1.44

10 months ago

0.1.35-rc26

10 months ago

0.1.43

10 months ago

0.1.35-rc25

10 months ago

0.1.35-rc24

10 months ago

0.1.35-rc23

10 months ago

0.1.35-rc22

10 months ago

0.1.35-rc21

10 months ago

0.1.35-rc20

10 months ago

0.1.35-rc19

10 months ago

0.1.35-rc18

10 months ago

0.1.35-rc17

10 months ago

0.1.35-rc16

10 months ago

0.1.43-prerelease1

10 months ago

0.1.35-rc15

10 months ago

0.1.35-rc14

10 months ago

0.1.43-rc1

10 months ago

0.1.35-rc13

10 months ago

0.1.35-rc12

10 months ago

0.1.35-rc11

10 months ago

0.1.35-rc10

10 months ago

0.1.42

10 months ago

0.1.41

10 months ago

0.1.35-rc9

10 months ago

0.1.40

10 months ago

0.1.35-rc8

10 months ago

0.1.39

10 months ago

0.1.39-rc1

10 months ago

0.1.39-rc0

10 months ago

0.1.38

10 months ago

0.1.35-rc7

10 months ago

0.1.35-rc6

10 months ago

0.1.38-rc0

10 months ago

0.1.38-rc1

10 months ago

0.1.37-rc1

10 months ago

0.1.37

10 months ago

0.1.35-rc5

10 months ago

0.1.37-rc.4

10 months ago

0.1.37-rc.3

10 months ago

0.1.37-rc.2

10 months ago

0.1.37-rc.1

10 months ago

0.1.37-rc.0

10 months ago

0.1.36

11 months ago

0.1.35

11 months ago

0.1.36-rc2

11 months ago

0.1.34

11 months ago

0.1.35-rc4

11 months ago

0.1.36-rc1

11 months ago

0.1.35-rc3

11 months ago

0.1.35-rc2

11 months ago

0.1.35-rc1

11 months ago

0.1.35-rc0

11 months ago

0.1.34-rc0

11 months ago

0.1.33

11 months ago

0.1.32

11 months ago

0.1.31

11 months ago

0.1.31-rc

11 months ago

0.1.30

11 months ago

0.1.30-rc4

11 months ago

0.1.30-rc3

11 months ago

0.1.30-rc2

11 months ago

0.1.30-rc1

11 months ago

0.1.30-rc0

11 months ago

0.1.27

11 months ago

0.1.26

11 months ago

0.1.25

11 months ago

0.1.24

11 months ago

0.1.23

11 months ago

0.1.22

11 months ago

0.1.21

11 months ago

0.1.20

11 months ago

0.1.19

11 months ago

0.1.18

11 months ago

0.1.17

11 months ago

0.1.16

11 months ago

0.1.15

11 months ago

0.1.14

11 months ago

0.1.13

11 months ago

0.1.12

11 months ago

0.1.11

11 months ago

0.1.10

11 months ago

0.1.9

11 months ago

0.1.8

12 months ago

0.1.7

12 months ago

0.1.6

12 months ago

0.1.5

12 months ago

0.1.4

12 months ago

0.1.3

12 months ago

0.1.2

12 months ago

0.1.1

12 months ago

0.1.0

12 months ago