2

i want to make a rails app to keep track of my cryptocurrency coins.

How would you save the values in the database? Which datatype field would you choose?

I thought about using the rails-money gem, but only BTC (Bitcoin) is set as a currency.

Thanks for you help

Benny

user993460
  • 821
  • 2
  • 8
  • 8

1 Answers1

1

What does your schema look like? I would have something like a Balances and CoinTypes table.

In the balance table you would track the crypto amounts as integers (like one would do for whole cents in USD meaning Satoshi's for BTC). You would need to be able to hold huge numbers in this column. I would do something like t.integer :coin_amount :int5, :limit => 5

Whereas, in the coin type table under each coin you can track the amount of decimal places you need to divide the crypto_amount column by.

Balances

| ID            | COIN_AMOUNT   |TYPE  | DESCRIPTION |
| ------------- |---------------|------|-------------|
| 1             | 2300000000000 | 1    | "Ledger"    |
| 2             | 100000000     | 2    | "Coinbase"  |
| 3             | 100000000     | 2    | "Gemini"    |


CoinTypes

| ID            | NAME          |DECIMALS  |
| ------------- |---------------|----------|
| 1             | "Ethereum"    | 18       |
| 2             | "Bitcoin"     | 8        |

Now you know you hold 1 BTC on Gemini and Coinbase respectively. You can also calculate the amount of ethereum you hold to be .0000023. I wouldn't store the dollar amounts in the database but rather calculate them using the api of whatever service you are holding your coins on.

A red flag to me though is that you are tracking balances on what I assume are external services. Remember if you don't manage the keys, they aren't your coins.

danielsmith1789
  • 1,056
  • 1
  • 12
  • 25
  • i have no schema for the balances, cause right now i dont know which data field type i have to choose. like you i want to store multiple coins. my models would be: - coin -> name, symbol, uid - wallet -> name, address, description - transaction -> this is for later, to transfer the coins from wallet to wallet (keep track of the transactions from and to exchanges, ...) something like cointracking.info , but not so full blown. – user993460 Feb 13 '18 at 17:03
  • You need to consider how many decimal places you want to hold. As I demonstrated you will need a different data type to store ethereum and bitcoin values to the smallest denomination accurately. I would read [this api article](http://api.rubyonrails.org/classes/ActiveRecord/ConnectionAdapters/SchemaStatements.html#method-i-add_column) on datatypes in Rails. – danielsmith1789 Feb 15 '18 at 07:01