rocklabs-io / ic-py

Python Agent Library for the DFINITY Internet Computer
MIT License
125 stars 25 forks source link

Error when sending cycles from cycles wallet to canister #69

Closed bodily11 closed 1 year ago

bodily11 commented 2 years ago

Error

---------------------------------------------------------------------------
CBORDecodeValueError                      Traceback (most recent call last)
/var/folders/yg/mzpfyl291vx30knxqlx43d2r0000gn/T/ipykernel_27050/2334640681.py in <module>
     44             params = encode(params)
     45             print(f'topping up {collection_name}, only has {canister_cycles/1e12}T cycles, adding 15T cycles.')
---> 46             response = agent.update_raw(cycles_wallet_canister_id,'wallet_call',params)

/opt/anaconda3/lib/python3.9/site-packages/ic/agent.py in update_raw(self, canister_id, method_name, arg, return_type, effective_canister_id, **kwargs)
    112         _ = self.call_endpoint(eid, req_id, data)
    113         # print('update.req_id:', req_id.hex())
--> 114         status, result = self.poll(eid, req_id, **kwargs)
    115         if status == 'rejected':
    116             raise Exception('Rejected: ' + result.decode())

/opt/anaconda3/lib/python3.9/site-packages/ic/agent.py in poll(self, canister_id, req_id, delay, timeout)
    200         status = None
    201         for _ in wait(delay, timeout):
--> 202             status, cert = self.request_status_raw(canister_id, req_id)
    203             if status == 'replied' or status == 'done' or status  == 'rejected':
    204                 break

/opt/anaconda3/lib/python3.9/site-packages/ic/agent.py in request_status_raw(self, canister_id, req_id)
    179             ['request_status'.encode(), req_id],
    180         ]
--> 181         cert = self.read_state_raw(canister_id, paths)
    182         status = lookup(['request_status'.encode(), req_id, 'status'.encode()], cert)
    183         if (status == None):

/opt/anaconda3/lib/python3.9/site-packages/ic/agent.py in read_state_raw(self, canister_id, paths)
    154         elif ret == b'Could not parse body as read request: invalid type: byte array, expected a sequence':
    155             raise ValueError('Could not parse body as read request: invalid type: byte array, expected a sequence')
--> 156         d = cbor2.loads(ret)
    157         cert = cbor2.loads(d['certificate'])
    158         return cert

CBORDecodeValueError: unknown unsigned integer subtype 0x1c

And my code:

    canister_to_receive_cycles = collection['id']
    collection_name = collection['name']
    params = encode([])
    result = agent.query_raw(canister_to_receive_cycles, 'availableCycles', params)
    if 'availableCycles' in result:
        continue # no availableCycles method on the canister
    else:
        canister_cycles = result[0]['value']
        if canister_cycles / 1e12 > 20:
            continue # don't need to top it up
        else:
            # TOP UP TIME
            # Send cycles from cycles wallet to canister
            cycles_wallet_canister_id = 'xxxxx-xxxxx-xxxxx'
            cycles_to_send = int(15e12) # 0.3T cycles is 0.3e12

            types = Types.Record({
                'args': Types.Vec(Types.Nat8),
                'cycles': Types.Nat64,
                'method_name': Types.Text,
                'canister': Types.Principal
            })

            values = {
                'args':encode([
                    {'type':Types.Record({'canister_id':Types.Principal}),
                     'value':{'canister_id':canister_to_receive_cycles}}]),
                'cycles':cycles_to_send,
                'method_name':'deposit_cycles',
                'canister':'aaaaa-aa'
            }
            params = [{'type':types,'value':values}]
            params = encode(params)
            print(f'topping up {collection_name}, only has {canister_cycles/1e12}T cycles, adding 15T cycles.')
            response = agent.update_raw(cycles_wallet_canister_id,'wallet_call',params)

I have the latest version of ic-py (git pulled and pip installed in the git repo). Any ideas why I might be seeing this unsigned int subtype cbor error parsing the response?

Myse1f commented 2 years ago

I ran this code and it top up successfully.

I have changed the error information. You can try again to see if there are more details about the error.

bodily11 commented 2 years ago

Ok, awesome. Now I get the same error, plus:

ValueError: Unable to decode cbor value: <html>
<head><title>502 Bad Gateway</title></head>
<body>
<center><h1>502 Bad Gateway</h1></center>
<hr><center>nginx/1.21.3</center>
</body>
</html>

Is this a boundary node error? I'm going to have to do some digging on what the 502 Bad Gateway error means...

Myse1f commented 2 years ago

I think it is a network issue, maybe the boundary node is down.