-
Notifications
You must be signed in to change notification settings - Fork 4
/
EthereumClient.cs
64 lines (55 loc) · 2.12 KB
/
EthereumClient.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
using System;
using Nethereum.Web3.Accounts;
using Nethereum.ABI.FunctionEncoding.Attributes;
using Nethereum.StandardTokenEIP20.Functions;
using Nethereum.Contracts.CQS;
using System.Threading.Tasks;
using System.Numerics;
namespace EthereumWalletApp.Core
{
public class EthClient
{
protected Nethereum.Web3.Web3 web3;
public RpcUrl rpcUrl{
get; protected set;
}
public EthClient(RpcUrl aRpcUrl){
rpcUrl = aRpcUrl;
web3 = setServer(rpcUrl);
}
protected Nethereum.Web3.Web3 setServer(RpcUrl aRpcUrl){
return new Nethereum.Web3.Web3(aRpcUrl.url);
}
public async Task<string> createAccount(string aPassword){
var ecKey = Nethereum.Signer.EthECKey.GenerateKey();
Task<string> newAccountTask = web3.Personal.NewAccount.SendRequestAsync(aPassword);
var newAccount = await newAccountTask;
return newAccount;
}
public async Task<BigInteger> getAccountBalance(string addressOwner, string contractAddress)
{
var tokenService = new Nethereum.StandardTokenEIP20.StandardTokenService(web3, contractAddress);
var ownerBalanceTask = tokenService.GetBalanceOfAsync<BigInteger>(addressOwner);
return await ownerBalanceTask;
}
public async Task<string> transferTokens(string senderAddress, string privateKey, string receiverAddress, string contractAddress, UInt64 tokens){
var transactionMessage = new TransferFunction(){
FromAddress = senderAddress,
To = receiverAddress,
TokenAmount = tokens
};
var transferHandler = web3.Eth.GetContractTrasactionHandler<TransferFunction>();
Task<string> transactionHashTask = transferHandler.SendRequestAsync(transactionMessage, contractAddress);
return await transactionHashTask;
}
}
public class RpcUrl
{
public string url{
get; protected set;
}
public RpcUrl(string aUrl){
url = aUrl;
}
}
}