client.js 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  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: 'settled',
  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: 'settled',
  49. asset,
  50. })
  51. }));
  52. return response.json();
  53. }
  54. async function test() {
  55. console.log(await deposit(addr1, 100, "BTC"));
  56. console.log(await trade(10, "BTC", addr1, addr2))
  57. }
  58. test()