Skip to content

Commit

Permalink
Add params models
Browse files Browse the repository at this point in the history
  • Loading branch information
tupui committed Mar 3, 2024
1 parent 7edecdc commit 86e21d2
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 4 deletions.
25 changes: 22 additions & 3 deletions src/soroban/models.py
Original file line number Diff line number Diff line change
@@ -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"]):
Expand Down Expand Up @@ -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]
12 changes: 12 additions & 0 deletions tests/params_invoke.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
[
{
"name": "issuer",
"type": "address",
"value": "CAARHG6ZR6DPHOSH5HAHPJZKXJV6YJVDRYZ7ADGPEC2MQMDUKAQCGNWB"
},
{
"name": "distributor",
"type": "int128",
"value": 10
}
]
19 changes: 18 additions & 1 deletion tests/test_models.py
Original file line number Diff line number Diff line change
@@ -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:
Expand Down Expand Up @@ -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)

0 comments on commit 86e21d2

Please sign in to comment.