I have created a faucet for the NEM testnet. Luckily, there was already [some information about NEM](https://forum.nem.io/t/2017-nem-link-list/3189) written in [Japanese](http://mijin.io/forums/forum/%E6%97%A5%E6%9C%AC%E8%AA%9E/nem/1459-nem%E9%96%A2%E9%80%A3%E3%82%B5%E3%82%A4%E3%83%88%E3%81%BE%E3%81%A8%E3%82%81), so I was able to start quickly to make the faucet. Thank you to the contributors who translated the information and this blog.
Let me explain about the NEM testnet and its faucet.
* [Source code of NEM Testnet Faucet](https://github.com/namuyan/NEM-testnet-Faucet.git)
* [PHP library source](https://github.com/namuyan/NEM-Api-Library)
####What is the NEM Testnet?
It is a peer-to-peer (P2P) network where developers “test” their programs using NEM virtual currencies. Most virtual currencies have an independent value so developers have some risks with testing new apps on the main network. Furthermore, there are some additional chances of other difficulties that means we could effect others on the mainnet.
Since the differences between the testnet and mainnet are only the value of the coins and strength of nodes, programs working on the main net can also work on testnet in the exact same way. And NEM has a more complicated structure with more options than Bitcoin or Counterparty so I strongly recommend that you confirm and test the behavior of your program on testnet before running it on the mainnet.
####What is the NEM Testnet Faucet?
It is a faucet by which you will get test XEM (the base currency on NEM) for the testnet. NEM has an ideal environment for developers because there is already a [testnet open](http://bob.nem.ninja/beta-testnet/) to everyone and developers are free to run their own node. But before you had to ask the NEM developers to send test XEM in order to run tests. It was inconvenient and slow, so I made the [NEM Testnet Faucet](http://namuyan.dip.jp/nem/main/index.php). Additionally, there was a bounty from the [bounties program](https://forum.nem.io/t/nem-s-prototype-project-bounty-program/2822) to build a faucet.
####How to use the NEM Testnet Faucet?
It’s very easy.
Please remember that a testnet address starts with “T”. (I recommend the using the [NanoWallet](https://www.nem.io/install.html) for testing purposes.)
1) Go to this website: http://namuyan.dip.jp/nem/main/index.php
2) Write down your testnet address in the “NEM Address” box
3) Write down any message in the “message” box (Please make a funny one!)
4) Confirm an authentication process via reCAPTCHA and click the “Click!” button
Then you have finished the process and in addition to the test XEM, you’ll get a randomly selected Mosaic asset as well.
Unfortunately, if you create a Mosaic, you need 5000 XEM. It’s hard to click the faucet 50 times, so I will plan to make another way (or contact the NEM developers).
####How to use SBM Faucet (SBMF)?
I also made a faucet for Mosaic assets. SBM stands for **S**mall **B**usiness **M**osaic, and the amount of it is limited 10,000 and it cannot be divided.
1) Send 10 XEM to “TDEK3DOKN54XWEVUNXJOLWDJMYEF2G7HPK2LRU5W” with a message
2) Write down `twistSBMF` in a message box
You should see “Complete!” in the [Deposit History](http://namuyan.dip.jp/nem/main/index.php) section and then you will get a randomly selected Mosaic!
####How to register your Mosaics on SBMF?
Plus you can register your own Mosaic on the faucet. When your Mosaic is selected, 3XEM on average will be sent to you.
You should be the creator of SBM and meet the SBM requirements
1) Send 10 XEM and your SBM to TDEK3DOKN54XWEVUNXJOLWDJMYEF2G7HPK2LRU5W with the certain message below.
2) Write down `sbmf,namespace:mosaic,[minimum numbers of your SBMF a once],[ maximum numbers your SBMF a once]` in the message box. For example, `sbmf,namuyan:namu,10,30`.
You should see “Complete!” in the Deposit History section!
Your mosaic assets are now mosaics on the SBMF Faucet.
####How to build the faucet
I used PHP and Mysql and referred to the “[NEM NIS API Documentation](http://bob.nem.ninja/docs/)” and “[NEM関連サイトまとめ](http://mijin.io/forums/forum/%E6%97%A5%E6%9C%AC%E8%AA%9E/nem/1459-nem%E9%96%A2%E9%80%A3%E3%82%B5%E3%82%A4%E3%83%88%E3%81%BE%E3%81%A8%E3%82%81)”.
There are two types of transactions actually. Version 1 is specifically for sending only XEM, and Version 2 is for sending Mosaic assets including also XEM. I rewrote the functions to class the method, please refer to this [Example](https://github.com/namuyan/NEM-Api-Library/tree/master/example).
* **timeStamp** is not an Unixtime to avoid [Year 2038 problem](https://en.wikipedia.org/wiki/Year_2038_problem). (It is not certain)
* **type** represents the type of transaction. Sending is 257. There are some other transactions for importance, creating a mosaic, renting a namespace, multisig edits, etc.
* **deadline** means the term during which transactions exists on mempool.
* **version** means a transaction version.
* **signer** means a public key of the creator of the transaction.
This is how I calculate the fee.
*$amount* is the amount of sending XEM.
*$message* is a message. If you don’t want to send any message, don’t bother.
*$mosaic* is to put a detail of mosaic asset into the array.
Officially the fee calculation is written as:
x = how many mosaics you send
`fee = Max(1, Min(25, x * 900_000 / MosaicSupply) - floor(0.8 * ln(9_000_000_000_000_000 / MosaicSupply * 10^divisibility)))`
But I wrote:
`$initialSupplybyUnit = $DetailMosaic['initialSupply'] * pow(10, $DetailMosaic['divisibility']);
$fee_tmp += round( max(1, min(25, $quantity * 900000 / $initialSupplybyUnit ) - floor(0.8 * log(9000000000000000 / $initialSupplybyUnit ))));`
The official way is not wrong but confuses me. There are two notations on the amount.
Without thinking, I made an error with the POST method (eg. /local/account/transfers/incoming )
error：Content type 'application/x-www-form-urlencoded' not supported
When you request POST to NIS, it must be “Content-Type: application/json”.
###How To Setup a Faucet
Please refer to the [README.md](https://github.com/namuyan/NEM-testnet-Faucet).
###### Install(in English)
1. Download source. `git clone https://github.com/namuyan/NEM-testnet-Faucet.git`
2. Make a *main* folder and copy all data of *NEM-testnet-Faucet* to the folder.
`mkdir main` and `cp -r /patt/to/NEM-testnet-Faucet/* /passs/too/main/`
3. Match the authority with * htdocs *. `chown -R daemon:daemon htdocs`
4. To fit local environment *cron/.htaccess* and *example/.htaccess*. DO NOT ALLOW OTHERS TO ACCESS!
If your local IP is `192.168.1.12`, set `allow from 192.168.3.0/24` → `allow from 192.168.1.12/24`
5. Create NEM account, `curl http://localhost:7890/account/generate` **DO NOT SHOW OTHERS**
6. Write the three account data (*$NEMAddress $NEMprikey $NEMpubkey*) to *config.php*.
7. Create DB account (Use different parameter to Example. I use same parameter of config.php as example)
１、Login as root `mysql -u root -p`
２、Create database `CREATE DATABASE nemdb CHARACTER SET utf8 COLLATE utf8_general_ci;`
３、Create user `CREATE USER 'nember'@'localhost' IDENTIFIED BY 'obama';`
４、Grant the user access to the DB `GRANT ALL PRIVILEGES ON nemdb.* TO 'nember'@'localhost';`
8. Create tables **After write down to config.php**
Example: If makedb.php on *htdocs/main/cron/makedb.php*, access to `http://localhost/main/cron/makedb.php`
Check no error output.
9. Run regularly by crontab.
１、To make a pass of *$root_dir* of *Deposit.php ImageReg.php SBMFaucet.php* in *main/cron*.
２、Get pass to type `pwd` at main folder.
４、Write down followings. (pass is original)
５、`*/4 * * * * /opt/lampp/bin/php /opt/lampp/htdocs/main/cron/Deposit.php >/dev/null 2>&1`
`*/5 * * * * /opt/lampp/bin/php /opt/lampp/htdocs/main/cron/ImageReg.php >/dev/null 2>&1`
`*/6 * * * * /opt/lampp/bin/php /opt/lampp/htdocs/main/cron/SBMFaucet.php >/dev/null 2>&1`
10. Register reCAPTCHA, and write Secretkey to function.php on 17 line.
11. When an error message occurs, please check PASS.
I would love to receive your kind donations.
* DonationCPaddress： 1BvRTmPCe47vee2CyrLi9AGeSEcrR2ciM4
* DonationNEMaddress： NAN7XFG52NL3V5AW3NTSYO77AVR6X5LYRJKXWKHY
* DonationMonacoin： MSYTEF7t62b9sjXt3oN9JokSjnYkvtcPFx
*The NEM Team would like to thank user Namuyan for creating this blog and project and user Coffetimes for translating it.*