app.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. // In browsers, use a <script> tag. In Node.js, uncomment the following line:
  2. const xrpl = require('xrpl')
  3. const express = require('express')
  4. const app = express()
  5. const port = 3000
  6. function createWallet() {
  7. const wallet = xrpl.Wallet.generate("ed25519")
  8. return wallet;
  9. }
  10. //const xrplServer = 'wss://s.altnet.rippletest.net:51233'
  11. //const xrplServer = 'ws://ripple-api1.dangnn.io:6005'
  12. const xrplServer = 'wss://xrplcluster.com'
  13. async function test(dstTag) {
  14. const client = new xrpl.Client(xrplServer)
  15. await client.connect()
  16. const response = await client.request({
  17. "command": "server_info",
  18. })
  19. console.log(response.result.info.validated_ledger)
  20. // const fund_result = await client.fundWallet()
  21. // const test_wallet = fund_result.wallet
  22. // console.log(fund_result)
  23. //rKkPNfowKsjU616jMfvvn2rSibtrNeh7B8 dst tag enabled
  24. //rtCeovN6T1wE3baFuBSdRWE4QaK9sHMGS
  25. //sendXrpTo(test_wallet.seed, "rtCeovN6T1wE3baFuBSdRWE4QaK9sHMGS", "1000")
  26. await client.disconnect()
  27. //await getTransactionFee()
  28. }
  29. test()
  30. //http://localhost:3000/wallet/getbalance?address=rJWfCDCRGY6vr43bkTXdXq7guEAjdyQCkf
  31. async function getBalance(address) {
  32. const client = new xrpl.Client(xrplServer)
  33. await client.connect()
  34. const response = await client.request({
  35. "command": "account_info",
  36. "account": address,
  37. "ledger_index": "validated"
  38. })
  39. await client.disconnect()
  40. console.log(address)
  41. console.log(response.result.account_data)
  42. let ret = {
  43. 'balance': response.result.account_data.Balance,
  44. 'ownerCount': response.result.account_data.OwnerCount,
  45. }
  46. return ret
  47. }
  48. async function getTransactionFee(fromSeed, to, amount) {
  49. const client = new xrpl.Client(xrplServer)
  50. await client.connect()
  51. const wallet = xrpl.Wallet.fromSeed(fromSeed)
  52. const prepared = await client.autofill({
  53. "TransactionType": "Payment",
  54. "Account": wallet.address,
  55. "Amount": amount,
  56. "Destination": to
  57. })
  58. await client.disconnect()
  59. return prepared.Fee
  60. }
  61. //http://localhost:3000/wallet/send?fromSeed=sEdVkTBxbtqUP7Y4Do63CbvQzarRjip&toAddr=rJWfCDCRGY6vr43bkTXdXq7guEAjdyQCkf&amount=30
  62. async function sendXrpTo(fromSeed, to, amount, dstTag) {
  63. const client = new xrpl.Client(xrplServer)
  64. await client.connect()
  65. const wallet = xrpl.Wallet.fromSeed(fromSeed)
  66. console.log(`send xrp ${wallet.address} -> ${to}, amount: ${amount}`);
  67. let transaction = {
  68. "TransactionType": "Payment",
  69. "Account": wallet.address,
  70. "Amount": amount,
  71. "Destination": to,
  72. }
  73. if (dstTag != undefined) {
  74. transaction["DestinationTag"] = parseInt(dstTag)
  75. }
  76. const prepared = await client.autofill(transaction)
  77. const max_ledger = prepared.LastLedgerSequence
  78. console.log("Prepared transaction instructions:", prepared)
  79. console.log("Transaction cost:", xrpl.dropsToXrp(prepared.Fee), "XRP")
  80. console.log("Transaction expires after ledger:", max_ledger)
  81. const signed = wallet.sign(prepared)
  82. console.log("Identifying hash:", signed.hash)
  83. console.log("Signed blob:", signed.tx_blob)
  84. const tx = await client.submitAndWait(signed.tx_blob)
  85. // Check transaction results -------------------------------------------------
  86. console.log("Transaction result:", tx.result.meta.TransactionResult)
  87. const result = xrpl.getBalanceChanges(tx.result.meta)
  88. console.log("Balance changes:", JSON.stringify(result, null, 2))
  89. await client.disconnect()
  90. return result
  91. }
  92. async function getReserve() {
  93. const client = new xrpl.Client(xrplServer)
  94. await client.connect()
  95. const response = await client.request({
  96. "command": "server_info",
  97. })
  98. await client.disconnect()
  99. let ret = {
  100. 'reserve_base': response.result.info.validated_ledger.reserve_base_xrp,
  101. 'reserve_inc': response.result.info.validated_ledger.reserve_inc_xrp
  102. }
  103. return ret
  104. }
  105. app.use(function (err, req, res, next) {
  106. console.error(err);
  107. res.status(500).send('Something broke!');
  108. });
  109. app.get('/wallet/create', (req, res) => {
  110. const wallet = createWallet()
  111. const resp = {}
  112. resp.addr = wallet.address
  113. resp.seed = wallet.seed
  114. res.send(resp)
  115. })
  116. app.get('/wallet/getBalance', async (req, res) => {
  117. try {
  118. const address = req.query['address']
  119. const balance = await getBalance(address)
  120. res.send(balance)
  121. } catch (e) {
  122. res.status(400).send(e.data)
  123. }
  124. })
  125. app.get('/wallet/send', async (req, res) => {
  126. try {
  127. const fromSeed = req.query['fromSeed']
  128. const toAddr = req.query['toAddr']
  129. const amount = req.query['amount']
  130. const destTag = req.query['destTag']
  131. const result = await sendXrpTo(fromSeed, toAddr, amount, destTag)
  132. res.send(result)
  133. } catch (e) {
  134. console.log('hello')
  135. res.status(400).send(e.data)
  136. }
  137. })
  138. app.get('/wallet/getTxFee', async (req, res) => {
  139. try {
  140. const fromSeed = req.query['fromSeed']
  141. const toAddr = req.query['toAddr']
  142. const amount = req.query['amount']
  143. const result = await getTransactionFee(fromSeed, toAddr, amount)
  144. res.send(result)
  145. } catch (e) {
  146. res.status(400).send(e.data)
  147. }
  148. })
  149. app.get('/wallet/getReserve', async (req, res) => {
  150. try {
  151. const reserve = await getReserve()
  152. //string으로 써야함
  153. res.send(reserve)
  154. } catch (e) {
  155. res.status(400).send(e.data)
  156. }
  157. })
  158. app.listen(port, () => {
  159. console.log(`wallet server listening on port ${port}`)
  160. })