Skip to content

Commit 981f2fc

Browse files
authored
Merge pull request #52 from rocklabs-io/common-cansiter
Common cansiter
2 parents 2ad196d + 65d6c05 commit 981f2fc

13 files changed

+1134
-111
lines changed

ic/agent.py

+12-20
Original file line numberDiff line numberDiff line change
@@ -45,49 +45,41 @@ def read_state_endpoint(self, canister_id, data):
4545
result = self.client.read_state(canister_id, data)
4646
return result
4747

48-
def query_raw(self, canister_id, method_name, *arg):
49-
assert len(arg) == 1 or len(arg) == 2
48+
def query_raw(self, canister_id, method_name, arg, return_type = None, effective_canister_id = None):
5049
req = {
5150
'request_type': "query",
5251
'sender': self.identity.sender().bytes,
5352
'canister_id': Principal.from_str(canister_id).bytes if isinstance(canister_id, str) else canister_id.bytes,
5453
'method_name': method_name,
55-
'arg': arg[0],
54+
'arg': arg,
5655
'ingress_expiry': self.get_expiry_date()
5756
}
5857
_, data = sign_request(req, self.identity)
59-
result = self.query_endpoint(canister_id, data)
58+
result = self.query_endpoint(canister_id if effective_canister_id is None else effective_canister_id, data)
6059
if result['status'] == 'replied':
61-
if len(arg) == 1:
62-
res = decode(result['reply']['arg'])
63-
else:
64-
res = decode(result['reply']['arg'], arg[1])
65-
return res
60+
return decode(result['reply']['arg'], return_type)
6661
elif result['status'] == 'rejected':
6762
return result['reject_message']
6863

69-
def update_raw(self, canister_id, method_name, *arg, **kwargs):
70-
assert len(arg) == 1 or len(arg) == 2
64+
def update_raw(self, canister_id, method_name, arg, return_type = None, effective_canister_id = None, **kwargs):
7165
req = {
7266
'request_type': "call",
7367
'sender': self.identity.sender().bytes,
7468
'canister_id': Principal.from_str(canister_id).bytes if isinstance(canister_id, str) else canister_id.bytes,
7569
'method_name': method_name,
76-
'arg': arg[0],
70+
'arg': arg,
7771
'ingress_expiry': self.get_expiry_date()
7872
}
7973
req_id, data = sign_request(req, self.identity)
80-
_ = self.call_endpoint(canister_id, req_id, data)
74+
eid = canister_id if effective_canister_id is None else effective_canister_id
75+
_ = self.call_endpoint(eid, req_id, data)
8176
# print('update.req_id:', req_id.hex())
82-
status, result = self.poll(canister_id, req_id, **kwargs)
77+
status, result = self.poll(eid, req_id, **kwargs)
8378
if status != 'replied':
84-
return status
79+
return status
8580
else:
86-
if len(arg) == 1:
87-
res = decode(result)
88-
else:
89-
res = decode(result, arg[1])
90-
return res
81+
return decode(result, return_type)
82+
9183

9284

9385
def read_state_raw(self, canister_id, paths):

ic/candid.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# candid.example.py shows how to use candid's en/decode
22

33
import leb128
4+
from collections.abc import Iterable
45
from struct import pack,unpack
56
from abc import abstractclassmethod, ABCMeta
67
from enum import Enum
@@ -528,7 +529,7 @@ def __init__(self, _type: Type):
528529
self._type = _type
529530

530531
def covariant(self, x):
531-
return type(x) == list and not False in list(map(self._type.covariant, x))
532+
return isinstance(x, Iterable) and not False in list(map(self._type.covariant, x))
532533

533534
def encodeValue(self, val):
534535
length = leb128.u.encode(len(val))

ic/canister.py

+9-5
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from .parser.DIDEmitter import *;
22
from antlr4 import *
33
from antlr4.InputStream import InputStream
4-
from .candid import encode
4+
from .candid import encode, FuncClass
55

66
class Canister:
77
def __init__(self, agent, canister_id, candid=None):
@@ -30,8 +30,9 @@ def __init__(self, agent, canister_id, candid=None):
3030
self.actor = emitter.getActor()
3131

3232
for name, method in self.actor["methods"].items():
33-
anno = None if len(method[2]) == 0 else method[2][0]
34-
setattr(self, name, CaniterMethod(agent, canister_id, name, method[0], method[1], anno))
33+
assert type(method) == FuncClass
34+
anno = None if len(method.annotations) == 0 else method.annotations[0]
35+
setattr(self, name, CaniterMethod(agent, canister_id, name, method.argTypes, method.retTypes, anno))
3536

3637
class CaniterMethod:
3738
def __init__(self, agent, canister_id, name, args, rets, anno = None):
@@ -50,19 +51,22 @@ def __call__(self, *args, **kwargs):
5051
for i, arg in enumerate(args):
5152
arguments.append({"type": self.args[i], "value": arg})
5253

54+
effective_cansiter_id = args[0]['canister_id'] if self.canister_id == 'aaaaa-aa' and len(args) > 0 and type(args[0]) == dict and 'canister_id' in args[0] else self.canister_id
5355
if self.anno == 'query':
5456
res = self.agent.query_raw(
5557
self.canister_id,
5658
self.name,
5759
encode(arguments),
58-
self.rets
60+
self.rets,
61+
effective_cansiter_id
5962
)
6063
else:
6164
res = self.agent.update_raw(
6265
self.canister_id,
6366
self.name,
6467
encode(arguments),
65-
self.rets
68+
self.rets,
69+
effective_cansiter_id
6670
)
6771

6872
if type(res) is not list:

ic/common/__init__.py

Whitespace-only changes.

0 commit comments

Comments
 (0)