Code Monkey home page Code Monkey logo

crypto-exchanger's Introduction

Crypto exchanger example

This is a guide on how to implement easy ERC20 coins and Ethereum exchanger based on nodeJS client for the Ethplorer Bulk API Monitor.

Steps

  • Creating a new eth address and sending it to a user
  • Watching this address for new ETH transactions of ERC20 transfers
  • Sending tokens to your cold address with filling address by the current gas price.
  • Sending ETH funds to your cold address
  • Stoping watching for the address
  • Notifying an administrator that funds were received

Installation

$ mkdir exchnager 
$ cd exchanger
$ npm init
$ npm i --save eth-bulk-monitor-client-nodejs

Development

Requiring of a MonitorApp:

const { MonitorApp } = require('eth-bulk-monitor-client-nodejs');

Initialization of new MonitorApp object with your api key:

const monitorApp = new MonitorApp("apiKey");

Starting of the watching process and signing for new operations and transactions:

monitorApp.watch((data) => {
        ...
    });

Creating a new ethereum address(easy to do with web3 library):

const newEthAddress = createNewEthAddress();

Adding new address to the watching:

monitorApp.monitor.addAddresses(newEthAddress);

Preparation is complete, from now we will get all transactions related to our address.

After the receiving callback from the watch function we should understand is it token transfer or ETH transaction:

monitorApp.watch((data) => {
        if (data.data.contract){
            // Here should be logic for a token transfer
            ...
        }else{
            // Here should be logic for a ETH transaction
            ...
        }
    });

For the token transfer we should deposit address with current gas price and only after that we able to send tokens to the cold address of your exchanger service.

if (data.data.contract){
    depositNewAddressWithGas(newEthAddress);
    sendTokensFromNewAddressToTheColdAddress(yourColdAddress, newEthAddress, data.value);
}

In the case of receiving ETH we can send it to your cold address directly.

}else{
    sendEthToTheColdAddress(yourColdAddress, newEthAddress, data.value);
}

Don't forget to remove address from the watching if will not work with it in the future.

monitorApp.monitor.removeAddress(newEthAddress);

Call unwatch function if you want to stop watching process.

monitorApp.unwatch();

So, now we can notify admin that tokens or ETH was sent to your cold address and the user able to receive expected funds regarding current exchange rates.

notifyAdmin();

Final code

const {MonitorApp} = require('eth-bulk-monitor-client-nodejs');
const monitorApp = new MonitorApp("apiKey");

monitorApp.watch((data) => {
        if (data.data.contract){
            depositNewAddressWithGas();
            sendTokensFromNewAddressToTheColdAddress();
        }else{
            sendEthToTheColdAddress();
        }
        monitorApp.monitor.removeAddresses(newEthAddress);
        monitorApp.unwatch();
        notifyAdmin();
    });
const newEthAddress = createNewEthAddress();
monitorApp.monitor.addAddresses(newEthAddress);

/*
The function which returns ethereum address.
This address should be sent to a user for deposit tokens or Ethereum
 */
function createNewEthAddress(){
    // ...
}

/*
The function which will deposit the created address with gas.
We need it for sending received tokens from a user to the cold address of your exchanger service.
 */
function depositNewAddressWithGas(){
    // ...
}

/*
The function which will send tokens from the created address to the cold address of your exchanger service.
 */
function sendTokensFromNewAddressToTheColdAddress(){
    // ...
}

/*
The function which will send received ETH from a user to the cold address of your exchanger service.
 */
function sendEthToTheColdAddress(){
    // ...
}

/*
Function for notifying the administrator of your exchanger service that tokens or ETH received on your cold address.
Now administrator able to send user expected resources regarding current exchange rate.
 */
function notifyAdmin(){
    // ...
}

Full version

A full example of a crypto exchanger service can be found here: link

crypto-exchanger's People

Contributors

egorvas avatar alexesnsk avatar artemko7v avatar

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.