|
1 | 1 | # tip.cc API Client |
| 2 | + |
| 3 | +Welcome to the tip.cc API Client npm package! |
| 4 | + |
| 5 | +# Installation |
| 6 | +Simply create an npm project if you don't have an already, and install the package. |
| 7 | +``` |
| 8 | +npm init |
| 9 | +npm i tipcc.js |
| 10 | +``` |
| 11 | + |
| 12 | +# Getting Started |
| 13 | +> Tip: Want to get started without an introduction? Check out our [documentation](https://tipccjs.org/). |
| 14 | +
|
| 15 | +You can create a simple TipccClient like this: |
| 16 | +```js |
| 17 | +import { TipccClient } from 'tipcc.js'; |
| 18 | + |
| 19 | +const client = TipccClient(myToken); |
| 20 | + |
| 21 | +client.on('ready', () => { |
| 22 | + console.log('TipccClient is ready!') |
| 23 | +}); |
| 24 | +``` |
| 25 | +`myToken` is your tip.cc API key. |
| 26 | + |
| 27 | +## A note on API values |
| 28 | +The tip.cc API uses the smallest denomination of currencies, giving values in atomic units. |
| 29 | +For an explanation of how this works, use [Ethereum's wei](https://www.investopedia.com/terms/w/wei.asp) as an example. |
| 30 | + |
| 31 | +tipcc.js uses the bignumber.js package to handle these numbers, and our API will return these in multiple functions. |
| 32 | + |
| 33 | +For a in-depth explanation of BigNumbers and available features, check their own [documentation](https://mikemcl.github.io/bignumber.js/). |
| 34 | + |
| 35 | +## Wallets |
| 36 | +To get your balance on tip.cc, use the [WalletManager](https://tipccjs.org/classes/WalletManager): |
| 37 | + |
| 38 | +```js |
| 39 | +client.on('ready', async () => { |
| 40 | + const wallet = await client.wallets.fetch('BNB'); |
| 41 | + if (!wallet) { |
| 42 | + return console.log('No BNB wallet found. Have you received any BNB?'); |
| 43 | + } |
| 44 | + |
| 45 | + console.log(`We've got ${wallet.balance.value} ${wallet.code} on our BNB wallet`); |
| 46 | + |
| 47 | + console.log(`This is approximately ${wallet.balance.usdValue} USD`); |
| 48 | +}); |
| 49 | +``` |
| 50 | + |
| 51 | +## Transactions |
| 52 | +To receive transactions as events, use [TransactionManager](https://tipccjs.org/classes/TransactionManager)'s events: |
| 53 | + |
| 54 | +```js |
| 55 | +client.transactions.on('tip', (transaction) => { |
| 56 | + const readableAmount = transaction.amount.value; |
| 57 | + const currencyCode = transaction.amount.currencyCode; |
| 58 | + const sender = transaction.sender.username; |
| 59 | + |
| 60 | + console.log(`Received ${readableAmount} ${currencyCode} from ${sender}`); |
| 61 | +}); |
| 62 | +``` |
| 63 | + |
| 64 | +You can also get a single or many transactions by id: |
| 65 | +```js |
| 66 | +client.on('ready', async () => { |
| 67 | + const oneTransaction = await client.transactions.fetch('one-id'); |
| 68 | + const manyTransactions = await client.transactions.fetchMany(['this-id', 'another-id']); |
| 69 | +}); |
| 70 | +``` |
| 71 | + |
| 72 | +Getting transactions based on a filter is also possible: |
| 73 | +```js |
| 74 | +client.on('ready', async () => { |
| 75 | + const transactionsByFilter = await client.transactions.fetchAll({ |
| 76 | + currency: 'BTC', |
| 77 | + limit: 5, |
| 78 | + }); |
| 79 | +}); |
| 80 | +``` |
| 81 | + |
| 82 | +Using no filter will get all transactions for the bot/user. |
| 83 | +This is not recommended, unless you know what you're doing. |
| 84 | + |
| 85 | +## Exchange rates |
| 86 | +Use the [ExchangeRateCache](https://tipccjs.org/classes/ExchangeRateCache) to get exchange rates: |
| 87 | + |
| 88 | +```js |
| 89 | +client.on('ready', async () => { |
| 90 | + const rate = client.exchangeRates.get('BTC'); |
| 91 | + if (!rate) { |
| 92 | + console.log('The rate for BTC could not be found.'); |
| 93 | + } |
| 94 | + |
| 95 | + console.log(`1 BTC is currently worth ${rate?.usdValue?.value} USD`); |
| 96 | +}); |
| 97 | +``` |
| 98 | +
|
| 99 | +This is also accessible on other structures, such as wallets: |
| 100 | +```js |
| 101 | +client.on('ready', async () => { |
| 102 | + const wallet = await client.wallets.fetch('BTC'); |
| 103 | + if (!wallet) { |
| 104 | + return console.log('No BTC wallet found. Have you received any BTC?'); |
| 105 | + } |
| 106 | +
|
| 107 | + console.log(`1 BTC is now worth ${wallet.exchangeRate.usdValue} USD`); |
| 108 | +}); |
| 109 | +``` |
| 110 | +
|
| 111 | +## Currencies |
| 112 | +The client provides caches for cryptocurrencies ([CryptocurrencyCache](https://tipccjs.org/classes/CryptocurrencyCache)) and fiats ([FiatCache](https://tipccjs.org/classes/FiatCache)). |
| 113 | +
|
| 114 | +This may be useful when you need some basic information about a currency. |
| 115 | +
|
| 116 | +Getting a cryptocurrency: |
| 117 | +```js |
| 118 | +client.on('ready', async () => { |
| 119 | + const btc = client.cryptos.get('BTC'); |
| 120 | + if (!btc) { |
| 121 | + return console.log('Could not find BTC in the cache.'); |
| 122 | + } |
| 123 | +
|
| 124 | + console.log(`BTC's full name is ${btc.name}`); |
| 125 | + console.log(`BTC's explorer is ${btc.explorer}`); |
| 126 | +}); |
| 127 | +``` |
| 128 | +
|
| 129 | +Getting a fiat: |
| 130 | +```js |
| 131 | +client.on('ready', async () => { |
| 132 | + const usd = client.fiats.get('USD'); |
| 133 | + if (!usd) { |
| 134 | + return console.log('Could not find USD in the cache.'); |
| 135 | + } |
| 136 | +
|
| 137 | + console.log(`USD's full name is ${usd.name}`); |
| 138 | + console.log(`USD uses ${usd.format.scale} decimals`); |
| 139 | +}); |
| 140 | +``` |
| 141 | +
|
| 142 | +## Exploring |
| 143 | +Feel free to check out our [documentation](https://tipccjs.org/) to learn about our API and how you can use it. |
| 144 | +
|
| 145 | +Notice that the examples above are bits of code separated from each other. |
| 146 | +You can often use provided properties to get your task done with fewer lines of code by combining the structures explained. |
| 147 | +
|
| 148 | +# License |
| 149 | +This project is licensed under the [MIT License](https://github.com/tipccjs/tipcc.js/blob/main/LICENSE). |
| 150 | +
|
| 151 | +# Disclaimer |
| 152 | +The authors of this package are not the authors of [tip.cc](https://tip.cc). |
| 153 | +We are not responsible for any loss of funds caused by incorrect usage, bugs, exploits, or other causes when using this package. |
0 commit comments