0

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?

0

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.

0

Here is my solution in C# (Nethereum):

        var contractABI = @"[{""constant"":true,""inputs"":[],""name"":""getBalance"",""outputs"":[{""internalType"":""uint256"",""name"":"""",""type"":""uint256""}],""payable"":false,""stateMutability"":""view"",""type"":""function""},{""constant"":false,""inputs"":[{""internalType"":""address payable"",""name"":""_to"",""type"":""address""},{""internalType"":""uint256"",""name"":""_value"",""type"":""uint256""}],""name"":""send"",""outputs"":[{""internalType"":""bool"",""name"":"""",""type"":""bool""}],""payable"":false,""stateMutability"":""nonpayable"",""type"":""function""},{""payable"":true,""stateMutability"":""payable"",""type"":""fallback""}]";
        var senderAddressTest = myAccount.AccountAddress;
        var recipientAddressTest = "0x7988dfD8E9ceCb888C1AeA7Cb416D44C6160Ef80";
        var senderPrivateKeyTest = myAccount.AccountPassword;
        var accountTest = new Nethereum.Web3.Accounts.Account(senderPrivateKeyTest);
        var web3Test = new Web3(accountTest, myAccount.AccountNetwork + "" + myAccount.InfuraApiKey); var contractByteCode = "0x608060405234801561001057600080fd5b5061011d806100206000396000f3fe60806040526004361060265760003560e01c806312065fe0146028578063d0679d3414604c575b005b348015603357600080fd5b50603a6095565b60408051918252519081900360200190f35b348015605757600080fd5b50608160048036036040811015606c57600080fd5b506001600160a01b038135169060200135609a565b604080519115158252519081900360200190f35b303190565b6000303182111560a957600080fd5b6040516001600160a01b0384169083156108fc029084906000818181858888f1935050505015801560de573d6000803e3d6000fd5b506001939250505056fea265627a7a723158207853790325ad5a0a48cccfd3f1d8bd7b195dafeaa0b0f8d9e899ea78a42e667a64736f6c634300050b0032";
        var receiptContract = await web3Test.Eth.DeployContract.SendRequestAndWaitForReceiptAsync(contractABI, contractByteCode, senderAddressTest, new HexBigInteger(900000), null);
        var contractAddress2 = receiptContract.ContractAddress;
        var contractTest2 = web3Test.Eth.GetContract(contractABI, contractAddress2);
        var transaction2 = await web3Test.Eth.GetEtherTransferService().TransferEtherAndWaitForReceiptAsync(contractAddress2, 1.00m, 2, new BigInteger(25000));
        var wieEtherTest2 = 1000000000000000000; // = 1 ETH
        var getBalanceFunction2 = contractTest2.GetFunction("getBalance");
        var resultGetBalance2 = await getBalanceFunction2.CallAsync<UInt64>(); // work
        var sendFunction2 = contractTest2.GetFunction("send");
        var gas2 = await sendFunction2.EstimateGasAsync(senderAddressTest, null, null, recipientAddressTest, wieEtherTest2);
        var receiptAmountSend2 = await sendFunction2.SendTransactionAndWaitForReceiptAsync(senderAddressTest, gas2, null, null, recipientAddressTest, wieEtherTest2);
  • In this code first I deployed the contract, and then send it the ether. Later I use the 'send' method to send ether to another account from the contract`s balance – חיים חדד Nov 8 at 14:40

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.