Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add contract invoke helper to ConcordiumProvider #238

Open
Bargsteen opened this issue Jan 5, 2023 · 1 comment
Open

Add contract invoke helper to ConcordiumProvider #238

Bargsteen opened this issue Jan 5, 2023 · 1 comment
Labels
[Type] Change Request Some visible functionality should be change.

Comments

@Bargsteen
Copy link

Description
The Concordium provider that you get from detectConcordiumProvider has a nice interface for contract init and update where it automatically serializes the parameters using a provided schema. But when you want to invoke a contract, you have to go down one level of abstraction and use jsonRpcClient.

It would be great if we could add a helper method for invoking contracts at the same level of abstraction as init and update.

Current

With contract updates, we can provide a JSON parameter and a module schema as a string

 client
.sendTransaction(
    currentAccountAddress,
    concordiumSDK.AccountTransactionType.Update,
    {
        amount: new concordiumSDK.CcdAmount(0n),
        contractAddress: {
            index: contractIndex,
            subindex: 0n,
        },
        receiveName: 'voting.vote',
        maxContractExecutionEnergy: 3000n,
    },
    votingOption,
    rawModuleSchema,
)
.then((msg) => alert(`Successfully sent vote with transaction hash: "${msg}"`))
.catch(alert);

But for invocations, you have to use the json rpc client directly, which is at a lower level of abstraction.

Here, you have to serialize the parameter and deserialize the return value manually.
You also have to specify contract and entrypoint as two separate parameters, instead of a receive name (<contract_name>.<entrypoint_name>).

const inputParams = serializeUpdateContractParameters("voting.view", { my: "JSON parameter"}, rawModuleSchema);

client.getJsonRpcClient().invokeContract({ // Using json rpc client
    contract: { index: BigInt(contractIndex), subindex: BigInt(0) },
    method: 'voting.view',
    parameter: inputParams
})
.then((viewResult) => {
    let returnValue = concordiumSDK.deserializeReceiveReturnValue(
        concordiumSDK.toBuffer(viewResult.returnValue, 'hex'),
        concordiumSDK.toBuffer(rawModuleSchema, 'base64'),
        "voting",
        "view", // NOTE: Here it is just the entrypoint name, i.e. the contract name is not prepended.
        concordiumSDK.SchemaVersion.V2
    );
})
.catch(alert);

Suggestion

client.invokeContract({                  //  <-- Helper function directly on client
    contract: { index: BigInt(contractIndex), subindex: BigInt(0) },
    method: 'voting.view',
    parameter: { my: "JSON parameter" }, // <-- Directly provide json schema
    rawModuleSchema                      // <-- And schema
})
.then((viewResult) => {
    // The viewResult is the deserialized return value.
    let field_a = viewResult.a;
})
.catch(alert);
@Bargsteen Bargsteen added the [Type] Change Request Some visible functionality should be change. label Jan 5, 2023
@abizjak
Copy link
Member

abizjak commented Jan 5, 2023

Can I add to this that it would also be nice to expose CIS2 functionality for querying balance and metadata directly. Like the cis2Client we have in the Rust SDK.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[Type] Change Request Some visible functionality should be change.
Projects
None yet
Development

No branches or pull requests

2 participants