client.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. const util = require("util");
  2. const addr1 = "foo";
  3. const addr2 = "bar";
  4. const fee = "fee";
  5. const percentage = 0.005;
  6. function dbg(obj) {
  7. console.log(util.inspect(obj, false, null, true /* enable colors */))
  8. }
  9. async function get_balance(account) {
  10. const response = await fetch(`http://127.0.0.1:8080/balance/${account}`);
  11. return response.json();
  12. }
  13. async function deposit(account, amount, asset) {
  14. const response = (await fetch("http://127.0.0.1:8080/deposit", {
  15. method: "POST",
  16. headers: {
  17. "Content-Type": "application/json",
  18. },
  19. body: JSON.stringify({
  20. memo: "deposit",
  21. account,
  22. cents: amount.toString(),
  23. tags: ['deposit'],
  24. asset,
  25. status: 'pending',
  26. })
  27. }));
  28. return response.json();
  29. }
  30. async function trade(amount, asset, from, amount_to, asset_to, to) {
  31. const request = {
  32. memo: "trade",
  33. debit: [
  34. {
  35. account: from,
  36. amount: amount.toString(),
  37. asset
  38. },
  39. {
  40. account: to,
  41. amount: amount_to.toString(),
  42. asset: asset_to,
  43. }
  44. ],
  45. credit: [
  46. {
  47. account: to,
  48. amount: (amount * (1 - percentage)).toString(),
  49. asset
  50. },
  51. {
  52. account: from,
  53. amount: amount_to.toString(),
  54. asset: asset_to
  55. },
  56. {
  57. account: fee,
  58. amount: (amount * percentage).toString(),
  59. asset
  60. }
  61. ],
  62. status: 'pending',
  63. asset,
  64. }
  65. console.log(request)
  66. const response = (await fetch("http://127.0.0.1:8080/tx", {
  67. method: "POST",
  68. headers: {
  69. "Content-Type": "application/json",
  70. },
  71. body: JSON.stringify(request)
  72. }));
  73. return response.json();
  74. }
  75. async function change_status(id, s_status) {
  76. const response = (await fetch(`http://127.0.0.1:8080/${id}`, {
  77. method: "POST",
  78. headers: {
  79. "Content-Type": "application/json",
  80. },
  81. body: JSON.stringify({
  82. status: s_status,
  83. memo: `change status to ${s_status}`,
  84. })
  85. }));
  86. return response.json();
  87. }
  88. async function depositAndConfirm(account, amount, asset) {
  89. let d = (await deposit(account, amount, asset));
  90. dbg(d);
  91. dbg(await change_status(d._rev, 'settled'));
  92. }
  93. async function test() {
  94. /* */
  95. await depositAndConfirm(addr1, 111512312, "BTC/8");
  96. await depositAndConfirm(addr2, 300512312, "USD/4");
  97. /* */
  98. let t = await trade(0.01, "BTC/8", addr1, 26751.11, "USD/4", addr2);
  99. dbg(t);
  100. t = await change_status(t._rev, 'processing',);
  101. dbg(t)
  102. console.log('set settle')
  103. dbg(await change_status(t._rev, 'settled'));
  104. dbg(await get_balance(addr1));
  105. dbg(await get_balance(addr2));
  106. dbg(await get_balance(fee));
  107. }
  108. test()