使用 NodeJS 調(diào)用 API 接口 進(jìn)行轉(zhuǎn)賬和 Bitshares 內(nèi)盤(pán)交易

使用官方的 bitsharesjs 庫(kù)進(jìn)行開(kāi)發(fā),參考了官方的轉(zhuǎn)賬例子,補(bǔ)充了下單的部分,更多的使用細(xì)節(jié)需要自己研究代碼。

這里附上轉(zhuǎn)賬和下單的示例函數(shù)。

/* import {Apis} from "bitsharesjs-ws";
import {ChainStore, FetchChain, PrivateKey, TransactionHelper, Aes, TransactionBuilder} from "bitsharesjs"; */
const Apis = require('bitsharesjs-ws').Apis;
const ChainStore = require('bitsharesjs').ChainStore;
const FetchChain = require('bitsharesjs').FetchChain;
const PrivateKey = require('bitsharesjs').PrivateKey;
const TransactionHelper = require('bitsharesjs').TransactionHelper;
const Aes = require('bitsharesjs').Aes;
const TransactionBuilder = require('bitsharesjs').TransactionBuilder;

const BTS_PRECISION = 100000;
const CNY_PRECISION = 10000;

const wss_url = "wss://bitshares-api.wancloud.io/ws";
var privKey = "privKey";//change to your privKey
let pKey = PrivateKey.fromWif(privKey);

const transfer = function (fromAccount, toAccount, sendAmount, memo) {
    return new Promise((resolve, reject) => {
        Apis.instance(wss_url, true)
            .init_promise.then((res) => {
                console.log("connected to:", res[0].network_name, "network");

                ChainStore.init().then(() => {

                    let memoSender = fromAccount;

                    Promise.all([
                        FetchChain("getAccount", fromAccount),
                        FetchChain("getAccount", toAccount),
                        FetchChain("getAccount", memoSender),
                        FetchChain("getAsset", sendAmount.asset),
                        FetchChain("getAsset", sendAmount.asset)
                    ]).then((res) => {
                        // console.log("got data:", res);
                        let [fromAccount, toAccount, memoSender, sendAsset, feeAsset] = res;

                        // Memos are optional, but if you have one you need to encrypt it here
                        let memoFromKey = memoSender.getIn(["options", "memo_key"]);
                        console.log("memo pub key:", memoFromKey);
                        let memoToKey = toAccount.getIn(["options", "memo_key"]);
                        let nonce = TransactionHelper.unique_nonce_uint64();

                        let memo_object = {
                            from: memoFromKey,
                            to: memoToKey,
                            nonce,
                            message: Aes.encrypt_with_checksum(
                                pKey,
                                memoToKey,
                                nonce,
                                memo
                            )
                        }

                        let tr = new TransactionBuilder()

                        tr.add_type_operation("transfer", {
                            fee: {
                                amount: 0,
                                asset_id: feeAsset.get("id")
                            },
                            from: fromAccount.get("id"),
                            to: toAccount.get("id"),
                            amount: { amount: sendAmount.amount, asset_id: sendAsset.get("id") },
                            memo: memo_object
                        })

                        tr.set_required_fees().then(() => {
                            tr.add_signer(pKey, pKey.toPublicKey().toPublicKeyString());
                            console.log("serialized transaction:", tr.serialize());
                            tr.broadcast(() => {
                                console.log("transaction done");
                            });
                        })
                    });
                });
            });

    });
}

const transferBTS = function (fromAccount, toAccount, amount, memo) {
    transfer(fromAccount, toAccount, { amount: amount * BTS_PRECISION, asset: "BTS" }, memo);
}

const create_order = function (orderAccount, sellAmount, buyAmount) {
    return new Promise((resolve, reject) => {
        Apis.instance(wss_url, true)
            .init_promise.then((res) => {
                console.log("connected to:", res[0].network_name, "network");

                ChainStore.init().then(() => {
                    Promise.all([
                        FetchChain("getAccount", orderAccount),
                        FetchChain("getAsset", sellAmount.asset),
                        FetchChain("getAsset", buyAmount.asset)
                    ]).then((res) => {
                        // console.log("got data:", res);
                        let orderAccount = res[0];
                        let sellAsset = res[1];
                        let feeAsset = sellAsset;
                        let buyAsset = res[2];

                        let tr = new TransactionBuilder()

                        tr.add_type_operation("limit_order_create", {
                            fee: {
                                amount: 0,
                                asset_id: feeAsset.get("id")
                            },
                            seller: orderAccount.get("id"),
                            amount_to_sell: { amount: sellAmount.amount, asset_id: sellAsset.get("id") },
                            min_to_receive: { amount: buyAmount.amount, asset_id: buyAsset.get("id") },
                            expiration: Math.floor(Date.now() / 1000) + 20,
                            fill_or_kill: false
                        })

                        tr.set_required_fees().then(() => {
                            tr.add_signer(pKey, pKey.toPublicKey().toPublicKeyString());
                            console.log("serialized transaction:", tr.serialize());
                            tr.broadcast(() => {
                                console.log("transaction done");
                            }).catch((err) => {
                                console.log(err);
                            });
                        })
                    });
                });
            });

    });
}

transferBTS("imba", "qiushaoxi", 0.05, "test nodejs");
create_order("imba", { amount: 0.5 * BTS_PRECISION, asset: "BTS" }, { amount: 5 * CNY_PRECISION, asset: "CNY" });
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • Android 自定義View的各種姿勢(shì)1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 178,769評(píng)論 25 709
  • 發(fā)現(xiàn) 關(guān)注 消息 iOS 第三方庫(kù)、插件、知名博客總結(jié) 作者大灰狼的小綿羊哥哥關(guān)注 2017.06.26 09:4...
    肇東周閱讀 15,055評(píng)論 4 61
  • 知識(shí)點(diǎn) webView設(shè)置 WebViewClient與WebChromeClient的區(qū)別 js調(diào)用java 應(yīng)...
    張年輪閱讀 497評(píng)論 0 1
  • 因?yàn)橐蛔牵矚g一個(gè)人! 在兒童時(shí)期,我跟著爸爸朋友的貨車(chē)來(lái)到南京,當(dāng)時(shí)我并不知道它是六朝古都,也不知道它是個(gè)大...
    妖不能幻化閱讀 360評(píng)論 0 1
  • 11 14 冷風(fēng)那個(gè)吹啊吹,吹啊吹,像沒(méi)穿褲子一樣,風(fēng)像刀片刮在臉上 幸福與否,皆在感受,心愛(ài)之人,總會(huì)遇見(jiàn)。在街...
    cara1x閱讀 196評(píng)論 0 0

友情鏈接更多精彩內(nèi)容