Skip to content

Commit 86e21d2

Browse files
committed
Add params models
1 parent 7edecdc commit 86e21d2

3 files changed

Lines changed: 52 additions & 4 deletions

File tree

src/soroban/models.py

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
import pathlib
22
from typing import Literal
33

4-
from pydantic import model_validator, HttpUrl
4+
from pydantic import BaseModel, ConfigDict, model_validator, HttpUrl
55
from pydantic_settings import BaseSettings, SettingsConfigDict
6-
from stellar_sdk import Keypair, Network
6+
from stellar_sdk import xdr
7+
from stellar_sdk import Keypair, Network, scval
78

8-
__all__ = ["Identity", "NetworkConfig"]
9+
__all__ = ["Identity", "NetworkConfig", "Parameter", "Parameters"]
910

1011

1112
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
7273
fname = _load_configuration(network, "network")
7374
network = NetworkConfig(_env_file=fname)
7475
return network
76+
77+
78+
class Parameter(BaseModel):
79+
model_config = ConfigDict(arbitrary_types_allowed=True)
80+
81+
name: str
82+
type: str
83+
value: int | float | str | xdr.SCVal
84+
85+
@model_validator(mode="after")
86+
def value_to_scval(self) -> "Parameter":
87+
if not isinstance(self.value, xdr.SCVal):
88+
self.value = getattr(scval, f"to_{self.type}")(self.value)
89+
return self
90+
91+
92+
class Parameters(BaseModel):
93+
args: list[Parameter]

tests/params_invoke.json

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
[
2+
{
3+
"name": "issuer",
4+
"type": "address",
5+
"value": "CAARHG6ZR6DPHOSH5HAHPJZKXJV6YJVDRYZ7ADGPEC2MQMDUKAQCGNWB"
6+
},
7+
{
8+
"name": "distributor",
9+
"type": "int128",
10+
"value": 10
11+
}
12+
]

tests/test_models.py

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1+
import json
12
import pathlib
23

34
import pytest
45
import soroban
5-
from stellar_sdk import Keypair
6+
from stellar_sdk import Keypair, scval
67

78

89
class TestIdentity:
@@ -37,3 +38,19 @@ class TestNetworkConfig:
3738
def test_from_network(self):
3839
testnet = pathlib.Path(__file__).parent / "testnet.toml"
3940
soroban.NetworkConfig.from_network(network=testnet)
41+
42+
43+
class TestParams:
44+
45+
def test_parameter(self):
46+
args = {"name": "distributor", "type": "int128", "value": 10}
47+
soroban.Parameter(**args)
48+
49+
args = {"name": "distributor", "type": "int128", "value": scval.to_int128(10)}
50+
soroban.Parameter(**args)
51+
52+
def test_parameters(self):
53+
fname = pathlib.Path(__file__).parent / "params_invoke.json"
54+
with open(fname, "rb") as fd:
55+
args = json.load(fd)
56+
soroban.Parameters(args=args)

0 commit comments

Comments
 (0)