0.1.59 • Published 4 years ago

cbay-sdk v0.1.59

Weekly downloads
-
License
MIT
Repository
github
Last release
4 years ago

cbay-sdk

js sdk 4 interactin' with the official cbay api

installation

npm i cbay-sdk

usage

prelude

import cbay from "cbay-sdk"

imports the sdk factory

const sdk = cbay(opts)

factors a new sdk configurable with opts, particularly:

{
  api: "string", // api base url - default https://api.cbay.nugget.digital/v0
  infura: "string", // infura endpoint - required if usin' sdk.pay(product)
  accessToken: "string", // cognito access token - tokens required 4 post
  refreshToken: "string", // cognito refresh token
  getCognitoUser() {}, // as per amazon-cognito-identity-js
  storeTokens(accessToken, refreshToken) {} // should store fresh tokens
}

getCognitoUser() and storeTokens() may be async

example

import cbay from "cbay-sdk"
const sdk = cbay(/* opts */)

const id = await sdk.post(product)

creates a new product listing

params

product object with the following properties:

{
    category,    // string - c below for valid categories
    title        // string
    description, // string
    units,       // number - u16 > 0
    currency,    // string - symbol - c below for valid currencies
    price,       // number - float - unit price
    payee,       // string - ethereum address or ens name
    contact,     // string - off-chain contact - fx telegram handle
    media        // File[] - optional pics
}

resolves

id string - product id - 8 char lowercase hex string

šŸ—‘ļø all products+pics are deleted from storage after 7 days

šŸ product posts are rate limited to 1 request per minute and source ip

const products = await sdk.list(category[, since][, limit])

lists the latest product offers in given category - from new 2 old

params

category string c below for valid values

since sth like "2021-10-24T10:55:47.185Z" - from Date#toISOString()

limit uint number

resolves

products product[] c below for particular props of a product

const product = await sdk.get(id)

gets a single product

params

id string - product id - 8 char lowercase hex string

resolves

product c below

list and get return (a) product(s) in the following format:

{
    category,    // string
    title        // string
    description, // string
    units,       // number - u16 > 0
    currency,    // string - symbol
    price,       // number - float - unit price
    payee,       // string - ethereum address or ens name
    contact,     // string - off-chain contact - fx telegram handle
    media,       // string[] - maybe - media object urls pointin' 2 s3
    modifiedAt,  // string - 2021-10-24T10:55:47.185Z
    expiry       // number - timestamp (milliseconds) 4 db auto deletion
}

const bool = sdk.postable(rawProduct)

validates given object for product essentials

const { tx, block } = await sdk.pay(product, units)

pay given product with a transfer of Ethereum or an ERC20 coin

params

product std sdk product

units number - purchase count - int gt 0 and lt 65536

resolves

tx string - tx hash of the corresponding transfer

block number - block height at tx inclusion

await sdk.refresh()

refreshes the internal access token

auto called once after encountering 401 responses while postin' products

await sdk.subscribe(email)

subscribe to the cbay newsletter

params

email user email address

const chains = sdk.Chains

static list of supported blockchain networks (Ethereum only for now)

[
  "mainnet",
  "rinkeby"
]

const cats = sdk.Categories

static list of valid cbay categories

[
  "electronics",
  "vehicles",
  "fashion",
  "home-and-garden",
  "collectibles-and-art",
  "sports",
  "toys",
  "music",
  "business-and-industrial",
  "other"
]

const symbols = sdk.Currencies

static list of valid cbay currencies

[
  "ETH",
  "WETH",
  "DAI",
  "USDC",
  "USDT"
]

const registry = sdk.ERC20Registry

maps symbols of sdk.Currencies to their sdk.Chains erc20 contract addresses incl. token decimals

{
  WETH: {
    mainnet: "0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2",
    rinkeby: "0xc778417e063141139fce010982780140aa0cd5ab",
    decimals: 18
  },
  DAI: {
    mainnet: "0x6b175474e89094c44da98b954eedeac495271d0f",
    rinkeby: "0x5592ec0cfb4dbc12d3ab100b257153436a1f0fea",
    decimals: 18
  },
  USDT: {
    mainnet: "0xdac17f958d2ee523a2206206994597c13d831ec7",
    rinkeby: "0x3b00ef435fa4fcff5c209a37d1f3dcff37c705ad",
    decimals: 6
  },
  USDC: {
    mainnet: "0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48",
    rinkeby: "0xeb8f08a975ab53e34d8a0330e0d34de942c95926",
    decimals: 6
  }
}

const abi = sdk.ERC20TransferAbi

erc20 transfer function abi

[
  {
    name: "transfer",
    type: "function",
    constant: false,
    payable: false,
    stateMutability: "nonpayable",
    inputs: [
      {
        name: "_to",
        type: "address"
      },
      {
        name: "_value",
        type: "uint256"
      }
    ],
    outputs: [
      {
        name: "",
        type: "bool"
      }
    ]
  }
]

license

MIT

0.1.52

4 years ago

0.1.53

4 years ago

0.1.54

4 years ago

0.1.55

4 years ago

0.1.56

4 years ago

0.1.57

4 years ago

0.1.58

4 years ago

0.1.59

4 years ago

0.1.51

4 years ago

0.1.50

4 years ago

0.1.41

4 years ago

0.1.42

4 years ago

0.1.43

4 years ago

0.1.44

4 years ago

0.1.45

4 years ago

0.1.46

4 years ago

0.1.47

4 years ago

0.1.48

4 years ago

0.1.40

4 years ago

0.1.38

4 years ago

0.1.39

4 years ago

0.1.30

4 years ago

0.1.31

4 years ago

0.1.32

4 years ago

0.1.33

4 years ago

0.1.34

4 years ago

0.1.35

4 years ago

0.1.36

4 years ago

0.1.37

4 years ago

0.1.27

4 years ago

0.1.28

4 years ago

0.1.29

4 years ago

0.1.26

4 years ago

0.1.12

4 years ago

0.1.13

4 years ago

0.1.14

4 years ago

0.1.15

4 years ago

0.1.20

4 years ago

0.1.21

4 years ago

0.1.22

4 years ago

0.1.23

4 years ago

0.1.24

4 years ago

0.1.25

4 years ago

0.1.16

4 years ago

0.1.17

4 years ago

0.1.18

4 years ago

0.1.19

4 years ago

0.1.11

4 years ago

0.1.10

4 years ago

0.1.9

4 years ago

0.1.8

4 years ago

0.1.7

4 years ago

0.1.6

4 years ago

0.1.3

4 years ago

0.1.2

4 years ago

0.1.1

4 years ago

0.1.0

4 years ago