[英]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.