From 86e21d272f87c75d1df86459ec1fc0bf613e2266 Mon Sep 17 00:00:00 2001 From: Pamphile Roy Date: Sun, 3 Mar 2024 18:42:07 +0100 Subject: [PATCH] Add params models --- src/soroban/models.py | 25 ++++++++++++++++++++++--- tests/params_invoke.json | 12 ++++++++++++ tests/test_models.py | 19 ++++++++++++++++++- 3 files changed, 52 insertions(+), 4 deletions(-) create mode 100644 tests/params_invoke.json diff --git a/src/soroban/models.py b/src/soroban/models.py index 75e8d8d..4e24064 100644 --- a/src/soroban/models.py +++ b/src/soroban/models.py @@ -1,11 +1,12 @@ import pathlib from typing import Literal -from pydantic import model_validator, HttpUrl +from pydantic import BaseModel, ConfigDict, model_validator, HttpUrl from pydantic_settings import BaseSettings, SettingsConfigDict -from stellar_sdk import Keypair, Network +from stellar_sdk import xdr +from stellar_sdk import Keypair, Network, scval -__all__ = ["Identity", "NetworkConfig"] +__all__ = ["Identity", "NetworkConfig", "Parameter", "Parameters"] def _load_configuration(id: str | pathlib.Path, kind: Literal["identity", "network"]): @@ -72,3 +73,21 @@ def from_network(cls, network: str | pathlib.Path | None = None) -> "NetworkConf fname = _load_configuration(network, "network") network = NetworkConfig(_env_file=fname) return network + + +class Parameter(BaseModel): + model_config = ConfigDict(arbitrary_types_allowed=True) + + name: str + type: str + value: int | float | str | xdr.SCVal + + @model_validator(mode="after") + def value_to_scval(self) -> "Parameter": + if not isinstance(self.value, xdr.SCVal): + self.value = getattr(scval, f"to_{self.type}")(self.value) + return self + + +class Parameters(BaseModel): + args: list[Parameter] diff --git a/tests/params_invoke.json b/tests/params_invoke.json new file mode 100644 index 0000000..0e47c3f --- /dev/null +++ b/tests/params_invoke.json @@ -0,0 +1,12 @@ +[ + { + "name": "issuer", + "type": "address", + "value": "CAARHG6ZR6DPHOSH5HAHPJZKXJV6YJVDRYZ7ADGPEC2MQMDUKAQCGNWB" + }, + { + "name": "distributor", + "type": "int128", + "value": 10 + } +] diff --git a/tests/test_models.py b/tests/test_models.py index c42cc62..bdd7408 100644 --- a/tests/test_models.py +++ b/tests/test_models.py @@ -1,8 +1,9 @@ +import json import pathlib import pytest import soroban -from stellar_sdk import Keypair +from stellar_sdk import Keypair, scval class TestIdentity: @@ -37,3 +38,19 @@ class TestNetworkConfig: def test_from_network(self): testnet = pathlib.Path(__file__).parent / "testnet.toml" soroban.NetworkConfig.from_network(network=testnet) + + +class TestParams: + + def test_parameter(self): + args = {"name": "distributor", "type": "int128", "value": 10} + soroban.Parameter(**args) + + args = {"name": "distributor", "type": "int128", "value": scval.to_int128(10)} + soroban.Parameter(**args) + + def test_parameters(self): + fname = pathlib.Path(__file__).parent / "params_invoke.json" + with open(fname, "rb") as fd: + args = json.load(fd) + soroban.Parameters(args=args)