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:
Open MARCO Console.
On the left sidebar, go to Wallet Instances > Instances.
Choose the wallet that will sign the transaction.
Open the Identity Permissions tab.
In the Add Identity field, write the service account's email address that will make the API call.
noteYou can find the service account's email address in the Service Accounts > List Service account page on the MARCO Console.
Click Add. This will add the service account to the list of identities with permission to view the wallet.
Choose the service account from the list.
In the Grant Role field, select Wallets user.
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:
Open MARCO Console.
On the left sidebar, go to Smart contract > Instances.
Choose the smart contract you want to call one of its methods.
Open the Identity Permissions tab.
In the Add Identity field, add the service account's email address that will make the API call.
noteYou can find the service account's email address in the Service Accounts > List Service account page on the MARCO Console.
Choose the service account from the list.
Ensure the service account has the Viewer role granted.
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 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 theglo.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 status using the transaction locator
from the previous step.
To do this, send a POST
request to the https://api-marco.finboot.com/ledgeropsgateway/ledger/operation/transaction/search?ledgerId=<LEDGER_ID>&locator=<LOCATOR_ID>
endpoint as follows:
curl --location --request GET 'https://api-marco.finboot.com/ledgeropsgateway/ledger/operation/transaction/search?ledgerId=<LEDGER_ID>&locator=<LOCATOR_ID>&withReceipt=true' \
--header 'ApplicationToken: <APPLICATION_TOKEN>' \
--header 'Accept: application/json' \
--header 'Authorization: Bearer <ACCESS_TOKEN>'
Replace:
APPLICATION_TOKEN
: The marpp token from the Get your API tokens guide.ACCESS_TOKEN
: The access token from the Get your API tokens guide.LEDGER_ID
: The ID of the network associated to the that belongs to the ledger. For example, you can useLGR-3c8de984e49
forfinboot-clique-v1
network. To get the list of ids, see Networks.LOCATOR_ID
: The locator ID of the transaction you sent in the previous step. For example,0x10e3df78af8e3abbd66fd9c5e4fb41908d5ce31360e7bd0d563779bd0a5c5362
.
Once the transaction receives a confirmation, you should see status
set to succeeded
in the response.
{
"locator": "0x10e3df78af8e3abbd66fd9c5e4fb41908d5ce31360e7bd0d563779bd0a5c5362",
"status": "succeeded"
}