client.js 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. const addr1 = "foo";
  2. const addr2 = "bar";
  3. const fee = "fee";
  4. const percentage = 0.01;
  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, 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. credit: [
  37. {
  38. account: to,
  39. amount: (amount * (1 - percentage)).toString(),
  40. asset,
  41. },
  42. {
  43. account: fee,
  44. amount: (amount * percentage).toString(),
  45. asset,
  46. }
  47. ],
  48. status: 'pending',
  49. asset,
  50. })
  51. }));
  52. return response.json();
  53. }
  54. async function settle(id) {
  55. const response = (await fetch(`http://127.0.0.1:8080/${id}`, {
  56. method: "POST",
  57. headers: {
  58. "Content-Type": "application/json",
  59. },
  60. body: JSON.stringify({
  61. status: "settled",
  62. memo: "trade",
  63. })
  64. }));
  65. return response.json();
  66. }
  67. async function test() {
  68. //const d = (await deposit(addr1, 100, "BTC"));
  69. //console.log(d);
  70. //console.log(await settle(d.id));
  71. const t = await trade(10, "BTC", addr1, addr2);
  72. console.log(await settle(t.id));
  73. }
  74. test()