1.5.9 • Published 1 year ago

@caskprotocol/sdk v1.5.9

Weekly downloads
-
License
MIT
Repository
github
Last release
1 year ago

Overview

The Cask SDK is a lower level API for interacting with the Cask Protocol using javascript.

For a simple to use, fully functional checkout widget, see the Cask Widgets repository.

The SDK code repository is located at https://github.com/CaskProtocol/cask-js-sdk.

Full SDK reference is available at https://caskprotocol.github.io/cask-js-sdk/.

Installation

npm install --save @caskprotocol/sdk

# OR

yarn add @caskprotocol/sdk

Setup

// require
const { CaskSDK } = require('@caskprotocol/sdk');

// modules
import { CaskSDK } from '@caskprotocol/sdk';

Usage

Quick Start

import { CaskSDK } from '@caskprotocol/sdk';

// setup cask instance using web3 connections provided by the signer 
const cask = new CaskSDK({
    environment: CaskSDK.environments.TESTNET,
    connections: {
        signer: web3Provider,
    },
    ipfs: {
        pinataApiKey: process.env.PINATA_API_KEY,
        pinataApiSecret: process.env.PINATA_API_SECRET,
    }
});

// initialize cask connections to the default chain for the testnet
await cask.init();

// show current balance of spendable USDC in Cask 
console.log(`Current Cask balance: ${await cask.vault.balance()}`);

// deposit 100 USDC to Cask balance
await cask.vault.approveAndDeposit({asset: 'USDC', amountSimple: 100});


const providerAddress = '0x....';
const planId = 12345;

const providerProfile = await cask.subscriptionPlans.loadProfile({address: providerAddress});
const planInfo = providerProfile.getPlan(planId);

console.log(`Subscribing to plan ${planInfo.name}`)


// subscribe to the specified service provider's plan 12345
const resp = await cask.subscriptions.create({provider: providerAddress, planId});
console.log(`Created subscription ${resp.subscriptionId}`);

cask.stop();

Static Utilities

The CaskSDK is both a constructor that creates a stateful instance to the set of Cask functionality as well as a namespaced set of utilities and helpers.

The full details of the various helpers and utilities be found in the SDK reference. Each namespace below is a link to the reference documentation for that helper/utility.

NameDescription
CaskSDK.abiAccess the various Cask contract ABIs
CaskSDK.chainsInformation about the various chains supported by the Cask protocol
CaskSDK.defaultChainsContains the map of which chains are supported in the various enviroments (testnet, production, etc..)
CaskSDK.deploymentsContains the addresses of the Cask protocol contracts on a given environment and chain
CaskSDK.environmentsCask protocol environments
CaskSDK.unitsUtilities and constants that represent financial unit formatting
CaskSDK.contractsHelpers to create ethers.Contract instances of the various Cask protocol contracts
CaskSDK.encData encryption
CaskSDK.ipfsIPFS reading/writing
CaskSDK.utilsLow level utilities such as data encoding and signing

Configuration

The top-level CaskSDK instance accepts a configuration object that is shared among all the provided services. This is also where the blockchain connections are established including the read-only and read-write (for sending txns) and websockets (for event listeners) are configured.

The top-level configuration keys are:

NameDescription
environmentWhich Cask environment to run in (testnet, production, etc)
connectionsEthers/web3 connections for read, read/write and websockets
ipfsConfigure which IPFS service to use
encData encryption configuration (if using)
eventsEvent listener configuration
pricesPrice feed configuration
defaultUnitsDefault unit formatting configuration

environment

Configure which Cask protocol environment in which to interact.

The environment can be one of:

  • CaskSDK.environments.TESTNET
  • CaskSDK.environments.PRODUCTION
  • CaskSDK.environments.DEVELOPMENT

connections

connections has 3 sub-objects with keys rpc, signer and ws with the value being a map of Chain ID to provider/signer configurations:

connections: {
    rpc: ...,
    signer: ...,
    ws:...
}

The value for an rpc configuration can be a string of an http(s) URL or an instance of an ethers Provider such as ethers.providers.JsonRpcProvider.

The value for a signer configuration must be an ethers Signer such as an ethers.Wallet.

The value for a ws (websocket) configuration should be a string to a wss URL. The ws entries are only required if using the events service.

If desired, rpc can be omitted, and any read-only operations will be done via the provider associated with the signer instead.

If no signer is provided, the SDK will be limited to read-only actions.

Any service method that expects an address will use the signer instead, if the address is not supplied.

Example:

connections: {
  rpc: {
    [CaskSDK.chains.POLYGON_MUMBAI.chainId]: process.env.MUMBAI_PROVIDER_URL,
    [CaskSDK.chains.AVAX_TESTNET.chainId]: process.env.FUJI_PROVIDER_URL,
  },
  signer: {
    [CaskSDK.chains.POLYGON_MUMBAI.chainId]: new ethers.Wallet(process.env.WALLET_PK, process.env.MUMBAI_PROVIDER_URL),
    [CaskSDK.chains.AVAX_TESTNET.chainId]: new ethers.Wallet(process.env.WALLET_PK, process.env.FUJI_PROVIDER_URL),
  },
  ws: {
    [CaskSDK.chains.POLYGON_MUMBAI.chainId]: process.env.MUMBAI_WEBSOCKET_URL,
    [CaskSDK.chains.AVAX_TESTNET.chainId]: process.env.FUJI_WEBSOCKET_URL,
  }
}

ipfs

Configure where IPFS read and write operations are performed.

Keys are:

  • ipfsProvider - Valid values are one from CaskSDK.ipfs.providers.<PROVIDER> and defaults to CaskSDK.ipfs.providers.PINATA.
  • ipfsGateway - URL to IPFS gateway used for read operations, with the default being Cask's IPFS gateway.

Any additional configuration items are provider-specific.

PINATA IPFS provider configuration:

  • pinataApiKey - Pinata API key
  • pinataApiSecret - Pinata API secret

Example:

ipfs: {
  pinataApiKey: process.env.PINATA_API_KEY,
  pinataApiSecret: process.env.PINATA_API_SECRET,
}

enc

TODO...

events

Configure the event listener.

Keys are:

  • enabled - true/false - Enable/disable the event listener. Default is false.
  • debug - true/false - Enable event debugging to console logging. Default is false.

prices

Configure the price/balance service. To enable, configure the interval, and all assets in the Cask protocol vault will be tracked. See the cask.prices.balance() and cask.prices.usdPrice() methods to read prices and balances.

Keys are:

  • walletAddress - Address to perform balance checks on. If not supplied and a signer was supplied in the connections, the address will be detected from there.
  • interval - Interval (in ms) in which to refresh prices/balances.
  • priceMaxAge - Max age (in seconds) for valid price data. If set, and a read is performed on an asset and the last asset price was acquired more than priceMaxAge seconds ago, an exception will be raised.

Units

Any method that accepts an amount or price accepts it with a suffix on the variable name with either:

  • Asset - Amount is expected to be a string containing the full asset decimals per its ERC-20 configuration
  • Simple - Expected to be a simple javascript floating point value, irrespective of the ERC-20 decimals.

For example, the cask.vault.deposit() method can accept either amountSimple or amountAsset.

Any method that returns a value can accept two input parameters of units and unitFormat. The units can be one of:

  • CaskSDK.units.SIMPLE - Basic floating point representation of values
  • CaskSDK.units.ASSET - String representation of values with the full decimals as defined by the ERC-20 asset.
  • CaskSDK.units.NUMERAL - Formatted value using the javascript numeraljs package with a default format using abbreviations and 2 decimals.

And the unitFormat is dependent on the format type. For CaskSDK.units.NUMERAL, the valid parameters are:

  • format - The numeraljs format to use to format the amount. The default can be found in CaskSDK.units.DEFAULT_NUMERAL_FORMAT.

Example:

// deposit 25.75 USDC into vault
cask.vault.deposit({asset: 'USDC', amountSimple: 25.75});

// return balance is NumeralJS string with three decimals and no abbreviation
const currentBalance = await cask.vault.balance({unit: CaskSDK.units.NUMERAL, unitFormat: {format: '0,0.000'}});

Services

The instance returned from instantiating an CaskSDK object contains all the services needed to interact with the Cask platform.

The full details of the API of each service can be found in the SDK reference. Each service below is a link to the reference documentation for that service.

NameDescription
cask.vaultInteract with the Cask vault such as depositing and withdrawing funds
cask.subscriptionsInteract with the Cask subscriptions service such as creating a new subscription, canceling an existing subscription, etc
cask.subscriptionPlansInteract with the Cask subscription plans such as becoming a service provider, setting up plans, etc
cask.eventsA service to register event listeners on the various Cask protocol contracts
cask.pricesA service to efficiently get asset prices and user balances
cask.dcaInterface with the Auto-Buy service

Testing

To run the SDK tests you need to have the Cask contracts running. In addition, you need to fund the test accounts with the mock stablecoins so they have funds for deposits, subscriptions, etc.

From within the cask-contracts repo:

yarn hardhat node # starts local blockchain running in the foreground

# in another shell, fund the test users with stables
yarn local fund

Then from within this repo:

yarn test
1.5.9

1 year ago

1.5.8

1 year ago

1.5.7

1 year ago

1.5.5

1 year ago

1.5.4

1 year ago

1.5.6

1 year ago

1.5.3

1 year ago

1.5.2

1 year ago

1.5.1

1 year ago

1.5.0

1 year ago

1.4.4

1 year ago

1.4.3

1 year ago

1.4.2

2 years ago

1.4.1

2 years ago

1.4.0

2 years ago

1.3.4

2 years ago

1.3.3

2 years ago

1.3.2

2 years ago

1.3.1

2 years ago

1.3.0

2 years ago

1.2.29

2 years ago

1.2.30

2 years ago

1.2.31

2 years ago

1.2.12

2 years ago

1.2.13

2 years ago

1.2.11

2 years ago

1.2.16

2 years ago

1.2.17

2 years ago

1.2.14

2 years ago

1.2.15

2 years ago

1.2.18

2 years ago

1.2.19

2 years ago

1.2.20

2 years ago

1.2.23

2 years ago

1.2.24

2 years ago

1.2.21

2 years ago

1.2.22

2 years ago

1.2.27

2 years ago

1.2.28

2 years ago

1.2.25

2 years ago

1.2.26

2 years ago

1.2.0

2 years ago

1.2.8

2 years ago

1.2.7

2 years ago

1.2.6

2 years ago

1.2.5

2 years ago

1.2.4

2 years ago

1.2.3

2 years ago

1.2.2

2 years ago

1.2.1

2 years ago

1.2.10

2 years ago

1.1.9

2 years ago

1.1.12

2 years ago

1.1.11

2 years ago

1.1.10

2 years ago

1.1.16

2 years ago

1.1.15

2 years ago

1.1.14

2 years ago

1.1.13

2 years ago

1.1.19

2 years ago

1.1.18

2 years ago

1.1.17

2 years ago

1.2.9

2 years ago

1.1.22

2 years ago

1.1.21

2 years ago

1.1.20

2 years ago

1.1.8

2 years ago

1.1.7

2 years ago

1.1.6

2 years ago

1.1.5

2 years ago

1.1.4

2 years ago

1.1.3

2 years ago

1.1.2

2 years ago

1.1.1

2 years ago

1.1.0

2 years ago

1.0.52

2 years ago

1.0.51

2 years ago

1.0.50

2 years ago

1.0.49

2 years ago

1.0.48

2 years ago

1.0.47

2 years ago

1.0.46

2 years ago

1.0.45

2 years ago

1.0.44

2 years ago

1.0.43

2 years ago

1.0.42

2 years ago

1.0.41

2 years ago

1.0.40

2 years ago

1.0.32

2 years ago

1.0.31

2 years ago

1.0.30

2 years ago

1.0.26

2 years ago

1.0.25

2 years ago

1.0.24

2 years ago

1.0.23

2 years ago

1.0.22

2 years ago

1.0.21

2 years ago

1.0.20

2 years ago

1.0.12

2 years ago

1.0.11

2 years ago

1.0.9

2 years ago

1.0.8

2 years ago

1.0.7

2 years ago

1.0.6

2 years ago

1.0.5

2 years ago

1.0.4

2 years ago

1.0.3

2 years ago

1.0.2

2 years ago

1.0.1

2 years ago

1.0.0

2 years ago