cbay-sdk
v0.1.59
Published
cbay sdk 4 browsers
Downloads
14
Readme
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"
}
]
}
]