1. 程式人生 > >交易所通過eosjs進行充值與轉賬的程式碼片段

交易所通過eosjs進行充值與轉賬的程式碼片段

連了兩個遠端測試節點

const rpcUrl = 'http://jungle2.cryptolions.io:80'

const actionUrl = 'http://junglehistory.cryptolions.io:18888' //只找到這個測試節點能夠提供actions記錄

 

//處理賬戶交易記錄,判斷從上一次掃描之後賬戶新增記錄,並根據備註給使用者充值
async function dealActions(accountName) {
    let rpcH = new JsonRpc(actionUrl, { fetch });
    let actionHistory = await rpcH.history_get_actions(accountName);
    let latestTime = 'select TRANS_TIME from RECHARGE where IFLAST=yes' //虛擬碼

    for (let i in actionHistory.actions) {  
        var block_time = actionHistory.actions[i].block_time    
        if(block_time > latestTime){
            let memo = actionHistory.actions[i].action_trace.act.data.memo
            let quantity = actionHistory.actions[i].action_trace.act.data.quantity
            let from = actionHistory.actions[i].action_trace.act.data.from
            let to = actionHistory.actions[i].action_trace.act.data.to
            let trx_id = actionHistory.actions[i].action_trace.trx_id
            let global_action_seq = actionHistory.actions[i].global_action_seq
            let block_num = actionHistory.actions[i].block_num
            let producer_block_id = actionHistory.actions[i].producer_block_id
            //let to = actionHistory.actions[i].action_trace.act.data.to
            
            console.log('memos'+i+'='+memo)
            //insert into RECHARGE values()  //插入充值表
            //update USER_ACCOUNT set BALANCE AVAILABLE_BALANCE where USER_ID = 0 //更新賬戶
        }
      };

    return actionHistory
}

 //轉賬操作 轉賬到賬戶accountName 'eosaccountb2';轉賬數額quantity '1.1234 EOS'
async function transfer(accountName,quantity) {
    let signatureProvider = new JsSignatureProvider([pkeys[0].privateKey]);
    let api = new Api({ rpc, signatureProvider, textDecoder: new TextDecoder(), textEncoder: new TextEncoder() });
    let result = await api.transact({
        actions: [{
            account: 'eosio.token',
            name: 'transfer',
            authorization: [{
                actor: pkeys[0].actor,
                permission: 'active',
            }],
            data: {
                from: pkeys[0].actor,
                to: accountName,    //提現地址
                quantity: quantity, //提現數量
                memo: '',
            },
        }]
    }, {
        blocksBehind: 3,
        expireSeconds: 30,
    });
    console.dir(result); 
    let transaction_id = result.transaction_id    
    let receipt= result.processed.receipt   //抵押的net和cpu,記到資料庫備份
    console.log('receipt===='+JSON.stringify(receipt))
    //insert into WITHDRAW() 
};