This repo provides all of the code needed to operate a token sale on the NEM blockchain.
This code powered the Cache Token Sale.
The code will do the following:
- Send your NEM Token (Mosaic) to purchasers
- Let purchasers buy with XEM
- Let purchasers buy with Coinbase Commerce
- Process KYC for XEM and manual crypto payments
Requires: Node v8.2+, MongoDB
- Clone this repo:
https://github.com/blockstart/nem-coinbase-token-sale-api.git
- Run
cd nem-coinbase-token-sale-api && npm install
- Run
npm install -g pm2 nodemon
- In a new terminal tab, run
mongod
to start the mongo daemon (if you run into errors in this process it means your MongoDB installation likely isn't properly set up). If successful, the mongo daemon will start, leave this running.
Seed Test Data
The only data needed on first build is the token packages you plan to sell.
- From within your project directory run:
cd test
- Run
mongoimport --db token-api-local --collection products --file products.json --jsonArray
Result will look similar to:
Build & Run the Project
To build the project and start the server run:
npm run localdev
If everything worked correctly you'll see something similar to this:
To test the running server, open up a new terminal tab or window and run:
curl http://localhost:3005/localdev/v1/product/all
You should see a printout of your products that you inserted earlier.
Hot reloading is enabled for development - as you make changes the code will recompile and the server will restart.
Unit Testing
To run through all the tests run:
npm run test
Hot reloading is enabled for unit testing.
Wallet & Secret Keys
Put the following lines in your .gitignore
file:
nem-config.json
*.wlt
Do NOT put your passwords, secret keys, or wallet files in version control. We left these in the project so you can see the format.
You will need to put a wallet on your server, and point to the path in the nem-config.json
file.
Your wallet and password is needed to sign transactions, ie automatically send your token to purchasers.
Environments
Use the environment files for your various API keys like Coinbase commerce.
They look like this: .env.development
and .env.prod
To deploy, use your favorite hosting provider and set up a Linux distribution. We use Digital Ocean for our hosting.
Get $15 FREE credit for Digital Ocean hosting.
Use this code on the billing page: DODEVSLOPES15
(non-affiliate - we asked for these codes for our community)
Requires: Node v8.2+, MongoDB
Prior to deployment you may want to have your pendingXEMCharges expire after 15 minutes. To set this up:
- Open a terminal window
- Run
mongo
to enter the mongo shell - Choose your database ie
use token-production-api
- Run
db.pendingXemCharges.createIndex({"createdAt": 1}, { expireAfterSeconds: 900})
This will cause records in this database collection to be deleted after 15 minutes automatically.
Deployment steps:
- Clone this repo:
https://github.com/blockstart/nem-coinbase-token-sale-api.git
- Run
cd nem-coinbase-token-sale-api && npm install
- Run
npm install -g pm2
- Run mongo as a background process
- Run
npm run build
thennpm start
You will now have a PM2 process running named api-token-production
running on port 4561
Learn more about PM2 here.
NGINX
We recommend NGINX for reverse proxy and node apps. Here is an example config that you can put in /etc/nginx/sites-available
server {
listen 80;
server_name yourapi.yourdomain.io;
location / {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-NginX-Proxy true;
proxy_pass http://localhost:4561;
proxy_ssl_session_reuse off;
proxy_set_header Host $http_host;
proxy_cache_bypass $http_upgrade;
proxy_redirect off;
}
}
A production deployment could flow like this:
- Register with Digital Ocean and create an Ubuntu droplet
- Run the API project with PM2 as written above
- Configure NGINX similar to above
That's it.
Your client apps will interface with the following REST endpoints:
- GET:
/product/all
- GET:
/product/purchase-lookup-coinbase/:tokenRecipientAddress
- POST:
/transaction/initiate-xem-purchase
- POST
/transaction/initiate-coinbase-purchase
- GET:
/info/tokens-sold
- PUT:
/info/update-usd
- PUT:
/kyc/email
This code uses the MIT license - use it as you see fit. We are not responsible for any bugs or issues in this code. Please review before publishing.
One piece of code that is on our server that is not here is the verification of multiple confirmations. We are still cleaning that code up. Know that it is important to make multiple verifications of a transaction before sending tokens to a recipient.