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

Included sample python code for DIDs and Price Oracles #2932

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
30 changes: 30 additions & 0 deletions _code-samples/did/py/account_did.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
from xrpl.models import LedgerEntry
from xrpl.clients import JsonRpcClient


# connect to the xrpl via a client
print("Connecting to client")
client = JsonRpcClient("https://s.altnet.rippletest.net:51234")
print("connected!!!")


# address of an account that has an existing DID
account_did_creator = "rQB1cBMMyFXshFQd6cj3eg7vSJZtYb6d8e"

# build the request for the account's DID
req = LedgerEntry(ledger_index="validated", did=account_did_creator)

# submit request and awaiting result
print("submitting request")
response = client.request(req)
result = response.result


# parse result
if "index" in result and "Account" in result["node"]:
print(f'DID index: {result["node"]["index"]}')
print(f'DID Document: {result["node"]["DIDDocument"]}')
print(f'Data: {result["node"]["Data"]}')
print(f'URI: {result["node"]["URI"]}')
else:
print("No DID found for this account")
33 changes: 33 additions & 0 deletions _code-samples/did/py/did_delete.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
from xrpl.models import DIDDelete
from xrpl.clients import JsonRpcClient
from xrpl.wallet import Wallet
from xrpl.transaction import submit_and_wait


# connect to the xrpl via a client
print("Connecting to client")
client = JsonRpcClient("https://s.altnet.rippletest.net:51234")
print("connected!!!")


# restore an account that has an existing DID
account_did_creator = Wallet.from_seed(seed="sEdTP3iaY4PE9vYm2LBCsrY4LaTpU7m")


# define the account DIDDelete transaction
did_delete_txn = DIDDelete(account=account_did_creator.address)

# sign, submit the did delete transaction and wait for result
print("signed and submitting did delete transaction. awaiting response...")
did_delete_response = submit_and_wait(
transaction=did_delete_txn,
wallet=account_did_creator,
client=client,
)

# Parse response for result
did_delete_result = did_delete_response.result

# Print result and transaction hash
print(did_delete_result["meta"]["TransactionResult"])
print(did_delete_result["hash"])
51 changes: 51 additions & 0 deletions _code-samples/did/py/did_set.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
# This allows you to create or update a DID
from xrpl.models import DIDSet
from xrpl.clients import JsonRpcClient
from xrpl.wallet import generate_faucet_wallet
from xrpl.transaction import submit_and_wait
from xrpl.utils import str_to_hex


# connect to the xrpl via a client
print("Connecting to client")
client = JsonRpcClient("https://s.altnet.rippletest.net:51234")
print("connected!!!")


# define/generate a wallet / account
account_did_creator = generate_faucet_wallet(client)

# define the document associated with the DID
document = "did:example:123#public-key-0"

# define the data associated with the DID
# The public attestations of identity credentials associated with the DID.
data = "did:example:123#key-1"

# define the uri associated with the DID
# The Universal Resource Identifier associated with the DID.
uri = "https://example.did.com/123"


# build DID SET transaction
# str_to_hex() converts the inputted string to blockchain understandable hexadecimal
did_set_txn = DIDSet(
account=account_did_creator.address,
did_document=str_to_hex(document),
data=str_to_hex(data),
uri=str_to_hex(uri),
)


# sign, submit the transaction and wait for the response
print("siging and submitting the transaction, awaiting a response")
did_set_txn_response = submit_and_wait(
transaction=did_set_txn, client=client, wallet=account_did_creator
)

# Parse response for result
did_set_txn_result = did_set_txn_response.result

# Print result and transaction hash
print(did_set_txn_result["meta"]["TransactionResult"])
print(did_set_txn_result["hash"])
1 change: 1 addition & 0 deletions _code-samples/price_oracles/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# Create, Update and Delete Price Oracles
77 changes: 77 additions & 0 deletions _code-samples/price_oracles/py/account_price_oracles.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
# neccesary imports
import datetime
from xrpl.clients import JsonRpcClient
from xrpl.models import AccountObjects, AccountObjectType


print("connecting to the test network")
# Connect to XRPL test network
client = JsonRpcClient("https://s.altnet.rippletest.net:51234")
print("connected!!!")


# define the account we are going to query
oracle_creator = "rBoSibkbwaAUEpkehYixQrXp4AqZez9WqA"

# define array for holding oracles an account has created
oracles_ = []

# build the request object
req = AccountObjects(
account=oracle_creator,
ledger_index="validated",
type=AccountObjectType.ORACLE,
)

# mak the request object
response = client.request(req)

# return the result
result = response.result

# parse the result and print
if "account_objects" in result:
oracles = result["account_objects"]
for oracle in oracles:
oracle_data = {}
price_data_ = []
oracle_data["oracle_id"] = oracle["index"]
oracle_data["owner"] = oracle["Owner"]
oracle_data["provider"] = oracle["Provider"]
oracle_data["asset_class"] = oracle["AssetClass"]
oracle_data["uri"] = oracle["URI"] if "URI" in oracle else ""
oracle_data["last_update_time"] = (
str(datetime.datetime.fromtimestamp(oracle["LastUpdateTime"]))
if "LastUpdateTime" in oracle
else ""
)
oracle_data["price_data_series"] = (
oracle["PriceDataSeries"] if "PriceDataSeries" in oracle else []
)

# sort price data series if any
if "PriceDataSeries" in oracle and len(oracle["PriceDataSeries"]) > 0:
price_data_series = oracle["PriceDataSeries"]
for price_data_serie in price_data_series:
price_data = {}
price_data["base_asset"] = price_data_serie["PriceData"]["BaseAsset"]

price_data["quote_asset"] = price_data_serie["PriceData"]["QuoteAsset"]

price_data["scale"] = (
price_data_serie["PriceData"]["Scale"]
if "Scale" in price_data_serie["PriceData"]
else ""
)
price_data["asset_price"] = (
price_data_serie["PriceData"]["AssetPrice"]
if "AssetPrice" in price_data_serie["PriceData"]
else ""
)

price_data_.append(price_data)
oracle_data["price_data_series"] = price_data_
oracles_.append(oracle_data)


print(oracles_)
78 changes: 78 additions & 0 deletions _code-samples/price_oracles/py/create_price_oracle.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
# neccesary imports
import datetime
from xrpl.wallet import generate_faucet_wallet
from xrpl.clients import JsonRpcClient
from xrpl.models import (
OracleSet,
)
from xrpl.transaction import submit_and_wait, sign_and_submit
from xrpl.models.transactions.oracle_set import PriceData

from xrpl.utils import str_to_hex


print("connecting to the test network")
# Connect to XRPL test network
client = JsonRpcClient("https://s.altnet.rippletest.net:51234")
print("connected!!!")

# create demo wallet
oracle_creator = generate_faucet_wallet(client=client)

# define the oracle document id
# this should be stored offline as the blockchain doesn't retrieve it in requests
oracle_document_id = 1

# define the provider's name and convert to hexadecimal e.g: band, chainlink etc
provider = str_to_hex("provider")

# define the uri of the provider and convert to hexadecimal
uri = str_to_hex("sampleprovider.com")


# define the last update time of the price data being passed to the oracle as a timestamp
# max time into the future is 5 minutes and max time into the past is 4 minutes from the current time
# we'll use the current date time for this
last_update_time = int(datetime.datetime.now().timestamp())


# define the asset class and convert to hexadecimal
# Describes the type of asset, such as "currency", "commodity", or "index".
asset_class = str_to_hex("currency")


# create a price data object, that will be tracked by the oracle
pd = PriceData(
base_asset="BTC",
quote_asset="USD",
asset_price=1000,
scale=4,
)

# create an array of up to 10 Price data objects
price_data_array = [pd]


print("building transaction")
# create price oracle transaction
oracle_set = OracleSet(
account=oracle_creator.address,
oracle_document_id=oracle_document_id,
provider=provider,
uri=uri,
last_update_time=last_update_time,
asset_class=asset_class,
price_data_series=price_data_array,
)


print("signing and submitting transaction, awaiting response")
# sign, submit, and wait forthe transaction result
oracle_set_txn_response = submit_and_wait(
transaction=oracle_set, client=client, wallet=oracle_creator
)


# print the result and transaction hash
print(oracle_set_txn_response.result["meta"]["TransactionResult"])
print(oracle_set_txn_response.result["hash"])
44 changes: 44 additions & 0 deletions _code-samples/price_oracles/py/delete_price_oracle.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
# neccesary imports
from xrpl.wallet import Wallet
from xrpl.clients import JsonRpcClient
from xrpl.models import (
OracleDelete,
)
from xrpl.transaction import submit_and_wait


print("connecting to the test network")
# Connect to XRPL test network
client = JsonRpcClient("https://s.altnet.rippletest.net:51234")
print("connected!!!")

# create demo wallet or use an existing one as created in the oracle set transaction
oracle_creator = Wallet.from_seed(seed="sEdTXKysakqYD1eRaBmYAGvmwFRbNWa")

# define the oracle document id
# this should be stored offline as the blockchain doesn't retrieve it in requests
oracle_document_id = 1


print("building transaction")
# create price oracle delete transaction
oracle_set = OracleDelete(
account=oracle_creator.address,
oracle_document_id=oracle_document_id,
)


print("signing and submitting transaction, awaiting response")
# sign, submit and wait for transaction result
oracle_set_txn_response = submit_and_wait(
transaction=oracle_set, client=client, wallet=oracle_creator
)


# print the result and transaction hash
print(oracle_set_txn_response.result["meta"]["TransactionResult"])
print(oracle_set_txn_response.result["hash"])

# check if the transaction was successfull
if oracle_set_txn_response.result["meta"]["TransactionResult"] == "tesSUCCESS":
print("oracle deleted succesfully")