坚固函数返回ethersjs/hardhat上的空数组,而不是在混音中返回吗?

发布于 2025-02-14 00:49:51 字数 1291 浏览 0 评论 0原文

嗨,我正在学习坚固的性能,我有一个问题,即一个功能似乎可以在混音中起作用,但对Etherjs/hardhat不起作用? (ABI也一样)

代码:

function getOwnCards() external view returns(uint[] memory _cards) {
    uint[] memory result = new uint[](userToCardsCount[msg.sender]);
    uint counter = 0;
    for (uint i = 0; i < cards.length; i++) {
        if (cardIdToUser[i] == msg.sender) {
            result[counter] = cards[i];
            counter++;
        }
    }
    return result;
}

混音IDE中的结果:

    0:
uint256[]: _cards 62180033277912311002789072835418559502128313467724667052439679862541172075622,31220469133090072532919887703804680190688655804240516732101690706958488894069,35568354368614544905898949308353469583138320508979990175291873927902398948450

Ethersjs中的结果:

[]

Fe代码:

const getCards = async () => {
    if (typeof window.ethereum !== 'undefined'){
        //@ts-ignore 
        const provider = new ethers.providers.Web3Provider(window.ethereum) 
        const contract = new ethers.Contract(cardsAddress, Cards.abi, provider)
        try {
            const data = await contract.getOwnCards()
            console.log(data)
        } catch (error) {
            console.log(error)
        }
    }
}

Hi I am learning Solidity and I have the problem that a function seems to work on remix ide but not with etherjs/hardhat? (The abi's are the same too)

The code:

function getOwnCards() external view returns(uint[] memory _cards) {
    uint[] memory result = new uint[](userToCardsCount[msg.sender]);
    uint counter = 0;
    for (uint i = 0; i < cards.length; i++) {
        if (cardIdToUser[i] == msg.sender) {
            result[counter] = cards[i];
            counter++;
        }
    }
    return result;
}

The result in remix ide:

    0:
uint256[]: _cards 62180033277912311002789072835418559502128313467724667052439679862541172075622,31220469133090072532919887703804680190688655804240516732101690706958488894069,35568354368614544905898949308353469583138320508979990175291873927902398948450

The result in ethersjs:

[]

The FE code:

const getCards = async () => {
    if (typeof window.ethereum !== 'undefined'){
        //@ts-ignore 
        const provider = new ethers.providers.Web3Provider(window.ethereum) 
        const contract = new ethers.Contract(cardsAddress, Cards.abi, provider)
        try {
            const data = await contract.getOwnCards()
            console.log(data)
        } catch (error) {
            console.log(error)
        }
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

稍尽春風 2025-02-21 00:49:51

解决方案1:

更改味精为给定参数:

function getCardsByOwner(address user) external view returns(uint[] memory _cards) {
    uint[] memory result = new uint[](userToCardsCount[user]);
    uint counter = 0;
    for (uint i = 0; i < cards.length; i++) {
        if (cardIdToUser[i] == user) {
            result[counter] = cards[i];
            counter++;
        }
    }
    return result;
}

解决方案2:

在fe中呼叫函数正确

const getCards = async () => {
    if (typeof window.ethereum !== 'undefined'){
        //@ts-ignore 
        const provider = new ethers.providers.Web3Provider(window.ethereum) 
        const signer = provider.getSigner()
        const contract = new ethers.Contract(cardsAddress, Cards.abi, signer)
        try {
            const data = await contract.getCardsByOwner(signer.getAddress())
            console.log(await contract.getOwnCards())
            console.log(data)
        } catch (error) {
            console.log(error)
        }
    }
}

Solution 1:

change msg.sender to a given parameter:

function getCardsByOwner(address user) external view returns(uint[] memory _cards) {
    uint[] memory result = new uint[](userToCardsCount[user]);
    uint counter = 0;
    for (uint i = 0; i < cards.length; i++) {
        if (cardIdToUser[i] == user) {
            result[counter] = cards[i];
            counter++;
        }
    }
    return result;
}

Solution 2:

call function in FE Correct

const getCards = async () => {
    if (typeof window.ethereum !== 'undefined'){
        //@ts-ignore 
        const provider = new ethers.providers.Web3Provider(window.ethereum) 
        const signer = provider.getSigner()
        const contract = new ethers.Contract(cardsAddress, Cards.abi, signer)
        try {
            const data = await contract.getCardsByOwner(signer.getAddress())
            console.log(await contract.getOwnCards())
            console.log(data)
        } catch (error) {
            console.log(error)
        }
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文