How to buy and sell bitcoin in Ruby

Time:2019-10-19

How to buy and sell bitcoin in Ruby

Scheme 1: currency transaction through exincore API

Exincore provides currency transaction API based on mixin network.

You can pay usdt to exincore. Exincore will transfer the bitcoin you purchased to you at the lowest price and the most favorable transaction fee. Each coin transaction is anonymous and can be verified on the blockchain. Only you and exincore know the details of the transaction!

Exincore doesn’t know who you are, it only knows your UUID.

Preparatory knowledge:

You first need to create a robot, method in tutorial 1.

Install dependency package

As we introduced in tutorial 1, we need to rely onmixin-botYou should have installed it first. Let’s install it hereeasy-uuid, msgpackTwo software packages.

 gem install msgpack
 gem install easy-uuid

Charge it to mixin network and read out its balance.

Exincore can conduct BTC, usdt, EOS, Eth and other transactions. Here we demonstrate that if you use usdt to buy BTC or BTC to buy usdt, check the wallet address first before trading!
The complete steps are as follows:

  • Check the balance and wallet address of bitcoin or usdt. And write down the address of the wallet.
  • From a third-party exchange or your cold wallet, charge the money to the wallet address above.
  • Check the balance of the currency again to see whether the account is good or not. (bitcoin’s arrival time is the height of five blocks, about 100 minutes).

Note that bitcoin and usdt have the same address.

if cmd == "aw"
  assetsInfo = walletAccount.read_assets()
  p "--------The Wallet Assets List-----------------"
  assetsInfo["data"].each { |x| puts x["symbol"] + " " +
                              x["balance"] + " " + x["public_key"] +
                              x["account_name"] + " " + x["account_tag"]}
  p "----------End of Wallet Assets --------------"
end

Query price information of exincore Market

What about the price information of exincore market? You should first understand what the base currency you are trading is. If you want to buy bitcoin and sell usdt, the base currency is usdt; if you want to buy usdt and sell bitcoin, the base currency is bitcoin.

if cmd == "8"
  Utils.ExinCoreMarketPriceRequest(USDT_ASSET_ID)
end
if cmd == "9"
  Utils.ExinCoreMarketPriceRequest(BTC_ASSET_ID)
end

def self.ExinCoreMarketPriceRequest(asset_id)
  full_url = "https://exinone.com/exincore/markets?base_asset=" + asset_id
  p full_url
  data = HTTP.get(full_url).body
  body = ""
  redData = data.readpartial
  while  redData != nil
    body = body + redData
    redData = data.readpartial
  end
  result = ActiveSupport::JSON.decode(body).with_indifferent_access
  result["data"].each { |x|
        puts x["exchange_asset_symbol"] +  "/" +  x["base_asset_symbol"] + " " +
             x["price"] + " " + x["minimum_amount"] + " " + x["maximum_amount"] +
             " " + x["exchanges"][0]
      }
end

Create a memo before trading!

In the second chapter, ruby bitcoin development tutorial based on mixin network: robots accept bitcoin and return it to users immediately. We have learned how to return bitcoin to users. Here, in addition to paying exincore, we also need to tell him what we want to buy and what we want to save the money we want to buy into memo.

memo1 = Base64.encode64(MessagePack.pack({
'A' => UUID.parse(USDT_ASSET_ID).to_raw
}))
memo = memo1.sub("\n","")

Complete process of currency transaction

When transferring money to exincore, write the memo into the currency you want to buy. Don’t make any mistakes with this memo. Otherwise, exincore needs manual operation to return money!
If you want to sell bitcoin to buy usdt, you can call it as follows:

const EXIN_BOT        = "61103d28-3ac2-44a2-ae34-bd956070dab1";
const BTC_ASSET_ID    = "c6d0c728-2624-429b-8e0d-d9d19b6592fa";
const EOS_ASSET_ID    = "6cfe566e-4aad-470b-8c9a-2fd35b49c68d";
const USDT_ASSET_ID   = "815b0b1a-2764-3736-8faa-42d694fa620a";
if cmd == "5"
  memo1 = Base64.encode64(MessagePack.pack({
  'A' => UUID.parse(USDT_ASSET_ID).to_raw
  }))
  memo = memo1.sub("\n","")
  p memo
  transInfo = walletAccount.create_transfer(walletAccount.encrypt_pin(DEFAULT_PIN),
                                    {
                                      asset_id: BTC_ASSET_ID,
                                      opponent_id: EXIN_BOT,
                                      amount: "0.0001",
                                      trace_id: SecureRandom.uuid,
                                      memo: memo
                                    })
   p transInfo
end

If you want to sell usdt to buy bitcoin, you can call it as follows:

if cmd == "6"
  memo1 = Base64.encode64(MessagePack.pack({
  'A' => UUID.parse(BTC_ASSET_ID).to_raw
  }))
  memo = memo1.sub("\n","")
  p memo
  transInfo = walletAccount.create_transfer(walletAccount.encrypt_pin(DEFAULT_PIN),
                                    {
                                      asset_id: USDT_ASSET_ID,
                                      opponent_id: EXIN_BOT,
                                      amount: "1",
                                      trace_id: SecureRandom.uuid,
                                      memo: memo
                                    })
   p transInfo
end

After the transaction is completed, exincore will transfer the currency you need to your account. Similarly, it will record the transaction price, transaction cost and other information in memo! You just need to untie it in the following way!

  • read_snapshotsRead the wallet’s transactions.
if cmd == "7"
  if wallet_userid == "5e4ad097-21e8-3f6b-98f7-9dc74dd99f77"
    dt = "2019-05-23T09:48:04.582099Z"
  else
    puts "Input the snapshots datetime: "
    dt = gets.chomp
  end
  p CGI.escape(dt)
  snaps = walletAccount.read_snapshots({
                                      limit: "10",
                                      offset: CGI.escape(dt),
                                      asset: BTC_ASSET_ID,
                                      order: "ASC"
                                      })
  snaps["data"].each { |x|
                      if x["amount"].to_f > 0
                        if x["data"] != nil
                          uData = MessagePack.unpack(Base64.decode64(x["data"]))
                          p uData
                          p "The code is " + uData["C"].to_s + " Price is " + uData["P"] + " Fee is " +
                            uData["F"]
                          p "Fee is Asset UUID is " + UUID.parse(uData["FA"]).to_s
                          p "Order id is  " + UUID.parse(uData["O"]).to_s
                        end
                      end
                      }
end

A successful transaction is as follows:

Make your choose(eg: q for Exit!):
7
"2019-05-23T09%3A48%3A04.582099Z"
{"C"=>1000, "P"=>"7562.91", "F"=>"0.000000264", "FA"=>"\xC6\xD0\xC7(&$B\x9B\x8E\r\xD9\xD1\x9Be\x92\xFA", "T"=>"R", "O"=>"\xC4\x00*\a\xF1\xB1C9\xBC\xCE%\xC3C\x88^R"}
"The code is 1000 Price is 7562.91 Fee is 0.000000264"
"Fee is Asset UUID is c6d0c728-2624-429b-8e0d-d9d19b6592fa"
"Order id is  c4002a07-f1b1-4339-bcce-25c343885e52"
-------------------------------------------------------------------------

Read balance in currency

Confirm the transaction by reading the balance of currency!

if cmd == "aw"
  assetsInfo = walletAccount.read_assets()
  p "--------The Wallet Assets List-----------------"
  assetsInfo["data"].each { |x| puts x["symbol"] + " " +
                              x["balance"] + " " + x["public_key"] +
                              x["account_name"] + " " + x["account_tag"]}
  p "----------End of Wallet Assets --------------"
end

Source code execution

implementruby bitcoin_wallet-ruby.rbIt’s time to start trading.

  • 1: Create Bitcoin Wallet and update PIN
  • 2: Read Bitcoin balance & address
  • 3: Read USDT balance & address
  • 4: Read EOS balance & address
  • tbb:Transfer BTC from Bot to Wallet
  • tbm:Transfer BTC from Wallet to Master
  • teb:Transfer EOS from Bot to Wallet
  • tem:Transfer EOS from Wallet to Master
  • tub:Transfer USDT from Bot to Wallet
  • tum:Transfer USDT from Wallet to Master
  • tcb:Transfer CNB from Bot to Wallet
  • tcm:Transfer CNB from Wallet to Master
  • 5: Pay 0.0001 BTC to ExinCore buy USDT
  • 6: Pay $1 USDT to ExinCore buy BTC
  • 7: Read Snapshots
  • 8: Fetch market price(USDT)
  • 9: Fetch market price(BTC)
  • ab: Read Bot Assets
  • aw: Read Wallet Assets
  • q: Exit

Make your choose(eg: q for Exit!):

Complete code

Solution Two: List your order on Ocean.One exchange