Created
June 17, 2018 09:57
-
-
Save dAAAb/3f2f4f46b2d56d57bc9281587da767d8 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
contract MultiTransfer { | |
function multiTransfer(address[] _addresses, uint256 amount) payable { | |
for (uint256 i = 0; i < _addresses.length; i++) { | |
_addresses[i].call.value(amount).gas(21000)(); | |
} | |
} | |
function() payable {} | |
} | |
contract SingleTransfer { | |
function singleTransfer(address _addr, uint256 amount) payable { | |
_addr.call.value(amount).gas(21000)(); | |
} | |
function() payable {} | |
} | |
// 已發佈到 Rinkeby https://rinkeby.etherscan.io/address/0x133ea8a5dcc5903b92a78f18b66fb729f288138d |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using Nethereum.Contracts; | |
using Nethereum.JsonRpc.UnityClient; | |
using Nethereum.RPC.Eth.DTOs; | |
using Nethereum.Hex.HexConvertors.Extensions; | |
using Nethereum.Hex.HexTypes; | |
using System; | |
using System.Numerics; | |
using System.Globalization; | |
using System.Collections; | |
using UnityEngine; | |
using UnityEngine.UI; | |
/* contract | |
* | |
pragma solidity ^0.4.0; | |
contract Sum | |
{ | |
function add(int256 num1, int256 num2) pure public returns(int256) | |
{ | |
return num1 + num2; | |
} | |
} | |
* */ | |
using Nethereum.Signer; | |
public class Main : MonoBehaviour | |
{ | |
private string m_Url = "https://rinkeby.infura.io"; | |
private string m_SenderAddress = "0xA8f31A73fC85e6B5BF281aE4C81fC8d00A4AcA21"; //要發出 ETH 的,被 Unity 掌控的錢包地址 | |
private string m_ContractAddress = "0x133EA8a5dcc5903b92a78f18b66fB729F288138D"; //singTransfer 合約地址 | |
private string m_PrivateKey = "04e75b04d2cda0ce81a6a625a1148d6ac53ce350b5f9ba478c71a6f51c319d2a"; //要發出 ETH 的,被 Unity 掌控的錢包私鑰 | |
private Contract m_Contract; | |
private static string m_ABI = @"[ | |
{ | |
'constant': false, | |
'inputs': [ | |
{ | |
'name': '_addr', | |
'type': 'address' | |
}, | |
{ | |
'name': 'amount', | |
'type': 'uint256' | |
} | |
], | |
'name': 'singleTransfer', | |
'outputs': [], | |
'payable': true, | |
'stateMutability': 'payable', | |
'type': 'function' | |
}, | |
{ | |
'payable': true, | |
'stateMutability': 'payable', | |
'type': 'fallback' | |
} | |
]"; | |
private string m_Input1 = ""; | |
private string m_Result = ""; | |
[SerializeField] private Button m_Btn = null; | |
private void Start() | |
{ | |
this.m_Contract = new Contract(null, m_ABI, m_ContractAddress); | |
} | |
IEnumerator CallAdd(string address, BigInteger amount) | |
{ | |
var function = m_Contract.GetFunction("singleTransfer"); | |
var _TransactionInput = function.CreateTransactionInput (m_SenderAddress, new HexBigInteger(2000000), new HexBigInteger(0), address, amount); | |
var _Request = new TransactionSignedUnityRequest(m_Url, m_PrivateKey, m_SenderAddress); | |
yield return _Request.SignAndSendTransaction(_TransactionInput); | |
//var _Result = _Request.Result; | |
//Debug.Log("_AddResult: " + _Result); | |
//m_Result = _Result.ToString(); | |
m_Btn.interactable = true; | |
} | |
public void OnClickAddBtn() | |
{ | |
m_Btn.interactable = false; | |
//var _Name = m_Input1; | |
BigInteger _BigAmount = BigInteger.Parse("888000000000000", NumberStyles.Integer); // 以 Gwei 為單位要轉的 ETH | |
string _Address = "0x12a0E25E62C1dBD32E505446062B26AECB65F028"; // 接收 ETH 的錢包 | |
StartCoroutine(CallAdd(_Address,_BigAmount)); | |
} | |
void OnGUI() | |
{ | |
GUI.skin.textField.fontSize = 40; | |
m_Input1 = GUI.TextField(new Rect(10, 100, 200, 45), m_Input1, 25); | |
GUI.skin.label.fontSize = 18; | |
GUI.Label(new Rect(10, 300, 800, 40), m_Result); | |
} | |
} | |
//using Nethereum.Contracts; | |
//using Nethereum.JsonRpc.UnityClient; | |
//using Nethereum.RPC.Eth.DTOs; | |
//using System; | |
//using System.Collections; | |
//using UnityEngine; | |
//using UnityEngine.UI; | |
/* contract | |
* | |
pragma solidity ^0.4.0; | |
contract Sum | |
{ | |
function add(int256 num1, int256 num2) pure public returns(int256) | |
{ | |
return num1 + num2; | |
} | |
} | |
* */ | |
//public class Main : MonoBehaviour | |
//{ | |
// private string m_Url = "https://rinkeby.infura.io"; | |
// private string m_ContractAddress = "0x97ad730824221ebc6d729f88f7ab36c04cee146a"; | |
// private Contract contract; | |
// private static string m_ABI = @"[{'constant':true,'inputs':[{'name':'num1','type':'int256'},{'name':'num2','type':'int256'}],'name':'add','outputs':[{'name':'','type':'int256'}],'payable':false,'stateMutability':'pure','type':'function'}]"; | |
// | |
// [SerializeField] private Button m_Btn = null; | |
// | |
// private void Start() | |
// { | |
// Debug.Log("Start"); | |
// this.contract = new Contract(null, m_ABI, m_ContractAddress); | |
// | |
// //StartCoroutine(CallAdd()); | |
// } | |
// | |
// IEnumerator CallAdd(int num1 = 1, int num2 = 2) | |
// { | |
// var _AddRequest = new EthCallUnityRequest(m_Url); | |
// yield return _AddRequest.SendRequest(GetSumCallInput(num1, num2), Nethereum.RPC.Eth.DTOs.BlockParameter.CreateLatest()); | |
// var _AddResult = _AddRequest.Result; | |
// | |
// Debug.Log("_AddResult: " + _AddResult); | |
// m_Result = _AddResult.ToString(); | |
// m_Btn.interactable = true; | |
// } | |
// | |
// private CallInput GetSumCallInput(int num1, int num2) | |
// { | |
// var function = contract.GetFunction("add"); | |
// return function.CreateCallInput(num1, num2); | |
// } | |
// | |
// public void OnClickAddBtn() | |
// { | |
// m_Btn.interactable = false; | |
// int _Num1 = Convert.ToInt32(m_Input1); | |
// int _Num2 = Convert.ToInt32(m_Input2); | |
// StartCoroutine(CallAdd(_Num1, _Num2)); | |
// } | |
// | |
// string m_Input1 = ""; | |
// string m_Input2 = ""; | |
// string m_Result = ""; | |
// void OnGUI() | |
// { | |
// GUI.skin.textField.fontSize = 40; | |
// m_Input1 = GUI.TextField(new Rect(10, 100, 200, 45), m_Input1, 25); | |
// m_Input2 = GUI.TextField(new Rect(300, 100, 200, 45), m_Input2, 25); | |
// | |
// GUI.skin.label.fontSize = 40; | |
// GUI.Label(new Rect(245, 100, 40, 40), "+"); | |
// | |
// GUI.skin.label.fontSize = 18; | |
// GUI.Label(new Rect(10, 300, 800, 40), m_Result); | |
// } | |
//} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment