require 'spec_helper' RSpec.describe PortalConnectors::CoinWalletClient do let(:client) { PortalConnectors::CoinWalletClient.singleton } describe "#get_address", vcr: true do it "returns the btc address" do address, ok = client.get_address(coin: "btc", account_id: 918) expect(ok).to be_truthy expect(address["address"]).to eq("35PHLets1dZxueFxcr8yWAE8QEp8gF1KGw") expect(address["coin"]).to eq("btc") end context "usdt coin" do it "returns the usdt address" do address, ok = client.get_address(coin: "usdt", account_id: 919) expect(ok).to be_truthy expect(address["address"]).to eq("165C7ZztTPpTkvDNdNVr49dtmCLFRCtdkt") expect(address["coin"]).to eq("usdt") end end context "coin is erct and address_type is contract" do it "returns the erct address and address_type is contract" do address, ok = client.get_address(coin: "erct", account_id: 919, address_type: "contract") expect(ok).to be_truthy expect(address["address"]).to eq("0x6242545220a0ef909fcf6a9e2e4dc638a5cdeaf6") expect(address["address_type"]).to eq("contract") expect(address["coin"]).to eq("erct") end end end describe "#get_address_name", vcr: true do it "returns the name of address correctly" do result, ok = client.get_address_name(coin: "pm", address: "U34617415") expect(ok).to be_truthy expect(result["name"]).to eq("remitano.com") end end describe "#send_withdrawal", vcr: true do it "returns the withdrawal data" do resp, ok = client.send_withdrawal(coin: "btc", address: "1F4ttRGNspjYMEutMpoUADpgLz9geJNp7Q", amount: 0.123, payment_id: "identifier1") expect(ok).to be_truthy expect(resp["tx_hash"]).to be_nil expect(resp["address"]).to eq("1F4ttRGNspjYMEutMpoUADpgLz9geJNp7Q") expect(resp["amount"]).to eq("0.123") expect(resp["status"]).to eq("pending") expect(resp["coin"]).to eq("btc") expect(resp["payment_id"]).to eq("identifier1") expect(resp["token_id"]).to be_nil end context "usdt coin" do it "returns the withdrawal data" do resp, ok = client.send_withdrawal(coin: "usdt", address: "1F4ttRGNspjYMEutMpoUADpgLz9geJNp7Q", amount: 100, payment_id: "identifier2") expect(ok).to be_truthy expect(resp["tx_hash"]).to be_nil expect(resp["address"]).to eq("1F4ttRGNspjYMEutMpoUADpgLz9geJNp7Q") expect(resp["amount"]).to eq("100.0") expect(resp["status"]).to eq("pending") expect(resp["coin"]).to eq("usdt") expect(resp["payment_id"]).to eq("identifier2") expect(resp["token_id"]).to be_nil end end context "bnb_testnft5tokenid2" do it "returns the withdrawal data" do resp, ok = client.send_withdrawal(coin: "bnb_testnft5tokenid2", address: "0xdf3b60379166664c76a2ad3ce9279568ee27a8a4", amount: 100, token_id: 1, payment_id: "identifier3") expect(ok).to be_truthy expect(resp["tx_hash"]).to be_nil expect(resp["address"]).to eq("0xdf3b60379166664c76a2ad3ce9279568ee27a8a4") expect(resp["amount"]).to eq("100.0") expect(resp["status"]).to eq("pending") expect(resp["coin"]).to eq("bnb_testnft5tokenid2") expect(resp["payment_id"]).to eq("identifier3") expect(resp["token_id"]).to eq(1) end end end describe "#cancel_withdrawal", vcr: true do it "returns cancel result" do resp, ok = client.cancel_withdrawal(coin: "usdt", payment_id: "identifier2") expect(ok).to be_truthy expect(resp["success"]).to be_truthy end end describe "#fetch_deposit", vcr: true do context "deposit does not exists" do it "returns nil" do resp, ok = client.fetch_deposit(coin: "btc", tx_hash: SecureRandom.hex(32), out_index: 0) expect(ok).to be_falsey expect(resp).to be_nil end end end describe "#fetch_restrictions", vcr: true do context "return restrictions on an address" do it "returns nil" do resp, ok = client.fetch_restrictions(coin: "xrp", address: "rEb8TK3gBgk5auZkwc6sHnwrGVJH8DuaLh") expect(ok).to be_truthy expect(resp["require_destination_tag"]).to be_truthy end end end describe "#fetch_block", vcr: true do context "find a block not mined yet" do it "returns last confirmed block" do resp, ok = client.fetch_block(coin: "btc", block_height: 200) expect(ok).to be_truthy expect(resp["block"]["block_height"]).to eq(110) end end end describe "#fetch_tx" do context "valid transaction hash", :vcr do it "returns transaction details" do tx_hash = "0x6f5d92b1927e4ec7ea505aee78143e61678c39a7768d60685c682da1e0a75fbc" resp, ok = client.fetch_tx(chain: "eth", tx_hash: tx_hash) expect(ok).to be_truthy expect(resp["transaction"]["hash"]).to eq(tx_hash) expect(resp["transaction"]["from"]).to eq("0xf58b54a57c3190a4a9daddedfffad9528e8efb9d") end end end describe "#decode_invoice" do context "valid invoice", :vcr do it "returns decoded invoice" do bolt11 = "lnbc70u1pn9gztwpp5klpqlvy5t0qzgxjmejul3km8m69p420q2w6p8dg" \ "hd2e6vvymhjlsdqqcqzysxqrrsssp5c99vs2caq53v897ua4et4msh95483zgn5" \ "dfp65xdpwl3g05jvfrq9qyyssqvpjatkmzklk89nm7xgmgmjdy3jh69pulxvnhp" \ "qnjcscw4ylt76hhys4h6we9yhzxeu7atpvyyp0tw73a9h28nhkwu7elkzrnmty8vfqp7ad3sa" res, ok = client.decode_invoice(bolt11: bolt11) expect(ok).to be_truthy expect(res).to eq( "type" => "bolt11 invoice", "currency" => "bc", "created_at" => 1_716_783_470, "expiry" => 3600, "payee" => "03a1f3afd646d77bdaf545cceaf079bab6057eae52c6319b63b5803d0989d6a72f", "amount_msat" => 7_000_000, "amount_sat" => "7000.0", "amount_btc" => "0.00007", "description" => "", "min_final_cltv_expiry" => 144, "payment_secret" => "c14ac82b1d0522c397dced72baee172d2a788913a3521d50cd0bbf143e926246", "features" => "024200", "payment_hash" => "b7c20fb0945bc0241a5bccb9f8db67de8a1aa9e053b413b5176ab3a6309bbcbf", "signature" => "304402206065d5db62b7ec72cf7e32368dc9a48cafa2879f33" \ "27708272c430ea93ebf6af02207242b7d3b2525c46cf3dd58584205eb77a3d2dd479decee7b3fb0873dac87624", "valid" => true ) end end context "invalid invoice", :vcr do it "returns false" do bolt11 = "invalid_nonce" res, ok = client.decode_invoice(bolt11: bolt11) expect(ok).to be_falsey expect(res["error"]).to eq "invalid invoice" end end end describe "#request_invoice" do context "successfully", :vcr do it "returns invoice" do res, ok = client.request_invoice(account_id: 29, amount_btc: 0.000_9) expect(ok).to be_truthy expect(res).to eq( "label" => "Inv-remitano-29-90000000-1717136529", "bolt11" => "lnbc900u1pn9jmyjsp5cdj8xn6wtarwrh6llde4zeajtm2sufk6mmhzhg" \ "0dklezmkkf7gcspp50c6wuxtpgkuhvu0tfhgkh9assg4p5k29wmhs83gm9h49eq" \ "ukxcusdqdwfjk66t5v9hx7cqpjrzjq2tt9ke59l8c0655mxqh2l7lf5l9gk74em" \ "6fr86ckhfcmlwh806ujr8q9sqqgtcqqqqqqqqqqqqqqeqq2qrzjqwryaup9lh50" \ "kkranzgcdnn2fgvx390wgj5jd07rwr3vxeje0glc7r8rkgqqpasqqqqqqqlgqqq" \ "qrucqjqrzjq25carzepgd4vqsyn44jrk85ezrpju92xyrk9apw4cdjh6yrwt5jg" \ "r8rcyqqv8sqqqqqqqqqqqqqqqqq9q9qxpqysgqf5ntd9dsd7608cn2kf4230ydr" \ "hufvumgt8hzpcqlvlr0cg9e8mhyqwxmmpvd6zd0k3j5jj0k4tvzceghavf7643pudl4ja8ffukaz3cqw9keut", "status" => "unpaid", "amount_msat" => 90_000_000, "amount_btc" => "0.0009", "amount_sat" => "90000.0", "app_name" => "remitano", "account_id" => 29, "payment_hash" => "7e34ee196145b97671eb4dd16b97b0822a1a594576ef03c51b2dea5c83963639", "expires_at" => 1_717_140_131 ) end end context "failed", :vcr do it "returns false" do res, ok = client.request_invoice(account_id: 29, amount_btc: 0.9) expect(ok).to be_falsey expect(res["error"]).to eq "The BTC amount is greater than max limit: 0.02" end end end describe "#nonce_key" do it "related to the class name" do expect(client.send :nonce_key).to eq "coin_wallet:nonce" end end describe "#portal_name" do it "related to the class name" do expect(client.send :portal_name).to eq "coin_wallet" end end describe "#next_nonce" do it "auto incremented" do expect(client.send :next_nonce).to eq "remitano-1" expect(client.send :next_nonce).to eq "remitano-2" end end end