123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453 |
- 'use strict'
-
- var assert = require('assert-diff')
- var fs = require('fs')
- var parseFinalBalances = require('../src/index').parseFinalBalances
-
- // Pay 100 XRP from rKmB to rLDY to create rLDY account
- var createAccountBalanceChanges = {
- rLDYrujdKUfVx28T9vRDAbyJ7G2WVXKo4K: [
- {
- value: '100',
- currency: 'XRP',
- counterparty: ''
- }
- ],
- rKmBGxocj9Abgy25J51Mk1iqFzW9aVF9Tc: [
- {
- value: '339.903994',
- currency: 'XRP',
- counterparty: ''
- }
- ]
- }
-
- // Pay 0.01 USD from rKmB to rLDY where rLDY starts with no USD
- var usdFirstPaymentBalanceChanges = {
- rKmBGxocj9Abgy25J51Mk1iqFzW9aVF9Tc: [
- {
- value: '1.535330905250352',
- currency: 'USD',
- counterparty: 'rMwjYedjc7qqtKYVLiAccJSmCwih4LnE2q'
- },
- {
- value: '239.807992',
- currency: 'XRP',
- counterparty: ''
- }
- ],
- rMwjYedjc7qqtKYVLiAccJSmCwih4LnE2q: [
- {
- counterparty: 'rKmBGxocj9Abgy25J51Mk1iqFzW9aVF9Tc',
- currency: 'USD',
- value: '-1.535330905250352'
- },
- {
- counterparty: 'rLDYrujdKUfVx28T9vRDAbyJ7G2WVXKo4K',
- currency: 'USD',
- value: '-0.01'
- }
- ],
- rLDYrujdKUfVx28T9vRDAbyJ7G2WVXKo4K: [
- {
- counterparty: 'rMwjYedjc7qqtKYVLiAccJSmCwih4LnE2q',
- currency: 'USD',
- value: '0.01'
- }
- ]
- }
-
- // Pay 0.2 USD from rLDY to rKmB where rLDY starts with 0.2 USD
- var usdFullPaymentBalanceChanges = {
- rKmBGxocj9Abgy25J51Mk1iqFzW9aVF9Tc: [
- {
- value: '1.545330905250352',
- currency: 'USD',
- counterparty: 'rMwjYedjc7qqtKYVLiAccJSmCwih4LnE2q'
- }
- ],
- rMwjYedjc7qqtKYVLiAccJSmCwih4LnE2q: [
- {
- counterparty: 'rKmBGxocj9Abgy25J51Mk1iqFzW9aVF9Tc',
- currency: 'USD',
- value: '-1.545330905250352'
- },
- {
- counterparty: 'rLDYrujdKUfVx28T9vRDAbyJ7G2WVXKo4K',
- currency: 'USD',
- value: '0'
- }
- ],
- rLDYrujdKUfVx28T9vRDAbyJ7G2WVXKo4K: [
- {
- value: '0',
- currency: 'USD',
- counterparty: 'rMwjYedjc7qqtKYVLiAccJSmCwih4LnE2q'
- },
- {
- value: '99.976002',
- currency: 'XRP',
- counterparty: ''
- }
- ]
- }
-
- // Pay 0.01 USD from rKmB to rLDY where rLDY starts with 0.01 USD
- var usdPaymentBalanceChanges = {
- rKmBGxocj9Abgy25J51Mk1iqFzW9aVF9Tc: [
- {
- value: '1.525330905250352',
- currency: 'USD',
- counterparty: 'rMwjYedjc7qqtKYVLiAccJSmCwih4LnE2q'
- },
- {
- value: '239.555992',
- currency: 'XRP',
- counterparty: ''
- }
- ],
- rMwjYedjc7qqtKYVLiAccJSmCwih4LnE2q: [
- {
- counterparty: 'rKmBGxocj9Abgy25J51Mk1iqFzW9aVF9Tc',
- currency: 'USD',
- value: '-1.525330905250352'
- },
- {
- counterparty: 'rLDYrujdKUfVx28T9vRDAbyJ7G2WVXKo4K',
- currency: 'USD',
- value: '-0.02'
- }
- ],
- rLDYrujdKUfVx28T9vRDAbyJ7G2WVXKo4K: [
- {
- counterparty: 'rMwjYedjc7qqtKYVLiAccJSmCwih4LnE2q',
- currency: 'USD',
- value: '0.02'
- }
- ]
- }
-
- // Set trust limit to 200 USD on rLDY when it has a trust limit of 100 USD
- // and has a balance of 0.02 USD
- var setTrustlineBalanceChanges = {
- rLDYrujdKUfVx28T9vRDAbyJ7G2WVXKo4K: [
- {
- counterparty: 'rMwjYedjc7qqtKYVLiAccJSmCwih4LnE2q',
- currency: 'USD',
- value: '0.02'
- },
- {
- value: '99.940002',
- currency: 'XRP',
- counterparty: ''
- }
- ],
- rMwjYedjc7qqtKYVLiAccJSmCwih4LnE2q: [
- {
- counterparty: 'rLDYrujdKUfVx28T9vRDAbyJ7G2WVXKo4K',
- currency: 'USD',
- value: '-0.02'
- }
- ]
- }
-
- var setTrustlineBalanceChanges3 = {
- rLDYrujdKUfVx28T9vRDAbyJ7G2WVXKo4K: [
- {
- counterparty: 'rMwjYedjc7qqtKYVLiAccJSmCwih4LnE2q',
- currency: 'USD',
- value: '0.02'
- },
- {
- counterparty: '',
- currency: 'XRP',
- value: '99.884302'
- }
- ],
- rMwjYedjc7qqtKYVLiAccJSmCwih4LnE2q: [
- {
- counterparty: 'rLDYrujdKUfVx28T9vRDAbyJ7G2WVXKo4K',
- currency: 'USD',
- value: '-0.02'
- }
- ]
- }
-
- var setTrustlineBalanceChanges2 = {
- rsApBGKJmMfExxZBrGnzxEXyq7TMhMRg4e: [
- {
- counterparty: 'rMwjYedjc7qqtKYVLiAccJSmCwih4LnE2q',
- currency: 'USD',
- value: '0'
- },
- {
- counterparty: '',
- currency: 'XRP',
- value: '9248.902096'
- }
- ],
- rMwjYedjc7qqtKYVLiAccJSmCwih4LnE2q: [
- {
- counterparty: 'rsApBGKJmMfExxZBrGnzxEXyq7TMhMRg4e',
- currency: 'USD',
- value: '0'
- },
- {
- counterparty: '',
- currency: 'XRP',
- value: '149.99998'
- }
- ]
- }
-
- // Set trust limit to 100 USD with balance of 10 USD on rLDY
- // when it has no trustline
- var createTrustlineBalanceChanges = {
- rLDYrujdKUfVx28T9vRDAbyJ7G2WVXKo4K: [
- {
- counterparty: 'rMwjYedjc7qqtKYVLiAccJSmCwih4LnE2q',
- currency: 'USD',
- value: '10'
- },
- {
- counterparty: '',
- currency: 'XRP',
- value: '99.740302'
- }
- ],
- rMwjYedjc7qqtKYVLiAccJSmCwih4LnE2q: [
- {
- counterparty: 'rLDYrujdKUfVx28T9vRDAbyJ7G2WVXKo4K',
- currency: 'USD',
- value: '-10'
- }
- ]
- }
-
- // Pay 0.02 USD from rLDY to rKmB when rLDY has a trust limit of 0
- // for USD, but still has a balance of 0.02 USD; which closes the trustline
- var deleteTrustlineBalanceChanges = {
- rKmBGxocj9Abgy25J51Mk1iqFzW9aVF9Tc: [
- {
- counterparty: 'rMwjYedjc7qqtKYVLiAccJSmCwih4LnE2q',
- currency: 'USD',
- value: '1.545330905250352'
- }
- ],
- rMwjYedjc7qqtKYVLiAccJSmCwih4LnE2q: [
- {
- counterparty: 'rKmBGxocj9Abgy25J51Mk1iqFzW9aVF9Tc',
- currency: 'USD',
- value: '-1.545330905250352'
- },
- {
- counterparty: 'rLDYrujdKUfVx28T9vRDAbyJ7G2WVXKo4K',
- currency: 'USD',
- value: '0'
- }
- ],
- rLDYrujdKUfVx28T9vRDAbyJ7G2WVXKo4K: [
- {
- counterparty: 'rMwjYedjc7qqtKYVLiAccJSmCwih4LnE2q',
- currency: 'USD',
- value: '0'
- },
- {
- counterparty: '',
- currency: 'XRP',
- value: '99.752302'
- }
- ]
- }
-
- var redeemBalanceChanges = {
- rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh: [
- {
- counterparty: 'rPMh7Pi9ct699iZUTWaytJUoHcJ7cgyziK',
- currency: 'USD',
- value: '-100'
- }
- ],
- rPMh7Pi9ct699iZUTWaytJUoHcJ7cgyziK: [
- {
- counterparty: 'rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh',
- currency: 'USD',
- value: '100'
- },
- {
- counterparty: '',
- currency: 'XRP',
- value: '999.99998'
- }
- ]
- }
-
- var redeemThenIssueBalanceChanges = {
- rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh: [
- {
- counterparty: 'rPMh7Pi9ct699iZUTWaytJUoHcJ7cgyziK',
- currency: 'USD',
- value: '100'
- }
- ],
- rPMh7Pi9ct699iZUTWaytJUoHcJ7cgyziK: [
- {
- counterparty: 'rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh',
- currency: 'USD',
- value: '-100'
- },
- {
- counterparty: '',
- currency: 'XRP',
- value: '999.99997'
- }
- ]
- }
-
- var multipathBalanceChanges = {
- rrnsYgWn13Z28GtRgznrSUsLfMkvsXCZSu: [
- {
- counterparty: 'r4nmQNH4Fhjfh6cHDbvVSsBv7KySbj4cBf',
- currency: 'USD',
- value: '0'
- },
- {
- counterparty: 'rnYDWQaRdMb5neCGgvFfhw3MBoxmv5LtfH',
- currency: 'USD',
- value: '-100'
- }
- ],
- r4nmQNH4Fhjfh6cHDbvVSsBv7KySbj4cBf: [
- {
- counterparty: 'rrnsYgWn13Z28GtRgznrSUsLfMkvsXCZSu',
- currency: 'USD',
- value: '0'
- },
- {
- counterparty: '',
- currency: 'XRP',
- value: '999.99999'
- },
- {
- counterparty: 'rJsaPnGdeo7BhMnHjuc3n44Mf7Ra1qkSVJ',
- currency: 'USD',
- value: '0'
- },
- {
- counterparty: 'rGpeQzUWFu4fMhJHZ1Via5aqFC3A5twZUD',
- currency: 'USD',
- value: '0'
- }
- ],
- rJsaPnGdeo7BhMnHjuc3n44Mf7Ra1qkSVJ: [
- {
- counterparty: 'r4nmQNH4Fhjfh6cHDbvVSsBv7KySbj4cBf',
- currency: 'USD',
- value: '0'
- },
- {
- counterparty: 'rnYDWQaRdMb5neCGgvFfhw3MBoxmv5LtfH',
- currency: 'USD',
- value: '-100'
- }
- ],
- rGpeQzUWFu4fMhJHZ1Via5aqFC3A5twZUD: [
- {
- counterparty: 'r4nmQNH4Fhjfh6cHDbvVSsBv7KySbj4cBf',
- currency: 'USD',
- value: '0'
- },
- {
- counterparty: 'rnYDWQaRdMb5neCGgvFfhw3MBoxmv5LtfH',
- currency: 'USD',
- value: '-100'
- }
- ],
- rnYDWQaRdMb5neCGgvFfhw3MBoxmv5LtfH: [
- {
- counterparty: 'rJsaPnGdeo7BhMnHjuc3n44Mf7Ra1qkSVJ',
- currency: 'USD',
- value: '100'
- },
- {
- counterparty: 'rrnsYgWn13Z28GtRgznrSUsLfMkvsXCZSu',
- currency: 'USD',
- value: '100'
- },
- {
- counterparty: 'rGpeQzUWFu4fMhJHZ1Via5aqFC3A5twZUD',
- currency: 'USD',
- value: '100'
- }
- ]
- }
-
-
- function loadFixture(filename) {
- var path = __dirname + '/fixtures/' + filename
- return JSON.parse(fs.readFileSync(path))
- }
-
- describe('parseFinalBalances', function() {
- it('XRP create account', function() {
- var paymentResponse = loadFixture('payment-xrp-create-account.json')
- var result = parseFinalBalances(paymentResponse.metadata)
- assert.deepEqual(result, createAccountBalanceChanges)
- })
- it('USD payment to account with no USD', function() {
- var filename = 'payment-iou-destination-no-balance.json'
- var paymentResponse = loadFixture(filename)
- var result = parseFinalBalances(paymentResponse.metadata)
- assert.deepEqual(result, usdFirstPaymentBalanceChanges)
- })
- it('USD payment of all USD in source account', function() {
- var paymentResponse = loadFixture('payment-iou-spend-full-balance.json')
- var result = parseFinalBalances(paymentResponse.metadata)
- assert.deepEqual(result, usdFullPaymentBalanceChanges)
- })
- it('USD payment to account with USD', function() {
- var paymentResponse = loadFixture('payment-iou.json')
- var result = parseFinalBalances(paymentResponse.metadata)
- assert.deepEqual(result, usdPaymentBalanceChanges)
- })
- it('Set trust limit to 0 with balance remaining', function() {
- var paymentResponse = loadFixture('trustline-set-limit-to-zero.json')
- var result = parseFinalBalances(paymentResponse.metadata)
- assert.deepEqual(result, setTrustlineBalanceChanges)
- })
- it('Create trustline', function() {
- var paymentResponse = loadFixture('trustline-create.json')
- var result = parseFinalBalances(paymentResponse.metadata)
- assert.deepEqual(result, createTrustlineBalanceChanges)
- })
- it('Set trustline', function() {
- var paymentResponse = loadFixture('trustline-set-limit.json')
- var result = parseFinalBalances(paymentResponse.metadata)
- assert.deepEqual(result, setTrustlineBalanceChanges3)
- })
- it('Set trustline 2', function() {
- var paymentResponse = loadFixture('trustline-set-limit-2.json')
- var result = parseFinalBalances(paymentResponse.metadata)
- assert.deepEqual(result, setTrustlineBalanceChanges2)
- })
- it('Delete trustline', function() {
- var paymentResponse = loadFixture('trustline-delete.json')
- var result = parseFinalBalances(paymentResponse.metadata)
- assert.deepEqual(result, deleteTrustlineBalanceChanges)
- })
- it('Redeem USD', function() {
- var paymentResponse = loadFixture('payment-iou-redeem.json')
- var result = parseFinalBalances(paymentResponse.result.meta)
- assert.deepEqual(result, redeemBalanceChanges)
- })
- it('Redeem then issue USD', function() {
- var paymentResponse = loadFixture('payment-iou-redeem-then-issue.json')
- var result = parseFinalBalances(paymentResponse.result.meta)
- assert.deepEqual(result, redeemThenIssueBalanceChanges)
- })
- it('Multipath USD payment', function() {
- var paymentResponse = loadFixture('payment-iou-multipath.json')
- var result = parseFinalBalances(paymentResponse.result.meta)
- assert.deepEqual(result, multipathBalanceChanges)
- })
- })
|