Type conversions
MultiBaas provides users with the ability to, on a per-method per-input/output parameter basis, adjust how the value is interpreted on its way to the blockchain, or adjust the return value on its way back from the blockchain.
One example would be to dynamically insert a decimal point into a smart contract method that returns an amount of type uint256. As exact decimal numbers are not supported by Solidity, the standard way of handling this situation is to use an integer and shift it by a fixed number of decimal places.
In the popular ERC20 token standard, a decimals() method provides the number of decimals place to shift the uint256 (unsigned integer) values for other methods input/output parameters that relate to token balances. For example, if decimals() returns 3, then the return value of the totalSupply() method should be shifted by 3 decimal places to the left.
We start by querying the totalSupply() method for the “autotoken” instance (an alias for the address of a smart contract deployed on the Ethereum blockchain).
Request:
POST .../chains/ethereum/addresses/autotoken/contracts/mltitoken/methods/totalSupply
Response:
{
"status": 200,
"message": "Success",
"result": {
"output": "123000",
"summarizedInputs": []
}
}
The total supply is therefore 123,000. If we query the contract’s decimals() method:
Request:
POST .../chains/ethereum/addresses/autotoken/contracts/mltitoken/methods/decimals
Response:
{
"status": 200,
"message": "Success",
"result": {
"output": 3,
"summarizedInputs": []
}
}
Therefore, the result of totalSupply(), along with all other balance/amount method input/output parameters should be shifted by 3 decimal places to the left.
Note that the return value of totalSupply() was automatically enclosed in quotations by MultiBaas. This is done because it is a uint256 value and could overflow JavaScript’s 32-bit limit on integer sizes. The application would have to handle this as a JavaScript big number or string, as appropriate. The return value of the decimals() function, on the other hand, is of type uint8 (maximum decimal 256), and is automatically left unquoted by MultiBaas, so that the application could use it directly as a JavaScript number.
We could instruct MultiBaas to automatically shift the result of the totalSupply() function by 3 places:
Request:
POST .../contracts/mltitoken/1.0/methods/totalSupply
{
"outputs": [
{
"typeConversion": {
"mode": "decimals",
"decimalsAbsolute": 3
}
}
]
}
Response:
{
"status": 200,
"message": "Success"
}
We can also instruct MultiBaas to use the output of another function to specify the number of decimals. In this way we can support a varying number of decimals across smart contract instances with a single configuration setup of MultiBaas.
This decimals function must satisfy the following requirements:
- Take no parameters
- Return a single integer value
- Be constant (i.e., not modify the blockchain state at all)
Making another request to MultiBaas to use the output of decimals() for the number of decimals of the totalSupply() method:
Request:
POST .../contracts/mltitoken/1.0/methods/totalSupply
{
"outputs": [
{
"typeConversion": {
"mode": "decimals",
"decimalsFunction": "decimals"
}
}
]
}
Response:
{
"status": 200,
"message": "Success"
}
Again calling the totalSupply() method:
Request:
POST .../chains/ethereum/addresses/autotoken/contracts/mltitoken/methods/totalSupply
Response:
{
"status": 200,
"message": "Success",
"result": {
"output": "123.000",
"summarizedInputs": []
}
}
The total supply is therefore 123.000, with the decimal place shifted three places to the left.
Type Conversion Previews
Type conversions can be previewed by running their inputs or outputs through a temporary, non-persisted type conversion. This is done to generate a preview of what the converted type will look like for presentation in a UI.
The API request and response to preview a type conversion is as follows.
Request:
POST .../chains/ethereum/addresses/typesrest/contracts/typesrest/methods/sendBytesAndReturnBytes
{
"preview": {
"inputs": [
{
"typeConversion": {
"mode": "base64Bytes"
}
}
],
"outputs": [
{
"typeConversion": {
"mode": "base64Bytes"
}
}
]
},
"args": [
"qxI="
]
}
Response:
{
"status": 200,
"message": "Success",
"result": {
"input": [
"0xab12"
],
"output": "qxI="
}
}
Web UI
From the UI you can also update the type conversion settings.
- Select Contracts from the main menu
- Select a specific contract then contract instance from the side menu
- Click Settings at the top of either the Details section or Methods section of the contract page to reveal the pencil icon next to each detail or method name
- Click the pencil icon of the field you wish to edit to reveal its type conversion settings
- Depending on the method's input and output types you will be prompted to select an applicable type conversion
- Select a type conversion and, if necessary, the type conversion settings
- Observe the preview and, if applicable, enter sample input or output to confirm the settings are correct
-
Click
Update
to apply the changes
- Note the changes will apply immediately!
- Click Done at the top of the section ( Details or Methods ) that you were editing to hide the pencil icon next to each detail or method name