Skip to content

Commit

Permalink
Release v0.6.2
Browse files Browse the repository at this point in the history
  • Loading branch information
5A11 authored Nov 29, 2022
2 parents f9a1e07 + 02ccc7c commit cb4bc0a
Showing 12 changed files with 223 additions and 2 deletions.
1 change: 1 addition & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
@@ -20,6 +20,7 @@ jobs:
PYPI_USERNAME: __token__
PYPI_PASSWORD: ${{secrets.FETCHBOT_PYPI_TOKEN}}
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} # required to make a release page
GH_TOKEN: ${{ github.token }}
run: |
pip install tomli packaging poetry
git config --global user.email "ci-bot@fetch.ai"
4 changes: 4 additions & 0 deletions HISTORY.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Release History

## 0.6.2

* feat: add migration for wasm contracts in aerial
* CI: fix release automation

## 0.6.1

6 changes: 6 additions & 0 deletions contracts/simple/schema/migrate_msg.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"$schema": "http://json-schema.org/draft-07/schema#",
"title": "MigrateMsg",
"type": "object",
"additionalProperties": false
}
Binary file modified contracts/simple/simple.wasm
Binary file not shown.
67 changes: 67 additions & 0 deletions cosmpy/aerial/contract/__init__.py
Original file line number Diff line number Diff line change
@@ -31,6 +31,7 @@
from cosmpy.aerial.contract.cosmwasm import (
create_cosmwasm_execute_msg,
create_cosmwasm_instantiate_msg,
create_cosmwasm_migrate_msg,
create_cosmwasm_store_code_msg,
)
from cosmpy.aerial.tx import Transaction
@@ -235,6 +236,69 @@ def instantiate(

return self._address

def upgrade(
self,
args: Any,
sender: Wallet,
new_path: str,
gas_limit: Optional[int] = None,
) -> SubmittedTx:
"""Store new contract code and migrate the current contract address.
:param args: args
:param sender: sender wallet address
:param new_path: path to new contract
:param gas_limit: transaction gas limit, defaults to None
:raises RuntimeError: Unable to extract contract code id
:return: contract address
"""
assert self._address, RuntimeError("Address was not set.")

if self._migrate_schema is not None:
validate(args, self._migrate_schema)

self._path = new_path
new_code_id = self.store(sender, gas_limit)

return self.migrate(args, sender, new_code_id, gas_limit)

def migrate(
self,
args: Any,
sender: Wallet,
new_code_id: int,
gas_limit: Optional[int] = None,
) -> SubmittedTx:
"""Migrate the current contract address to new code id.
:param args: args
:param sender: sender wallet address
:param new_code_id: Code id of the newly deployed contract
:param gas_limit: transaction gas limit, defaults to None
:raises RuntimeError: Unable to extract contract code id
:return: contract address
"""
assert self._address, RuntimeError("Address was not set.")

if self._migrate_schema is not None:
validate(args, self._migrate_schema)

# build up the migrate transaction
migrate_msg = create_cosmwasm_migrate_msg(
new_code_id,
args,
self._address,
sender.address(),
)
tx = Transaction()
tx.add_message(migrate_msg)

return prepare_and_broadcast_basic_transaction(
self._client, tx, sender, gas_limit=gas_limit
).wait_to_complete()

def deploy(
self,
args: Any,
@@ -359,6 +423,7 @@ def _load_schema(self, schema_path: Optional[str]):
self._instantiate_schema: Optional[Dict[str, Any]] = None
self._query_schema: Optional[Dict[str, Any]] = None
self._execute_schema: Optional[Dict[str, Any]] = None
self._migrate_schema: Optional[Dict[str, Any]] = None

if schema_path is None:
return
@@ -374,6 +439,8 @@ def _load_schema(self, schema_path: Optional[str]):
self._query_schema = schema
elif "execute" in msg_type:
self._execute_schema = schema
elif "migrate" in msg_type:
self._migrate_schema = schema

@property
def data(self):
25 changes: 25 additions & 0 deletions cosmpy/aerial/contract/cosmwasm.py
Original file line number Diff line number Diff line change
@@ -28,6 +28,7 @@
from cosmpy.protos.cosmwasm.wasm.v1.tx_pb2 import (
MsgExecuteContract,
MsgInstantiateContract,
MsgMigrateContract,
MsgStoreCode,
)

@@ -85,6 +86,30 @@ def create_cosmwasm_instantiate_msg(
return msg


def create_cosmwasm_migrate_msg(
code_id: int,
args: Any,
contract_address: Address,
sender_address: Address,
) -> MsgMigrateContract:
"""Create cosmwasm migrate message.
:param code_id: code id
:param args: args
:param contract_address: sender address
:param sender_address: sender address
:return: cosmwasm migrate message
"""
msg = MsgMigrateContract(
sender=str(sender_address),
code_id=code_id,
msg=json_encode(args).encode("UTF8"),
contract=str(contract_address),
)

return msg


def create_cosmwasm_execute_msg(
sender_address: Address,
contract_address: Address,
4 changes: 4 additions & 0 deletions cosmpy/tx/rest_client.py
Original file line number Diff line number Diff line change
@@ -137,3 +137,7 @@ def _fix_messages(messages: List[Dict[str, Any]]):
message["msg"] = base64.b64encode(
json_encode(message["msg"]).encode("UTF8")
).decode()
if message["@type"] == "/cosmwasm.wasm.v1.MsgMigrateContract":
message["msg"] = base64.b64encode(
json_encode(message["msg"]).encode("UTF8")
).decode()
56 changes: 56 additions & 0 deletions docs/api/aerial/contract/__init__.md
Original file line number Diff line number Diff line change
@@ -156,6 +156,62 @@ Instantiate the contract.

contract address

<a id="cosmpy.aerial.contract.__init__.LedgerContract.upgrade"></a>

#### upgrade

```python
def upgrade(args: Any,
sender: Wallet,
new_path: str,
gas_limit: Optional[int] = None) -> SubmittedTx
```

Store new contract code and migrate the current contract address.

**Arguments**:

- `args`: args
- `sender`: sender wallet address
- `new_path`: path to new contract
- `gas_limit`: transaction gas limit, defaults to None

**Raises**:

- `RuntimeError`: Unable to extract contract code id

**Returns**:

contract address

<a id="cosmpy.aerial.contract.__init__.LedgerContract.migrate"></a>

#### migrate

```python
def migrate(args: Any,
sender: Wallet,
new_code_id: int,
gas_limit: Optional[int] = None) -> SubmittedTx
```

Migrate the current contract address to new code id.

**Arguments**:

- `args`: args
- `sender`: sender wallet address
- `new_code_id`: Code id of the newly deployed contract
- `gas_limit`: transaction gas limit, defaults to None

**Raises**:

- `RuntimeError`: Unable to extract contract code id

**Returns**:

contract address

<a id="cosmpy.aerial.contract.__init__.LedgerContract.deploy"></a>

#### deploy
23 changes: 23 additions & 0 deletions docs/api/aerial/contract/cosmwasm.md
Original file line number Diff line number Diff line change
@@ -53,6 +53,29 @@ Create cosmwasm instantiate message.

cosmwasm instantiate message

<a id="cosmpy.aerial.contract.cosmwasm.create_cosmwasm_migrate_msg"></a>

#### create`_`cosmwasm`_`migrate`_`msg

```python
def create_cosmwasm_migrate_msg(code_id: int, args: Any,
contract_address: Address,
sender_address: Address) -> MsgMigrateContract
```

Create cosmwasm migrate message.

**Arguments**:

- `code_id`: code id
- `args`: args
- `contract_address`: sender address
- `sender_address`: sender address

**Returns**:

cosmwasm migrate message

<a id="cosmpy.aerial.contract.cosmwasm.create_cosmwasm_execute_msg"></a>

#### create`_`cosmwasm`_`execute`_`msg
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -6,7 +6,7 @@ markers = [

[tool.poetry]
name = "cosmpy"
version = "0.6.1"
version = "0.6.2"
description = "A library for interacting with the cosmos networks"
authors = ["Fetch.AI Limited"]
readme = "README.md"
21 changes: 20 additions & 1 deletion tests/integration/test_contract.py
Original file line number Diff line number Diff line change
@@ -98,7 +98,9 @@ def test_deployed_contract(self):
contract = LedgerContract(
None, ledger, code_id=contract._code_id # pylint: disable=protected-access
)
contract_address = contract.instantiate({}, wallet)
contract_address = contract.instantiate(
{}, wallet, admin_address=wallet.address()
)
assert contract_address

# use by address
@@ -116,6 +118,15 @@ def test_deployed_contract(self):
assert result["exists"]
assert result["value"] == value

# Upgrade contract
original_contract_address = contract.address
original_code_id = contract.code_id

tx_res = deployed_contract.upgrade({}, wallet, CONTRACT_PATH)
assert tx_res.response
assert deployed_contract.address == original_contract_address
assert deployed_contract.code_id != original_code_id

@pytest.mark.integration
@pytest.mark.flaky(reruns=MAX_FLAKY_RERUNS, reruns_delay=RERUNS_DELAY)
def test_contract_schema_validation(self):
@@ -142,6 +153,14 @@ def test_contract_schema_validation(self):
except Exception as exc:
raise ValidationTestFailure("Msg should have failed validation") from exc

try:
bad_msg = {"bad": 1}
contract.migrate(bad_msg, wallet, 1).wait_to_complete()
except ValidationError:
pass
except Exception as exc:
raise ValidationTestFailure("Msg should have failed validation") from exc


class TestContractRestAPI(TestContract):
"""Test dorado rest api"""
16 changes: 16 additions & 0 deletions tests/unit/test_aerial/contract/test_cosmwasm_handlers.py
Original file line number Diff line number Diff line change
@@ -22,6 +22,7 @@
from cosmpy.aerial.contract import (
create_cosmwasm_execute_msg,
create_cosmwasm_instantiate_msg,
create_cosmwasm_migrate_msg,
)
from cosmpy.crypto.address import Address

@@ -60,3 +61,18 @@ def test_create_execute_msg():
assert msg.funds[0].amount == "15"
assert msg.funds[1].denom == "another"
assert msg.funds[1].amount == "42"


def test_create_migrate_msg():
"""Test create migrate message."""
sender = Address("fetch1r3d4azhlak4w00c5n02t9l35a3n6462vrnunel")
contract_address = Address(
"fetch1j4t8vtg8dus7tpy6xrk9xnz5z4644qljeqtee2yw73ksvj3tqaeqp4pcec"
)

msg = create_cosmwasm_migrate_msg(1, {}, contract_address, sender)

assert msg.sender == str(sender)
assert msg.code_id == 1
assert msg.contract == contract_address
assert msg.sender == str(sender)

0 comments on commit cb4bc0a

Please sign in to comment.