Decoding Kernel Responses in Solidity
Last updated
Was this helpful?
Last updated
Was this helpful?
This page explains how to decode responses from kernel(s) in your Solidity smart contract. The decoding part of Solidity plays a crucial role in both your smart contract and Token Authority. Following the methods presented on this page will allow your smart contract and/or Token Authority to see the responses from kernels.
It is worth mentioning that the decoding section can be used for both TA and registered smart contracts.
Decoding on-chain is fairly simple.
Let's use kernel 337 as an example.
As we can see from the figure above, the Function Return Type of this on-chain kernel is uint256.
The decoding part of Solidity is shown in the example snippet below.
The names of variables from the off-chain response matter.
The number value from off-chain kernels can be decoded using int256.
The output after decoding on Solidity would give the value in 18 decimal places.
For example, a value of 12.34 from an off-chain kernel would be 12340000000000000000.
This includes the float value as well.
The below table shows a line of code which might be helpful to decode off-chain kernel(s) responses.
Method: GET
Response:
{
"name": "John"
}
This example would fall into the first case of the below table.
It is worth mentioning that the decoding part can be used in both the TA and registered smart contract.
The variable names in your smart contract do not have to match those in the kernel response.
However, aligning them can make tracking easier.
number (no decimal places)
float
boolean
string
null
Null values require a precise decoding method
array
simple response
Simple responses, such as in the example ,can also be decoded
multiple fields object
multiple fields object 2
nested object
The decoding part of off-chain kernels will have to be sorted in alphabetical order.
The reason why variable names from off-chain kernels matter is because of when the KRNL node packs the responses from off-chain kernels all together. Even though the API response does not return with the alphabetical order, the decoding part of the Solidity smart contract still requires it to be decoded alphabetically.
Let's use the mock off-chain response as in the below example.
The decoding part in Solidity should be in the below snippet.
When it comes to null value responses, it is important to handle responses precisely.
Null value responses from the off-chain kernels could potentially cause the transaction to fail if the decoding part of Solidity does not handle the response properly.
The Solidity language does not offer a "null" value.
A kernel ID of ABC will be used as an example of a simple off-chain kernel.
When the value is null, it will return the default value of the actual type from the OpenAPI schema.
We have to observe the response data type when it does not come back with null value.
Let's hypothetically say that kernel ABC can give us either a string value or null value.
This means that the response can be either "null" or "string".
The decoding part in the Solidity smart contract would have to be string.
When it comes to simple responses from off-chain kernels, such as in the code snippet below, KRNL nodes can also handle and send them to the Solidity smart contract.
Fundamentally, this is a rare incident that may take place when calling a request to an API, but it does not mean that it cannot happen.
The code-base of the KRNL node will handle these types of responses similar to the object responses.
See below for a simpler explanation.
The decoding part of a Solidity smart contract should be straightforward.
The reason why the node can handle the simple responses, like in this scenario, is not because it happens automatically, but because our team has already handled this type of response.
We recommend visiting the Supported OpenAPI Specifications page () to understand more about how to decode off-chain kernels.
Example Endpoint: