1

I have a situation where I believe I followed along the documentation and online information to make it work, but I haven't managed to do so yet.

I have migrated from Viem 0.8.x towards 1.4.1. This is the function I try to read from my ABI:

{
        inputs: [],
        name: 'getTreasuryAndPhilantrophyBalanceInUSD',
        outputs: [
            {
                internalType: 'uint256',
                name: 'treasuryBalance',
                type: 'uint256',
            },
            {
                internalType: 'uint256',
                name: 'philantrophyBalance',
                type: 'uint256',
            },
        ],
        stateMutability: 'view',
        type: 'function',
},

I export my ABI as a const, in a TS file:

import { Abi } from 'viem';

export const abiTest: Abi = [...] as const;

This is my ReadContract implementation:

const resultRead = await client.readContract({
        abi: abiCfaPeriphery,
        address: '...' as Address,
        functionName: 'getTreasuryAndPhilantrophyBalanceInUSD',
    });

    console.log('Result Read: ', resultRead);

My console log appears as:

Result Read:  [ 902828817075004553524n, 0n ]

Before, I used to get:

Result Read:  [ 902828817075004553524n, 0n, treasuryBalance:xxxx, philantrophyBalance:xxxxx ]

So in my TypeScript implementation I could do:

console.log(resultRead.treasuryBalance);
console.log(resultRead.philantrophyBalance);

But somehow I have lost this ability, which has broken all my implementation. Is there's something I'm missing?

I have read all the information here:

Viem Type Inference Explanation

TypeScript Const assertions

Thanks

Raül
  • 137
  • 3
  • 15
  • looks like you already gutted the abi field from that json file, you either need do create a property within the json file called abi, or you need to import it as the abi itself without the {} – johnny 5 Aug 16 '23 at 13:47

1 Answers1

1

I managed to figure out the issue through asking in the official github repo, opening a discussion. The answer is here:

https://viem.sh/docs/faq.html#why-is-a-contract-function-return-type-returning-an-array-instead-of-an-object

Basically, I was using ethers.js earlier, and it seems that ethers was returning a hybrid Array/Object type, which is not supposed to be happening through Javascript.

Basically, in order to achieve the same feature, you should be returning tuples in your smart contract, so the object is defined.

Raül
  • 137
  • 3
  • 15