Skip to content

Commit

Permalink
fix(VER-2653): Fix create_and_install_canister_with_candid() (#53)
Browse files Browse the repository at this point in the history
Since the PocketIC server does not add cycles to canisters anymore by
default, the convenience function
`create_and_install_canister_with_candid()` is broken because it does
not add cycles to the canister before installing a Wasm.
  • Loading branch information
fxgst authored Jan 25, 2024
1 parent b94390a commit 88798f7
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 11 deletions.
10 changes: 8 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,23 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).



## 2.0.2 - 2024-01-25

### Fixed
- Fixed a bug in `create_and_install_canister_with_candid()` on application subnets by adding 2T cycles to the
canister before installing a Wasm


## 2.0.1 - 2023-11-23

### Added
- Support for PocketIC server version 2.0.1


### Changed
- When the PocketIC binary is not found, the error now points to the PocketIC repo instead of the download link



## 2.0.0 - 2023-11-21

### Added
Expand Down
7 changes: 4 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,18 @@ from pocket_ic import PocketIC

pic = PocketIC()
canister_id = pic.create_canister()
pic.add_cycles(canister_id, 2_000_000_000_000)
pic.add_cycles(canister_id, 2_000_000_000_000) # 2T cycles
pic.install_code(...)

# make canister calls
response = pic.update_call(canister_id, method="greeting", ...)
response = pic.update_call(canister_id, method="greet", ...)
assert(response == 'Hello, PocketIC!')
```
... or even directly with a canister object:
```python
my_canister = pic.create_and_install_canister_with_candid(...)
# call your canister methods with native Python syntax
respone = my_canister.greeting()
response = my_canister.greet()
assert(response == 'Hello, PocketIC!')
```

Expand Down
12 changes: 7 additions & 5 deletions pocket_ic/pocket_ic.py
Original file line number Diff line number Diff line change
Expand Up @@ -407,16 +407,17 @@ def create_and_install_canister_with_candid(
self,
candid: str,
wasm_module: bytes,
init_args: dict,
init_args: Optional[dict] = None,
subnet: Optional[ic.Principal] = None,
) -> ic.Canister:
"""Creates a canister, installs the provided WASM with the given init arguments. Returns a canister object.
For an example on how to use the canister object, see `/examples/ledger_canister_test.py`.
"""Creates a canister, charges it with 2T cycles and installs the provided WASM with the given init arguments.
Returns a canister object. For an example on how to use the canister object,
see `/examples/ledger_canister/ledger_canister_test.py`.
Args:
candid (str): a valid candid file describing the canister interface
wasm_module (bytes): the canister wasm as bytes
init_args (dict): the init args as required by the candid file
init_args (Optional[dict], optional): the init args as required by the candid file, defaults to `None`
subnet (Optional[ic.Principal], optional): optional subnet ID where to install the
canister, defaults to `None`
Expand All @@ -436,7 +437,8 @@ def create_and_install_canister_with_candid(
arg = [{"type": canister_arguments[0], "value": init_args}]
else:
raise ValueError("The candid file appears to be malformed")


self.add_cycles(canister_id, 2_000_000_000_000)
self.install_code(canister_id, wasm_module, arg)
return canister

Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "pocket_ic"
version = "2.0.1"
version = "2.0.2"
description = "PocketIC: A Canister Smart Contract Testing Platform"
authors = [
"The Internet Computer Project Developers <dept-testing_&_verification@dfinity.org>",
Expand Down

0 comments on commit 88798f7

Please sign in to comment.