pinknetworkjs
This is an api wrapper for https://pink.network/documentation . Please read our API documentation
to understand the different responses.
Getting Started
It can be used in the browser by including the minified version or
in a node project
Installing
Use it in a npm environment:
npm install pinknetworkjs
Use it in the browser:
<script type="text/javascript" src="socket.io.min.js" />
<script type="text/javascript" src="./node_modules/pinknetworkjs/pinknetwork.min.js" />
Bankroll
Non Cycle Roll Example
const pinknetwork = require("pinknetworkjs");
const bankroll = new pinknetwork.bankroll()
let betconfig = bankroll.createBetConfigByMultiplier(2, 5);
let memo = bankroll.createRollTransactionMemo(10, "rake.recipient", betconfig)
if(memo === false) {
console.log("bankroll too small")
}
bankroll.getRollSubscription().onNewRollResult((roll) => {
console.log(roll)
})
Cycle Roll Example
const pinknetwork = require("pinknetworkjs");
const bankroll = new pinknetwork.bankroll()
const roll_id = 4;
let betconfig = bankroll.createBetConfig(2, 1, 4700, 10000);
let memo = bankroll.createCycleRollTransactionMemo(roll_id, 10, betconfig)
if(memo === false) {
console.log("bankroll too small")
}
bankroll.getCycleRollSubscription(roll_id).onNewRollResult((roll) => {
console.log(roll)
});
bankroll.getCycleRollSubscription(roll_id).onNewBet((bet) => {
console.log(bet)
})
Chat Example
const pinknetwork = require("pinknetworkjs");
const chat = new pinknetwork.chat("chatroom");
async function login_to_chat_version_1() {
let sig = await scatter.getArbitrarySignature(user.publicKey, chat.getAuthenticationSignText());
return await chat.authenticate_arbitrary(sig, user.account.publicKey, user.account.name, true);
}
async function login_to_chat_version_2() {
let sig = await api.transact(chat.getAuthenticationTransaction(user.account.name, user.account.authority), {broadcast: false, sign: true})
return await chat.authenticate_transaction(sig.signatures[0], user.account.publicKey, user.account.name, user.account.authority, true);
}
async function login_to_chat_version_3() {
let sig = await scatter.authenticate(chat.getNonce(), "login", user.publicKey)
await chat.login(sig, user.publicKey, user.account.name, true);
}
chat.onLoad(function(messages) {
})
chat.onMessage(function(message) {
})
chat.onError(function(message) {
})
chat.onLogin(function(info) {
})
chat.onLogout(function() {
});
Documentation
Bankroll
The bankroll api has the following classes, but you can only interact with the BankrollAPI class
which will return the other classes.
class BankrollAPI
-
getRollSubscription()
- @return RollSubscription // object of class RollSubscription
-
getCycleRollSubscription(int roll_id)
- @param roll_id // roll_id of the subscription
- @return CycleRollSubscription // object with the id roll_id
-
createBetConfigByMultiplier(float multiplier, float rake, int max_roll = 10000)
- @param multiplier // example 2.1
- @param rake // rake the rake_recipient will receive 1.0 = 1.0%
- @param max_roll // maximum number a roll result can be
- @return BetConfig // initiate BetConfig class with multiplier and rake (lower_bound = 1 and upper_bound will be calculated)
-
createBetConfigByRange(int lower_bound, int upper_bound, float rake, int max_roll = 10000)
- @param lower_bound // between 1 and max_roll
- @param upper_bound // between lower_bound and max_roll
- @param rake // rake the rake_recipient will receive 1.0 = 1.0%
- @param max_roll // maximum number a roll result can be
- @return BetConfig // initiate BetConfig class with lower_bound, upper_bound and rake (multiplier will be calculated)
-
createBetConfig(float multiplier, int lower_bound, int upper_bound, int max_roll = 10000)
- @param multiplier // example 2.1
- @param lower_bound // between 1 and max_roll
- @param upper_bound // between lower_bound and max_roll
- @param max_roll // maximum number a roll result can be
- @return BetConfig // initiate BetConfig class
-
createRollTransactionMemo(float amount, string rake_recipient, BetConfig bet_config)
- @param amount // WAX which is sent (used to verify whether the bet is allowed)
- @param rake_recipient // Account name which receives the rake
- @param bet_config // BetConfig object
- @return string // memo of the transaction
-
createCycleRollTransactionMemo(int roll_id, float amount, BetConfig bet_config)
- @param roll_id // roll_id of the cycle roll
- @param amount // WAX which is sent (used to verify whether the bet is allowed)
- @param bet_config // BetConfig object
- @return string // memo of the transaction
-
async request(string endpoint, params = {}, version = 1, method = "GET")
- @param endpoint // example "rolls/1"
- @param params // example {"limit": 50}
- @param version // api endpoint version
- @param method // GET or POST. If POST, the params will be the data
- @return {"success": bool, "data": mixed, "code": int, "message": string}
-
ROLL ENDPOINTS
async getRollHistory(limit = 50, page = 1, rake_recipient = null, bettor = null)
// endpoint /v1/rollsasync getRollResult(roll_id)
// endpoint /v1/rolls/:roll_idasync getRollRanking(rake_recipient = null, sort = "wagered", time = 0, limit = 50, page = 1)
// endpoint /v1/rolls/rankingasync getRollAccountRanking(account, rake_recipient = null, time = 0)
// endpoint /v1/rolls/ranking/:account
-
CYCLE ENDPOINTS
async getCycleRollInfo(roll_id)
// endpoint /v1/cycles/info/:roll_idasync getCycleRollRanking(roll_id)
// endpoint /v1/cycles/ranking/:roll_idasync getCycleRollAccountRanking(roll_id, account, time = 0)
// endpoint /v1/cycles/ranking/:roll_id/:accountasync getCycleRollHistory(roll_id, limit = 50, page = 1, bettor = null)
// endpoint /v1/cycles/:roll_idasync getCycleRollResult(roll_id, cycle_id)
// endpoint /v1/cycles/:roll_id/:cycle_id
-
STATISTIC ENDPOINTS
async getBankrollBalance()
// endpoint /v1/available-funds/latestasync getBankrollBalanceHistory(step = 3600 * 6, time = 0)
// endpoint /v1/available-fundsasync getExchangeRate(token_1 = "pink", token_2 = "wax")
// endpoint /v1/exchange-rate/:token_pair/latestasync getExchangeRateHistory(token_1 = "pink", token_2 = "wax", step = 3600 * 6, time = 0)
// endpoint /v1/exchange-rate/:token_pairasync getBankrollProfit()
// endpoint /v1/profit/latestasync getBankrollProfitHistory(step = 3600 * 6, time = 0)
// endpoint /v1/profit
class BetConfig
This is a config class which stores multiplier, lower_bound, upper_bound and max_roll
and is needed to create transaction memos or calculate the max bet. The rake is also stored
indirectly in this class because it is calculated with the difference between multiplier and
winning percentage (upper_bound - lower_bound + 1) / max_roll
.
The methods createBetConfigByMultiplier
, createBetConfigByRange
, createBetConfig
are
used to initiate objects of this class.
- GETTER
getLowerBound()
@return intgetUpperBound()
@return intgetMultiplier()
@return floatgetMaxRoll()
@return int
class RollSubscription
This class contains a socket connection to the endpoint /bankroll/v1/rolls/subscribe
and fires events whenever the bankroll is updated or a new roll came in which you have subscribed to.
-
Methods
getMaxBet(BetConfig bet_config)
-
Events
onNewRollResult(callback cb)
onBankrollUpdate(callback cb)
-
Subscribers
subscribeRakeRecipient(string wax_account)
subscribeIdentifier(string identifier)
subscribeAll()
class CycleRollSubscription
This class contains the socket connection to the endpoint /bankroll/v1/cycles/subscribe/[:roll_id]
and
fires a event when a new bet or a new result was published on the blockchain for roll_id
-
Methods
getMaxBet(BetConfig bet_config)
-
Events
onNewRollResult(callback cb)
onNewBet(callback cb)
onRollReduction(callback cb)
onBankrollUpdate(callback cb)
Chat
The chat api allows to provide a simple chat with scatter (or any other wallet which can sign) authentication and connects
to the pink.network backend servers, so it can be used in frontend only applications.
class ChatAPI
-
Methods
login(string signature, string publicKey, string account_name)
// use scatter method authenticate to sign the nonce
signature
The signature which is returned by scatter.authenticate(chat.getNonce(), "login", publicKey)
publicKey
the public Key of the account which you want to authenticateaccount_name
the account name bound to the public key
authenticate_arbitrary(string signature, string publicKey, string account_name, bool stay_logged_in)
// creates an authentication token which can be used to authenticate to the chat in the future without having to sign anything
signature
The method scatter.getArbitrarySignature
of the text which is returned by getAuthenticationSignText()
publicKey
the public Key of the account which you want to authenticateaccount_name
the account name bound to the public keystay_logged_in
if set to true in browser a cookie will be set which will automatically authenticate the user in the future (you dont need to do anything if this is true)
authenticate_transaction(string signature, string publicKey, string account_name, string permission, bool stay_logged_in)
// creates an authentication token which can be used to authenticate to the chat in the future without having to sign anything
signature
The signature of the transaction which is returned by getAuthenticationTransaction()
publicKey
the public Key of the account which you want to authenticateaccount_name
the account name bound to the public keypermission
the permission which is used to sign the transactionstay_logged_in
if set to true in browser a cookie will be set which will automatically authenticate the user in the future (you dont need to do anything if this is true)
logout()
// sends logout events and destroys the user's chat sessionsend(string message)
// sends a chat message
-
Getter
isAuthenticated()
// is client authenticated?getNonce()
// returns the nonce which has to be signed by the user if you use the login methodgetAuthenticationSignText()
// returns the text the user has to sign to use the authenticate_arbitrary methodgetAuthenticationTransaction()
// returns the transaction the user has to sign to use the authenticate_transaction method
-
Events
onMessage(callback cb)
// new message came inonLoad(callback cb)
// chat loaded (last 25 messages)onError(callback cb)
// error occurred. message is passed as argumentonLogout(callback cb)
// the socket connection destroyed the user's sessiononLogin(callback cb)
// the authentication was successful. The user info object is passed as callback argument