client.js 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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 deposit(account, amount, asset) {
  10. const response = (await fetch("http://127.0.0.1:8080/deposit", {
  11. method: "POST",
  12. headers: {
  13. "Content-Type": "application/json",
  14. },
  15. body: JSON.stringify({
  16. memo: "deposit",
  17. account,
  18. amount: amount.toString(),
  19. asset,
  20. status: 'pending',
  21. })
  22. }));
  23. return response.json();
  24. }
  25. async function trade(amount, asset, from, amount_to, asset_to, to) {
  26. const response = (await fetch("http://127.0.0.1:8080/tx", {
  27. method: "POST",
  28. headers: {
  29. "Content-Type": "application/json",
  30. },
  31. body: JSON.stringify({
  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. }));
  66. return response.json();
  67. }
  68. async function change_status(id, s_status) {
  69. const response = (await fetch(`http://127.0.0.1:8080/${id}`, {
  70. method: "POST",
  71. headers: {
  72. "Content-Type": "application/json",
  73. },
  74. body: JSON.stringify({
  75. status: s_status,
  76. memo: `change status to ${s_status}`,
  77. })
  78. }));
  79. return response.json();
  80. }
  81. async function test() {
  82. let d = (await deposit(addr1, 100, "BTC/8"));
  83. dbg(d);
  84. dbg(await change_status(d.id, 'settled'));
  85. d = (await deposit(addr2, 1000000, "USD/4"));
  86. dbg(await change_status(d.id, 'settled'));
  87. const t = await trade(1, "BTC/8", addr1, 26751.11, "USD/4", addr2);
  88. dbg(t);
  89. dbg(await change_status(t.id, 'processing',));
  90. dbg(await change_status(t.id, 'settled'));
  91. }
  92. test()