rocklabs-io / ic-py

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

how to send records similar to dfx #86

Open aknowel opened 1 year ago

aknowel commented 1 year ago

Hi I have a problem using ic-py library.

I would like to mint canister tokens and send request similar to

dfx canister --network ${NETWORK} call ${CANISTER_ADDRESS} transfer '(record {
  memo = 0 : nat64;
  amount = record { e8s = 100 : nat64 };
  fee = record { e8s = 0 : nat64 };
  from_subaccount = null;
  to = '${TOKENS_TRANSFER_ACCOUNT_ID_BYTES}' : vec nat8;
  created_at_time = null;
})'

But I am not sure how to use ic-py params to fulfill my needs. It would be ideal to do something similar to

params = {
    "memo": 0,
    "amount": { "e8s": 100 },
    "fee": { "e8s": 0 },
    "from_subaccount": None,
    "to": ["${TOKENS_TRANSFER_ACCOUNT_ID_BYTES}"],
    "created_at_time": None,
}
result = agent.update_raw("${CANISTER_ADDRESS}", "transfer", encode(params))
print(result)

If you have any idea what approach could I take, please share. Library documentation has only few simple examples and so I couldn't get it done yet

Myse1f commented 1 year ago

You need to provide the type structure, like

# transfer 100 token to blackhole address `aaaaa-aa`
params = [
    {'type': Types.Principal, 'value': 'aaaaa-aa'},
    {'type': Types.Nat, 'value': 10000000000}
]
result = agent.update_raw("gvbup-jyaaa-aaaah-qcdwa-cai", "transfer", encode(params))

For ur code, set params like this:

params = [{
    'type': Types.Record({
          "memo": Types.Nat64,
          "amont": Types.Record({"e8s": Types.Nat64}),
          ......
     }), 
     'value': {
    "memo": 0,
    "amount": { "e8s": 100 },
    "fee": { "e8s": 0 },
    "from_subaccount": None,
    "to": ["${TOKENS_TRANSFER_ACCOUNT_ID_BYTES}"],
    "created_at_time": [], # None is empty array
}
}]

Better to use Canister to parse the did file. Pls refer to here