Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for PriceOracles #701

Merged
merged 32 commits into from
May 9, 2024
Merged
Show file tree
Hide file tree
Changes from 14 commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
30b6522
include SetOracle and DeleteOracle transactions
ckeshava Apr 18, 2024
5764dc4
update the definitions.json with Oracle LedgerEntry types, new SField…
ckeshava Apr 18, 2024
c8a2bb3
[WIP] rename transactions to OracleSet, OracleDelete to maintain comp…
ckeshava Apr 19, 2024
6919e22
update the CI/CD file to latest rippled docker image.
ckeshava Apr 22, 2024
1545fee
snippets test for set, delete oracle
ckeshava Apr 29, 2024
e811c41
[WIP] updates to the SetOracle integration test
ckeshava Apr 30, 2024
d2237c2
GetAggregatePrice request implementation, unit and snippet tests.
ckeshava Apr 30, 2024
6a16e6e
LedgerEntry unit tests -- validate the behavior of PriceOracle objects
ckeshava Apr 30, 2024
d698d28
integration tests for OracleSet, OracleDelete transactions
ckeshava Apr 30, 2024
dad085f
Fix: use int data type, not str for oracle_document_id
ckeshava Apr 30, 2024
2af51b1
update changelog
ckeshava Apr 30, 2024
170dafd
update rippled version docker image
ckeshava May 1, 2024
bcec52c
fix: Use TypedDict instead of NestedModel to represent OracleInfo
ckeshava May 1, 2024
df462ba
address Mayukha's comments: rename files and variables to match defs.…
ckeshava May 1, 2024
7256c4c
include LedgerEntry verification in oracles snippets test
ckeshava May 4, 2024
9c7ce5e
address review comments by mayukha and omar
ckeshava May 6, 2024
9d1b476
remove snippets tests for PriceOracle feature
ckeshava May 6, 2024
23b0ef0
Update CHANGELOG.md
ckeshava May 6, 2024
d08ba7f
add unique values to each client run
mvadari May 6, 2024
c044ce7
Merge branch 'async-value' of https://github.com/XRPLF/xrpl-py into p…
ckeshava May 6, 2024
1a02a58
remove the usage of random.randint in OracleSet integraton tests
ckeshava May 6, 2024
7dd3f17
Merge branch 'poracle' of https://github.com/ckeshava/xrpl-py into po…
ckeshava May 6, 2024
958ee1a
Update tests/integration/transactions/test_delete_oracle.py
ckeshava May 7, 2024
915ff35
address review comments
ckeshava May 7, 2024
3957867
use "is not None" to check for non-existence of an optional field
ckeshava May 7, 2024
8251981
fixed the error message in OracleSet validation
ckeshava May 8, 2024
56ff610
Update tests/unit/models/requests/test_ledger_entry.py
ckeshava May 8, 2024
7f39f50
Update tests/unit/models/requests/test_ledger_entry.py
ckeshava May 8, 2024
db3d27f
rename test files to match transaction names
ckeshava May 8, 2024
72afddf
validate the exception message in negative test cases
ckeshava May 9, 2024
49370f1
include validity checks on LastUpdateTime
ckeshava May 9, 2024
e98980b
Merge branch 'main' into poracle
ckeshava May 9, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions .ci-config/rippled.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -170,3 +170,11 @@ fixNFTokenRemint
# 2.0.0-b4 Amendments
XChainBridge
DID
# 2.2.0-b3 Amendments
fixNFTokenReserve
fixInnerObjTemplate
fixAMMOverflowOffer
PriceOracle
fixEmptyDID
fixXChainRewardRounding
fixPreviousTxnID
2 changes: 1 addition & 1 deletion .github/workflows/integration_test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ name: Integration test

env:
POETRY_VERSION: 1.4.2
RIPPLED_DOCKER_IMAGE: rippleci/rippled:2.0.0-b4
RIPPLED_DOCKER_IMAGE: rippleci/rippled:2.2.0-b3

on:
push:
Expand Down
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
## [[Unreleased]]
- Included `ctid` field in the `tx` request.

### Added
- Support for the PriceOracle amendment (XLS-47d).
ckeshava marked this conversation as resolved.
Show resolved Hide resolved

### Fixed
- Added support for `XChainModifyBridge` flag maps (fixing an issue with `NFTokenCreateOffer` flag names)
- Fixed `XChainModifyBridge` validation to allow just clearing of `MinAccountCreateAmount`
Expand Down
2 changes: 1 addition & 1 deletion CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ poetry run poe test_unit
To run integration tests, you'll need a standalone rippled node running with WS port `6006` and JSON RPC port `5005`. You can run a docker container for this:

```bash
docker run -p 5005:5005 -p 6006:6006 --interactive -t --volume $PWD/.ci-config:/opt/ripple/etc/ --platform linux/amd64 rippleci/rippled:2.0.0-b4 /opt/ripple/bin/rippled -a --conf /opt/ripple/etc/rippled.cfg
docker run -p 5005:5005 -p 6006:6006 --interactive -t --volume $PWD/.ci-config:/opt/ripple/etc/ --platform linux/amd64 rippleci/rippled:2.2.0-b3 /opt/ripple/bin/rippled -a --conf /opt/ripple/etc/rippled.cfg
```

Breaking down the command:
Expand Down
65 changes: 65 additions & 0 deletions snippets/oracle.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
"""Example of how we can create, update and delete oracles"""
import time

from xrpl.clients import JsonRpcClient
from xrpl.models.transactions.oracle_delete import OracleDelete
from xrpl.models.transactions.oracle_set import OracleSet, PriceData
from xrpl.transaction.reliable_submission import submit_and_wait
from xrpl.utils import str_to_hex
from xrpl.wallet import generate_faucet_wallet

# Create a client to connect to the dev-network
client = JsonRpcClient("https://s.devnet.rippletest.net:51234")

wallet = generate_faucet_wallet(client, debug=True)


_PROVIDER = str_to_hex("provider")
_ASSET_CLASS = str_to_hex("currency")
_ORACLE_DOC_ID = 1

create_tx = OracleSet(
account=wallet.address,
oracle_document_id=_ORACLE_DOC_ID,
provider=_PROVIDER,
asset_class=_ASSET_CLASS,
last_update_time=int(time.time()),
price_data_series=[
PriceData(base_asset="XRP", quote_asset="USD", asset_price=740, scale=1),
PriceData(base_asset="BTC", quote_asset="EUR", asset_price=100, scale=2),
],
)

response = submit_and_wait(create_tx, client, wallet)
# TODO: Keshava
# print(response.result['meta']['TransactionResult'] == 'tesSUCCESS') # does not work
print(
"Result of SetOracle transaction: " + response.result["meta"]["TransactionResult"]
)

# update the oracle data
update_tx = OracleSet(
account=wallet.address,
oracle_document_id=_ORACLE_DOC_ID,
last_update_time=int(time.time()),
price_data_series=[
PriceData(base_asset="XRP", quote_asset="USD", asset_price=742, scale=1),
PriceData(base_asset="BTC", quote_asset="EUR", asset_price=103, scale=2),
],
)
response = submit_and_wait(update_tx, client, wallet)

print(
"Result of the Update Oracle transaction: "
+ response.result["meta"]["TransactionResult"]
)

ckeshava marked this conversation as resolved.
Show resolved Hide resolved

# delete the oracle
delete_tx = OracleDelete(account=wallet.address, oracle_document_id=_ORACLE_DOC_ID)
response = submit_and_wait(delete_tx, client, wallet)

print(
"Result of DeleteOracle transaction: "
+ response.result["meta"]["TransactionResult"]
)
48 changes: 48 additions & 0 deletions snippets/oracle_aggregate.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
"""Snippet demonstrates obtaining aggregate statistics of PriceOracles"""
import time

from xrpl.clients import JsonRpcClient
from xrpl.models.requests.get_aggregate_price import GetAggregatePrice, OracleInfo
from xrpl.models.transactions.oracle_set import OracleSet, PriceData
from xrpl.transaction.reliable_submission import submit_and_wait
from xrpl.utils import str_to_hex
from xrpl.wallet import generate_faucet_wallet

# Create a client to connect to the dev-network
client = JsonRpcClient("https://s.devnet.rippletest.net:51234")

_PROVIDER = str_to_hex("provider")
_ASSET_CLASS = str_to_hex("currency")

# list stores the (account, oracle_document_id) information
oracle_info = []

for i in range(10):
# new (pseudo-random) addresses are generated
wallet = generate_faucet_wallet(client, debug=True)
create_tx = OracleSet(
account=wallet.address,
oracle_document_id=i,
provider=_PROVIDER,
asset_class=_ASSET_CLASS,
last_update_time=int(time.time()),
price_data_series=[
PriceData(
base_asset="XRP", quote_asset="USD", asset_price=740 + i, scale=1
),
PriceData(
base_asset="BTC", quote_asset="EUR", asset_price=100 + i, scale=2
),
],
)

response = submit_and_wait(create_tx, client, wallet)

# store the (account, oracle_document_id) for future use
oracle_info.append(OracleInfo(account=wallet.address, oracle_document_id=i))

get_agg_request = GetAggregatePrice(
base_asset="XRP", quote_asset="USD", oracles=oracle_info
)
response = client.request(get_agg_request)
print(response)
61 changes: 61 additions & 0 deletions tests/integration/transactions/test_delete_oracle.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import random
import time

from tests.integration.integration_test_case import IntegrationTestCase
from tests.integration.it_utils import (
sign_and_reliable_submission_async,
test_async_and_sync,
)
from tests.integration.reusable_values import WALLET
from xrpl.models import AccountObjects, AccountObjectType, OracleDelete, OracleSet
from xrpl.models.response import ResponseStatus
from xrpl.models.transactions.oracle_set import PriceData
from xrpl.utils import str_to_hex

_PROVIDER = str_to_hex("chainlink")
_ASSET_CLASS = str_to_hex("currency")


class TestDeleteOracle(IntegrationTestCase):
@test_async_and_sync(globals())
async def test_basic(self, client):
oracle_id = random.randint(100, 10000)

# Create PriceOracle, to be deleted later
tx = OracleSet(
account=WALLET.address,
# if oracle_document_id is not modified, the (sync, async) +
# (json, websocket) combination of integration tests will update the same
# oracle object using identical "LastUpdateTime". Updates to an oracle must
# be more recent than its previous LastUpdateTime
oracle_document_id=oracle_id,
ckeshava marked this conversation as resolved.
Show resolved Hide resolved
provider=_PROVIDER,
asset_class=_ASSET_CLASS,
last_update_time=int(time.time()),
ckeshava marked this conversation as resolved.
Show resolved Hide resolved
price_data_series=[
PriceData(
base_asset="XRP", quote_asset="USD", asset_price=740, scale=1
),
PriceData(
base_asset="BTC", quote_asset="EUR", asset_price=100, scale=2
),
],
)
response = await sign_and_reliable_submission_async(tx, WALLET, client)
self.assertEqual(response.status, ResponseStatus.SUCCESS)
self.assertEqual(response.result["engine_result"], "tesSUCCESS")

# Create PriceOracle to delete
tx = OracleDelete(
account=WALLET.address,
oracle_document_id=oracle_id,
)
response = await sign_and_reliable_submission_async(tx, WALLET, client)
self.assertEqual(response.status, ResponseStatus.SUCCESS)
self.assertEqual(response.result["engine_result"], "tesSUCCESS")

# confirm that the PriceOracle was actually deleted
account_objects_response = await client.request(
AccountObjects(account=WALLET.address, type=AccountObjectType.ORACLE)
)
self.assertEqual(len(account_objects_response.result["account_objects"]), 0)
52 changes: 52 additions & 0 deletions tests/integration/transactions/test_set_oracle.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import random
import time

from tests.integration.integration_test_case import IntegrationTestCase
from tests.integration.it_utils import (
sign_and_reliable_submission_async,
test_async_and_sync,
)
from tests.integration.reusable_values import WALLET
from xrpl.models import AccountObjects, AccountObjectType, OracleSet
from xrpl.models.response import ResponseStatus
from xrpl.models.transactions.oracle_set import PriceData
from xrpl.utils import str_to_hex

_PROVIDER = str_to_hex("provider")
_ASSET_CLASS = str_to_hex("currency")


class TestSetOracle(IntegrationTestCase):
@test_async_and_sync(globals())
async def test_all_fields(self, client):
tx = OracleSet(
account=WALLET.address,
# if oracle_document_id is not modified, the (sync, async) +
# (json, websocket) combination of integration tests will update the same
# oracle object using identical "LastUpdateTime". Updates to an oracle must
# be more recent than its previous LastUpdateTime
oracle_document_id=random.randint(100, 300),
ckeshava marked this conversation as resolved.
Show resolved Hide resolved
provider=_PROVIDER,
asset_class=_ASSET_CLASS,
last_update_time=int(time.time()),
price_data_series=[
PriceData(
base_asset="XRP", quote_asset="USD", asset_price=740, scale=1
),
PriceData(
base_asset="BTC", quote_asset="EUR", asset_price=100, scale=2
),
],
)
response = await sign_and_reliable_submission_async(tx, WALLET, client)
self.assertEqual(response.status, ResponseStatus.SUCCESS)
self.assertEqual(response.result["engine_result"], "tesSUCCESS")

# confirm that the PriceOracle was actually created
account_objects_response = await client.request(
AccountObjects(account=WALLET.address, type=AccountObjectType.ORACLE)
)

# subsequent integration tests (sync/async + json/websocket) add one
# oracle object to the account
self.assertTrue(len(account_objects_response.result["account_objects"]) > 0)
76 changes: 76 additions & 0 deletions tests/unit/models/requests/test_get_aggregate_price.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
from unittest import TestCase

from xrpl.models import XRPLModelException
from xrpl.models.requests import GetAggregatePrice
from xrpl.models.requests.get_aggregate_price import OracleInfo

_ACCT_STR_1 = "rBwHKFS534tfG3mATXSycCnX8PAd3XJswj"
_ORACLE_DOC_ID_1 = 1

_ACCT_STR_2 = "rDMKwhm13oJBxBgiWS2SheZhKT5nZP8kez"
_ORACLE_DOC_ID_2 = 2


class TestGetAggregatePrice(TestCase):
def test_invalid_requests(self):
"""Unit test to validate invalid requests"""
with self.assertRaises(XRPLModelException):
# oracles array must contain at least one element
GetAggregatePrice(
base_asset="USD",
quote_asset="XRP",
oracles=[],
)

with self.assertRaises(XRPLModelException):
# base_asset is missing in the request
GetAggregatePrice(
quote_asset="XRP",
oracles=[
OracleInfo(
account=_ACCT_STR_1, oracle_document_id=_ORACLE_DOC_ID_1
),
OracleInfo(
account=_ACCT_STR_2, oracle_document_id=_ORACLE_DOC_ID_2
),
],
)

with self.assertRaises(XRPLModelException):
# quote_asset is missing in the request
GetAggregatePrice(
base_asset="USD",
oracles=[
OracleInfo(
account=_ACCT_STR_1, oracle_document_id=_ORACLE_DOC_ID_1
),
OracleInfo(
account=_ACCT_STR_2, oracle_document_id=_ORACLE_DOC_ID_2
),
],
)

def test_valid_request(self):
"""Unit test for validating archetypical requests"""
request = GetAggregatePrice(
base_asset="USD",
quote_asset="XRP",
oracles=[
OracleInfo(account=_ACCT_STR_1, oracle_document_id=_ORACLE_DOC_ID_1),
OracleInfo(account=_ACCT_STR_2, oracle_document_id=_ORACLE_DOC_ID_2),
],
)
self.assertTrue(request.is_valid())

# specifying trim and time_threshold value
request = GetAggregatePrice(
base_asset="USD",
quote_asset="XRP",
oracles=[
OracleInfo(account=_ACCT_STR_1, oracle_document_id=_ORACLE_DOC_ID_1),
OracleInfo(account=_ACCT_STR_2, oracle_document_id=_ORACLE_DOC_ID_2),
],
trim=20,
time_threshold=10,
)
self.assertTrue(request.is_valid())
35 changes: 34 additions & 1 deletion tests/unit/models/requests/test_ledger_entry.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

from xrpl.models import XRP, LedgerEntry, XChainBridge
from xrpl.models.exceptions import XRPLModelException
from xrpl.models.requests.ledger_entry import RippleState
from xrpl.models.requests.ledger_entry import Oracle, RippleState


class TestLedgerEntry(TestCase):
Expand Down Expand Up @@ -119,3 +119,36 @@ def test_has_multiple_query_params_is_invalid(self):
index="hello",
account_root="hello",
)

# fetch a valid PriceOracle object
def test_get_priceoracle(self):
ckeshava marked this conversation as resolved.
Show resolved Hide resolved
# oracle_document_id is specified as uint
req = LedgerEntry(
oracle=Oracle(
account="rB6XJbxKx2oBSK1E3Hvh7KcZTCCBukWyhv",
oracle_document_id=1,
),
)
self.assertTrue(req.is_valid())

# oracle_document_id is specified as string
req = LedgerEntry(
oracle=Oracle(
account="rB6XJbxKx2oBSK1E3Hvh7KcZTCCBukWyhv",
oracle_document_id="1",
),
)
self.assertTrue(req.is_valid())

def test_invalid_priceoracle_object(self):
ckeshava marked this conversation as resolved.
Show resolved Hide resolved
# missing oracle_document_id
with self.assertRaises(XRPLModelException):
LedgerEntry(
oracle=Oracle(account="rB6XJbxKx2oBSK1E3Hvh7KcZTCCBukWyhv"),
)

# missing account information
with self.assertRaises(XRPLModelException):
LedgerEntry(
oracle=Oracle(oracle_document_id=1),
)
Loading
Loading