0

I have this code:

function regAgent(uint[] memory _preferences) notOwner onlyBeforeEnd public {
    require(msg.sender.balance - fee >= 0, "You can't pay the fee.");
    require(!registered[msg.sender], "Agent is already registered.");
    Agent memory newAgent = Agent(agents.length, msg.sender, _preferences);
    agents.push(newAgent);
    registered[msg.sender] = true;
    (bool success, ) = owner.call.value(100)("");
    // the agent must pay the fee to participate
    // owner must recieve that fee
}

It is assumed that a participant calls that function, pays a fee, and that fee is paid to the address of the owner (the person who deployed the SC). But there is something that is not clear to me... If I add this line (bool success, ) = owner.call.value(100)(""); the owner is supposed to receive 100 (gwei, ethers?). But who gives it to him? How do I get the participant to transfer his ether to the owner?

Could anyone show me an example? Any info is welcome too. Thanks a lot.

1

A contract only has access to its own funds.

(bool success, ) = owner.call.value(100)("");

This code send 100 wei from the contract's balance to owner address.

If no units is given solidity assumes it is wei, 1 ether = 10^18 wei.


In order to make a function accept ether payments it has to be declared as payable, then msg.value will contain the sent amount.

function buy(uint256 itemId) public payable {
    require(msg.value >= 0.01 ether, "Not enough ether sent");

    // Sent ethers to wallet 
    uint256 amount = msg.value;
    payable(wallet).transfer(amount);

    // Generate event
    emit ItemSold(itemId, amount);
}
2
  • Oh... I didn't know that. And... how could I send ether yo SC? How can I take the SC address? But could it be done the other way around? (the participant must pay money into the SC fund)
    – Joncarre
    May 10 '21 at 8:18
  • 1
    @Joncarre I've added an example how to use together with payable to require the user to send ethers and forward them to a recipient wallet.
    – Ismael
    May 10 '21 at 18:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.