-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #115 from multiversx/update-23-1
ABI: handle CodeMetadataValue
- Loading branch information
Showing
6 changed files
with
131 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import io | ||
from typing import Any | ||
|
||
from multiversx_sdk.abi.shared import read_bytes_exactly | ||
from multiversx_sdk.core.code_metadata import (CODE_METADATA_LENGTH, | ||
CodeMetadata) | ||
|
||
|
||
class CodeMetadataValue: | ||
def __init__(self, value: bytes = b"") -> None: | ||
self.value = value | ||
|
||
@classmethod | ||
def new_from_code_metadata(cls, code_metadata: CodeMetadata) -> "CodeMetadataValue": | ||
return cls(code_metadata.serialize()) | ||
|
||
def encode_nested(self, writer: io.BytesIO): | ||
writer.write(self.value) | ||
|
||
def encode_top_level(self, writer: io.BytesIO): | ||
writer.write(self.value) | ||
|
||
def decode_nested(self, reader: io.BytesIO): | ||
length = CODE_METADATA_LENGTH | ||
data = read_bytes_exactly(reader, length) | ||
self.value = data | ||
|
||
def decode_top_level(self, data: bytes): | ||
self.value = data | ||
|
||
def set_payload(self, value: Any): | ||
if isinstance(value, bytes): | ||
self.value = CodeMetadata.new_from_bytes(value).serialize() | ||
elif isinstance(value, CodeMetadata): | ||
self.value = value.serialize() | ||
else: | ||
raise ValueError(f"cannot set payload for code metadata (should be either a CodeMetadata or bytes, but got: {type(value)})") | ||
|
||
def get_payload(self) -> Any: | ||
return self.value | ||
|
||
def __eq__(self, other: Any) -> bool: | ||
return isinstance(other, CodeMetadataValue) and self.value == other.value | ||
|
||
def __bytes__(self) -> bytes: | ||
return self.value |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import re | ||
|
||
import pytest | ||
|
||
from multiversx_sdk.abi.code_metadata_value import CodeMetadataValue | ||
from multiversx_sdk.core.code_metadata import CodeMetadata | ||
|
||
|
||
def test_new_from_code_metadata(): | ||
value = CodeMetadataValue.new_from_code_metadata(CodeMetadata()) | ||
assert value.get_payload() == bytes([0x05, 0x00]) | ||
|
||
value = CodeMetadataValue.new_from_code_metadata(CodeMetadata(upgradeable=True, readable=True, payable=True, payable_by_contract=True)) | ||
assert value.get_payload() == bytes([0x05, 0x06]) | ||
|
||
|
||
def test_set_payload_and_get_payload(): | ||
# Simple | ||
value = CodeMetadataValue() | ||
value.set_payload(bytes([0x05, 0x00])) | ||
assert value.get_payload() == bytes([0x05, 0x00]) | ||
|
||
# With CodeMetadata | ||
value = CodeMetadataValue() | ||
value.set_payload(CodeMetadata(upgradeable=True, readable=True, payable=True, payable_by_contract=True)) | ||
assert value.get_payload() == bytes([0x05, 0x06]) | ||
|
||
# With errors | ||
with pytest.raises(ValueError, match=re.escape("cannot set payload for code metadata (should be either a CodeMetadata or bytes, but got: <class 'dict'>)")): | ||
CodeMetadataValue().set_payload({}) | ||
|
||
with pytest.raises(ValueError, match="code metadata buffer has length 4, expected 2"): | ||
CodeMetadataValue().set_payload(bytes([0, 1, 2, 3])) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters