Solidity
使用智能合約將乙太幣從一個賬戶轉移到另一個賬戶
我正在嘗試將乙太幣從賬戶 A 轉移到智能合約,然後從智能合約轉移到賬戶 B。我編寫了一些在 Remix IDE 上幾乎可以執行的程式碼,但我完全迷失了 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; } }
如果我在 Remix 的值文本框中插入輸入,這將正常工作,如下圖所示:
重新混合 IDE 值
但我需要使用基於 C# 的 Dapp 傳輸乙太幣。那麼,我應該如何在那裡設置 msg.value 呢?我可以使用 Solidity 來實現嗎?
這是我在 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);
你不能使用 Solidity。這只是編寫智能合約的語言。你有(至少)兩個選擇:
web3.js
如果你的 C# dApp 有一個基於瀏覽器的前端,你可以使用庫 web3.js。JavaScript 程式碼中可能的呼叫如下所示:
const receiverAddress=""xxx; await MyFirstContract.methods.send(reveiverAddress).send({ from: account, value: amountToSendWei });
amountToSendWei
是您要轉賬的金額。函式名稱“send”可能會產生問題,因為它已在 web3.xml 中使用。乙太坊
乙太坊的開源 .NET 集成庫。我沒有這方面的個人經驗。