Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

balancechanges-test.js 9.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  1. 'use strict'
  2. var assert = require('assert-diff')
  3. var fs = require('fs')
  4. var parseBalanceChanges = require('../src/index').parseBalanceChanges
  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: '-100.012',
  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: '-0.01',
  27. currency: 'USD',
  28. counterparty: 'rMwjYedjc7qqtKYVLiAccJSmCwih4LnE2q'
  29. },
  30. {
  31. value: '-0.012',
  32. currency: 'XRP',
  33. counterparty: ''
  34. }
  35. ],
  36. rMwjYedjc7qqtKYVLiAccJSmCwih4LnE2q: [
  37. {
  38. counterparty: 'rKmBGxocj9Abgy25J51Mk1iqFzW9aVF9Tc',
  39. currency: 'USD',
  40. value: '0.01'
  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: '0.2',
  61. currency: 'USD',
  62. counterparty: 'rMwjYedjc7qqtKYVLiAccJSmCwih4LnE2q'
  63. }
  64. ],
  65. rMwjYedjc7qqtKYVLiAccJSmCwih4LnE2q: [
  66. {
  67. value: '-0.2',
  68. currency: 'USD',
  69. counterparty: 'rKmBGxocj9Abgy25J51Mk1iqFzW9aVF9Tc'
  70. },
  71. {
  72. value: '0.2',
  73. currency: 'USD',
  74. counterparty: 'rLDYrujdKUfVx28T9vRDAbyJ7G2WVXKo4K'
  75. }
  76. ],
  77. rLDYrujdKUfVx28T9vRDAbyJ7G2WVXKo4K: [
  78. {
  79. value: '-0.2',
  80. currency: 'USD',
  81. counterparty: 'rMwjYedjc7qqtKYVLiAccJSmCwih4LnE2q'
  82. },
  83. {
  84. value: '-0.012',
  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 = usdFirstPaymentBalanceChanges
  92. // Set trust limit to 200 USD on rLDY when it has a trust limit of 100 USD
  93. // and has a balance of 0.02 USD
  94. var setTrustlineBalanceChanges = {
  95. rLDYrujdKUfVx28T9vRDAbyJ7G2WVXKo4K: [
  96. {
  97. value: '-0.012',
  98. currency: 'XRP',
  99. counterparty: ''
  100. }
  101. ]
  102. }
  103. var setTrustlineBalanceChanges2 = {
  104. rsApBGKJmMfExxZBrGnzxEXyq7TMhMRg4e: [
  105. {
  106. counterparty: '',
  107. currency: 'XRP',
  108. value: '-0.00001'
  109. }
  110. ]
  111. }
  112. // Set trust limit to 100 USD with balance of 10 USD
  113. // on rLDY when it has no trustline
  114. var createTrustlineBalanceChanges = {
  115. rLDYrujdKUfVx28T9vRDAbyJ7G2WVXKo4K: [
  116. {
  117. counterparty: 'rMwjYedjc7qqtKYVLiAccJSmCwih4LnE2q',
  118. currency: 'USD',
  119. value: '10'
  120. },
  121. {
  122. counterparty: '',
  123. currency: 'XRP',
  124. value: '-0.012'
  125. }
  126. ],
  127. rMwjYedjc7qqtKYVLiAccJSmCwih4LnE2q: [
  128. {
  129. counterparty: 'rLDYrujdKUfVx28T9vRDAbyJ7G2WVXKo4K',
  130. currency: 'USD',
  131. value: '-10'
  132. }
  133. ]
  134. }
  135. // Pay 0.02 USD from rLDY to rKmB when rLDY has a trust limit of 0
  136. // for USD, but still has a balance of 0.02 USD; which closes the trustline
  137. var deleteTrustlineBalanceChanges = {
  138. rKmBGxocj9Abgy25J51Mk1iqFzW9aVF9Tc: [
  139. {
  140. value: '0.02',
  141. currency: 'USD',
  142. counterparty: 'rMwjYedjc7qqtKYVLiAccJSmCwih4LnE2q'
  143. }
  144. ],
  145. rMwjYedjc7qqtKYVLiAccJSmCwih4LnE2q: [
  146. {
  147. value: '-0.02',
  148. currency: 'USD',
  149. counterparty: 'rKmBGxocj9Abgy25J51Mk1iqFzW9aVF9Tc'
  150. },
  151. {
  152. value: '0.02',
  153. currency: 'USD',
  154. counterparty: 'rLDYrujdKUfVx28T9vRDAbyJ7G2WVXKo4K'
  155. }
  156. ],
  157. rLDYrujdKUfVx28T9vRDAbyJ7G2WVXKo4K: [
  158. {
  159. value: '-0.02',
  160. currency: 'USD',
  161. counterparty: 'rMwjYedjc7qqtKYVLiAccJSmCwih4LnE2q'
  162. },
  163. {
  164. value: '-0.012',
  165. currency: 'XRP',
  166. counterparty: ''
  167. }
  168. ]
  169. }
  170. var redeemBalanceChanges = {
  171. rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh: [
  172. {
  173. currency: 'USD',
  174. counterparty: 'rPMh7Pi9ct699iZUTWaytJUoHcJ7cgyziK',
  175. value: '100'
  176. }
  177. ],
  178. rPMh7Pi9ct699iZUTWaytJUoHcJ7cgyziK: [
  179. {
  180. currency: 'USD',
  181. counterparty: 'rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh',
  182. value: '-100'
  183. },
  184. {
  185. currency: 'XRP',
  186. counterparty: '',
  187. value: '-0.00001'
  188. }
  189. ]
  190. }
  191. var redeemThenIssueBalanceChanges = {
  192. rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh: [
  193. {
  194. currency: 'USD',
  195. counterparty: 'rPMh7Pi9ct699iZUTWaytJUoHcJ7cgyziK',
  196. value: '200'
  197. }
  198. ],
  199. rPMh7Pi9ct699iZUTWaytJUoHcJ7cgyziK: [
  200. {
  201. currency: 'USD',
  202. counterparty: 'rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh',
  203. value: '-200'
  204. },
  205. {
  206. currency: 'XRP',
  207. counterparty: '',
  208. value: '-0.00001'
  209. }
  210. ]
  211. }
  212. var multipathBalanceChanges = {
  213. rrnsYgWn13Z28GtRgznrSUsLfMkvsXCZSu: [
  214. {
  215. counterparty: 'r4nmQNH4Fhjfh6cHDbvVSsBv7KySbj4cBf',
  216. currency: 'USD',
  217. value: '100'
  218. },
  219. {
  220. counterparty: 'rnYDWQaRdMb5neCGgvFfhw3MBoxmv5LtfH',
  221. currency: 'USD',
  222. value: '-100'
  223. }
  224. ],
  225. r4nmQNH4Fhjfh6cHDbvVSsBv7KySbj4cBf: [
  226. {
  227. counterparty: 'rrnsYgWn13Z28GtRgznrSUsLfMkvsXCZSu',
  228. currency: 'USD',
  229. value: '-100'
  230. },
  231. {
  232. counterparty: '',
  233. currency: 'XRP',
  234. value: '-0.00001'
  235. },
  236. {
  237. counterparty: 'rJsaPnGdeo7BhMnHjuc3n44Mf7Ra1qkSVJ',
  238. currency: 'USD',
  239. value: '-100'
  240. },
  241. {
  242. counterparty: 'rGpeQzUWFu4fMhJHZ1Via5aqFC3A5twZUD',
  243. currency: 'USD',
  244. value: '-100'
  245. }
  246. ],
  247. rJsaPnGdeo7BhMnHjuc3n44Mf7Ra1qkSVJ: [
  248. {
  249. counterparty: 'r4nmQNH4Fhjfh6cHDbvVSsBv7KySbj4cBf',
  250. currency: 'USD',
  251. value: '100'
  252. },
  253. {
  254. counterparty: 'rnYDWQaRdMb5neCGgvFfhw3MBoxmv5LtfH',
  255. currency: 'USD',
  256. value: '-100'
  257. }
  258. ],
  259. rGpeQzUWFu4fMhJHZ1Via5aqFC3A5twZUD: [
  260. {
  261. counterparty: 'r4nmQNH4Fhjfh6cHDbvVSsBv7KySbj4cBf',
  262. currency: 'USD',
  263. value: '100'
  264. },
  265. {
  266. counterparty: 'rnYDWQaRdMb5neCGgvFfhw3MBoxmv5LtfH',
  267. currency: 'USD',
  268. value: '-100'
  269. }
  270. ],
  271. rnYDWQaRdMb5neCGgvFfhw3MBoxmv5LtfH: [
  272. {
  273. counterparty: 'rJsaPnGdeo7BhMnHjuc3n44Mf7Ra1qkSVJ',
  274. currency: 'USD',
  275. value: '100'
  276. },
  277. {
  278. counterparty: 'rrnsYgWn13Z28GtRgznrSUsLfMkvsXCZSu',
  279. currency: 'USD',
  280. value: '100'
  281. },
  282. {
  283. counterparty: 'rGpeQzUWFu4fMhJHZ1Via5aqFC3A5twZUD',
  284. currency: 'USD',
  285. value: '100'
  286. }
  287. ]
  288. }
  289. // Set trust limit to zero on rLDY when it has a balance of 0.02 USD
  290. var removeTrustBalanceChanges = setTrustlineBalanceChanges
  291. function loadFixture(filename) {
  292. var path = __dirname + '/fixtures/' + filename
  293. return JSON.parse(fs.readFileSync(path))
  294. }
  295. describe('parseBalanceChanges', function() {
  296. it('XRP create account', function() {
  297. var paymentResponse = loadFixture('payment-xrp-create-account.json')
  298. var result = parseBalanceChanges(paymentResponse.metadata)
  299. assert.deepEqual(result, createAccountBalanceChanges)
  300. })
  301. it('USD payment to account with no USD', function() {
  302. var filename = 'payment-iou-destination-no-balance.json'
  303. var paymentResponse = loadFixture(filename)
  304. var result = parseBalanceChanges(paymentResponse.metadata)
  305. assert.deepEqual(result, usdFirstPaymentBalanceChanges)
  306. })
  307. it('USD payment of all USD in source account', function() {
  308. var paymentResponse = loadFixture('payment-iou-spend-full-balance.json')
  309. var result = parseBalanceChanges(paymentResponse.metadata)
  310. assert.deepEqual(result, usdFullPaymentBalanceChanges)
  311. })
  312. it('USD payment to account with USD', function() {
  313. var paymentResponse = loadFixture('payment-iou.json')
  314. var result = parseBalanceChanges(paymentResponse.metadata)
  315. assert.deepEqual(result, usdPaymentBalanceChanges)
  316. })
  317. it('Set trust limit to 0 with balance remaining', function() {
  318. var paymentResponse = loadFixture('trustline-set-limit-to-zero.json')
  319. var result = parseBalanceChanges(paymentResponse.metadata)
  320. assert.deepEqual(result, removeTrustBalanceChanges)
  321. })
  322. it('Create trustline', function() {
  323. var paymentResponse = loadFixture('trustline-create.json')
  324. var result = parseBalanceChanges(paymentResponse.metadata)
  325. assert.deepEqual(result, createTrustlineBalanceChanges)
  326. })
  327. it('Set trustline', function() {
  328. var paymentResponse = loadFixture('trustline-set-limit.json')
  329. var result = parseBalanceChanges(paymentResponse.metadata)
  330. assert.deepEqual(result, setTrustlineBalanceChanges)
  331. })
  332. it('Set trustline 2', function() {
  333. var paymentResponse = loadFixture('trustline-set-limit-2.json')
  334. var result = parseBalanceChanges(paymentResponse.metadata)
  335. assert.deepEqual(result, setTrustlineBalanceChanges2)
  336. })
  337. it('Delete trustline', function() {
  338. var paymentResponse = loadFixture('trustline-delete.json')
  339. var result = parseBalanceChanges(paymentResponse.metadata)
  340. assert.deepEqual(result, deleteTrustlineBalanceChanges)
  341. })
  342. it('Redeem USD', function() {
  343. var paymentResponse = loadFixture('payment-iou-redeem.json')
  344. var result = parseBalanceChanges(paymentResponse.result.meta)
  345. assert.deepEqual(result, redeemBalanceChanges)
  346. })
  347. it('Redeem then issue USD', function() {
  348. var paymentResponse = loadFixture('payment-iou-redeem-then-issue.json')
  349. var result = parseBalanceChanges(paymentResponse.result.meta)
  350. assert.deepEqual(result, redeemThenIssueBalanceChanges)
  351. })
  352. it('Multipath USD payment', function() {
  353. var paymentResponse = loadFixture('payment-iou-multipath.json')
  354. var result = parseBalanceChanges(paymentResponse.result.meta)
  355. assert.deepEqual(result, multipathBalanceChanges)
  356. })
  357. })