Solidity – Transfer Ether from One Account to Another Using Smart Contract

address.transferethernethereumsolidity

I`m trying to transfer ether from account A to a smart contract and from the smart contract to account B. I wrote some code that approximately works on Remix IDE, but I completely got lost with the msg.value.

pragma solidity ^0.5.11;
contract MyFirstContract
{
    function() external payable { }
        function getBalance() public view returns(uint)
        {
            return address(this).balance;
        }
        
    function send(address payable _To )public payable returns (bool)
    {
     _To.transfer(msg.value);
     return true;
    }
}

This works fine if I insert an input in the value textbox on Remix, as shown in the next image:
enter image description here

Remix IDE value

But I need to transfer ether using my Dapp which based on C#. So, how am I supposed to set the msg.value there? Can I do it using Solidity?

Best Answer

You can not use Solidity. That is solely the language to write smart contracts in. You have (at least) 2 options:

web3.js

If your C# dApp has a browser based frontend you can use the library web3.js. A possible call in your JavaScript code would look something like this:

const receiverAddress=""xxx;
await MyFirstContract.methods.send(reveiverAddress).send({ from: account, value: amountToSendWei });

amountToSendWei is the amount you wanna transfer. The function name "send" might create issues because it is already used within web3.

Nethereum

An open source .NET integration library for Ethereum. I don't have personal experience with this one.

Related Topic