Skip to content

Commit

Permalink
Add support for generating bindings for token contracts.
Browse files Browse the repository at this point in the history
  • Loading branch information
overcat committed Nov 29, 2024
1 parent 53a7c25 commit b85e4b1
Show file tree
Hide file tree
Showing 6 changed files with 297 additions and 25 deletions.
15 changes: 15 additions & 0 deletions stellar_contract_bindings/metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import dataclasses
from typing import List, Optional, Tuple, Type, Union

import xdrlib3
from stellar_sdk import xdr


Expand Down Expand Up @@ -108,3 +109,17 @@ def parse_entries(
offset += len(entry.to_xdr_bytes())
entries.append(entry)
return entries


def get_token_sc_spec_entry() -> list[xdr.SCSpecEntry]:
"""Get the token contract spec entry."""

# A little bit hacky, but it works
# TODO: find a way to get the token contract spec entry in the repo
# https://github.com/stellar/stellar-cli/blob/a11a924d310c1602e7b579377daa3e373010ac0e/cmd/soroban-cli/src/get_spec.rs#L77
raw_xdr = "AAAAAAAAAYpSZXR1cm5zIHRoZSBhbGxvd2FuY2UgZm9yIGBzcGVuZGVyYCB0byB0cmFuc2ZlciBmcm9tIGBmcm9tYC4KClRoZSBhbW91bnQgcmV0dXJuZWQgaXMgdGhlIGFtb3VudCB0aGF0IHNwZW5kZXIgaXMgYWxsb3dlZCB0byB0cmFuc2ZlcgpvdXQgb2YgZnJvbSdzIGJhbGFuY2UuIFdoZW4gdGhlIHNwZW5kZXIgdHJhbnNmZXJzIGFtb3VudHMsIHRoZSBhbGxvd2FuY2UKd2lsbCBiZSByZWR1Y2VkIGJ5IHRoZSBhbW91bnQgdHJhbnNmZXJyZWQuCgojIEFyZ3VtZW50cwoKKiBgZnJvbWAgLSBUaGUgYWRkcmVzcyBob2xkaW5nIHRoZSBiYWxhbmNlIG9mIHRva2VucyB0byBiZSBkcmF3biBmcm9tLgoqIGBzcGVuZGVyYCAtIFRoZSBhZGRyZXNzIHNwZW5kaW5nIHRoZSB0b2tlbnMgaGVsZCBieSBgZnJvbWAuAAAAAAAJYWxsb3dhbmNlAAA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dmluZyBpdHMKYWxsb3dhbmNlIGNvbnN1bWVkIGR1cmluZyB0aGUgdHJhbnNmZXIuCiogYGZyb21gIC0gVGhlIGFkZHJlc3MgaG9sZGluZyB0aGUgYmFsYW5jZSBvZiB0b2tlbnMgd2hpY2ggd2lsbCBiZQp3aXRoZHJhd24gZnJvbS4KKiBgdG9gIC0gVGhlIGFkZHJlc3Mgd2hpY2ggd2lsbCByZWNlaXZlIHRoZSB0cmFuc2ZlcnJlZCB0b2tlbnMuCiogYGFtb3VudGAgLSBUaGUgYW1vdW50IG9mIHRva2VucyB0byBiZSB0cmFuc2ZlcnJlZC4KCiMgRXZlbnRzCgpFbWl0cyBhbiBldmVudCB3aXRoIHRvcGljcyBgWyJ0cmFuc2ZlciIsIGZyb206IEFkZHJlc3MsIHRvOiBBZGRyZXNzXSwKZGF0YSA9IGFtb3VudDogaTEyOGAAAAAAAAANdHJhbnNmZXJfZnJvbQAAAAAAAAQAAAAAAAAAB3NwZW5kZXIAAAAAEwAAAAAAAAAEZnJvbQAAABMAAAAAAAAAAnRvAAAAAAATAAAAAAAAAAZhbW91bnQAAAAAAAsAAAAA"
unpacker = xdrlib3.Unpacker(base64.b64decode(raw_xdr))
specs = []
while unpacker.get_position() < len(base64.b64decode(raw_xdr)):
specs.append(xdr.SCSpecEntry.unpack(unpacker))
return specs
84 changes: 66 additions & 18 deletions stellar_contract_bindings/python.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import keyword
import os
from typing import List

Expand All @@ -7,11 +8,7 @@
from stellar_sdk import xdr

from stellar_contract_bindings import __version__ as stellar_contract_bindings_version
from stellar_contract_bindings.metadata import parse_contract_metadata
from stellar_contract_bindings.utils import (
get_wasm_hash_by_contract_id,
get_contract_wasm_by_hash,
)
from stellar_contract_bindings.utils import get_specs_by_contract_id


def is_tuple_struct(entry: xdr.SCSpecUDTStructV0) -> bool:
Expand Down Expand Up @@ -539,13 +536,63 @@ def parse_result_xdr_fn(output: List[xdr.SCSpecTypeDef]):
return client_rendered_code


def generate_binding(wasm: bytes, client_type: str) -> str:
generated = []
generated.append(render_info())
# append _ to keyword
def append_underscore(specs: List[xdr.SCSpecEntry]):
for spec in specs:
if spec.kind == xdr.SCSpecEntryKind.SC_SPEC_ENTRY_UDT_STRUCT_V0:
assert spec.udt_struct_v0 is not None
if keyword.iskeyword(spec.udt_struct_v0.name.decode()):
spec.udt_struct_v0.name = spec.udt_struct_v0.name + b"_"
for field in spec.udt_struct_v0.fields:
if keyword.iskeyword(field.name.decode()):
field.name = field.name + b"_"
if spec.kind == xdr.SCSpecEntryKind.SC_SPEC_ENTRY_UDT_UNION_V0:
assert spec.udt_union_v0 is not None
if keyword.iskeyword(spec.udt_union_v0.name.decode()):
spec.udt_union_v0.name = spec.udt_union_v0.name + b"_"
for union_case in spec.udt_union_v0.cases:
if (
union_case.kind
== xdr.SCSpecUDTUnionCaseV0Kind.SC_SPEC_UDT_UNION_CASE_TUPLE_V0
):
if keyword.iskeyword(union_case.tuple_case.name.decode()):
union_case.tuple_case.name = union_case.tuple_case.name + b"_"
elif (
union_case.kind
== xdr.SCSpecUDTUnionCaseV0Kind.SC_SPEC_UDT_UNION_CASE_VOID_V0
):
if keyword.iskeyword(union_case.void_case.name.decode()):
union_case.void_case.name = union_case.void_case.name + b"_"
else:
raise ValueError(f"Unsupported union case kind: {union_case.kind}")
if spec.kind == xdr.SCSpecEntryKind.SC_SPEC_ENTRY_FUNCTION_V0:
assert spec.function_v0 is not None
if keyword.iskeyword(spec.function_v0.name.sc_symbol.decode()):
spec.function_v0.name.sc_symbol = spec.function_v0.name.sc_symbol + b"_"
for param in spec.function_v0.inputs:
if keyword.iskeyword(param.name.decode()):
param.name = param.name + b"_"
if spec.kind == xdr.SCSpecEntryKind.SC_SPEC_ENTRY_UDT_ENUM_V0:
assert spec.udt_enum_v0 is not None
if keyword.iskeyword(spec.udt_enum_v0.name.decode()):
spec.udt_enum_v0.name = spec.udt_enum_v0.name + b"_"
for enum_case in spec.udt_enum_v0.cases:
if keyword.iskeyword(enum_case.name.decode()):
enum_case.name = enum_case.name + b"_"
if spec.kind == xdr.SCSpecEntryKind.SC_SPEC_ENTRY_UDT_ERROR_ENUM_V0:
assert spec.udt_error_enum_v0 is not None
if keyword.iskeyword(spec.udt_error_enum_v0.name.decode()):
spec.udt_error_enum_v0.name = spec.udt_error_enum_v0.name + b"_"
for error_enum_case in spec.udt_error_enum_v0.cases:
if keyword.iskeyword(error_enum_case.name.decode()):
error_enum_case.name = error_enum_case.name + b"_"


metadata = parse_contract_metadata(wasm)
specs = metadata.spec
def generate_binding(specs: List[xdr.SCSpecEntry], client_type: str) -> str:
append_underscore(specs)

generated = []
generated.append(render_info())
generated.append(render_imports(client_type))

for spec in specs:
Expand Down Expand Up @@ -599,16 +646,13 @@ def command(contract_id: str, rpc_url: str, output: str, client_type: str):
if output is None:
output = os.getcwd()
try:
wasm_id = get_wasm_hash_by_contract_id(contract_id, rpc_url)
click.echo(f"Got wasm id: {wasm_id.hex()}")
wasm_code = get_contract_wasm_by_hash(wasm_id, rpc_url)
click.echo(f"Got wasm code")
specs = get_specs_by_contract_id(contract_id, rpc_url)
except Exception as e:
click.echo(f"Error: {str(e)}", err=True)
click.echo(f"Get contract specs failed: {e}", err=True)
raise click.Abort()

click.echo("Generating Python bindings")
generated = generate_binding(wasm_code, client_type=client_type)
generated = generate_binding(specs, client_type=client_type)
if not os.path.exists(output):
os.makedirs(output)
output_path = os.path.join(output, "bindings.py")
Expand All @@ -621,8 +665,12 @@ def command(contract_id: str, rpc_url: str, output: str, client_type: str):


if __name__ == "__main__":
from stellar_contract_bindings.metadata import parse_contract_metadata

wasm_file = "/Users/overcat/repo/lightsail/stellar-contract-bindings/tests/contracts/target/wasm32-unknown-unknown/release/python.wasm"
with open(wasm_file, "rb") as f:
wasm = f.read()
generated_code = generate_binding(wasm, "both")
print(generated_code)

specs = parse_contract_metadata(wasm).spec
generated = generate_binding(specs, client_type="both")
print(generated)
37 changes: 33 additions & 4 deletions stellar_contract_bindings/utils.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
from typing import List

from stellar_sdk import SorobanServer
from stellar_sdk import xdr, Address
from stellar_contract_bindings.metadata import (
parse_contract_metadata,
get_token_sc_spec_entry,
)


def get_contract_wasm_by_hash(wasm_hash: bytes, rpc_url: str) -> bytes:
def get_specs_by_wasm_hash(wasm_hash: bytes, rpc_url: str) -> list[xdr.SCSpecEntry]:
"""Get the contract wasm by wasm hash.
:param wasm_hash: The wasm hash.
Expand All @@ -19,10 +25,11 @@ def get_contract_wasm_by_hash(wasm_hash: bytes, rpc_url: str) -> bytes:
if not resp.entries:
raise ValueError(f"Wasm not found, wasm id: {wasm_hash.hex()}")
data = xdr.LedgerEntryData.from_xdr(resp.entries[0].xdr)
return data.contract_code.code
meta_data = data.contract_code.code
return parse_contract_metadata(meta_data).spec


def get_wasm_hash_by_contract_id(contract_id: str, rpc_url: str) -> bytes:
def get_specs_by_contract_id(contract_id: str, rpc_url: str) -> list[xdr.SCSpecEntry]:
"""Get the wasm hash by contract id.
:param contract_id: The contract id.
Expand All @@ -43,4 +50,26 @@ def get_wasm_hash_by_contract_id(contract_id: str, rpc_url: str) -> bytes:
if not resp.entries:
raise ValueError(f"Contract not found, contract id: {contract_id}")
data = xdr.LedgerEntryData.from_xdr(resp.entries[0].xdr)
return data.contract_data.val.instance.executable.wasm_hash.hash
if (
data.contract_data.val.instance.executable.type
== xdr.ContractExecutableType.CONTRACT_EXECUTABLE_STELLAR_ASSET
):
return get_token_sc_spec_entry()
elif (
data.contract_data.val.instance.executable.type
== xdr.ContractExecutableType.CONTRACT_EXECUTABLE_WASM
):
return get_specs_by_wasm_hash(
data.contract_data.val.instance.executable.wasm_hash.hash, rpc_url
)
else:
raise ValueError(
f"Unknown executable type, type: {data.contract_data.val.instance.executable.type}"
)


if __name__ == "__main__":
get_specs_by_contract_id(
"CAS3J7GYLGXMF6TDJBBYYSE3HQ6BBSMLNUQ34T6TZMYMW2EVH34XOWMA",
"https://mainnet.sorobanrpc.com",
)
Loading

0 comments on commit b85e4b1

Please sign in to comment.