1

I'm trying to get return the balance of an account and it's a fairly simple function in Solidity:

 function getUserBalance(address user) public returns (uint256) { return balanceOf(user, MONEY_TOKEN_ID); } 

But whenever I try to run the endpoint on postman, I keep running into the same issue where it returns as an array of objects.

// app.js const MarketplaceABI = require("./artifacts/contracts/Marketplace.sol/Marketplace.json").abi; const walletSigner = new ethers.Wallet(process.env.PRIVATE_KEY, provider); const contractAddress = require("./ignition//deployments/chain-31337/deployed_addresses.json")["MarketplaceModule#Marketplace"]; const marketplace = new ethers.Contract(contractAddress, MarketplaceABI, walletSigner); app.get('/user-balance/:address', async (req, res) => { try { const { address } = req.params; const balance = await marketplace.getUserBalance(address); console.log("user-balance: ", balance) const formattedBalance = ethers.toString(balance); console.log("Balance fetched for address:", address, "Formatted Balance:", formattedBalance); res.send({ balance: formattedBalance }); } catch (error) { console.error(error.message); res.status(500).send({ error: error.message }); } }) 
user-balance: ContractTransactionResponse { provider: JsonRpcProvider {}, blockNumber: null, blockHash: null, index: undefined, hash: '0xaf94e4f3a77a2ee5f3389ab4351219d215d90696f9d592e94b03f5579156fcca', type: 2, to: '0x5FbDB2315678afecb367f032d93F642f64180aa3', from: '0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266', nonce: 0, gasLimit: 21433n, gasPrice: undefined, maxPriorityFeePerGas: 1000000000n, maxFeePerGas: 3000000000n, maxFeePerBlobGas: null, data: '0x4773489200000000000000000000000070997970c51812dc3a010c7d01b50e0d17dc79c8', value: 0n, chainId: 31337n, signature: Signature { r: "0xc712f2559d963f1e9aacb6b29b7a287c7d98e11eeac7476635d226844c99dd1a", s: "0x1714249c8d9dadb84129fbb199cdbf363153f12c24f22b544b2df302eabaad44", yParity: 0, networkV: null }, accessList: [], blobVersionedHashes: null } Balance fetched for address: 0x70997970C51812dc3A010C7d01b50e0d17dc79C8 Formatted Balance: [object Object] 

How do I convert this just a simple number or a string that returns the actual balance of the account?

1 Answer 1

0

The issue arises because the getUserBalance function is not marked as view, causing it to behave like a transaction rather than a read-only call. Transactions in Ethereum return a ContractTransactionResponse object, not the actual result of the function.

Change the immutability of the function to view:

function getUserBalance(address user) public view returns (uint256) { return balanceOf(user, MONEY_TOKEN_ID); } 
1
  • Hey! This has partially worked. I'm getting a number-ish from solidity (returns something like 700n) but I still can't seem to convert it into a number. It remains [object object]. Commented Jan 21 at 4:03

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.