XRPL Docs (Korean)
  • XRPL Docs(Kor)
  • Introduction
    • XRP Ledger란?
    • XRP란?
    • Crypto Wallets
    • Transactions and Requests
    • Software Ecosystem
  • Use Cases
    • 결제(Payments)
      • P2P 결제(Peer-to-Peer Payments)
      • 예금 제한(Restricting Deposits)
      • 스마트 컨트랙트(Smart Contracts)
    • 토큰화(Tokenization)
      • 스테이블코인 발행인(Stablecoin Issuer)
      • NFT 마켓플레이스 개요(NFT Marketplace Overview)
    • 탈중앙화 금융(Decentralized Finance)
      • 알고리즘 트레이딩(Algorithmic Trading)
      • 거래소에 XRP 상장하기((List XRP as an Exchange)
  • Concepts
    • 소개
      • 컨센서스 소개
      • XRP
      • 소프트웨어 생태계
    • XRP Ledger 서버
      • rippled 서버 모드(rippled Server Modes)
      • 클러스터링(Clustering)
      • Ledger 역사
      • 피어 프로토콜(Peer Protocol)
      • 트랜잭션 검열 감지(Transaction Censorship Detection)
      • 병렬 네트워크(Parallel Networks)
      • 수정안(Amendments)
        • XRP Ledger에 코드를 기여하는 방법
        • 알려진 수정안
      • 클리오 서버(The Clio Server)
    • 컨센서스 네트워크(Consensus Protocol)
      • 컨센서스 구조(Consensus Structure)
      • 컨센서스 원칙과 규칙(Consensus Principles and Rules)
      • 공격과 실패 모드에 대한 컨센서스 보호(Consensus Protections Against Attacks and Failure Modes)
      • 불변성 체크
      • 부정 UNL
      • 트랜잭션 취소 정보
      • 트랜잭션 변조 가능성
      • 수수료 투표
      • 컨센서스 연구
    • Ledgers
      • Ledger 구조(Ledger Structure)
      • 개방형, 폐쇄형, 검증형 Ledgers(Open, Closed, and Validated Ledgers)
      • Ledger 마감 시간(Ledger Close Times)
    • 트랜잭션(Transactions)
      • 수수료(Fees)
      • 신뢰할 수 있는 트랜잭션 제출(Reliable Transaction Submission)
      • 보안 서명(Secure Signing)
      • 출발, 데스티네이션 태그(Source and Destination Tags)
      • 트랜잭션 비용(Transaction Cost)
      • 트랜잭션 대기열(Transaction Queue)
      • 결과의 불변성(Finality of Results)
        • 트랜잭션 결과 조회(Look Up Transaction Results)
        • Transaction Malleability
    • 결제 유형
      • XRP 직접 결제
      • 교차 화폐 결제
      • 수표
      • 에스크로
      • 부분 결제
      • 결제 채널
    • 토큰(Tokens)
      • Non-Fungible Tokens
        • NFT 정보 저장소(NFT Payload Storage)
        • XRP Ledger에서 NFT 토큰 거래(Trading NFTokens on the XRP Ledger)
        • NFT Reserve Requirements
        • 일괄 발행(Batch minting)
        • 다른 계정에게 NFT 발행 권한 부여(Authorizing Another Account to Mint Your NFTs)
        • NFT 경매 진행하기(Running an NFT Auction)
        • NFT를 컬렉션으로 발행하기(Minting NFTs into Collections)
        • NFT의 고정 공급 보장하기(Guaranteeing a Fixed Supply of NFTs)
        • NFT 관련 API(NFT APIs)
      • 신뢰선과 발급(Trust Lines and Issuing)
      • 승인된 신뢰선(Authorized Trust Lines)
      • 토큰 환수(Clawing Back Tokens)
      • Freezing Tokens(토큰 동결)
        • 동결에 대한 일반적인 오해(Common Misunderstandings about Freezes )
      • Rippling
      • 이체 수수료(Transfer Fees)
      • 경로(Paths)
      • Demurrage(과잉보유비용)
      • 탈중앙화 거래소(Decentralized Exchange)
        • 제안(Offers)
        • Auto-Bridging
        • Tick Size
        • AMM(Automated Market Makers)
    • 계정
      • 다중 서명
      • 티켓
      • 계정 유형
      • 계정 삭제
      • 준비금(Reserves)
      • 주소(Addresses)
      • 암호화 키(Cryptographic Keys)
      • 입금 승인(Deposit Authorization)
  • Tutorials
    • 퍼블릭 서버(Public Servers)
    • Python
      • Python으로 시작하기(Get Started Using Python)
      • python 모듈형 튜토리얼(Modular Tutorials in Python)
        • python을 이용한 Send Payments(Send Payments Using Python)
          • 계정 생성 및 XRP 전송(Create Accounts and Send XRP Using Python)
          • 신뢰 생성 및 Currency 전송 (Create Trust Line and Send Currency Using Python)
          • 시간 보류 에스크로 생성(Create Time-based Escrows Using Python)
        • python을 이용한 NFTs(NFTs Using Python)
          • NFTs 발행과 소각(Mint and Burn NFTs Using Python)
          • NFTs 전송 (Transfer NFTs Using Python)
          • NFT 판매 중개 (Broker an NFT Sale Using Python)
          • 공인 발행인 지정 (Assign an Authorized Minter Using Python)
          • NFTs 일괄 발행 (Batch Mint NFTs Using Python)
        • Python에서 데스크톱 지갑 구축(Build a Desktop Wallet in Python)
    • JavaScript
      • JavaScript로 시작하기(Get Started Using JavaScript)
      • JavaScript 모듈형 튜토리얼(Modular Tutorials in JavaScript)
        • JavaScript를 이용한 Send Payments(Send Payments Using JavaScript)
          • JavaScript를 이용한 계정 생성 및 XRP 전송(Create Accounts and Send XRP Using JavaScript)
          • JavaScript를 이용한 신뢰선 생성 및 화폐 전송(Create Trust Line and Send Currency Using JavaScript)
          • 시간 기반 에스크로 생성하기(Create Time-based Escrows Using JavaScript)
          • 조건부 에스크로 생성하기(Create Conditional Escrows Using JavaScript)
        • JavaScript를 이용한 NFTs(NFTs Using JavaScript)
          • JavaScript를 이용한 NFTs 발행 및 소각(Mint and Burn NFTs Using JavaScript)
          • JavaScript를 이용한 NFTs 전송(Transfer NFTs Using JavaScript)
          • JavaScript를 이용한 NFT 판매 중개(Broker an NFT Sale Using JavaScript)
          • JavaScript를 이용한 공인 발행인 지정(Assign an Authorized Minter Using JavaScript)
          • JavaScript를 이용한 NFTs 일괄 발행(Batch Mint NFTs Using JavaScript)
      • JavaScript를 이용한 브라우저 지갑 개발(Build a Browser Wallet in JavaScript)
      • JavaScript를 이용한 데스크탑 지갑 개발(Build a Desktop Wallet in JavaScript)
    • Java
      • Java로 시작하기(Get Started Using Java)
    • HTTP / Websocket APIs
      • HTTP/WebSocket API 사용 시작하기(Get Started Using HTTP / WebSocket APIs)
      • WebSocket으로 수신 결제 모니터링(Monitor Incoming Payments with WebSocket)
    • Tasks
      • 계정 설정 관리(Manage Account Settings)
        • 일반 키 쌍 할당
        • 일반 키 쌍 변경 또는 제거
        • 마스터 키 쌍 비활성화
        • 다중 서명 설정
        • 다중 서명 트랜잭션 전송
        • 데스티네이션 태그 필요
        • 오프라인 계정 설정 튜토리얼
        • 티켓 사용(Use Tickets)
      • XRP 보내기(Send XRP)
      • 특수 결제 유형 사용(Use Specialized Payment Types)
        • 에스크로 사용(Use escrow)
          • 시간 보류 에스크로 보내기(Send a Time-Held Escrow)
          • 조건부 보류 에스크로 보내기(Send a Conditionally-Held Escrow)
          • 만료된 에스크로 취소(Cancel an Expired Escrow)
          • 에스크로 조회(Look up Escrows)
          • 에스크로를 스마트 컨트랙트로 사용(Use an Escrow as a Smart Contract)
        • 결제 채널 사용(Use Payment Channels)
          • 결제 채널을 열어 거래소 간 네트워크 활성화(Open a Payment Channel to Enable an Inter-Exchange Network)
        • 수표 사용(Use Checks)
          • 수표 전송(Send a Check)
          • 정확한 금액의 수표 현금화(Cash a Check for an Exact Amount)
          • 유연한 금액의 수표 현금화(Cash a Check for a Flexible Amount)
          • 수표 취소(Cancel a Check)
          • 발신자별 수표 조회(Look Up Checks by Sender)
          • 수취인별 수표 조회(Look Up Checks by Recipient)
      • 토큰 사용(Use Tokens)
        • 대체가능한 토큰 발행(Issue a Fungible Token)
        • 탈중앙화 거래소에서 거래(Trade in the Decentralized Exchange)
        • 동결 금지 활성화
        • 글로벌 동결 시행
        • 신뢰선 동결하기
    • Apps 구축
      • JS에서 데스크톱 지갑 구축
      • JS에서 브라우저 지갑 구축
    • XRP Ledger 비즈니스
      • XRP 차트에 거래소 등록하기
      • 스테이블코인 발행자 되기
    • rippled 서버 관리
      • rippled 설치
        • 시스템 요구 사항
        • CentOS/Red Hat에 yum으로 설치하기
        • 우분투 또는 데비안 리눅스에 설치
        • 리눅스에서 자동 업데이트
        • CentOS/Red Hat에서 수동 업데이트
        • 우분투 또는 데비안에서 수동 업데이트
        • 리포팅 모드에서 rippled 빌드 및 실행
        • 용량 계획
        • rippled v1.3.x 마이그레이션 지침
      • rippled 구성
        • rippled를 검증인으로 실행하기
        • rippled를 스톡 서버로 실행
        • 수정안 투표 구성
        • 수정안 테스트
        • StatsD 구성
        • rippled를 병렬 네트워크에 연결하기
        • 온라인 삭제 구성
        • 권고 삭제 구성
        • 히스토리 샤딩 구성
        • 전체 히스토리 구성
        • gRPC 구성
        • 공개 서명 사용
      • 피어링 구성
        • 클러스터 rippled 서버
        • 비공개 서버 구성
        • 피어 크롤러 구성
        • 링크 압축 사용
        • 피어링을 위한 포트 포워드
        • 특정 피어에 수동으로 연결
        • 최대 피어 수 설정
        • 피어 예약 사용
      • stand-alone 모드에서 rippled 기능 테스트하기
        • stand-alone 모드에서 새 제네시스 ledger 시작하기
        • stand-alone 모드에서 저장된 ledger 불러오기
        • stand-alone 모드에서 ledger 진행하기
      • 문제 해결
        • rippled 문제 진단하기
        • 상태 확인 개입
        • 로그 메시지 이해
        • rippled 서버가 동기화되지 않음
        • rippled 서버가 수정이 차단됨
        • rippled 서버가 시작되지 않음
        • SQLite 트랜잭션 데이터베이스 페이지 크기 문제 해결
    • 클리오 서버 관리
      • 우분투 리눅스에 클리오 설치
  • References
    • XRP Ledger 프로토콜 참조(XRP Ledger Protocol Reference)
      • 기본 데이터 유형(Basic Data Types)
        • base58 인코딩(base58 Encodings)
        • 화폐 형식(Currency Formats)
        • NFToken
      • Ledger 데이터 형식(Ledger Data Formats)
        • Ledger 헤더(Ledger Header)
        • Ledger 객체 IDs
        • Ledger 객체 유형
          • AccountRoot
          • Amendments
          • AMM(experimental - 수정중)
          • Check
          • DepositPreauth
          • DirectoryNode
          • Escrow
          • FeeSettings
          • LedgerHashes
          • NegativeUNL
          • NFTokenOffer
          • NFTokenPage
          • Offer
          • PayChannel
          • RippleState
          • SignerList
          • Ticket
      • 트랜잭션 참조(Transaction Reference)
        • 트랜잭션 공통 필드(Transaction Common Fields)
        • 트랜잭션 유형(Transaction Types)
          • AccountSet
          • AccountDelete
          • AMMBid
          • AMMCreate
          • AMMDelete
          • AMMDeposit
          • CheckCancel
          • CheckCash
          • CheckCreate
          • DepositPreauth
          • EscrowCancel
          • EscrowCreate
          • EscrowFinish
          • NFTokenAcceptOffer
          • NFTokenBurn
          • NFTokenCancelOffer
          • NFTokenCreateOffer
          • NFTokenMint
          • OfferCancel
          • OfferCreate
          • Payment
          • PaymentChannelClaim
          • PaymentChannelCreate
          • PaymentChannelFund
          • SetRegularKey
          • SignerListSet
          • TicketCreate
          • TrustSet
        • Pseudo-Transactions
          • EnableAmendment
          • SetFee
          • UNLModify
        • 트랜잭션 결과(Transaction Results)
          • tec Codes
          • tef Codes
          • tel Codes
          • tem Codes
          • ter Codes
          • tes Success
        • 트랜잭션 메타데이터(Transaction Metadata)
      • Binary Format
    • 클라이언트 라이브러리
      • JavaScript / TypeScript 클라이언트 라이브러
        • ripple-lib 1.x에서 xrpl.js 2.x로의 마이그레이션 가이드
      • Python 클라이언트 라이브러리
      • Java 클라이언트 라이브러리
      • Ruby 클라이언트 라이브러리
    • HTTP / WebSocket APIs
      • API 규칙
        • 요청 형식
        • 응답 형식
        • 오류 형식
        • 마커 및 페이지네이션
        • 속도 제한
        • rippled 서버 상태
      • 공개 API 메소드
        • 계정 메소드
          • account_channels
          • account_currencies
          • account_info
          • account_lines
          • account_nfts
          • account_objects
          • account_offers
          • account_tx
          • gateway_balances
          • noripple_check
        • Ledger 메소드
          • ledger
          • ledger_closed
          • ledger_current
          • ledger_data
          • ledger_entry
        • 트랜잭션 메소드
          • submit
          • submit_multisigned
          • transaction_entry
          • tx
          • tx_history
        • 경로와 오더북 메소드
          • book_offers
          • deposit_authorized
          • nft_buy_offers
          • nft_sell_offers
          • path_find
          • ripple_path_find
        • 결제 채널 메소드
          • channel_authorize
          • channel_verify
        • 구독 메소드
          • 구독
          • 구독 취소
        • Server Info 메소드
          • fee
          • manifest
          • server_info (rippled)
          • server_state
        • 클리오 서버
          • server_info
          • ledger
          • nft_history
          • nft_info
        • 유틸리티 메소드
          • json
          • ping
          • random
      • 관리자 API 메소드
        • 키 생성 방법
          • validation_create
          • wallet_propose
        • 로깅 및 데이터 관리 메소드
          • can_delete
          • crawl_shards
          • download_shard
          • ledger_cleaner
          • ledger_request
          • log_level
          • logrotate
          • node_to_shard
        • 서버 컨트롤 메소드
          • ledger_accept
          • stop
          • validation_seed
        • 서명 메소드
          • sign
          • sign_for
        • 피어 관리 메소드
          • connect
          • peer_reservations_add
          • peer_reservations_del
          • peer_reservations_list
          • peers
        • 상태 및 디버깅 메소드
          • consensus_info
          • feature
          • fetch_info
          • get_counts
          • print
          • validator_info
          • validators
        • rippled 커맨드라인 사용 참조
        • 피어 포트 메소드
          • 상태 확인
          • 피어 크롤러
          • 유효성 검증인 목록 메소드
    • xrp-ledger.toml File
  • Infrastructure
    • 커맨드 라인 사용법(Commandline Usage)
    • Install rippled
      • System Requirements
      • Install on CentOS/RedHat with yum
      • Install on Ubuntu or Debian Linux
      • Update Automatically on Linux
      • Update Manually on CentOS/Red Hat
      • Update Manually on Ubuntu or Debian
      • Build and Run rippled in Reporting Mode
      • Capacity Planning
    • Configure rippled
      • Server Modes
        • Run rippled as a Validator
        • Run rippled as a Stock Server
      • Data Retention
        • Configure Full History
        • 온라인 삭제(Online Deletion)
        • Configure Online Deletion
        • Configure Advisory Deletion
        • 히스토리 샤딩(History Sharding)
        • Configure History Sharding
      • Configure Amendment Voting
      • Test Amendments
      • Configure StatsD
      • Connect Your rippled to a Parallel Network
      • Configure gRPC
      • Enable Public Signing
    • Peering
      • Cluster rippled Servers
      • Configure a Private Server
      • Configure the Peer Crawler
      • Enable Link Compression
      • Forward Ports for Peering
      • Manually Connect to a Specific Peer
      • Set Maximum Number of Peers
      • Use a Peer Reservation
    • Testing and Auditing
      • Start a New Genesis Ledger in Stand-Alone Mode
      • Load a Saved Ledger in Stand-Alone Mode
      • Advance the Ledger in Stand-Alone Mode
    • Troubleshooting
      • Diagnosing Problems with rippled
      • Health Check Interventions
      • Understanding Log Messages
      • rippled Server Doesn't Sync
      • rippled Server is Amendment Blocked
      • rippled Server Won't Start
    • Install Clio on Ubuntu Linux
    • Run a Private Network with Docker
Powered by GitBook
On this page
  • 요구 사항
  • 1. XRP Ledger에 연결하기
  • 2. 들어오는 메시지를 핸들러에 전달하기
  • 3. 계정에 가입하기
  • 4. 들어오는 결제 내용 읽기
  • 다음 단계
  • 기타 프로그래밍 언어
  • 각주
  • See Also
  1. Tutorials
  2. HTTP / Websocket APIs

WebSocket으로 수신 결제 모니터링(Monitor Incoming Payments with WebSocket)

이 튜토리얼은 WebSocket API를 사용하여 들어오는 지불을 모니터링하는 방법을 보여줍니다. 모든 XRP Ledger 거래는 공개적이므로, 누구든지 어떤 주소로든 들어오는 지불을 모니터링할 수 있습니다.

WebSocket은 클라이언트와 서버가 하나의 연결을 열고, 동일한 연결을 통해 양방향으로 메시지를 보낸 후, 명시적으로 닫히거나 연결이 실패할 때까지 열려 있는 모델을 따릅니다. 이는 클라이언트가 각 요청에 대해 새로운 연결을 열고 닫는 HTTP 기반 API 모델(JSON-RPC 및 RESTful API 포함)과 대조적입니다.

Tip:

이 페이지의 예제들은 웹 브라우저에서 원시적으로 실행할 수 있도록 JavaScript를 사용합니다. 만약 JavaScript로 개발하고 있다면, 일부 작업을 단순화하기 위해 xrpl.js 라이브러리를 사용할 수도 있습니다. 이 튜토리얼은 xrpl.js를 사용하지 않고 거래를 모니터링하는 방법을 보여주므로, 기본적으로 XRP Ledger 클라이언트 라이브러리가 없는 다른 프로그래밍 언어로 단계를 번역할 수 있습니다.

요구 사항

  • 이 페이지의 예제들은 JavaScript와 WebSocket 프로토콜을 사용하며, 이들은 모든 주요 현대 브라우저에서 사용 가능합니다. 만약 JavaScript에 대한 지식과 WebSocket 클라이언트가 있는 다른 프로그래밍 언어에 대한 전문 지식이 있다면, 당신의 선택한 언어로 지시사항을 조정하면서 따라갈 수 있습니다.

  • 안정적인 인터넷 연결과 XRP Ledger 서버에 대한 접근이 필요합니다. 내장된 예제들은 Ripple의 공개 서버 풀에 연결합니다. 만약 rippled 또는 클리오 서버를 직접 운영하고 있다면, 그 서버에 로컬로 연결할 수도 있습니다.

  • 올림 오류 없이 XRP 값을 적절하게 처리하기 위해, 64비트 부호 없는 정수에 대한 수학 연산을 수행할 수 있는 숫자 유형에 대한 접근이 필요합니다. 이 튜토리얼의 예제들은 big.js를 사용합니다. 토큰과 작업할 때에는 더욱 더 정밀성이 필요합니다. 자세한 정보는 Currency Precision을 참조하세요.

1. XRP Ledger에 연결하기

들어오는 지불을 모니터링하는 첫 단계는 XRP Ledger, 특히 rippled 서버에 연결하는 것입니다.

다음 JavaScript 코드는 Ripple의 공개 서버 클러스터 중 하나에 연결합니다. 그 다음 콘솔에 메시지를 로그하고, ping 메소드드를 사용하여 요청을 보내고, 서버 측에서 메시지를 받을 때마다 콘솔에 다시 로그를 작성하는 핸들러를 설정합니다.

const socket = new WebSocket('wss://s.altnet.rippletest.net:51233')
socket.addEventListener('open', (event) => {
  // This callback runs when the connection is open
  console.log("Connected!")
  const command = {
    "id": "on_open_ping_1",
    "command": "ping"
  }
  socket.send(JSON.stringify(command))
})
socket.addEventListener('message', (event) => {
  console.log('Got message from server:', event.data)
})
socket.addEventListener('close', (event) => {
  // Use this event to detect when you have become disconnected
  // and respond appropriately.
  console.log('Disconnected...')
})

위 예제는 Ripple의 Test Net의 공개 API 서버 중 하나에 안전한 연결(wss://)을 엽니다. 대신 로컬에서 실행 중인 rippled 서버에 기본 설정으로 연결하려면, 로컬 6006포트에 보안이 안된 연결(ws://)을 열어 다음과 같은 첫 줄을 사용하세요:

const socket = new WebSocket('ws://localhost:6006')

Tip:

기본적으로 로컬 rippled 서버에 연결하면 서버 정보, 그리고 인터넷을 통해 공개 서버에 연결할 때 사용할 수 있는 공개 메소드 등, 관리자 메소드드와 일부 응답에서 관리자 전용 데이터의 전체 세트에 접근할 수 있습니다.

2. 들어오는 메시지를 핸들러에 전달하기

WebSocket 연결은 양방향으로 여러 메시지를 가질 수 있고 요청과 응답 사이에 엄격한 1:1 대응이 없으므로, 들어오는 각 메시지를 어떻게 처리할지 식별해야 합니다. 이를 코딩하는 좋은 모델은 들어오는 메시지를 읽고 각 메시지를 처리하는 데 필요한 코드 경로로 전달하는 "디스패처" 함수를 설정하는 것입니다. 메시지를 적절하게 전달하는 데 도움이 되도록, rippled 서버는 모든 WebSocket 메시지에 type 필드를 제공합니다:

  • 클라이언트 측의 요청에 대한 직접 응답인 메시지의 경우 유형은 문자열 응답입니다. 이 경우 서버는 다음 기능도 제공합니다:

  • 응답 대상 요청에 제공된 ID와 일치하는 ID 필드입니다. (응답이 순서에 따라 도착할 수 있으므로 중요합니다.)

  • API가 요청을 성공적으로 처리했는지 여부를 나타내는 상태 필드입니다. 문자열 값 성공은 성공적인 응답을 나타냅니다. 문자열 값 오류는 오류를 나타냅니다.

Warning:

트랜잭션을 제출할 때 WebSocket 메시지의 최상위 수준에서 성공했다고 해서 트랜잭션 자체가 성공한 것은 아닙니다. 서버가 사용자의 요청을 이해했음을 나타낼 뿐입니다. 트랜잭션의 실제 결과를 조회하려면 트랜잭션 결과 조회를 참조하십시오.

  • 구독에서 보내는 확인할 메일 메시지의 경우 유형은 새 트랜잭션, ledger 또는 유효성 검사 통지 또는 진행 중인 경로 찾기 요청에 대한 확인할 메일 메시지 유형을 나타냅니다. 클라이언트는 이러한 메시지에 가입한 경우에만 메시지를 수신합니다.

Tip: JavaScript용 xrpl.js 라이브러리는 기본적으로 이 단계를 처리합니다. 모든 비동기 API 요청은 약속을 사용하여 응답을 제공하며 클라이언트의 .on(이벤트, 콜백) 메소드를 사용하여 스트림을 수신할 수 있습니다.

다음 JavaScript 코드는 API 요청을 편리한 비동기 Promise로 만들기 위한 도우미 기능을 정의하고, 다른 유형의 메시지를 글로벌 처리기에 매핑하기 위한 인터페이스를 설정합니다:

const AWAITING = {}
const handleResponse = function(data) {
  if (!data.hasOwnProperty("id")) {
    console.error("Got response event without ID:", data)
    return
  }
  if (AWAITING.hasOwnProperty(data.id)) {
    AWAITING[data.id].resolve(data)
  } else {
    console.warn("Response to un-awaited request w/ ID " + data.id)
  }
}

let autoid_n = 0
function api_request(options) {
  if (!options.hasOwnProperty("id")) {
    options.id = "autoid_" + (autoid_n++)
  }

  let resolveHolder;
  AWAITING[options.id] = new Promise((resolve, reject) => {
    // Save the resolve func to be called by the handleResponse function later
    resolveHolder = resolve
    try {
      // Use the socket opened in the previous example...
      socket.send(JSON.stringify(options))
    } catch(error) {
      reject(error)
    }
  })
  AWAITING[options.id].resolve = resolveHolder;
  return AWAITING[options.id]
}

const WS_HANDLERS = {
  "response": handleResponse
  // Fill this out with your handlers in the following format:
  // "type": function(event) { /* handle event of this type */ }
}
socket.addEventListener('message', (event) => {
  const parsed_data = JSON.parse(event.data)
  if (WS_HANDLERS.hasOwnProperty(parsed_data.type)) {
    // Call the mapped handler
    WS_HANDLERS[parsed_data.type](parsed_data)
  } else {
    console.log("Unhandled message from server", event)
  }
})

// Show api_request functionality
async function pingpong() {
  console.log("Ping...")
  const response = await api_request({command: "ping"})
  console.log("Pong!", response)
}
// Add pingpong() to the 'open' listener for socket

3. 계정에 가입하기

거래가 계정에 영향을 미칠 때마다 실시간 알림을 받으려면 가입 방법으로 계정을 가입할 수 있습니다. 사실, 그것은 당신 자신의 계정일 필요는 없습니다. 모든 거래가 공개적이기 때문에, 당신은 어떤 계정이든 가입할 수 있고 심지어는 계정의 조합도 가입할 수 있습니다.

하나 이상의 계정에 가입한 후, 서버는 지정된 계정에 영향을 미치는 각 검증된 트랜잭션에 대해 "type": "transaction" 메시지를 발송합니다. 이를 확인하려면 트랜잭션 메시지에서 true인 "validated"를 찾습니다.

다음 코드 샘플은 테스트 네트워크 수도꼭지의 송신 주소를 구독합니다. 이전 단계의 처리기를 디스패처에 추가하여 이러한 트랜잭션마다 메시지를 기록합니다.

async function do_subscribe() {
  const sub_response = await api_request({
    command:"subscribe",
    accounts: ["rPT1Sjq2YGrBMTttX4GZHjKu9dyfzbpAYe"]
  })
  if (sub_response.status === "success") {
    console.log("Successfully subscribed!")
  } else {
    console.error("Error subscribing: ", sub_response)
  }
}
// Add do_subscribe() to the 'open' listener for socket

const log_tx = function(tx) {
  console.log(tx.transaction.TransactionType + " transaction sent by " +
              tx.transaction.Account +
              "\n  Result: " + tx.meta.TransactionResult +
              " in ledger " + tx.ledger_index +
              "\n  Validated? " + tx.validated)
}
WS_HANDLERS["transaction"] = log_tx

다음 예에서는 다른 창 또는 다른 장치에서 트랜잭션 보낸 사람을 열고 가입한 주소로 트랜잭션을 전송합니다.

4. 들어오는 결제 내용 읽기

계정에 가입하면 해당 계정에 대한 모든 트랜잭션뿐만 아니라 토큰 거래와 같이 계정에 간접적으로 영향을 미치는 트랜잭션에 대한 메시지가 표시됩니다. 계정이 언제 수신된 지불을 받았는지 인식하는 것이 목표라면 트랜잭션 스트림을 필터링하고 실제 전달된 금액을 기준으로 지불을 처리해야 합니다. 다음 정보를 찾습니다:

  • validated field는 트랜잭션의 결과가 최종임을 나타냅니다. 계정에 가입할 때는 항상 이와 같아야 하지만 accounts_proposed 또는 transactions_proposed 스트림에도 가입한 경우 서버는 확인되지 않은 트랜잭션에 대해 동일한 연결에 유사한 메시지를 보냅니다. 예방 조치로 항상 확인된 필드를 확인하는 것이 좋습니다.

  • meta.TransactionResult field는 트랜잭션 결과입니다. 결과가 tesSUCCESS가 아닌 경우 트랜잭션이 실패하고 값을 전달할 수 없습니다.

  • transaction.Account field는 트랜잭션의 보낸 사람입니다. 다른 사람이 보낸 거래만 찾는 경우 이 필드가 사용자의 계정 주소와 일치하는 모든 거래를 무시할 수 있습니다. (본인에게 통화 간 결제가 가능합니다.)

  • transaction.TransactionType field는 트랜잭션의 유형입니다. 계정에 통화를 전달할 수 있는 거래 유형은 다음과 같습니다:

    • 결제 트랜잭션은 XRP 또는 토큰을 전달할 수 있습니다. 트랜잭션을 기준으로 필터링합니다.수신자의 주소를 포함하는 대상 필드이며, 항상 meta.delivered_금액을 사용하여 실제로 결제가 얼마나 전달되었는지 확인합니다. XRP 양은 문자열 형식으로 지정됩니다.

Warning:

대신 transaction.Amount 필드를 사용하면 부분 결제 악용에 취약할 수 있습니다.악의적인 사용자는 이 취약성을 이용하여 사용자를 속여서 사용자가 지불한 금액보다 더 많은 금액을 거래하거나 인출할 수 있습니다.

  • CheckCash 트랜잭션을 통해 다른 계정의 CheckCreate 트랜잭션에서 승인된 돈을 받을 수 있습니다. CheckCash 트랜잭션의 메타데이터를 확인하여 해당 계정이 받은 통화 수를 확인합니다.

    • EscrowFinish 트랜잭션은 이전 EscrowCreate 트랜잭션에서 생성된 에스크로를 완료하여 XRP를 제공할 수 있습니다. EscrowFinish 트랜잭션의 메타데이터를 확인하여 Escrow에서 XRP를 받은 계정과 금액을 확인합니다.

    • OfferCreate 트랜잭션은 귀하의 계정이 이전에 XRP Ledger의 분산 거래소에 배치된 제안을 소비하여 XRP 또는 토큰을 전달할 수 있습니다. 만약 당신이 제안을 하지 않는다면, 당신은 이런 식으로 돈을 받을 수 없습니다. 메타데이터를 보고 계정이 받은 통화, 받은 통화 및 금액을 확인합니다.

    • PaymentChannelClaim 트랜잭션은 결제 채널에서 XRP를 전송할 수 있습니다. 메타데이터를 확인하여 트랜잭션에서 XRP를 받은 계정(있는 경우)을 확인합니다.

    • Payment Channel Fund 트랜잭션은 폐쇄된(만료된) 지불 채널에서 발신자에게 XRP를 반환할 수 있습니다.

  • Meta 필드에는 트랜잭션 메타데이터가 포함되어 있으며, 여기에는 정확히 어느 통화 또는 어느 통화가 전달되었는지가 포함됩니다. 트랜잭션 메타데이터를 이해하는 방법에 대한 자세한 내용은 트랜잭션 결과 조회를 참조하십시오.

다음 샘플 코드는 위의 모든 트랜잭션 유형의 트랜잭션 메타데이터를 조사하여 계정이 받은 XRP 양을 보고합니다:

function CountXRPDifference(affected_nodes, address) {
  // Helper to find an account in an AffectedNodes array and see how much
  // its balance changed, if at all. Fortunately, each account appears at most
  // once in the AffectedNodes array, so we can return as soon as we find it.

  // Note: this reports the net balance change. If the address is the sender,
  // the transaction cost is deducted and combined with XRP sent/received

  for (let i=0; i<affected_nodes.length; i++) {
    if ((affected_nodes[i].hasOwnProperty("ModifiedNode"))) {
      // modifies an existing ledger entry
      let ledger_entry = affected_nodes[i].ModifiedNode
      if (ledger_entry.LedgerEntryType === "AccountRoot" &&
          ledger_entry.FinalFields.Account === address) {
        if (!ledger_entry.PreviousFields.hasOwnProperty("Balance")) {
          console.log("XRP balance did not change.")
        }
        // Balance is in PreviousFields, so it changed. Time for
        // high-precision math!
        const old_balance = new Big(ledger_entry.PreviousFields.Balance)
        const new_balance = new Big(ledger_entry.FinalFields.Balance)
        const diff_in_drops = new_balance.minus(old_balance)
        const xrp_amount = diff_in_drops.div(1e6)
        if (xrp_amount.gte(0)) {
          console.log("Received " + xrp_amount.toString() + " XRP.")
          return
        } else {
          console.log("Spent " + xrp_amount.abs().toString() + " XRP.")
          return
        }
      }
    } else if ((affected_nodes[i].hasOwnProperty("CreatedNode"))) {
      // created a ledger entry. maybe the account just got funded?
      let ledger_entry = affected_nodes[i].CreatedNode
      if (ledger_entry.LedgerEntryType === "AccountRoot" &&
          ledger_entry.NewFields.Account === address) {
        const balance_drops = new Big(ledger_entry.NewFields.Balance)
        const xrp_amount = balance_drops.div(1e6)
        console.log("Received " + xrp_amount.toString() + " XRP (account funded).")
        return
      }
    } // accounts cannot be deleted at this time, so we ignore DeletedNode
  }

  console.log("Did not find address in affected nodes.")
  return
}

function CountXRPReceived(tx, address) {
  if (tx.meta.TransactionResult !== "tesSUCCESS") {
    console.log("Transaction failed.")
    return
  }
  if (tx.transaction.TransactionType === "Payment") {
    if (tx.transaction.Destination !== address) {
      console.log("Not the destination of this payment.")
      return
    }
    if (typeof tx.meta.delivered_amount === "string") {
      const amount_in_drops = new Big(tx.meta.delivered_amount)
      const xrp_amount = amount_in_drops.div(1e6)
      console.log("Received " + xrp_amount.toString() + " XRP.")
      return
    } else {
      console.log("Received non-XRP currency.")
      return
    }
  } else if (["PaymentChannelClaim", "PaymentChannelFund", "OfferCreate",
          "CheckCash", "EscrowFinish"].includes(
          tx.transaction.TransactionType)) {
    CountXRPDifference(tx.meta.AffectedNodes, address)
  } else {
    console.log("Not a currency-delivering transaction type (" +
                tx.transaction.TransactionType + ").")
  }
}

다음 단계

  • 트랜잭션 결과를 조회하여 트랜잭션이 수행한 작업을 정확하게 확인하고 적절하게 대응할 수 있도록 소프트웨어를 구축합니다.

  • 자신의 주소에서 XRP를 전송해 보십시오.

  • 에스크로, 수표 또는 지불 채널과 같은 고급 유형의 트랜잭션을 모니터링하고 수신 알림에 응답합니다.

기타 프로그래밍 언어

많은 프로그래밍 언어에는 WebSocket 연결을 통해 데이터를 보내고 받을 수 있는 라이브러리가 있습니다. JavaScript가 아닌 다른 언어로 Rippled의 WebSocket API와 통신하는 것을 미리 시작하고 싶다면 다음 예를 참조하십시오:

package main

// Connect to the XRPL Ledger using websocket and subscribe to an account
// translation from the JavaScript example to Go
// https://xrpl.org/monitor-incoming-payments-with-websocket.html
// This example uses the Gorilla websocket library to create a websocket client
// install: go get github.com/gorilla/websocket

import (
    "encoding/json"
    "flag"
    "log"
    "net/url"
    "os"
    "os/signal"
    "time"

    "github.com/gorilla/websocket"
)

// websocket address
var addr = flag.String("addr", "s.altnet.rippletest.net:51233", "http service address")

// Payload object
type message struct {
    Command  string   `json:"command"`
    Accounts []string `json:"accounts"`
}

func main() {
    flag.Parse()
    log.SetFlags(0)

    var m message

    // check for interrupts and cleanly close the connection
    interrupt := make(chan os.Signal, 1)
    signal.Notify(interrupt, os.Interrupt)

    u := url.URL{Scheme: "ws", Host: *addr, Path: "/"}
    log.Printf("connecting to %s", u.String())

    // make the connection
    c, _, err := websocket.DefaultDialer.Dial(u.String(), nil)
    if err != nil {
        log.Fatal("dial:", err)
    }
    // on exit close
    defer c.Close()

    done := make(chan struct{})

    // send a subscribe command and a target XRPL account
    m.Command = "subscribe"
    m.Accounts = append(m.Accounts, "rUCzEr6jrEyMpjhs4wSdQdz4g8Y382NxfM")

    // struct to JSON marshalling
    msg, _ := json.Marshal(m)
    // write to the websocket
    err = c.WriteMessage(websocket.TextMessage, []byte(string(msg)))
    if err != nil {
        log.Println("write:", err)
        return
    }

    // read from the websocket
    _, message, err := c.ReadMessage()
    if err != nil {
        log.Println("read:", err)
        return
    }
    // print the response from the XRP Ledger
    log.Printf("recv: %s", message)

    // handle interrupt
    for {
        select {
        case <-done:
            return
        case <-interrupt:
            log.Println("interrupt")

            // Cleanly close the connection by sending a close message and then
            // waiting (with timeout) for the server to close the connection.
            err := c.WriteMessage(websocket.CloseMessage, websocket.FormatCloseMessage(websocket.CloseNormalClosure, ""))
            if err != nil {
                log.Println("write close:", err)
                return
            }
            select {
            case <-done:
            case <-time.After(time.Second):
            }
            return
        }
    }
}
import asyncio
import websockets
import json

# Using client libraries for ASYNC functions and websockets are needed in python.
# To install, use terminal command 'pip install asyncio && pip install websockets'

# Handles incoming messages
async def handler(websocket):
    message = await websocket.recv()
    return message

# Use this to send API requests
async def api_request(options, websocket):
    try:
        await websocket.send(json.dumps(options))
        message = await websocket.recv()
        return json.loads(message)
    except Exception as e:
        return e

# Tests functionality of API_Requst
async def pingpong(websocket):
    command = {
        "id": "on_open_ping_1",
        "command": "ping"
    }
    value = await api_request(command, websocket)
    print(value)

async def do_subscribe(websocket):
    command = await api_request({
        'command': 'subscribe',
        'accounts': ['rPT1Sjq2YGrBMTttX4GZHjKu9dyfzbpAYe']
        }, websocket)

    if command['status'] == 'success':
        print('Successfully Subscribed!')
    else:
        print("Error subscribing: ", command)
    print('Received message from server', await handler(websocket))


async def run():
    # Opens connection to ripple testnet
    async for websocket in websockets.connect('wss://s.altnet.rippletest.net:51233'):
        try:
           await pingpong(websocket)
           await do_subscribe(websocket)
        except websockets.ConnectionClosed:
            print('Disconnected...')

# Runs the webhook on a loop
def main():
    loop = asyncio.get_event_loop()
    loop.run_until_complete(run())
    loop.close()
    print('Restarting Loop')

if __name__ == '__main__':
    main()

Tip:

선택한 프로그래밍 언어가 표시되지 않습니까? 이 페이지의 맨 위에 있는 "Edit on GitHub" 링크를 클릭하고 자신만의 샘플 코드를 제공합니다!

각주

  1. 실제로 HTTP 기반 API를 여러 번 호출할 때 클라이언트와 서버는 여러 요청 및 응답에 대해 동일한 연결을 재사용할 수 있습니다. 이러한 방식을 HTTP 영구 연결 또는 킵얼라이브(keep-alive)라고 합니다. 개발 관점에서 HTTP 기반 API를 사용하는 코드는 기본 연결이 새 것인지 재사용되는지에 관계없이 동일합니다.

See Also

  • Concepts:

  • Tutorials:

  • References:

PreviousHTTP/WebSocket API 사용 시작하기(Get Started Using HTTP / WebSocket APIs)NextTasks

Last updated 1 year ago

- 트랜잭션의 성공 또는 실패가 최종인지 확인하는 방법. (간단한 버전: 트랜잭션이 유효한 대장에 있는 경우, 결과와 메타데이터가 최종적으로 표시됩니다.)

- 메타데이터에 나타나는 메타데이터 형식 및 필드의 요약

- 트랜잭션에 대해 가능한 모든 결과 코드의 테이블입니다.

Transaction Basics
Finality of Results
Reliable Transaction Submission
Look Up Transaction Results
Transaction Types
Transaction Metadata
Transaction Results