Skip to content

Commit

Permalink
Merge pull request #85 from terra-money/feature/decode
Browse files Browse the repository at this point in the history
Feature/decode
  • Loading branch information
Geoff Lee authored Mar 18, 2022
2 parents 9fa0c38 + ba8702a commit b4bc740
Show file tree
Hide file tree
Showing 30 changed files with 622 additions and 86 deletions.
42 changes: 42 additions & 0 deletions integration_tests/send_async.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import asyncio
import base64
from pathlib import Path

from terra_sdk.client.lcd import LCDClient
from terra_sdk.client.lcd.api.tx import CreateTxOptions
from terra_sdk.core import Coins
from terra_sdk.core.bank import MsgSend
from terra_sdk.core.tx import SignMode
from terra_sdk.key.mnemonic import MnemonicKey


def main():
terra = LCDClient(
url="http://localhost:1317/",
chain_id="localterra",
)
key = MnemonicKey(
mnemonic="notice oak worry limit wrap speak medal online prefer cluster roof addict wrist behave treat actual wasp year salad speed social layer crew genius"
)
test1 = terra.wallet(key=key)

msg = MsgSend(
"terra1x46rqay4d3cssq8gxxvqz8xt6nwlz4td20k38v",
"terra17lmam6zguazs5q5u6z5mmx76uj63gldnse2pdp",
Coins(uluna=20000),
)
print(msg)
tx = test1.create_and_sign_tx(
CreateTxOptions(
msgs=[msg],
gas_prices="0.15uluna",
gas="63199", # gas="auto", gas_adjustment=1.1
)
)
print(tx)

result = terra.tx.broadcast_async(tx)
print(result)


main()
42 changes: 42 additions & 0 deletions integration_tests/send_sync.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import asyncio
import base64
from pathlib import Path

from terra_sdk.client.lcd import LCDClient
from terra_sdk.client.lcd.api.tx import CreateTxOptions
from terra_sdk.core import Coins
from terra_sdk.core.bank import MsgSend
from terra_sdk.core.tx import SignMode
from terra_sdk.key.mnemonic import MnemonicKey


def main():
terra = LCDClient(
url="http://localhost:1317/",
chain_id="localterra",
)
key = MnemonicKey(
mnemonic="notice oak worry limit wrap speak medal online prefer cluster roof addict wrist behave treat actual wasp year salad speed social layer crew genius"
)
test1 = terra.wallet(key=key)

msg = MsgSend(
"terra1x46rqay4d3cssq8gxxvqz8xt6nwlz4td20k38v",
"terra17lmam6zguazs5q5u6z5mmx76uj63gldnse2pdp",
Coins(uluna=20000),
)
print(msg)
tx = test1.create_and_sign_tx(
CreateTxOptions(
msgs=[msg],
gas_prices="0.15uluna",
gas="63199", # gas="auto", gas_adjustment=1.1
)
)
print(tx)

result = terra.tx.broadcast_sync(tx)
print(result)


main()
4 changes: 2 additions & 2 deletions integration_tests/tx_distribution.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from terra_sdk.core.distribution import (
MsgFundCommunityPool,
MsgSetWithdrawAddress,
MsgWithdrawDelegationReward,
MsgWithdrawDelegatorReward,
MsgWithdrawValidatorCommission,
)

Expand All @@ -25,7 +25,7 @@ def main():
msgWCom = MsgWithdrawValidatorCommission(
validator_address="terravaloper1dcegyrekltswvyy0xy69ydgxn9x8x32zdy3ua5"
)
msgWDel = MsgWithdrawDelegationReward(
msgWDel = MsgWithdrawDelegatorReward(
delegator_address="terra1x46rqay4d3cssq8gxxvqz8xt6nwlz4td20k38v",
validator_address="terravaloper1dcegyrekltswvyy0xy69ydgxn9x8x32zdy3ua5",
)
Expand Down
50 changes: 49 additions & 1 deletion mypy.ini
Original file line number Diff line number Diff line change
@@ -1,4 +1,49 @@
[mypy]
# Specify the target platform details in config, so your developers are
# free to run mypy on Windows, Linux, or macOS and get consistent
# results.
python_version=3.9
platform=linux

# flake8-mypy expects the two following for sensible formatting
show_column_numbers=True
show_error_context=False

# do not follow imports (except for ones found in typeshed)
follow_imports=skip

# since we're ignoring imports, writing .mypy_cache doesn't make any sense
cache_dir=/dev/null

# suppress errors about unsatisfied imports
ignore_missing_imports=True

# allow untyped calls as a consequence of the options above
disallow_untyped_calls=False

# allow returning Any as a consequence of the options above
warn_return_any=False

# treat Optional per PEP 484
strict_optional=True

# ensure all execution paths are returning
warn_no_return=True

# lint-style cleanliness for typing needs to be disabled; returns more errors
# than the full run.
warn_redundant_casts=False
warn_unused_ignores=False

# The following are off by default since they're too noisy.
# Flip them on if you feel adventurous.
disallow_untyped_defs=False
check_untyped_defs=False

files = terra_sdk
show_error_codes = True
pretty = True


[mypy-ecdsa.*]
ignore_missing_imports=True
Expand All @@ -13,4 +58,7 @@ ignore_missing_imports=True
ignore_missing_imports=True

[mypy-nest_asyncio]
ignore_missing_imports=True
ignore_missing_imports=True

[mypy-terra_proto]
ignore_missing_imports=True
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ license = "MIT"
packages = [{ include = "terra_sdk" }]
readme = "README.md"
repository = "https://github.com/terra-money/terra.py"
version = "2.0.4"
version = "2.0.5"

[tool.poetry.dependencies]
aiohttp = "^3.8.1"
Expand Down
2 changes: 2 additions & 0 deletions terra_sdk/client/lcd/api/tx.py
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,7 @@ async def broadcast_sync(
SyncTxBroadcastResult: result
"""
res = await self._broadcast(tx, "BROADCAST_MODE_SYNC", options)
res = res.get("tx_response")
return SyncTxBroadcastResult(
txhash=res.get("txhash"),
raw_log=res.get("raw_log"),
Expand All @@ -296,6 +297,7 @@ async def broadcast_async(
AsyncTxBroadcastResult: result
"""
res = await self._broadcast(tx, "BROADCAST_MODE_ASYNC", options)
res = res.get("tx_response")
return AsyncTxBroadcastResult(
txhash=res.get("txhash"),
)
Expand Down
66 changes: 66 additions & 0 deletions terra_sdk/core/authz/data.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
from terra_proto.cosmos.staking.v1beta1 import (
StakeAuthorizationValidators as StakeAuthorizationValidators_pb,
)
from betterproto.lib.google.protobuf import Any as Any_pb

from terra_sdk.core import AccAddress, Coin, Coins
from terra_sdk.util.base import BaseTerraData
Expand All @@ -37,12 +38,24 @@
class Authorization(BaseTerraData):
"""Base class for authorization types."""

@staticmethod
def from_amino(amino: dict) -> Authorization:
from terra_sdk.util.parse_authorization import parse_authorization_amino

return parse_authorization_amino(amino)

@staticmethod
def from_data(data: dict) -> Authorization:
from terra_sdk.util.parse_authorization import parse_authorization

return parse_authorization(data)

@staticmethod
def from_proto(proto: Any_pb) -> Authorization:
from terra_sdk.util.parse_authorization import parse_authorization_proto

return parse_authorization_proto(proto)


@attr.s
class SendAuthorization(Authorization):
Expand Down Expand Up @@ -75,6 +88,16 @@ def from_data(cls, data: dict) -> SendAuthorization:
def to_proto(self) -> SendAuthorization_pb:
return SendAuthorization_pb(spend_limit=self.spend_limit.to_proto())

@classmethod
def from_proto(cls, proto: SendAuthorization_pb) -> SendAuthorization:
return cls(spend_limit=Coins.from_proto(proto.spend_limit))

@classmethod
def from_amino(cls, amino: dict) -> SendAuthorization:
value = amino["value"]
return cls(spend_limit=Coins.from_amino(value["spend_limit"]))



@attr.s
class GenericAuthorization(Authorization):
Expand Down Expand Up @@ -102,6 +125,15 @@ def from_data(cls, data: dict) -> GenericAuthorization:
def to_proto(self) -> GenericAuthorization_pb:
return GenericAuthorization_pb(msg=self.msg)

@classmethod
def from_proto(cls, proto: GenericAuthorization_pb) -> GenericAuthorization:
return cls(msg=proto.msg)

@classmethod
def from_amino(cls, amino: dict) -> SendAuthorization:
value = amino["value"]
return cls(msg=value["msg"])


@attr.s
class AuthorizationGrant(JSONSerializable):
Expand Down Expand Up @@ -138,6 +170,21 @@ def to_proto(self) -> Grant_pb:
expiration=self.expiration,
)

@classmethod
def from_proto(cls, proto: Grant_pb) -> AuthorizationGrant:
return cls(
authorization=Authorization.from_proto(proto.authorization),
expiration=parser.parse(proto.expiration),
)

@classmethod
def from_amino(cls, amino: dict) -> AuthorizationGrant:
value = amino["value"]
return cls(
authorization=Authorization.from_amino(amino["authorization"]),
expiration=amino["expiration"]
)


@attr.s
class StakeAuthorizationValidators(JSONSerializable):
Expand All @@ -156,6 +203,10 @@ def from_data(cls, data: dict) -> StakeAuthorizationValidators:
def to_proto(self):
return StakeAuthorizationValidators_pb(address=self.address)

@classmethod
def from_proto(cls, proto: StakeAuthorizationValidators_pb) -> StakeAuthorizationValidators:
return cls(address=proto.address)


@attr.s
class StakeAuthorization(Authorization):
Expand Down Expand Up @@ -200,3 +251,18 @@ def to_proto(self) -> StakeAuthorization_pb:
allow_list=self.allow_list.to_proto() if self.allow_list else None,
deny_list=self.deny_list.to_proto() if self.deny_list else None,
)

@classmethod
def from_proto(cls, proto: StakeAuthorization_pb) -> StakeAuthorization:
return StakeAuthorization(
authorization_type=proto.authorization_type,
max_tokens=Coins.from_proto(proto.max_tokens)
if proto.max_tokens
else None,
allow_list=StakeAuthorizationValidators.from_proto(proto.allow_list)
if proto.allow_list
else None,
deny_list=StakeAuthorizationValidators.from_proto(proto.deny_list)
if proto.deny_list
else None,
)
47 changes: 47 additions & 0 deletions terra_sdk/core/authz/msgs.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,20 @@ def from_data(cls, data: dict) -> MsgExecAuthorized:
def to_proto(self) -> MsgExec_pb:
return MsgExec_pb(grantee=self.grantee, msgs=[m.pack_any() for m in self.msgs])

@classmethod
def from_proto(cls, proto: MsgExec_pb) -> MsgExecAuthorized:
return cls(
grantee=proto.grantee, msgs=[Msg.from_proto(md) for md in proto.msgs]
)

@classmethod
def from_amino(cls, amino: dict) -> MsgExecAuthorized:
value = amino["value"]
return cls(
grantee=value["grantee"],
msgs=[Msg.from_amino(msg) for msg in value["msgs"]]
)


@attr.s
class MsgGrantAuthorization(Msg):
Expand Down Expand Up @@ -114,6 +128,23 @@ def to_proto(self) -> MsgGrant_pb:
granter=self.granter, grantee=self.grantee, grant=self.grant.to_proto()
)

@classmethod
def from_proto(cls, proto: MsgGrant_pb) -> MsgGrantAuthorization:
return cls(
granter=proto.granter,
grantee=proto.grantee,
grant=AuthorizationGrant.from_proto(proto.grant)
)

@classmethod
def from_amino(cls, amino: dict) -> MsgGrantAuthorization:
value = amino["value"]
return cls(
grantee=value["grantee"],
granter=value["granter"],
grant=AuthorizationGrant.from_amino(value["grant"])
)


@attr.s
class MsgRevokeAuthorization(Msg):
Expand Down Expand Up @@ -144,6 +175,14 @@ def to_amino(self) -> dict:
},
}

def to_data(self) -> dict:
return {
"@type": self.type_url,
"granter": self.granter,
"grantee": self.grantee,
"msg_type_url": self.msg_type_url,
}

@classmethod
def from_data(cls, data: dict) -> MsgRevokeAuthorization:
return cls(
Expand All @@ -156,3 +195,11 @@ def to_proto(self) -> MsgRevoke_pb:
return MsgRevoke_pb(
granter=self.granter, grantee=self.grantee, msg_type_url=self.msg_type_url
)

@classmethod
def from_proto(cls, proto: MsgRevoke_pb) -> MsgRevokeAuthorization:
return cls(
granter=proto.granter,
grantee=proto.grantee,
msg_type_url=proto.msg_type_url,
)
6 changes: 3 additions & 3 deletions terra_sdk/core/bank/msgs.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,9 @@ def to_data(self) -> dict:
@classmethod
def from_proto(cls, proto: MsgSend_pb) -> MsgSend:
return cls(
from_address=proto["from_address"],
to_address=proto["to_address"],
amount=Coins.from_proto(proto["amount"]),
from_address=proto.from_address,
to_address=proto.to_address,
amount=Coins.from_proto(proto.amount),
)

def to_proto(self) -> MsgSend_pb:
Expand Down
Loading

0 comments on commit b4bc740

Please sign in to comment.