Giter Club home page Giter Club logo

tipcc.js's Introduction

tip.cc API Client

Welcome to the tip.cc API Client npm package!

Installation

Simply create an npm project if you don't have an already, and install the package.

npm init
npm i tipcc.js

Getting Started

Tip: Want to get started without an introduction? Check out our documentation.

You can create a simple TipccClient like this:

import { TipccClient } from 'tipcc.js';

const client = TipccClient(myToken);

client.on('ready', () => {
  console.log('TipccClient is ready!');
});

myToken is your tip.cc API key.

A note on API values

The tip.cc API uses the smallest denomination of currencies, giving values in atomic units. For an explanation of how this works, use Ethereum's wei as an example.

tipcc.js uses the bignumber.js package to handle these numbers, and our API will return these in multiple functions.

For a in-depth explanation of BigNumbers and available features, check their own documentation.

Wallets

To get your balance on tip.cc, use the WalletManager:

client.on('ready', async () => {
  const wallet = await client.wallets.fetch('BNB');
  if (!wallet) {
    return console.log('No BNB wallet found. Have you received any BNB?');
  }

  console.log(
    `We've got ${wallet.balance.value} ${wallet.code} on our BNB wallet`,
  );

  console.log(`This is approximately ${wallet.balance.usdValue} USD`);
});

Transactions

Events

To receive transactions as events, use TransactionManager's events:

client.transactions.on('tip', (transaction) => {
  const readableAmount = transaction.amount.value;
  const currencyCode = transaction.amount.currencyCode;
  const sender = transaction.sender.username;

  console.log(`Received ${readableAmount} ${currencyCode} from ${sender}`);
});

Fetching

You can also get a single or many transactions by id:

client.on('ready', async () => {
  const oneTransaction = await client.transactions.fetch('one-id');
  const manyTransactions = await client.transactions.fetchMany([
    'this-id',
    'another-id',
  ]);
});

Getting transactions based on a filter is also possible:

client.on('ready', async () => {
  const transactionsByFilter = await client.transactions.fetchAll({
    currency: 'BTC',
    limit: 5,
  });
});

Using no filter will get all transactions for the bot/user. This is not recommended, unless you know what you're doing.

Creating (sending)

You can send a transaction to one or more users:

client.on('ready', async () => {
  const transaction = await client.transactions.create({
    recipient_s: ['discord-id-here'],
    value: '0.1',
    currencyCode: 'BTC',
  });

  const amount = transaction[0].amount;

  console.log(`${amount.value} ${amount.currencyCode} successfully sent to ${transaction[0].recipient.username}`);
});

Notice that you can choose between using a rawValue or value when sending a transaction. The value will automatically get converted, while rawValue assumes you have done this conversion yourself.

Exchange rates

Use the ExchangeRateCache to get exchange rates:

client.on('ready', async () => {
  const rate = client.exchangeRates.get('BTC');
  if (!rate) {
    console.log('The rate for BTC could not be found.');
  }

  console.log(`1 BTC is currently worth ${rate?.usdValue?.value} USD`);
});

This is also accessible on other structures, such as wallets:

client.on('ready', async () => {
  const wallet = await client.wallets.fetch('BTC');
  if (!wallet) {
    return console.log('No BTC wallet found. Have you received any BTC?');
  }

  console.log(`1 BTC is now worth ${wallet.exchangeRate.usdValue} USD`);
});

Currencies

The client provides caches for cryptocurrencies (CryptocurrencyCache) and fiats (FiatCache).

This may be useful when you need some basic information about a currency.

Getting a cryptocurrency:

client.on('ready', async () => {
  const btc = client.cryptos.get('BTC');
  if (!btc) {
    return console.log('Could not find BTC in the cache.');
  }

  console.log(`BTC's full name is ${btc.name}`);
  console.log(`BTC's explorer is ${btc.explorer}`);
});

Getting a fiat:

client.on('ready', async () => {
  const usd = client.fiats.get('USD');
  if (!usd) {
    return console.log('Could not find USD in the cache.');
  }

  console.log(`USD's full name is ${usd.name}`);
  console.log(`USD uses ${usd.format.scale} decimals`);
});

Exploring

Feel free to check out our documentation to learn about our API and how you can use it.

Notice that the examples above are bits of code separated from each other. You can often use provided properties to get your task done with fewer lines of code by combining the structures explained.

License

This project is licensed under the MIT License.

Disclaimer

The authors of this package are not the authors of tip.cc. We are not responsible for any loss of funds caused by incorrect usage, bugs, exploits, or other causes when using this package.

tipcc.js's People

Contributors

dependabot[bot] avatar walledgarden avatar zerowave022 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

tipcc.js's Issues

Format Amount

This feature would give library users the option to format a tip in the currently know tip.cc format by calling a function in the amount class. Preferably, at client initalization there is a new option, which allows to pass a currencies-emojis map to the client, which would be used for this feature too (ex. BTC => <BTC:12312312312312312>...). The format for tips would be as follows

<#Amount>.toString(withUsd defaults to true)

<emojiName:emojiId> currencyAmount currencySymbol (โ‰ˆ $usdValue)
currencyAmount would represent the this.humanValue.toFixed(currency.format.scale)

Converting currency amounts to USD value natively

This feature would improve the quality of the library, because people would have a native function in the Amount class which would convert the amount in the USD value by using the cached currencies.

<#Amount>.toFiat(fiatCurrency defaults to USD)

Add proper README and tutorials

There are some documentation improvements which need to be made:

  • Add a README
  • Add an introduction and/or getting started page

File structure

Reordering the files in a standard structure following compareable API wrappers improves the quality of the code and helps other developers to understand the code better.

Add Prettier for code formatting

While everyone has their own coding style, using one code formatter with a configuration file will make sure everyone is following one code style.

Make documentation easier to understand

The documentation needs an overhaul, so it is easy to understand. This will involve making names shorter, changing the order of items being displayed and so on.
Closing this issue will require changing the exports on the library in a way that renders well on Typedoc.
This will also make the project more organized in general.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.