This content was deleted by the author. You can see it from Blockchain History logs.

Getting the entire account history (Steem.js) - Rewriting code and drinking beer

20180709_171027_0001_1280.jpg

I keep rewriting the code for the same little function. I'm sorry. It's my first JavaScript experience. At least I'm happy that it's slightly more readable, with less lines.

Eventually I'll have a little repository (github) to share. I'm only doing this because I've been focusing down on learning new things and don't have much to share for now.

Again, it's nothing more than searching through an account history with a filter that checks sender / receiver / memo. Without any filters, you get the entire history.

Running the code with my own name @roundbeargames, for example, I can see that the account received total of 2193.956 STEEM over its lifespan (the number itself doesn't mean anything).

htga1280.jpg


I'm working on a few other mechanics that'll eventually come together into a single program. I'll start sharing them as soon as they're ready. For the time being, let's have a little beer and keep working. (I'm not Chinese but I like Tsingtao)

20180709_190841_0001_1280.jpg


<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>Transfers</title>
    <script src="https://cdn.steemjs.com/lib/latest/steem.min.js"></script>
    <script type="text/javascript" src="steemNode.js"></script>
    <script type="text/javascript" src="sortSteem.js"></script>
    <script type="text/javascript" src="history.js"></script>
    <script type="text/javascript" src="transfers.js"></script>
</head>

<body>
    <script>
        setReceiver("roundbeargames");
        getTransfers("roundbeargames");
    </script>
</body>

</html>

//@ts-check

function getTransfers(accountName) {
    console.log("getting transfer data from: " + accountName)
    transferHistory.transfers = null;
    transferHistory.total = null;
    // @ts-ignore
    downloadHistory(accountName);
    waitHistory();
}

function getFilteredTransfers() {
    return transferHistory;
}

function setReceiver(receiver) {
    tFilter.receiver = receiver;
}

function setSender(sender) {
    tFilter.sender = sender;
}

function waitHistory() {
    // @ts-ignore
    if (getHistory() == null) {
        setTimeout("waitHistory()", 500);
    } else {
        filterTransfers();
    }
}

function TransferParams() {
    this.sender = null;
    this.receiver = null;
}

function filterTransfers() {
    tempTransfers = new Array();
    console.log("");
    console.log("transfer history");
    let filterSender = false;
    let filterReceiver = false;
    let add = false;

    if (tFilter.receiver != null) {
        console.log("filter by receiver: " + tFilter.receiver);
        filterReceiver = true;
    }

    if (tFilter.sender != null) {
        console.log("filter by sender: " + tFilter.sender);
        filterSender = true;
    }

    // @ts-ignore
    let history = getHistory();

    for (let i = 0; i < history.length; i++) {
        const op = history[i][1].op;
        const opType = history[i][1].op[0];
        const action = history[i][1];
        const amount = op[1].amount;
        add = false;

        if (opType == "transfer") {
            if (filterSender == true && filterReceiver == true) {
                if (op[1].from == tFilter.sender && op[1].to == tFilter.receiver) {
                    add = true;
                }
            } else if (filterSender == true && filterReceiver == false) {
                if (op[1].from == tFilter.sender) {
                    add = true;
                }
            } else if (filterSender == false && filterReceiver == true) {
                if (op[1].to == tFilter.receiver) {
                    add = true;
                }
            } else {
                add = true;
            }

            if (add) {
                tempTransfers.push(action);
                total.sortCurrency(amount, "STEEM");
                total.sortCurrency(amount, "SBD");
            }
        }
    }

    transferHistory.transfers = tempTransfers;
    transferHistory.total = total;

    console.log(transferHistory);
}

let tempTransfers = null;
const transferHistory = { "transfers": null, "total": null };
const tFilter = new TransferParams();
// @ts-ignore
const total = new CurrencySort();

//@ts-check

/**
 * 
 * @param {number} from 
 * @param {number} limit 
 */
function getPartHistory(from, limit) {
    // @ts-ignore
    steem.api.getAccountHistory(filter.accountName, from, limit, function(err, result) {
        console.log("(" + from + ", " + limit + ") error: " + err);
        let halt = false;

        if (err == null) {
            for (let i = result.length - 1; i >= 0; i--) {
                //halt on index (inclusive)
                if (indexLimitFound(result[i])) {
                    halt = true;
                    tempHistory.push(result[i]);
                    break;
                }
                //halt on memo (inclusive)
                else if (memoFound(result[i])) {
                    halt = true;
                    tempHistory.push(result[i]);
                    break;
                }

                //normal result
                tempHistory.push(result[i]);
            }

            const next = tempHistory[tempHistory.length - 1][0] - 1;
            if (next <= 0 || halt == true) {
                accountHistory = tempHistory;
                console.log("done");
                console.log(accountHistory);
            } else {
                if (limit > next) {
                    limit = next;
                }
                getPartHistory(next, limit);
            }
        } else {
            console.log(err);
            console.log("");
            console.log("trying again");
            console.log("");
            getPartHistory(from, limit);
        }
    });
}

function HistoryParams() {
    this.accountName = null;
    this.from = null;
    this.limit = null;
}

function SearchLimit() {
    this.index = null;
    this.sender = null;
    this.memo = null; //memo must match sender
}

function indexLimitFound(transaction) {
    if (transaction[0] == searchLimit.index) {
        if (searchLimit.index != 0) {
            console.log("found index: " + searchLimit.index);
        }
        return true;
    }
    return false;
}

function memoFound(transaction) {
    if (transaction[1].op[0] == "transfer") {
        if (transaction[1].op[1].memo == searchLimit.memo &&
            transaction[1].op[1].from == searchLimit.sender) {
            console.log("found memo (halting search): " + searchLimit.memo);
            console.log("found sender: " + searchLimit.sender)
            return true;
        }
    }
    return false;
}

function downloadHistory(accountName) {
    console.log("");
    console.log("fetching account history..");
    accountHistory = null;
    filter.accountName = accountName;
    filter.from = -1;
    filter.limit = 10000;
    getPartHistory(filter.from, filter.limit);
}

function getHistory() {
    return accountHistory;
}

/**
 * 
 * @param {number} index 
 */
function setSearchIndex(index) {
    searchLimit.index = index;
}

/**
 * 
 * @param {string} memo 
 * @param {string} sender 
 */
function setSearchMemo(memo, sender) {
    searchLimit.memo = memo;
    searchLimit.sender = sender;
}

let accountHistory = null;
let tempHistory = new Array();
const filter = new HistoryParams();
const searchLimit = new SearchLimit();