Skip to content
This repository has been archived by the owner on Nov 16, 2022. It is now read-only.

Pyband: add message delegate #2835

Merged
merged 6 commits into from
Nov 12, 2020
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG_UNRELEASED.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@

### Helpers

- (impv) [\#2835](https://github.com/bandprotocol/bandchain/pull/2835) pyband: Add msg delegate
- (impv) [\#2830](https://github.com/bandprotocol/bandchain/pull/2830) pyband: Add msg send
- (impv) [\#2826](https://github.com/bandprotocol/bandchain/pull/2826) pyband: Add Pyband test on Github Action
- (impv) [\#2807](https://github.com/bandprotocol/bandchain/pull/2807) pyband: Refactor and wrote tests for client and send tx
Expand Down
25 changes: 25 additions & 0 deletions helpers/pyband/pyband/message.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,3 +91,28 @@ def validate(self) -> bool:
coin.validate()

return True


@dataclass
class MsgDelegate(Msg):
delegator_address: Address
validator_address: Address
amount: Coin

def as_json(self) -> dict:
return {
"type": "cosmos-sdk/MsgDelegate",
"value": {
"delegator_address": self.delegator_address.to_acc_bech32(),
"validator_address": self.validator_address.to_val_bech32(),
"amount": self.amount.as_json(),
},
}

def get_sender(self) -> Address:
return self.delegator_address

def validate(self) -> bool:
self.amount.validate()

return True
Pitchanai marked this conversation as resolved.
Show resolved Hide resolved
47 changes: 47 additions & 0 deletions helpers/pyband/tests/message_delegate_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import pytest
from pyband.data import Coin
from pyband.message import MsgDelegate
from pyband.wallet import Address


def test_msg_delegate_creation_success():
msg_delegate = MsgDelegate(
delegator_address=Address.from_acc_bech32(
"band1jrhuqrymzt4mnvgw8cvy3s9zhx3jj0dq30qpte"
),
validator_address=Address.from_val_bech32(
"bandvaloper1j9vk75jjty02elhwqqjehaspfslaem8pr20qst"
),
amount=Coin(amount=1000000, denom="uband"),
)

assert msg_delegate.validate() == True

assert msg_delegate.as_json() == {
"type": "cosmos-sdk/MsgDelegate",
"value": {
"delegator_address": "band1jrhuqrymzt4mnvgw8cvy3s9zhx3jj0dq30qpte",
"validator_address": "bandvaloper1j9vk75jjty02elhwqqjehaspfslaem8pr20qst",
"amount": {"amount": "1000000", "denom": "uband"},
},
}

assert (
msg_delegate.get_sender().to_acc_bech32()
== "band1jrhuqrymzt4mnvgw8cvy3s9zhx3jj0dq30qpte"
)


def test_msg_delegate_coin_fail():
msg_delegate = MsgDelegate(
delegator_address=Address.from_acc_bech32(
"band1jrhuqrymzt4mnvgw8cvy3s9zhx3jj0dq30qpte"
),
validator_address=Address.from_val_bech32(
"bandvaloper1j9vk75jjty02elhwqqjehaspfslaem8pr20qst"
),
amount=Coin(amount=-1000000, denom="uband"),
)

with pytest.raises(ValueError, match="Expect amount more than 0"):
msg_delegate.amount.validate()