Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

finalbalances-test.js 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453
  1. 'use strict'
  2. var assert = require('assert-diff')
  3. var fs = require('fs')
  4. var parseFinalBalances = require('../src/index').parseFinalBalances
  5. // Pay 100 XRP from rKmB to rLDY to create rLDY account
  6. var createAccountBalanceChanges = {
  7. rLDYrujdKUfVx28T9vRDAbyJ7G2WVXKo4K: [
  8. {
  9. value: '100',
  10. currency: 'XRP',
  11. counterparty: ''
  12. }
  13. ],
  14. rKmBGxocj9Abgy25J51Mk1iqFzW9aVF9Tc: [
  15. {
  16. value: '339.903994',
  17. currency: 'XRP',
  18. counterparty: ''
  19. }
  20. ]
  21. }
  22. // Pay 0.01 USD from rKmB to rLDY where rLDY starts with no USD
  23. var usdFirstPaymentBalanceChanges = {
  24. rKmBGxocj9Abgy25J51Mk1iqFzW9aVF9Tc: [
  25. {
  26. value: '1.535330905250352',
  27. currency: 'USD',
  28. counterparty: 'rMwjYedjc7qqtKYVLiAccJSmCwih4LnE2q'
  29. },
  30. {
  31. value: '239.807992',
  32. currency: 'XRP',
  33. counterparty: ''
  34. }
  35. ],
  36. rMwjYedjc7qqtKYVLiAccJSmCwih4LnE2q: [
  37. {
  38. counterparty: 'rKmBGxocj9Abgy25J51Mk1iqFzW9aVF9Tc',
  39. currency: 'USD',
  40. value: '-1.535330905250352'
  41. },
  42. {
  43. counterparty: 'rLDYrujdKUfVx28T9vRDAbyJ7G2WVXKo4K',
  44. currency: 'USD',
  45. value: '-0.01'
  46. }
  47. ],
  48. rLDYrujdKUfVx28T9vRDAbyJ7G2WVXKo4K: [
  49. {
  50. counterparty: 'rMwjYedjc7qqtKYVLiAccJSmCwih4LnE2q',
  51. currency: 'USD',
  52. value: '0.01'
  53. }
  54. ]
  55. }
  56. // Pay 0.2 USD from rLDY to rKmB where rLDY starts with 0.2 USD
  57. var usdFullPaymentBalanceChanges = {
  58. rKmBGxocj9Abgy25J51Mk1iqFzW9aVF9Tc: [
  59. {
  60. value: '1.545330905250352',
  61. currency: 'USD',
  62. counterparty: 'rMwjYedjc7qqtKYVLiAccJSmCwih4LnE2q'
  63. }
  64. ],
  65. rMwjYedjc7qqtKYVLiAccJSmCwih4LnE2q: [
  66. {
  67. counterparty: 'rKmBGxocj9Abgy25J51Mk1iqFzW9aVF9Tc',
  68. currency: 'USD',
  69. value: '-1.545330905250352'
  70. },
  71. {
  72. counterparty: 'rLDYrujdKUfVx28T9vRDAbyJ7G2WVXKo4K',
  73. currency: 'USD',
  74. value: '0'
  75. }
  76. ],
  77. rLDYrujdKUfVx28T9vRDAbyJ7G2WVXKo4K: [
  78. {
  79. value: '0',
  80. currency: 'USD',
  81. counterparty: 'rMwjYedjc7qqtKYVLiAccJSmCwih4LnE2q'
  82. },
  83. {
  84. value: '99.976002',
  85. currency: 'XRP',
  86. counterparty: ''
  87. }
  88. ]
  89. }
  90. // Pay 0.01 USD from rKmB to rLDY where rLDY starts with 0.01 USD
  91. var usdPaymentBalanceChanges = {
  92. rKmBGxocj9Abgy25J51Mk1iqFzW9aVF9Tc: [
  93. {
  94. value: '1.525330905250352',
  95. currency: 'USD',
  96. counterparty: 'rMwjYedjc7qqtKYVLiAccJSmCwih4LnE2q'
  97. },
  98. {
  99. value: '239.555992',
  100. currency: 'XRP',
  101. counterparty: ''
  102. }
  103. ],
  104. rMwjYedjc7qqtKYVLiAccJSmCwih4LnE2q: [
  105. {
  106. counterparty: 'rKmBGxocj9Abgy25J51Mk1iqFzW9aVF9Tc',
  107. currency: 'USD',
  108. value: '-1.525330905250352'
  109. },
  110. {
  111. counterparty: 'rLDYrujdKUfVx28T9vRDAbyJ7G2WVXKo4K',
  112. currency: 'USD',
  113. value: '-0.02'
  114. }
  115. ],
  116. rLDYrujdKUfVx28T9vRDAbyJ7G2WVXKo4K: [
  117. {
  118. counterparty: 'rMwjYedjc7qqtKYVLiAccJSmCwih4LnE2q',
  119. currency: 'USD',
  120. value: '0.02'
  121. }
  122. ]
  123. }
  124. // Set trust limit to 200 USD on rLDY when it has a trust limit of 100 USD
  125. // and has a balance of 0.02 USD
  126. var setTrustlineBalanceChanges = {
  127. rLDYrujdKUfVx28T9vRDAbyJ7G2WVXKo4K: [
  128. {
  129. counterparty: 'rMwjYedjc7qqtKYVLiAccJSmCwih4LnE2q',
  130. currency: 'USD',
  131. value: '0.02'
  132. },
  133. {
  134. value: '99.940002',
  135. currency: 'XRP',
  136. counterparty: ''
  137. }
  138. ],
  139. rMwjYedjc7qqtKYVLiAccJSmCwih4LnE2q: [
  140. {
  141. counterparty: 'rLDYrujdKUfVx28T9vRDAbyJ7G2WVXKo4K',
  142. currency: 'USD',
  143. value: '-0.02'
  144. }
  145. ]
  146. }
  147. var setTrustlineBalanceChanges3 = {
  148. rLDYrujdKUfVx28T9vRDAbyJ7G2WVXKo4K: [
  149. {
  150. counterparty: 'rMwjYedjc7qqtKYVLiAccJSmCwih4LnE2q',
  151. currency: 'USD',
  152. value: '0.02'
  153. },
  154. {
  155. counterparty: '',
  156. currency: 'XRP',
  157. value: '99.884302'
  158. }
  159. ],
  160. rMwjYedjc7qqtKYVLiAccJSmCwih4LnE2q: [
  161. {
  162. counterparty: 'rLDYrujdKUfVx28T9vRDAbyJ7G2WVXKo4K',
  163. currency: 'USD',
  164. value: '-0.02'
  165. }
  166. ]
  167. }
  168. var setTrustlineBalanceChanges2 = {
  169. rsApBGKJmMfExxZBrGnzxEXyq7TMhMRg4e: [
  170. {
  171. counterparty: 'rMwjYedjc7qqtKYVLiAccJSmCwih4LnE2q',
  172. currency: 'USD',
  173. value: '0'
  174. },
  175. {
  176. counterparty: '',
  177. currency: 'XRP',
  178. value: '9248.902096'
  179. }
  180. ],
  181. rMwjYedjc7qqtKYVLiAccJSmCwih4LnE2q: [
  182. {
  183. counterparty: 'rsApBGKJmMfExxZBrGnzxEXyq7TMhMRg4e',
  184. currency: 'USD',
  185. value: '0'
  186. },
  187. {
  188. counterparty: '',
  189. currency: 'XRP',
  190. value: '149.99998'
  191. }
  192. ]
  193. }
  194. // Set trust limit to 100 USD with balance of 10 USD on rLDY
  195. // when it has no trustline
  196. var createTrustlineBalanceChanges = {
  197. rLDYrujdKUfVx28T9vRDAbyJ7G2WVXKo4K: [
  198. {
  199. counterparty: 'rMwjYedjc7qqtKYVLiAccJSmCwih4LnE2q',
  200. currency: 'USD',
  201. value: '10'
  202. },
  203. {
  204. counterparty: '',
  205. currency: 'XRP',
  206. value: '99.740302'
  207. }
  208. ],
  209. rMwjYedjc7qqtKYVLiAccJSmCwih4LnE2q: [
  210. {
  211. counterparty: 'rLDYrujdKUfVx28T9vRDAbyJ7G2WVXKo4K',
  212. currency: 'USD',
  213. value: '-10'
  214. }
  215. ]
  216. }
  217. // Pay 0.02 USD from rLDY to rKmB when rLDY has a trust limit of 0
  218. // for USD, but still has a balance of 0.02 USD; which closes the trustline
  219. var deleteTrustlineBalanceChanges = {
  220. rKmBGxocj9Abgy25J51Mk1iqFzW9aVF9Tc: [
  221. {
  222. counterparty: 'rMwjYedjc7qqtKYVLiAccJSmCwih4LnE2q',
  223. currency: 'USD',
  224. value: '1.545330905250352'
  225. }
  226. ],
  227. rMwjYedjc7qqtKYVLiAccJSmCwih4LnE2q: [
  228. {
  229. counterparty: 'rKmBGxocj9Abgy25J51Mk1iqFzW9aVF9Tc',
  230. currency: 'USD',
  231. value: '-1.545330905250352'
  232. },
  233. {
  234. counterparty: 'rLDYrujdKUfVx28T9vRDAbyJ7G2WVXKo4K',
  235. currency: 'USD',
  236. value: '0'
  237. }
  238. ],
  239. rLDYrujdKUfVx28T9vRDAbyJ7G2WVXKo4K: [
  240. {
  241. counterparty: 'rMwjYedjc7qqtKYVLiAccJSmCwih4LnE2q',
  242. currency: 'USD',
  243. value: '0'
  244. },
  245. {
  246. counterparty: '',
  247. currency: 'XRP',
  248. value: '99.752302'
  249. }
  250. ]
  251. }
  252. var redeemBalanceChanges = {
  253. rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh: [
  254. {
  255. counterparty: 'rPMh7Pi9ct699iZUTWaytJUoHcJ7cgyziK',
  256. currency: 'USD',
  257. value: '-100'
  258. }
  259. ],
  260. rPMh7Pi9ct699iZUTWaytJUoHcJ7cgyziK: [
  261. {
  262. counterparty: 'rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh',
  263. currency: 'USD',
  264. value: '100'
  265. },
  266. {
  267. counterparty: '',
  268. currency: 'XRP',
  269. value: '999.99998'
  270. }
  271. ]
  272. }
  273. var redeemThenIssueBalanceChanges = {
  274. rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh: [
  275. {
  276. counterparty: 'rPMh7Pi9ct699iZUTWaytJUoHcJ7cgyziK',
  277. currency: 'USD',
  278. value: '100'
  279. }
  280. ],
  281. rPMh7Pi9ct699iZUTWaytJUoHcJ7cgyziK: [
  282. {
  283. counterparty: 'rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh',
  284. currency: 'USD',
  285. value: '-100'
  286. },
  287. {
  288. counterparty: '',
  289. currency: 'XRP',
  290. value: '999.99997'
  291. }
  292. ]
  293. }
  294. var multipathBalanceChanges = {
  295. rrnsYgWn13Z28GtRgznrSUsLfMkvsXCZSu: [
  296. {
  297. counterparty: 'r4nmQNH4Fhjfh6cHDbvVSsBv7KySbj4cBf',
  298. currency: 'USD',
  299. value: '0'
  300. },
  301. {
  302. counterparty: 'rnYDWQaRdMb5neCGgvFfhw3MBoxmv5LtfH',
  303. currency: 'USD',
  304. value: '-100'
  305. }
  306. ],
  307. r4nmQNH4Fhjfh6cHDbvVSsBv7KySbj4cBf: [
  308. {
  309. counterparty: 'rrnsYgWn13Z28GtRgznrSUsLfMkvsXCZSu',
  310. currency: 'USD',
  311. value: '0'
  312. },
  313. {
  314. counterparty: '',
  315. currency: 'XRP',
  316. value: '999.99999'
  317. },
  318. {
  319. counterparty: 'rJsaPnGdeo7BhMnHjuc3n44Mf7Ra1qkSVJ',
  320. currency: 'USD',
  321. value: '0'
  322. },
  323. {
  324. counterparty: 'rGpeQzUWFu4fMhJHZ1Via5aqFC3A5twZUD',
  325. currency: 'USD',
  326. value: '0'
  327. }
  328. ],
  329. rJsaPnGdeo7BhMnHjuc3n44Mf7Ra1qkSVJ: [
  330. {
  331. counterparty: 'r4nmQNH4Fhjfh6cHDbvVSsBv7KySbj4cBf',
  332. currency: 'USD',
  333. value: '0'
  334. },
  335. {
  336. counterparty: 'rnYDWQaRdMb5neCGgvFfhw3MBoxmv5LtfH',
  337. currency: 'USD',
  338. value: '-100'
  339. }
  340. ],
  341. rGpeQzUWFu4fMhJHZ1Via5aqFC3A5twZUD: [
  342. {
  343. counterparty: 'r4nmQNH4Fhjfh6cHDbvVSsBv7KySbj4cBf',
  344. currency: 'USD',
  345. value: '0'
  346. },
  347. {
  348. counterparty: 'rnYDWQaRdMb5neCGgvFfhw3MBoxmv5LtfH',
  349. currency: 'USD',
  350. value: '-100'
  351. }
  352. ],
  353. rnYDWQaRdMb5neCGgvFfhw3MBoxmv5LtfH: [
  354. {
  355. counterparty: 'rJsaPnGdeo7BhMnHjuc3n44Mf7Ra1qkSVJ',
  356. currency: 'USD',
  357. value: '100'
  358. },
  359. {
  360. counterparty: 'rrnsYgWn13Z28GtRgznrSUsLfMkvsXCZSu',
  361. currency: 'USD',
  362. value: '100'
  363. },
  364. {
  365. counterparty: 'rGpeQzUWFu4fMhJHZ1Via5aqFC3A5twZUD',
  366. currency: 'USD',
  367. value: '100'
  368. }
  369. ]
  370. }
  371. function loadFixture(filename) {
  372. var path = __dirname + '/fixtures/' + filename
  373. return JSON.parse(fs.readFileSync(path))
  374. }
  375. describe('parseFinalBalances', function() {
  376. it('XRP create account', function() {
  377. var paymentResponse = loadFixture('payment-xrp-create-account.json')
  378. var result = parseFinalBalances(paymentResponse.metadata)
  379. assert.deepEqual(result, createAccountBalanceChanges)
  380. })
  381. it('USD payment to account with no USD', function() {
  382. var filename = 'payment-iou-destination-no-balance.json'
  383. var paymentResponse = loadFixture(filename)
  384. var result = parseFinalBalances(paymentResponse.metadata)
  385. assert.deepEqual(result, usdFirstPaymentBalanceChanges)
  386. })
  387. it('USD payment of all USD in source account', function() {
  388. var paymentResponse = loadFixture('payment-iou-spend-full-balance.json')
  389. var result = parseFinalBalances(paymentResponse.metadata)
  390. assert.deepEqual(result, usdFullPaymentBalanceChanges)
  391. })
  392. it('USD payment to account with USD', function() {
  393. var paymentResponse = loadFixture('payment-iou.json')
  394. var result = parseFinalBalances(paymentResponse.metadata)
  395. assert.deepEqual(result, usdPaymentBalanceChanges)
  396. })
  397. it('Set trust limit to 0 with balance remaining', function() {
  398. var paymentResponse = loadFixture('trustline-set-limit-to-zero.json')
  399. var result = parseFinalBalances(paymentResponse.metadata)
  400. assert.deepEqual(result, setTrustlineBalanceChanges)
  401. })
  402. it('Create trustline', function() {
  403. var paymentResponse = loadFixture('trustline-create.json')
  404. var result = parseFinalBalances(paymentResponse.metadata)
  405. assert.deepEqual(result, createTrustlineBalanceChanges)
  406. })
  407. it('Set trustline', function() {
  408. var paymentResponse = loadFixture('trustline-set-limit.json')
  409. var result = parseFinalBalances(paymentResponse.metadata)
  410. assert.deepEqual(result, setTrustlineBalanceChanges3)
  411. })
  412. it('Set trustline 2', function() {
  413. var paymentResponse = loadFixture('trustline-set-limit-2.json')
  414. var result = parseFinalBalances(paymentResponse.metadata)
  415. assert.deepEqual(result, setTrustlineBalanceChanges2)
  416. })
  417. it('Delete trustline', function() {
  418. var paymentResponse = loadFixture('trustline-delete.json')
  419. var result = parseFinalBalances(paymentResponse.metadata)
  420. assert.deepEqual(result, deleteTrustlineBalanceChanges)
  421. })
  422. it('Redeem USD', function() {
  423. var paymentResponse = loadFixture('payment-iou-redeem.json')
  424. var result = parseFinalBalances(paymentResponse.result.meta)
  425. assert.deepEqual(result, redeemBalanceChanges)
  426. })
  427. it('Redeem then issue USD', function() {
  428. var paymentResponse = loadFixture('payment-iou-redeem-then-issue.json')
  429. var result = parseFinalBalances(paymentResponse.result.meta)
  430. assert.deepEqual(result, redeemThenIssueBalanceChanges)
  431. })
  432. it('Multipath USD payment', function() {
  433. var paymentResponse = loadFixture('payment-iou-multipath.json')
  434. var result = parseFinalBalances(paymentResponse.result.meta)
  435. assert.deepEqual(result, multipathBalanceChanges)
  436. })
  437. })