摘要:完整的步驟如下檢查比特幣或的余額,錢包地址。比特幣的到帳時間是個區塊的高度,約分鐘。
通過 Nodejs 買賣Bitcoin
Github Repo
方案一: 通過ExinCore API進行幣幣交易Exincore 提供了基于Mixin Network的幣幣交易API.
你可以支付USDT給ExinCore, ExinCore會以最低的價格,最優惠的交易費將你購買的比特幣轉給你, 每一幣交易都是匿名的,并且可以在區塊鏈上進行驗證,交易的細節只有你與ExinCore知道!
ExinCore 也不知道你是誰,它只知道你的UUID.
預備知識:你先需要創建一個機器人, 方法在 教程一.
安裝依賴包正如教程一里我們介紹過的, 我們需要依賴 mixin-node-client, 你應該先安裝過它了, 這兒我們再安裝其它的軟件包.
yarn add fast-csv inquirer msgpack5 path pem-file
or
npm i fast-csv inquirer msgpack5 path pem-file充幣到 Mixin Network, 并讀出它的余額.
通過ExinCore API, 可以進行BTC, USDT, EOS, ETH 等等交易, 此處演示用 USDT購買BTC 或者 用BTC購買USDT。交易前,先檢查一下錢包地址。
完整的步驟如下:
檢查比特幣或USDT的余額,錢包地址。并記下錢包地址。
從第三方交易所或者你的冷錢包中,將幣充到上述錢包地址。
再檢查一下幣的余額,看到帳與否。(比特幣的到帳時間是5個區塊的高度,約100分鐘)。
比特幣與USDT的充值地址是一樣的。
let aesKey = ""; const privateKeyBytes = pem.decode(Buffer.from(data[0])); const aesKeyBuffer = await oaepDecrypt( Buffer.from(data[1], "base64"), privateKeyBytes, "SHA-256", Buffer.from(data[2]) ); aesKey = Buffer.from(aesKeyBuffer).toString("base64"); // console.log(aesKey); const newUserConfig = {clientId: data[3], aesKey: aesKey, privateKey: data[0], sessionId: data[2], clientSecret: "do not need", assetPin: data[4]}; // console.log(newUserConfig); const newUserClient = new HttpClient(newUserConfig); if ( args.type === TYPE_ASSETS_INFO ) { const assetsInfo = await newUserClient.getUserAssets(); console.log(assetsInfo); } else if (args.type === TYPE_BITCOIN_INFO) { // console.log("You choice to 1:", args); const assetInfo = await newUserClient.getUserAsset(BTC_ASSET_ID); console.log("Bitcoin address is ", assetInfo.public_key); console.log("Bitcoin balance is ", assetInfo.balance); console.log("Bitcoin price is (USD) ", assetInfo.price_usd);查詢ExinCore市場的價格信息
如何來查詢ExinCore市場的價格信息呢?你要先了解你交易的基礎幣是什么,如果你想買比特幣,賣出USDT,那么基礎貨幣就是USDT;如果你想買USDT,賣出比特幣,那么基礎貨幣就是比特幣.
if ( args.type === TYPE_FETCH_USDT_MARKETINFO ) { // Make a request var instance = axios.create({ baseURL: "https://exinone.com/exincore/markets", timeout: 3000, headers: {"X-Custom-Header": "foobar"} }); instance.get("?base_asset=" + USDT_ASSET_ID) .then(function(response) { console.log(response.data.data); }); } else if ( args.type === TYPE_FETCH_BTC_MARKETINFO ) { var instance = axios.create({ baseURL: "https://exinone.com/exincore/markets", timeout: 3000, headers: {"X-Custom-Header": "foobar"} }); instance.get("?base_asset=" + BTC_ASSET_ID) .then(function(response) { console.log(response.data.data); }); }交易前,創建一個Memo!
在第二章里,基于Mixin Network的 Nodejs 比特幣開發教程: 機器人接受比特幣并立即退還用戶, 我們學習過退還用戶比特幣,在這里,我們除了給ExinCore支付幣外,還要告訴他我們想購買的幣是什么,即將想購買的幣存到memo里。
const bytes = Buffer.from( BTC_ASSET_ID.replace(/-/g, ""), "hex" ); const memo = msgpack .encode({ A: bytes, }) .toString("base64"); console.log(memo);幣幣交易的完整流程
轉幣給ExinCore時,將memo寫入你希望購買的幣,否則,ExinCore會直接退幣給你!
如果你想賣出比特幣買入USDT,調用方式如下:
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"; //change to your third exchange/cold btc wallet address const BTC_WALLET_ADDR = "14T129GTbXXPGXXvZzVaNLRFPeHXD1C25C"; const EOS_WALLET_NAME = "huobideposit"; const EOS_WALLET_TAG = "1872050"; //change to your mixin messenger account "s uuid const MASTER_UUID = "0b4f49dc-8fb4-4539-9a89-fb3afc613747"; else if ( args.type === TYPE_EXCHANGE_BTC_USDT ) { // Pack memo const bytes = Buffer.from( USDT_ASSET_ID.replace(/-/g, ""), "hex" ); const memo = msgpack .encode({ A: bytes, }) .toString("base64"); console.log(memo); // gaFBxBDG0McoJiRCm44N2dGbZZL6 const assetInfo = await newUserClient.getUserAsset(BTC_ASSET_ID); console.log("The Wallet "s BTC balance is ", assetInfo.balance); if ( assetInfo.balance >= 0.0001 ) { const Obj = { assetId: BTC_ASSET_ID, recipientId: EXIN_BOT, traceId: newUserClient.getUUID(), amount: "0.0001", memo: memo, } console.log(Obj); newUserClient.transferFromBot(Obj); } else { console.log("Not enough BTC!"); } }
如果你想賣出USDT買入比特幣,調用方式如下:
else if ( args.type === TYPE_EXCHANGE_USDT_BTC ) { // Pack memo const bytes = Buffer.from( BTC_ASSET_ID.replace(/-/g, ""), "hex" ); const memo = msgpack .encode({ A: bytes, }) .toString("base64"); console.log(memo); const assetInfo = await newUserClient.getUserAsset(USDT_ASSET_ID); console.log("The Wallet "s BTC balance is ", assetInfo.balance); if ( assetInfo.balance >= 1 ) { const Obj = { assetId: USDT_ASSET_ID, recipientId: EXIN_BOT, traceId: newUserClient.getUUID(), amount: "1", memo: memo, } console.log(Obj); newUserClient.transferFromBot(Obj); } else { console.log("Not enough USDT!"); } }
交易完成后,Exincore會將你需要的幣轉到你的帳上,同樣,會在memo里,記錄成交價格,交易費用等信息!你只需要按下面的方式解開即可!
getSnapshots 讀取錢包的交易記錄。
if ( args.type === TYPE_READ_SNAPSHOTS ) { const prompts = [ { name: "datetime", type: "input", message: "Input iso8601 datetime: ", }, ]; const answers = await inquirer.prompt(prompts); console.log(answers); console.log(encodeURIComponent(answers.datetime)); const snapshots = await newUserClient.getSnapshots({ limit: 10, asset: USDT_ASSET_ID, offset: answers.datetime, order: "ASC"}); // console.log(snapshots); snapshots.forEach(function(element) { if ( element.amount > 0) { if ( element.data != null ) { console.log(element.amount); console.log(element.data); const buf = Buffer.from(element.data, "base64"); console.log(msgpack.decode(buf)); const codeInt = msgpack.decode(buf).C; if ( codeInt === 1000 ) { console.log("Successful Exchange"); } else { console.log("Go to there get more info https://github.com/exinone/exincore#code error code: " + codeStr);} const hexStr = Buffer.from(msgpack.decode(buf).FA).toString("hex"); const uuid = `${hexStr.slice(0,8)}-${hexStr.slice(8,12)}-${hexStr.slice(12,16)}-${hexStr.slice(16,20)}-${hexStr.slice(20)}`; console.log("Asset uuid is :" + uuid); const priceStr = msgpack.decode(buf).P; console.log("Price is :" + priceStr); const feeStr = msgpack.decode(buf).F; console.log("Fee is :" + feeStr); console.log("percent of Fee is :" + (feeStr/element.amount)* 100 + " %"); const tStr = msgpack.decode(buf).T; console.log("T is :" + tStr); } } }); }
一次成功的交易如下:
Make your choose 16: Read snapshots You choice to : { type: "16: Read snapshots" } You wallet is : 0b10471b-1aed-3944-9eda-5ab947562761 ? Input iso8601 datetime: 2019-04-08T05:16:33.615253Z { datetime: "2019-04-08T05:16:33.615253Z" } 2019-04-08T05%3A16%3A33.615253Z 0.5228004 hqFDzQPooVCnNTI0OC45OKFGqTAuMDAxMDQ5OKJGQcQQgVsLGidkNzaPqkLWlPpiCqFUoVKhT8QQeJyt3MrqSGOpbqzFXy5JUw== { C: 1000, P: "5248.98", F: "0.0010498", FA:讀取幣的余額, T: "R", O: } Successful Exchange Asset uuid is :815b0b1a-2764-3736-8faa-42d694fa620a Price is :5248.98 Fee is :0.0010498 percent of Fee is :0.2008032128514056 % T is :R You select the : 0b10471b-1aed-3944-9eda-5ab947562761 You select the wallet 0b10471b-1aed-3944-9eda-5ab947562761
通過讀取幣的余額,來確認交易情況!
if (args.type === TYPE_BITCOIN_INFO) { // console.log("You choice to 1:", args); const assetInfo = await newUserClient.getUserAsset(BTC_ASSET_ID); console.log("Bitcoin address is ", assetInfo.public_key); console.log("Bitcoin balance is ", assetInfo.balance); console.log("Bitcoin price is (USD) ", assetInfo.price_usd); }源代碼執行
編譯執行,即可開始交易了.
[x] node bitcoin-wallet-nodejs.js 編譯項目.
本代碼執行時的命令列表:
Make your choose(select the uuid for open the specified wallet): 0b10471b-1aed-3944-9eda-5ab947562761
You select the : 0b10471b-1aed-3944-9eda-5ab947562761
You select the wallet 0b10471b-1aed-3944-9eda-5ab947562761
?
Make your choose (Use arrow keys)
0: Read All Asssets Balance
1: Read Bitcoin Balance & Address
2: Read USDT Balance & Address
3: Read EOS Balance & Address
4: Transfer BTC from Bot to Wallet
5: Transfer EOS from Bot to Wallet
6: Transfer BTC from Wallet to Master
7: Transfer EOS from Wallet to Master
8: Verify Wallet PIN
9: BTC withdraw
10: EOS withdraw
11: Fetch BTC withdrawal info
12: Fetch EOS withdrawal info
13: Fetch USDT Market info
14: Fetch BTC Market info
14: Transfer 0.0001 BTC buy USDT
15: Transfer USDT $1 buy BTC
16: Read snapshots
Exit
完整代碼
Solution Two: List your order on Ocean.One exchange文章版權歸作者所有,未經允許請勿轉載,若此文章存在違規行為,您可以聯系管理員刪除。
轉載請注明本文地址:http://m.specialneedsforspecialkids.com/yun/24635.html
摘要:課程簡介創建一個機器人機器人接受比特幣并立即退還用戶創建比特幣錢包買賣實時兌換買賣在自由市場掛單買賣買賣任意在自由市場掛單買賣其他編程語言比特幣開發教程如下比特幣開發教程比特幣開發教程比特幣開發教程比特幣開發教程比特幣開發教程比特幣開發教程 showImg(https://segmentfault.com/img/bVboFQ6?w=1200&h=659); 課程簡介 PHP 創建一...
摘要:是一個免費的極速的端對端加密數字貨幣交易系統官方資源匯總課程簡介創建一個機器人比特幣開發教程機器人接受比特幣并立即退還用戶比特幣開發教程創建比特幣錢包買賣實時兌換買賣在自由市場掛單買賣買賣任意在自由市場掛單買賣其他編程語言匯總比特幣開發教程 showImg(https://segmentfault.com/img/remote/1460000019234638?w=1200&h=659...
摘要:課程簡介創建一個機器人機器人接受比特幣并立即退還用戶創建比特幣錢包買賣實時兌換買賣在自由市場掛單買賣買賣任意在自由市場掛單買賣其他編程語言比特幣開發教程如下比特幣開發教程比特幣開發教程比特幣開發教程比特幣開發教程比特幣開發教程比特幣開發教程 showImg(https://segmentfault.com/img/bVboFQ6?w=1200&h=659); 課程簡介 PHP 創建一...
摘要:是一個免費,秒確認的高速轉賬網絡。我們撰寫了基于多種語言的系列教程,開發者可以分鐘搭建一個比特幣收款應用。教程創建機器人接收發送消息機器人接收發送比特幣創建比特幣錢包,轉賬買賣比特幣開發者資料匯總 Mixin Network是一個免費,1秒確認的高速轉賬網絡。我們撰寫了基于多種語言的系列教程,開發者可以15分鐘搭建一個比特幣收款應用。 教程 PHP Go Node.js C# Py...
摘要:是一個免費,秒確認的高速轉賬網絡。我們撰寫了基于多種語言的系列教程,開發者可以分鐘搭建一個比特幣收款應用。教程創建機器人接收發送消息機器人接收發送比特幣創建比特幣錢包,轉賬買賣比特幣開發者資料匯總 Mixin Network是一個免費,1秒確認的高速轉賬網絡。我們撰寫了基于多種語言的系列教程,開發者可以15分鐘搭建一個比特幣收款應用。 教程 PHP Go Node.js C# Py...
閱讀 3665·2021-11-15 11:37
閱讀 2320·2021-09-24 10:39
閱讀 2450·2021-07-25 21:37
閱讀 1439·2019-08-30 15:56
閱讀 2585·2019-08-30 15:55
閱讀 952·2019-08-30 15:54
閱讀 2124·2019-08-30 14:21
閱讀 855·2019-08-30 11:24