import os
from decimal import Decimal
from xrpl.models.currencies import (
IssuedCurrency,
XRP,
)
from xrpl.utils import (
drops_to_xrp,
get_balance_changes,
xrp_to_drops,
)
from account import XrplAccount
from transaction import XrplTransaction
from request import XrplRequest
from utils import Logger
2. 스크립트 실행 디렉토리 설정
스크립트의 현재 위치를 기반으로 로그 파일과 지갑 정보와 같은 다른 리소스의 경로를 찾기 위해 사용됩니다.
이 코드는 XRP Ledger의 오더북에서 특정 화폐 쌍에 대한 정보를 가져옵니다. 여기서 we_want["currency"]는 원하는 화폐, we_spend["currency"]는 지불할 화폐를 나타냅니다.
6. 오더북 offer 분석
오더북에서 받아온 offer을 분석하여 원하는 조건에 부합하는 offer이 있는지 확인합니다.
offers = orderbook_info.get("offers", [])
want_amt = Decimal(we_want["value"])
running_total = Decimal(0)
if len(offers) == 0:
logger.log("매칭 오더북에 offer이 없습니다. 즉시 실행될 가능성이 낮습니다.")
오더북에서 가져온 offer을 분석합니다.
offers에는 오더북의 offer 목록이 들어 있습니다.
want_amt는 원하는 화폐의 금액을 나타냅니다.
7. offer 목록 순회하며 조건 확인하기
else:
for o in offers:
if Decimal(o["quality"]) <= proposed_quality:
logger.log(
f"일치하는 제안이 발견되었고, {o.get('owner_funds')} "
f"{we_want['currency']}로 자금이 마련되었습니다."
)
running_total += Decimal(o.get("owner_funds", Decimal(0)))
if running_total >= want_amt:
logger.log("전체 제안이 즉시 체결될 가능성이 높습니다.")
break
else:
logger.log("나머지 주문은 너무 비쌉니다.")
break
offer 목록을 순회하며 각 offer의 quality가 원하는 quality보다 낮은지 확인합니다.
만약 offer의 품질이 원하는 품질보다 낮다면, 그 offer의 자금을 running_total에 더합니다.
offers_affected = 0
for affnode in result["meta"]["AffectedNodes"]:
...
15. 거래 결과 분석
트랜잭션 결과의 메타 데이터를 분석하여 영향을 받은 노드와 offer의 상태를 확인하고 로깅합니다.
if "ModifiedNode" in affnode:
if affnode["ModifiedNode"]["LedgerEntryType"] == "Offer":
# 일반적으로 Offer 유형의 ModifiedNode는
# 이전 Offer가 이 Offer에 의해 부분적으로 소비되었음을 나타냅니다.
offers_affected += 1
elif "DeletedNode" in affnode:
if affnode["DeletedNode"]["LedgerEntryType"] == "Offer":
# 제거된 Offer는 완전히 소비되었거나,
# 만료되거나 또는 자금이 부족한 것으로 확인되었을 수 있습니다.
offers_affected += 1
elif "CreatedNode" in affnode:
if affnode["CreatedNode"]["LedgerEntryType"] == "RippleState":
logger.log("Trust line을 생성했습니다.")
elif affnode["CreatedNode"]["LedgerEntryType"] == "Offer":
offer = affnode["CreatedNode"]["NewFields"]
logger.log(
f"{offer['Account']}의 소유인 Offer가 생성되었습니다. "
f"TakerGets={amt_str(offer['TakerGets'])} 및 "
f"TakerPays={amt_str(offer['TakerPays'])}."
)
logger.log(f"일치하는 Offer {offers_affected}개 수정 또는 제거")