From 9dfce3a78d984c5db57caca3de3056656b670752 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B8rn=20Olav?= Date: Thu, 9 Nov 2023 10:37:30 +0100 Subject: [PATCH] rpc: complete initial ethereumrpc implementation --- lib/rpc/{rpc_eth.dart => rpc_ethereum.dart} | 55 ++++++++++++++++++--- 1 file changed, 47 insertions(+), 8 deletions(-) rename lib/rpc/{rpc_eth.dart => rpc_ethereum.dart} (59%) diff --git a/lib/rpc/rpc_eth.dart b/lib/rpc/rpc_ethereum.dart similarity index 59% rename from lib/rpc/rpc_eth.dart rename to lib/rpc/rpc_ethereum.dart index 94995579..a889a5b9 100644 --- a/lib/rpc/rpc_eth.dart +++ b/lib/rpc/rpc_ethereum.dart @@ -1,18 +1,20 @@ import 'package:http/http.dart'; +import 'package:sidesail/config/sidechains.dart'; import 'package:sidesail/pages/tabs/settings/node_settings_tab.dart'; -import 'package:sidesail/rpc/rpc.dart'; +import 'package:sidesail/rpc/models/core_transaction.dart'; +import 'package:sidesail/rpc/rpc_sidechain.dart'; import 'package:web3dart/web3dart.dart'; -abstract class EthereumRPC extends RPCConnection { - Future call(String rpc, [args]); -} +abstract class EthereumRPC extends SidechainRPC {} class EthereumRPCLive extends EthereumRPC { + final sgweiPerSat = 1000000000; + late Web3Client _client; @override - Future call(String rpc, [args]) async { - return _client.makeRPCCall(rpc, args); + Future callRAW(String method, [params]) async { + return _client.makeRPCCall(method, params); } // Apparently Ethereum doesn't have a conf file? @@ -22,7 +24,9 @@ class EthereumRPCLive extends EthereumRPC { _client = Web3Client(url, Client()); } - EthereumRPCLive._create(); + EthereumRPCLive._create() { + chain = EthereumSidechain(); + } static Future create() async { final rpc = EthereumRPCLive._create(); @@ -45,6 +49,42 @@ class EthereumRPCLive extends EthereumRPC { await _client.getChainId(); return; } + + @override + Future<(double, double)> getBalance() async { + final account = await _account(); + final balance = await _client.getBalance(account); + return (balance.getInEther.toDouble(), 0.0); + } + + Future _account() async { + final accountFut = await callRAW('eth_accounts'); + final accounts = await accountFut as List; + + if (accounts.isEmpty) { + throw Exception('Create account from cli using personal.newAccount before getting balance'); + } + + return EthereumAddress.fromHex(accounts[0] as String); + } + + Future _deposit(int amountSat, int feeSat) async { + final amount = sgweiPerSat * amountSat; + final fee = sgweiPerSat * feeSat; + final account = await _account(); + final deposit = await callRAW('eth_deposit', [account.hex, _toHex(amount), _toHex(fee)]); + return deposit as bool; + } + + String _toHex(int number) { + return '0x${number.toRadixString(16)}'; + } + + @override + Future> listTransactions() async { + // TODO: Implement listtransactions + return List.empty(); + } } /// List of all known RPC methods available / @@ -54,7 +94,6 @@ final ethRpcMethods = [ 'net_version', 'net_listening', 'net_peerCount', - 'eth_protocolVersion', 'eth_syncing', 'eth_coinbase', 'eth_chainId',