简体   繁体   English

Solidity:“如果您发送价值,则应支付称为 function 并且您发送的价值应小于当前余额”

[英]Solidity: "The called function should be payable if you send value and the value you send should be less than your current balance"

Im new to Solidity, I got a problem that when I call purchaseCard function, it always returns the error The called function should be payable if you send value and the value you send should be less than your current balance.我刚接触 Solidity,我遇到一个问题,当我调用purchaseCard function 时,它总是返回错误The called function should be payable if you send value and the value you send should be less than your current balance. on Remix IDE.在混音 IDE 上。

I think the problem is at the complexity of my structs or I probably called variables not allowed.我认为问题在于我的结构的复杂性,或者我可能称变量为不允许。 But I still cant find fix after many days researching.但是经过多天的研究,我仍然找不到解决方法。


These are my structs.这些是我的结构。

struct Card {
    string name;
    uint price;
}
mapping(uint => Card) public cards;
uint public numberOfTypes;

struct Purchase {
    Card card;
    uint purchaseDate;
}

struct User {
    uint numberOfCards;
    Purchase[] purchase;
    bool exist;
}
mapping(address => User) public users;

And these are my functions, I will make it short.这些是我的功能,我会简短地说。

// function addCard(string memory _name, uint _price) public isOwner;
// function removeCard(uint _id) public isOwner;
// function checkExistedUser(address _userAddress) public view returns(bool);

function purchaseCard(uint _id) public {
    User storage user = users[msg.sender];
    if (!checkExistedUser(msg.sender)) {
        user.exist = true;
        user.numberOfCards = 0;
    }
    Purchase storage purchase = user.purchase[user.numberOfCards];
    purchase.card = cards[_id];
    purchase.purchaseDate = block.timestamp;
    user.numberOfCards++;
}

// function expiredCard(uint _id) public;
// function showRemainingDate(uint _id) public view returns(uint);
// function showPurchasedCards() public view returns (Purchase[] memory);

This is my full code: https://github.com/SnowyField1906/code/blob/main/purchase.sol这是我的完整代码: https://github.com/SnowyField1906/code/blob/main/purchase.sol

Thank you very much, I hope to learn more things.非常感谢,希望能学到更多东西。

In this case, you cannot create an empty 'space' for purchase array in storage before fill it because the type of this operation is an array and not a mapping!在这种情况下,您不能在填充之前在存储中为purchase数组创建一个空的“空间”,因为此操作的类型是数组而不是映射!

To solve this issue, you can use the push() method (available only for storage array) to fill purchase() array into Purchase struct.为了解决这个问题,您可以使用push()方法(仅适用于存储数组)将 purchase() 数组填充到Purchase结构中。

Following your logic, you must change purchaseCard() method in this way:按照您的逻辑,您必须以这种方式更改purchaseCard()方法:

function purchaseCard(uint _id) public {
   User storage user = users[msg.sender];
   if (!checkExistedUser(msg.sender)) {
     user.exist = true;
     user.numberOfCards = 0;
   }
   user.purchase.push(Purchase(cards[_id], block.timestamp));
   user.numberOfCards++;
}

In Solidity, If you have to send/receive ETHs then the payable function should be used.在 Solidity 中,如果您必须发送/接收 ETH,则应使用payable的 function。 Try this:尝试这个:

function purchaseCard(uint _id) public payable {

or add payable to your constructor.或添加payable给您的构造函数。

pls refer to this: https://solidity-by-example.org/payable/请参考: https://solidity-by-example.org/payable/

And btw pls next time share the complete code snippet.顺便说一句,请下次分享完整的代码片段。

I can't able to understand the entire code and flow.. but while debugging..我无法理解整个代码和流程..但是在调试时..

the revert occurs in the belowline of the purchasecard function还原发生在购买卡 function 的下一行

 Purchase storage purchase = user.purchase[user.numberOfCards];

It seems like you are accessing the user mapping which does not contain any User Struct..您似乎正在访问不包含任何用户结构的用户映射。

I think you forgot to assing the value to the user mapping in the addCard Function.我认为您忘记将值分配给 addCard Function 中的用户映射。

I may be wrong and I hope it helps..我可能是错的,我希望它有帮助..

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 solidity 交易错误,如果您发送 value 应该支付调用的 function 并且您发送的 value 应该小于您当前的余额 - solidity transaction error, The called function should be payable if you send value and the value you send should be less than your current balance 我收到“如果您发送价值并且您发送的价值应该小于您当前的余额,则应该支付所谓的 function。” - I am getting "The called function should be payable if you send value and the value you send should be less than your current balance." 可靠性:调用另一个合约的函数时出错。 错误:发送值时应向构造函数付款 - Solidity: Error when calling a function of another contract. Error: The constructor should be payable when you send value solidity中payable function如何通过msg.value方式收款? - How payable function in solidity receives money by msg.value method? 如何使用 HardHat 调用可支付的 Solidity 函数? - How to invoke a payable solidity function using HardHat? 撤回 function 发送仅适用于“应付地址”类型的对象 - Withdraw function send is only available for objects of type "address payable" Solidity 文档薄荷并发送 function - Solidity documentation mint and send function 证明你在 Solidity 中执行了视图 function - Prove that you executed view function in Solidity 叫function突然需要应付 - Called function suddenly needs to be payable 如何测试 Solidity 应付合同 - How to test a Solidity payable contract
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM