rocklabs-io / ic-py

Python Agent Library for the DFINITY Internet Computer
MIT License
128 stars 27 forks source link

Struggling encoding params #27

Closed kylelangham closed 2 years ago

kylelangham commented 2 years ago

Hello, I'm struggling to understand how to encode the params correctly for a types record. I'm trying to query the list_neurons method of 'rrkah-fqaaa-aaaaa-aaaaq-cai' (canlista). I've tried multiple combinations for the encoding, but receive errors similar to below.

`from ic.client import Client from ic.identity import Identity from ic.agent import Agent from ic.candid import encode, decode, Types

iden = Identity() client = Client(url = "https://ic0.app") agent = Agent(iden, client) params = [{'type': Types.Record({'name':Types.Text, 'assets': Types.Nat}), 'value': {'name': 'neuron_ids','assets': 4009}},{'type': Types.Bool, 'value': True}] params = encode(params) can_id = 'rrkah-fqaaa-aaaaa-aaaaq-cai' function = 'list_neurons' response = agent.query_raw(can_id, function, params) print(response) `

Error: IC0503: Canister rrkah-fqaaa-aaaaa-aaaaq-cai trapped explicitly: Panicked at 'Deserialization Failed: "Fail to decode argument 0 from table0 to record { neuron_ids : vec nat64; include_neurons_readable_by_caller : bool }"', /ic/rs/rust_canisters/dfn_core/src/endpoint.rs:34:41

bodily11 commented 2 years ago

You probably want something like this. This works for me.

from ic.client import Client
from ic.identity import Identity
from ic.agent import Agent
from ic.candid import encode, decode, Types

iden = Identity()
client = Client(url = "https://ic0.app")
agent = Agent(iden, client)

types = Types.Record({
        'neuron_ids':Types.Vec(Types.Nat64),
        'include_neurons_readable_by_caller': Types.Bool,
})

values = {
        'neuron_ids':[4008],
        'include_neurons_readable_by_caller': False,
}

params = [{'type': types, 'value': values}]
params = encode(params)
can_id = 'rrkah-fqaaa-aaaaa-aaaaq-cai'
function = 'list_neurons'
response = agent.query_raw(can_id, function, params)
kylelangham commented 2 years ago

That did the trick! Thanks!