Skip to main content

Call a smart contract method

In this guide, you will learn how to call a smart contract method using MARCO API.

Before you begin

Before you start, complete the following prerequisites:

1. Get permission to use the wallet

Behind the scenes, when you call a smart contract method on MARCO, the platform creates a low-level representation of the transaction to be sent to the chosen ledger. This transaction must be signed with the sender's wallet's private key before it's broadcasted to the network.

To grant privileges to the service account to use the sender's wallet, follow these steps:

  1. Open MARCO Console.

  2. On the left sidebar, go to Wallet Instances > Instances.

    Wallet instances list

  3. Choose the wallet that will sign the transaction.

  4. Open the Identity Permissions tab.

  5. In the Add Identity field, write the service account's email address that will make the API call.

    Wallet instances identity permissions

    note

    You can find the service account's email address in the Service Accounts > List Service account page on the MARCO Console.

  6. Click Add. This will add the service account to the list of identities with permission to view the wallet.

  7. Choose the service account from the list.

  8. In the Grant Role field, select Wallets user.

    Wallet instances grant role

  9. Click Grant. This will grant the service account permission to use the wallet.

2. Get permission to call the smart contract

Next, grant privileges to the service account to view the smart contract. To do this, follow the steps below:

  1. Open MARCO Console.

  2. On the left sidebar, go to Smart contract > Instances.

    SCI list

  3. Choose the smart contract you want to call one of its methods.

  4. Open the Identity Permissions tab.

  5. In the Add Identity field, add the service account's email address that will make the API call.

    SCI identity permissions

    note

    You can find the service account's email address in the Service Accounts > List Service account page on the MARCO Console.

  6. Choose the service account from the list.

  7. Ensure the service account has the Viewer role granted.

    SCI role

3. Call the smart contract method

For this guide, we assume that you have deployed a contract named registry-contract-instance on the finboot-clique-v1 network. This contract has the following method you want to call:

function addMember(address _address) external;

To call the smart contract method, send a POST request to the https://api-marco.finboot.com/resource-proxy/marco-cloud-wallet/api/v1/cloud-wallet/send-glo-transaction endpoint as follows:

curl --location --request POST 'https://api-marco.finboot.com/resource-proxy/marco-cloud-wallet/api/v1/cloud-wallet/send-glo-transaction' \
--header 'ApplicationToken: <APPLICATION_TOKEN>' \
--header 'Authorization: Bearer <ACCESS_TOKEN>' \
--header 'Content-Type: application/json' \
--data-raw '{
"glo":{
"version":"0.1.0",
"type":"contract_call",
"options":{
"contract_instance":{
"lookup_service":{
"type":"marco",
"value":"marco"
},
"resource":"<SMART_CONTRACT_INSTANCE_ID>"
},
"method_name":"<SMART_CONTRACT_METHOD_NAME>",
"args":[
"<ARG0>"
]
},
"meta":{

}
},
"walletId":"<SENDER_WALET_ID>",
"password":"<SENDER_WALET_PASSWORD>",
"options":{"secureSessionToken":null}
}'

Replace:

  • APPLICATION_TOKEN: The marpp token from the Get your API tokens guide.
  • ACCESS_TOKEN: The service account token from the Get your API tokens guide.
  • SMART_CONTRACT_INSTANCE_ID: The resource ID of the smart contract instance you want to call. For example, registry-contract-instance.
  • SMART_CONTRACT_METHOD_NAME: The name of the method you want to call. For example, addMember.
  • ARG0: Argument you want to pass to the method. For example, 0x3f5CE5FBFe3E9af3971dD833D26bA9b5C936f0bE. Note that you can pass multiple arguments in the glo.args array.
  • SENDER_WALLET_ID: The resource ID of the wallet you want to send the transaction from. For example, test-wallet-sender.
  • SENDER_WALET_PASSWORD: The password of the wallet you want to send the transaction from.

Once the request is sent, the API will return the result of the method call. For example:

{
"version": "0.0.1",
"ledgerId": "finboot-clique-v1",
"locator": "0x10e3df78af8e3abbd66fd9c5e4fb41908d5ce31360e7bd0d563779bd0a5c5362",
"trl": {...},
"intent": "e1fc9135-7b31-4f5c-bc1e-574a4aae0fc5",
"receipts": [
{
"hash": "0x10e3df78af8e3abbd66fd9c5e4fb41908d5ce31360e7bd0d563779bd0a5c5362"
}
]
}

4. Check the transaction status

Finally, you can check the transaction information using the trl object from the previous step. To do this, send a POST request to the https://api-marco.finboot.com/resource-proxy/<EXPLORER_PLUGIN>/api/tx/search/trl endpoint as follows:

curl --location --request POST 'https://api-marco.finboot.com/resource-proxy/<EXPLORER_PLUGIN>/api/tx/search/trl' \
--header 'ApplicationToken: <APPLICATION_TOKEN>' \
--header 'Content-Type: application/json' \
--header 'Authorization: Bearer <ACCESS_TOKEN>' \
--data-raw '{
"search": {
"version": "0.0.1",
"absoluteResourceLocator": {
"version": "0.0.1",
"path": [
{
"contextualLocator": {
"lookup_service": {
"type": "marco",
"value": "marco"
},
"resource": "finboot-clique-v1"
},
"metadata": {
"resourceType": "ledger"
}
},
{
"contextualLocator": {
"lookup_service": {
"type": "ledger",
"value": "ethereum"
},
"resource": "0x1E20E5d5612bC233b2e11DD3Dd148A2D3Bb6ab72"
},
"metadata": {
"resourceType": "wallet"
}
},
{
"contextualLocator": {
"lookup_service": {
"type": "ledger",
"value": "ethereum"
},
"resource": "0x12"
},
"metadata": {
"resourceType": "transaction"
}
}
]
}
},
"results": [],
"totalItems": 1
}'

Replace:

  • EXPLORER_PLUGIN: The ID of the explorer plugin that belongs to the ledger. For example, you can use finboot-clique-v1-explorer-1-plugin for finboot-clique-v1 network.
  • search: The trl object you received in the previous step.

Once the transaction receives a confirmation, you should see a blockHeight under the results field.

{
"search": {
...
"results": [
{
"blockHeight": 22696533,
"hash": "0x10e3df78af8e3abbd66fd9c5e4fb41908d5ce31360e7bd0d563779bd0a5c5362",
"value": "0",
"nonce": "0xe",
"from": "0xC7C9f18aC95DcEaDf08A7A9cf47485C2c4F0D49E",
"to": "0x528f7841E8402edF3b1D1548Ed8960bB92FBc9BE",
"receipt": {
"status": "0x1",
"contractAddress": null
}
}
],
...
}