-
-
Notifications
You must be signed in to change notification settings - Fork 2k
Open
Labels
enhancementNew feature or improvement.New feature or improvement.fixed/completeThis Bug is fixed or Enhancement is complete and published.This Bug is fixed or Enhancement is complete and published.v6Issues regarding v6Issues regarding v6
Description
Ethers Version
6.11.1
Search Terms
tuple,object,array
Describe the Problem
struct Description {
string moniker;
string identity;
string website;
string securityContact;
string details;
}
struct CommissionRates {
uint256 rate;
uint256 maxRate;
uint256 maxChangeRate;
}
enum BondStatus {
Unspecified,
Unbonded,
Unbonding,
Bonded
}
struct Validator {
address operatorAddress;
string consensusPubkey;
bool jailed;
BondStatus status;
uint256 tokens;
uint256 delegatorShares;
Description description;
int64 unbondingHeight;
int64 unbondingTime;
Commission commission;
uint256 minSelfDelegation;
}
function validator(
address validatorAddr
) external view returns (Validator calldata validator);
This is an interface I defined for querying validators based on addresses. The data returned from the query is as follows:
[
"0x7a24464c2A92C3774f1C7b0FFCbeee759Fa9934E",
"ckQp0mBGYUnbBf1v3PJC4nkdGDuSW2MYO5CazB83J+E=",
false,
"3",
"100000000000000000000",
"100000000000000000000000000000000000000",
[
"node0",
"",
"",
"",
""
],
"0",
"0",
[
[
"100000000000000000",
"1000000000000000000",
"1000000000000000000"
],
"1712309039"
],
"1"
]
However, returning data in the form of an Array is not very convenient for frontend usage. Is it possible to return the data in the form of an Object, like the following:
{
"operatorAddress": "0x7a24464c2A92C3774f1C7b0FFCbeee759Fa9934E",
"consensusPubkey": "ckQp0mBGYUnbBf1v3PJC4nkdGDuSW2MYO5CazB83J+E=",
"jailed": false,
"status": "Bonded",
"tokens": "100000000000000000000",
"delegatorShares": "100000000000000000000000000000000000000",
"description": {
"moniker": "node0",
"identity": "",
"website": "",
"securityContact": "",
"details": ""
},
"unbondingHeight": 0,
"unbondingTime": 0,
"commission": {
"rate": "100000000000000000",
"maxRate": "1000000000000000000",
"maxChangeRate": "1000000000000000000"
},
"minSelfDelegation": "1"
}
Code Snippet
import { ethers } from 'ethers';
import fs from 'fs-extra';
import path from 'path';
BigInt.prototype.toJSON = function () {
return this.toString();
};
export const main = async () => {
const { rpc, contracts, stakingAddress } = await fs.readJSON('../cfg.json');
const { abi } = await fs.readJSON(path.join(contracts, 'staking/IStaking.sol/IStaking.json'));
const provider = new ethers.JsonRpcProvider(rpc);
// input params
const validatorAddress = '0x7a24464c2A92C3774f1C7b0FFCbeee759Fa9934E';
const staking = new ethers.Contract(stakingAddress, abi, provider);
const validator = await staking.validator(validatorAddress);
console.log('validator', JSON.stringify(validator, undefined, 2));
};
main();
Contract ABI
{
"inputs": [
{
"internalType": "address",
"name": "validatorAddr",
"type": "address"
}
],
"name": "validator",
"outputs": [
{
"components": [
{
"internalType": "address",
"name": "operatorAddress",
"type": "address"
},
{
"internalType": "string",
"name": "consensusPubkey",
"type": "string"
},
{
"internalType": "bool",
"name": "jailed",
"type": "bool"
},
{
"internalType": "enum BondStatus",
"name": "status",
"type": "uint8"
},
{
"internalType": "uint256",
"name": "tokens",
"type": "uint256"
},
{
"internalType": "uint256",
"name": "delegatorShares",
"type": "uint256"
},
{
"components": [
{
"internalType": "string",
"name": "moniker",
"type": "string"
},
{
"internalType": "string",
"name": "identity",
"type": "string"
},
{
"internalType": "string",
"name": "website",
"type": "string"
},
{
"internalType": "string",
"name": "securityContact",
"type": "string"
},
{
"internalType": "string",
"name": "details",
"type": "string"
}
],
"internalType": "struct Description",
"name": "description",
"type": "tuple"
},
{
"internalType": "int64",
"name": "unbondingHeight",
"type": "int64"
},
{
"internalType": "int64",
"name": "unbondingTime",
"type": "int64"
},
{
"components": [
{
"components": [
{
"internalType": "uint256",
"name": "rate",
"type": "uint256"
},
{
"internalType": "uint256",
"name": "maxRate",
"type": "uint256"
},
{
"internalType": "uint256",
"name": "maxChangeRate",
"type": "uint256"
}
],
"internalType": "struct CommissionRates",
"name": "commissionRates",
"type": "tuple"
},
{
"internalType": "int64",
"name": "updateTime",
"type": "int64"
}
],
"internalType": "struct Commission",
"name": "commission",
"type": "tuple"
},
{
"internalType": "uint256",
"name": "minSelfDelegation",
"type": "uint256"
}
],
"internalType": "struct Validator",
"name": "validator",
"type": "tuple"
}
],
"stateMutability": "view",
"type": "function"
}
Errors
Can a function that returns tuple data return an Object instead of an Array?
Environment
node.js (v12 or newer)
Environment (Other)
no
Metadata
Metadata
Assignees
Labels
enhancementNew feature or improvement.New feature or improvement.fixed/completeThis Bug is fixed or Enhancement is complete and published.This Bug is fixed or Enhancement is complete and published.v6Issues regarding v6Issues regarding v6