playground

The world is a playground.

Ripple (XRP) の残高を取得 (JavaScript)

  • Ripple (XRP) の残高を取得する方法のメモです。
  • Ripple 公式のライブラリを使用します。
  • Ripple 公式ライブラリ ripple-lib: https://github.com/ripple/ripple-lib

ripple-lib のインストール

  • 公式では、yarn を推奨しているけど、以下は npm でインストールする例
$ npm install ripple-lib

コード例

  • getAccountInfo の返り値のオブジェクトが持っている xrpBalance を読みます。
const RippleAPI = require('ripple-lib').RippleAPI;

const api = new RippleAPI({
  server: 'wss://s1.ripple.com' // Public rippled server hosted by Ripple, Inc.
});

api.connect().then(() => {
    getXrpBalance('r39wDgtwjJkF5NAnXQhy3Z68dDboGebNJv');
}).then(() => {
  return api.disconnect();
}).catch(console.error);

function getXrpBalance(address) {
    api.getAccountInfo(address).then(info => {
        console.log(info.xrpBalance);
    });
};