client.js 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. const addr1 = "foo";
  2. const addr2 = "bar";
  3. const fee = "fee";
  4. const percentage = 0.005;
  5. async function deposit(account, amount, asset) {
  6. const response = (await fetch("http://127.0.0.1:8080/deposit", {
  7. method: "POST",
  8. headers: {
  9. "Content-Type": "application/json",
  10. },
  11. body: JSON.stringify({
  12. memo: "deposit",
  13. account,
  14. amount: amount.toString(),
  15. status: 'pending',
  16. asset,
  17. })
  18. }));
  19. return response.json();
  20. }
  21. async function trade(amount, asset, from, amount_to, asset_to, to) {
  22. const response = (await fetch("http://127.0.0.1:8080/tx", {
  23. method: "POST",
  24. headers: {
  25. "Content-Type": "application/json",
  26. },
  27. body: JSON.stringify({
  28. memo: "trade",
  29. debit: [
  30. {
  31. account: from,
  32. amount: amount.toString(),
  33. asset,
  34. },
  35. {
  36. account: to,
  37. amount: amount_to.toString(),
  38. asset: asset_to,
  39. }
  40. ],
  41. credit: [
  42. {
  43. account: to,
  44. amount: (amount * (1 - percentage)).toString(),
  45. asset,
  46. },
  47. {
  48. account: from,
  49. amount: amount_to.toString(),
  50. asset: asset_to,
  51. },
  52. {
  53. account: fee,
  54. amount: (amount * percentage).toString(),
  55. asset,
  56. }
  57. ],
  58. status: 'pending',
  59. asset,
  60. })
  61. }));
  62. return response.json();
  63. }
  64. async function change_status(id, s_status) {
  65. const response = (await fetch(`http://127.0.0.1:8080/${id}`, {
  66. method: "POST",
  67. headers: {
  68. "Content-Type": "application/json",
  69. },
  70. body: JSON.stringify({
  71. status: s_status,
  72. memo: `change status to ${s_status}`,
  73. })
  74. }));
  75. return response.json();
  76. }
  77. async function test() {
  78. let d = (await deposit(addr1, 100, "BTC"));
  79. console.log(await change_status(d.id, 'settled'));
  80. d = (await deposit(addr2, 1000000, "USD"));
  81. console.log(await change_status(d.id, 'settled'));
  82. const t = await trade(1, "BTC", addr1, 26751.11, "USD", addr2);
  83. console.log(t);
  84. console.log(await change_status(t.id, 'processing',));
  85. console.log(await change_status(t.id, 'settled'));
  86. }
  87. test()