Skip to content

Commit 621c1ad

Browse files
committed
Allow vec in parameter value
1 parent b2d3e96 commit 621c1ad

3 files changed

Lines changed: 47 additions & 6 deletions

File tree

src/soroban/models.py

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -75,18 +75,28 @@ def from_network(cls, network: str | pathlib.Path | None = None) -> "NetworkConf
7575
return network
7676

7777

78-
class Parameter(BaseModel):
78+
class Argument(BaseModel):
7979
model_config = ConfigDict(arbitrary_types_allowed=True)
8080

81-
name: str
8281
type: str
83-
value: int | float | str | xdr.SCVal
82+
value: int | str | xdr.SCVal
83+
84+
85+
class Parameter(Argument):
86+
name: str
87+
value: int | str | xdr.SCVal | list[Argument | xdr.SCVal]
8488

8589
@model_validator(mode="after")
8690
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
91+
if isinstance(self.value, list):
92+
self.value = [self._value_to_scval(val) for val in self.value]
93+
return self._value_to_scval(self)
94+
95+
@staticmethod
96+
def _value_to_scval(value: Argument | xdr.SCVal):
97+
if isinstance(value, Argument) and not isinstance(value.value, xdr.SCVal):
98+
value = getattr(scval, f"to_{value.type}")(value.value)
99+
return value
90100

91101

92102
class Parameters(BaseModel):

tests/params_invoke.json

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,19 @@
88
"name": "distributor",
99
"type": "int128",
1010
"value": 10
11+
},
12+
{
13+
"name": "claimants",
14+
"type": "vec",
15+
"value": [
16+
{
17+
"type": "uint32",
18+
"value": 12
19+
},
20+
{
21+
"type": "int64",
22+
"value": 20
23+
}
24+
]
1125
}
1226
]

tests/test_models.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,23 @@ def test_parameter(self):
4949
args = {"name": "distributor", "type": "int128", "value": scval.to_int128(10)}
5050
soroban.Parameter(**args)
5151

52+
args = {
53+
"name": "thresholds",
54+
"type": "vec",
55+
"value": [scval.to_int128(10), {"type": "uint32", "value": 4}],
56+
}
57+
soroban.Parameter(**args)
58+
59+
args = {
60+
"name": "thresholds",
61+
"type": "vec",
62+
"value": [
63+
scval.to_int128(10),
64+
{"type": "int128", "value": scval.to_int128(10)},
65+
],
66+
}
67+
soroban.Parameter(**args)
68+
5269
def test_parameters(self):
5370
fname = pathlib.Path(__file__).parent / "params_invoke.json"
5471
with open(fname, "rb") as fd:

0 commit comments

Comments
 (0)