Skip to content

Commit d1a2e9a

Browse files
authored
feat(SDK): add Execute.add_from_receipt (#14)
1 parent fe62656 commit d1a2e9a

2 files changed

Lines changed: 46 additions & 0 deletions

File tree

ruffsack/messages/execute.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@ class ExecuteBase(EIP712Message):
2525

2626

2727
class Execute(ManagerAccessMixin):
28+
MAX_CALLS = 8
29+
MAX_CALLDATA_SIZE = 16_388
30+
2831
def __init__(
2932
self,
3033
sack: "Ruffsack | None" = None,
@@ -54,6 +57,17 @@ class Execute(ExecuteBase):
5457
self.message = Execute(parent=parent)
5558

5659
def add_raw(self, target: "AddressType", value: int = 0, data: bytes = b"") -> Self:
60+
if len(self.message.calls) >= self.MAX_CALLS:
61+
raise RuntimeError(
62+
"Ruffsack does not support more than 8 calls per execute transaction."
63+
)
64+
65+
if len(data) > self.MAX_CALLDATA_SIZE:
66+
raise RuntimeError(
67+
"Ruffsack calls do not support data field larger than"
68+
f" {self.MAX_CALLDATA_SIZE} bytes."
69+
)
70+
5771
self.message.calls.append(Call(target=target, value=value, data=data))
5872
return self
5973

@@ -64,6 +78,13 @@ def add(self, call, *args, value: int = 0) -> Self:
6478
data=call.encode_input(*args),
6579
)
6680

81+
def add_from_receipt(self, receipt: "ReceiptAPI") -> Self:
82+
return self.add_raw(
83+
target=receipt.receiver,
84+
value=receipt.value,
85+
data=receipt.data,
86+
)
87+
6788
def __call__(
6889
self, sack: "Ruffsack | None" = None, **txn_args
6990
) -> "ReceiptAPI | None":

tests/test_execute.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import pytest
2+
from ape.utils import ZERO_ADDRESS
3+
from packaging.version import Version
4+
from ruffsack.messages import Execute
5+
6+
7+
def test_size_limits():
8+
# NOTE: Use `Execute` directly to avoid parametrized fixture setup
9+
txn = Execute(
10+
parent=b"\x00" * 32,
11+
version=Version("0.1"),
12+
address=ZERO_ADDRESS,
13+
chain_id=1,
14+
)
15+
16+
with pytest.raises(RuntimeError):
17+
# Can't add `.data` larger than `Execute.MAX_CALLDATA_SIZE`
18+
txn.add_raw(ZERO_ADDRESS, data=b"\x00" * (Execute.MAX_CALLDATA_SIZE + 1))
19+
20+
for _ in range(Execute.MAX_CALLS):
21+
txn.add_raw(ZERO_ADDRESS, data=b"\x00" * Execute.MAX_CALLDATA_SIZE)
22+
23+
with pytest.raises(RuntimeError):
24+
# Can't add more than `Execute.MAX_CALLS`
25+
txn.add_raw(ZERO_ADDRESS, data=b"\x00" * Execute.MAX_CALLDATA_SIZE)

0 commit comments

Comments
 (0)