client.js 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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 response = (await fetch("http://127.0.0.1:8080/tx", {
  32. method: "POST",
  33. headers: {
  34. "Content-Type": "application/json",
  35. },
  36. body: JSON.stringify({
  37. memo: "trade",
  38. debit: [
  39. {
  40. account: from,
  41. amount: amount.toString(),
  42. asset
  43. },
  44. {
  45. account: to,
  46. amount: amount_to.toString(),
  47. asset: asset_to,
  48. }
  49. ],
  50. credit: [
  51. {
  52. account: to,
  53. amount: (amount * (1 - percentage)).toString(),
  54. asset
  55. },
  56. {
  57. account: from,
  58. amount: amount_to.toString(),
  59. asset: asset_to
  60. },
  61. {
  62. account: fee,
  63. amount: (amount * percentage).toString(),
  64. asset
  65. }
  66. ],
  67. status: 'pending',
  68. asset,
  69. })
  70. }));
  71. return response.json();
  72. }
  73. async function change_status(id, s_status) {
  74. const response = (await fetch(`http://127.0.0.1:8080/${id}`, {
  75. method: "POST",
  76. headers: {
  77. "Content-Type": "application/json",
  78. },
  79. body: JSON.stringify({
  80. status: s_status,
  81. memo: `change status to ${s_status}`,
  82. })
  83. }));
  84. return response.json();
  85. }
  86. async function test() {
  87. let d = (await deposit(addr1, 1512312, "BTC/8"));
  88. dbg(d);
  89. return
  90. dbg(await change_status(d._id, 'settled'));
  91. d = (await deposit(addr2, 1001234, "USD/4"));
  92. dbg(await change_status(d._id, 'settled'));
  93. const t = await trade(1, "BTC/8", addr1, 26751.11, "USD/4", addr2);
  94. dbg(t);
  95. dbg(await change_status(t._id, 'processing',));
  96. console.log('set settle')
  97. dbg(await change_status(t._id, 'settled'));
  98. dbg(await get_balance(addr1));
  99. dbg(await get_balance(addr2));
  100. dbg(await get_balance(fee));
  101. }
  102. test()