仮想通貨サービス BitCapitalZ の日記

仮想通貨関連のブログです Ripple, Ethereum 仮想通貨2.0に注目しています

【リップル】ripple-lib を使ってRippleネットワーク上のトランザクションの通知を受け取る【Ripple】

Ripple WebSocket API をラップした ripple-lib では Rippleネットワーク上のトランザクションは以下のコードでリアルタイムに取得できます。トランザクションが発生する度にtransactionListenerがディスパッチされます。

/* Loading ripple-lib with Node.js */
var Remote = require('ripple-lib').Remote;

/* Loading ripple-lib in a webpage */
// var Remote = ripple.Remote;

var remote = new Remote({
  // see the API Reference for available options
  servers: [ 'wss://s1.ripple.com:443' ]
});

remote.connect(function() {
  remote.on('transaction_all', transactionListener);
});

function transactionListener (transaction_data) {
  console.log(JSON.stringify(transaction_data));
}

これを利用して作ったページが Ripple取引状況観察 です。


取得したデータは transaction_data に入っていて以下のようなデータです。

{
    "engine_result": "tesSUCCESS", 
    "engine_result_code": 0, 
    "engine_result_message": "The transaction was applied. Only final in a validated ledger.", 
    "ledger_hash": "2BA7BE7C5F27F04917701CBFBF44657FDFF33BA9F74F1C6EF21B1CCF18F82020", 
    "ledger_index": 11276815, 
    "meta": {
        "AffectedNodes": [
            {
                "ModifiedNode": {
                    "FinalFields": {
                        "Account": "rfCFLzNJYvvnoGHWQYACmJpTgkLUaugLEw", 
                        "AccountTxnID": "C765097537246A125B09CE5888F28792B7BD1B56D8F6AB7E90B4271C8E7D96DE", 
                        "Balance": "163573031854", 
                        "Flags": 0, 
                        "OwnerCount": 29, 
                        "RegularKey": "r9S56zu6QeJD5d8A7QMfLAeYavgB9dhaX4", 
                        "Sequence": 2973899
                    }, 
                    "LedgerEntryType": "AccountRoot", 
                    "LedgerIndex": "2880A9B4FB90A306B576C2D532BFE390AB3904642647DCF739492AA244EF46D1", 
                    "PreviousFields": {
                        "AccountTxnID": "4A6AD2DDDE3BE3F204C2883239C319F278939A5A5E3A8562F230495A50AE42F7", 
                        "Balance": "163573042854", 
                        "OwnerCount": 28, 
                        "Sequence": 2973898
                    }, 
                    "PreviousTxnID": "4A6AD2DDDE3BE3F204C2883239C319F278939A5A5E3A8562F230495A50AE42F7", 
                    "PreviousTxnLgrSeq": 11276815
                }
            }, 
            {
                "ModifiedNode": {
                    "FinalFields": {
                        "ExchangeRate": "55038E8F5CFFA198", 
                        "Flags": 0, 
                        "RootIndex": "8C28E5438902173D0254ECA2C504AAF68FBEF982D7C74AFD55038E8F5CFFA198", 
                        "TakerGetsCurrency": "0000000000000000000000004254430000000000", 
                        "TakerGetsIssuer": "DD39C650A96EDA48334E70CC4A85B8B2E8502CD3", 
                        "TakerPaysCurrency": "0000000000000000000000004254430000000000", 
                        "TakerPaysIssuer": "0A20B3C85F482532A9578DBB3950B85CA06594D1"
                    }, 
                    "LedgerEntryType": "DirectoryNode", 
                    "LedgerIndex": "8C28E5438902173D0254ECA2C504AAF68FBEF982D7C74AFD55038E8F5CFFA198"
                }
            }, 
            {
                "CreatedNode": {
                    "LedgerEntryType": "Offer", 
                    "LedgerIndex": "DFA624FBB22D533BE27E96C15C135BA961D50F1602B7FF80F3F4EA168BFE457E", 
                    "NewFields": {
                        "Account": "rfCFLzNJYvvnoGHWQYACmJpTgkLUaugLEw", 
                        "BookDirectory": "8C28E5438902173D0254ECA2C504AAF68FBEF982D7C74AFD55038E8F5CFFA198", 
                        "Expiration": 475285029, 
                        "OwnerNode": "00000000000000E4", 
                        "Sequence": 2973898, 
                        "TakerGets": {
                            "currency": "BTC", 
                            "issuer": "rMwjYedjc7qqtKYVLiAccJSmCwih4LnE2q", 
                            "value": "1.91"
                        }, 
                        "TakerPays": {
                            "currency": "BTC", 
                            "issuer": "rvYAfWj5gh67oV6fW32ZzP3Aw4Eubs59B", 
                            "value": "1.91223722474574"
                        }
                    }
                }
            }, 
            {
                "ModifiedNode": {
                    "FinalFields": {
                        "Flags": 0, 
                        "IndexPrevious": "00000000000000E1", 
                        "Owner": "rfCFLzNJYvvnoGHWQYACmJpTgkLUaugLEw", 
                        "RootIndex": "F435FBBEC9654204D7151A01E686BAA8CB325A472D7B61C7916EA58B59355767"
                    }, 
                    "LedgerEntryType": "DirectoryNode", 
                    "LedgerIndex": "FAB65ADCA624173CAFB8CEC77A90885D7F0681AA7BBE1674102138C31E4D114F"
                }
            }
        ], 
        "TransactionIndex": 19, 
        "TransactionResult": "tesSUCCESS"
    }, 
    "mmeta": {
        "_affectedAccounts": [
            "rfCFLzNJYvvnoGHWQYACmJpTgkLUaugLEw", 
            "r9S56zu6QeJD5d8A7QMfLAeYavgB9dhaX4", 
            "0000000000000000000000004254430000000000", 
            "DD39C650A96EDA48334E70CC4A85B8B2E8502CD3", 
            "0A20B3C85F482532A9578DBB3950B85CA06594D1", 
            "rMwjYedjc7qqtKYVLiAccJSmCwih4LnE2q", 
            "rvYAfWj5gh67oV6fW32ZzP3Aw4Eubs59B"
        ], 
        "_affectedBooks": [
            "BTC/rMwjYedjc7qqtKYVLiAccJSmCwih4LnE2q:BTC/rvYAfWj5gh67oV6fW32ZzP3Aw4Eubs59B"
        ], 
        "nodes": [
            {
                "diffType": "ModifiedNode", 
                "entryType": "AccountRoot", 
                "fields": {
                    "Account": "rfCFLzNJYvvnoGHWQYACmJpTgkLUaugLEw", 
                    "AccountTxnID": "C765097537246A125B09CE5888F28792B7BD1B56D8F6AB7E90B4271C8E7D96DE", 
                    "Balance": "163573031854", 
                    "Flags": 0, 
                    "OwnerCount": 29, 
                    "RegularKey": "r9S56zu6QeJD5d8A7QMfLAeYavgB9dhaX4", 
                    "Sequence": 2973899
                }, 
                "fieldsFinal": {
                    "Account": "rfCFLzNJYvvnoGHWQYACmJpTgkLUaugLEw", 
                    "AccountTxnID": "C765097537246A125B09CE5888F28792B7BD1B56D8F6AB7E90B4271C8E7D96DE", 
                    "Balance": "163573031854", 
                    "Flags": 0, 
                    "OwnerCount": 29, 
                    "RegularKey": "r9S56zu6QeJD5d8A7QMfLAeYavgB9dhaX4", 
                    "Sequence": 2973899
                }, 
                "fieldsNew": {}, 
                "fieldsPrev": {
                    "AccountTxnID": "4A6AD2DDDE3BE3F204C2883239C319F278939A5A5E3A8562F230495A50AE42F7", 
                    "Balance": "163573042854", 
                    "OwnerCount": 28, 
                    "Sequence": 2973898
                }, 
                "ledgerIndex": "2880A9B4FB90A306B576C2D532BFE390AB3904642647DCF739492AA244EF46D1", 
                "nodeType": "ModifiedNode"
            }, 
            {
                "diffType": "ModifiedNode", 
                "entryType": "DirectoryNode", 
                "fields": {
                    "ExchangeRate": "55038E8F5CFFA198", 
                    "Flags": 0, 
                    "RootIndex": "8C28E5438902173D0254ECA2C504AAF68FBEF982D7C74AFD55038E8F5CFFA198", 
                    "TakerGetsCurrency": "0000000000000000000000004254430000000000", 
                    "TakerGetsIssuer": "DD39C650A96EDA48334E70CC4A85B8B2E8502CD3", 
                    "TakerPaysCurrency": "0000000000000000000000004254430000000000", 
                    "TakerPaysIssuer": "0A20B3C85F482532A9578DBB3950B85CA06594D1"
                }, 
                "fieldsFinal": {
                    "ExchangeRate": "55038E8F5CFFA198", 
                    "Flags": 0, 
                    "RootIndex": "8C28E5438902173D0254ECA2C504AAF68FBEF982D7C74AFD55038E8F5CFFA198", 
                    "TakerGetsCurrency": "0000000000000000000000004254430000000000", 
                    "TakerGetsIssuer": "DD39C650A96EDA48334E70CC4A85B8B2E8502CD3", 
                    "TakerPaysCurrency": "0000000000000000000000004254430000000000", 
                    "TakerPaysIssuer": "0A20B3C85F482532A9578DBB3950B85CA06594D1"
                }, 
                "fieldsNew": {}, 
                "fieldsPrev": {}, 
                "ledgerIndex": "8C28E5438902173D0254ECA2C504AAF68FBEF982D7C74AFD55038E8F5CFFA198", 
                "nodeType": "ModifiedNode"
            }, 
            {
                "bookKey": "BTC/rMwjYedjc7qqtKYVLiAccJSmCwih4LnE2q:BTC/rvYAfWj5gh67oV6fW32ZzP3Aw4Eubs59B", 
                "diffType": "CreatedNode", 
                "entryType": "Offer", 
                "fields": {
                    "Account": "rfCFLzNJYvvnoGHWQYACmJpTgkLUaugLEw", 
                    "BookDirectory": "8C28E5438902173D0254ECA2C504AAF68FBEF982D7C74AFD55038E8F5CFFA198", 
                    "Expiration": 475285029, 
                    "OwnerNode": "00000000000000E4", 
                    "Sequence": 2973898, 
                    "TakerGets": {
                        "currency": "BTC", 
                        "issuer": "rMwjYedjc7qqtKYVLiAccJSmCwih4LnE2q", 
                        "value": "1.91"
                    }, 
                    "TakerPays": {
                        "currency": "BTC", 
                        "issuer": "rvYAfWj5gh67oV6fW32ZzP3Aw4Eubs59B", 
                        "value": "1.91223722474574"
                    }
                }, 
                "fieldsFinal": {}, 
                "fieldsNew": {
                    "Account": "rfCFLzNJYvvnoGHWQYACmJpTgkLUaugLEw", 
                    "BookDirectory": "8C28E5438902173D0254ECA2C504AAF68FBEF982D7C74AFD55038E8F5CFFA198", 
                    "Expiration": 475285029, 
                    "OwnerNode": "00000000000000E4", 
                    "Sequence": 2973898, 
                    "TakerGets": {
                        "currency": "BTC", 
                        "issuer": "rMwjYedjc7qqtKYVLiAccJSmCwih4LnE2q", 
                        "value": "1.91"
                    }, 
                    "TakerPays": {
                        "currency": "BTC", 
                        "issuer": "rvYAfWj5gh67oV6fW32ZzP3Aw4Eubs59B", 
                        "value": "1.91223722474574"
                    }
                }, 
                "fieldsPrev": {}, 
                "ledgerIndex": "DFA624FBB22D533BE27E96C15C135BA961D50F1602B7FF80F3F4EA168BFE457E", 
                "nodeType": "CreatedNode"
            }, 
            {
                "diffType": "ModifiedNode", 
                "entryType": "DirectoryNode", 
                "fields": {
                    "Flags": 0, 
                    "IndexPrevious": "00000000000000E1", 
                    "Owner": "rfCFLzNJYvvnoGHWQYACmJpTgkLUaugLEw", 
                    "RootIndex": "F435FBBEC9654204D7151A01E686BAA8CB325A472D7B61C7916EA58B59355767"
                }, 
                "fieldsFinal": {
                    "Flags": 0, 
                    "IndexPrevious": "00000000000000E1", 
                    "Owner": "rfCFLzNJYvvnoGHWQYACmJpTgkLUaugLEw", 
                    "RootIndex": "F435FBBEC9654204D7151A01E686BAA8CB325A472D7B61C7916EA58B59355767"
                }, 
                "fieldsNew": {}, 
                "fieldsPrev": {}, 
                "ledgerIndex": "FAB65ADCA624173CAFB8CEC77A90885D7F0681AA7BBE1674102138C31E4D114F", 
                "nodeType": "ModifiedNode"
            }
        ]
    }, 
    "status": "closed", 
    "transaction": {
        "Account": "rfCFLzNJYvvnoGHWQYACmJpTgkLUaugLEw", 
        "Expiration": 475285029, 
        "Fee": "11000", 
        "Flags": 2147483648, 
        "LastLedgerSequence": 11276816, 
        "OfferSequence": 2973843, 
        "Sequence": 2973898, 
        "SigningPubKey": "034841BF24BD72C7CC371EBD87CCBF258D8ADB05C18DE207130364A97D8A3EA524", 
        "TakerGets": {
            "currency": "BTC", 
            "issuer": "rMwjYedjc7qqtKYVLiAccJSmCwih4LnE2q", 
            "value": "1.91"
        }, 
        "TakerPays": {
            "currency": "BTC", 
            "issuer": "rvYAfWj5gh67oV6fW32ZzP3Aw4Eubs59B", 
            "value": "1.91223722474574"
        }, 
        "TransactionType": "OfferCreate", 
        "TxnSignature": "304402206065C8C678EE1AA315801EDA9E7E160F5930D016C087251FA2EFA8ACCC383926022040D05914FEEE6E09836E2563A21139CC735423E59133FC890B85B070378BC4EE", 
        "date": 475284430, 
        "hash": "C765097537246A125B09CE5888F28792B7BD1B56D8F6AB7E90B4271C8E7D96DE", 
        "owner_funds": "11.48342340037239"
    }, 
    "type": "transaction", 
    "validated": true
}

TransactionType の部分を見るとこのトランザクションは OfferCreate だとわかります。TransactionType によってどんなデータが含まれるかは異なります。