Skip to content

Commit

Permalink
rpc: move functions from Sidechain to Testchain
Browse files Browse the repository at this point in the history
  • Loading branch information
octobocto committed Nov 9, 2023
1 parent d8cc11c commit d805813
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 72 deletions.
9 changes: 5 additions & 4 deletions lib/config/dependencies.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import 'package:sidesail/config/sidechains.dart';
import 'package:sidesail/providers/balance_provider.dart';
import 'package:sidesail/providers/transactions_provider.dart';
import 'package:sidesail/routing/router.dart';
import 'package:sidesail/rpc/rpc_eth.dart';
import 'package:sidesail/rpc/rpc_ethereum.dart';
import 'package:sidesail/rpc/rpc_mainchain.dart';
import 'package:sidesail/rpc/rpc_sidechain.dart';
import 'package:sidesail/rpc/rpc_testchain.dart';
Expand Down Expand Up @@ -63,14 +63,15 @@ Future<void> setSidechainRPC(Sidechain chain) async {
break;

case SidechainType.ethereum:
final testchainRPC = await EthereumRPCLive.create();
final ethRPC = await EthereumRPCLive.create();
if (GetIt.I.isRegistered<EthereumRPC>()) {
GetIt.I.unregister<EthereumRPC>();
}
GetIt.I.registerLazySingleton<EthereumRPC>(
() => testchainRPC,
() => ethRPC,
);
sidechainRPC = testchainRPC;

sidechainRPC = ethRPC;
break;
}

Expand Down
26 changes: 4 additions & 22 deletions lib/rpc/rpc_sidechain.dart
Original file line number Diff line number Diff line change
@@ -1,39 +1,21 @@
import 'dart:async';

import 'package:sidesail/config/dependencies.dart';
import 'package:sidesail/config/sidechains.dart';
import 'package:sidesail/rpc/models/core_transaction.dart';
import 'package:sidesail/rpc/rpc.dart';

/// RPC connection for all sidechain nodes
abstract class SidechainRPC extends RPCConnection {
Future<(double, double)> getBalance();

Future<String> mainSend(
String address,
double amount,
double sidechainFee,
double mainchainFee,
);
Future<String> mainGenerateAddress();
Future<int> mainBlockCount();

Future<String> sideSend(
String address,
double amount,
bool subtractFeeFromAmount,
);
Future<String> sideGenerateAddress();
Future<int> sideBlockCount();
Future<double> sideEstimateFee();
Future<dynamic> callRAW(String method, [List<dynamic>? params]);

// TODO: Don't do a CoreTransaction here
Future<(double, double)> getBalance();
Future<List<CoreTransaction>> listTransactions();

Future<dynamic> callRAW(String method, [dynamic params]);

late Sidechain chain;
void setChain(Sidechain newChain) {
chain = newChain;
setSidechainRPC(newChain);
notifyListeners();
}
}
Expand Down
18 changes: 18 additions & 0 deletions lib/rpc/rpc_testchain.dart
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,24 @@ import 'package:sidesail/rpc/rpc_withdrawal_bundle.dart';

/// RPC connection the sidechain node.
abstract class TestchainRPC extends SidechainRPC {
Future<String> mainSend(
String address,
double amount,
double sidechainFee,
double mainchainFee,
);
Future<String> mainGenerateAddress();
Future<int> mainBlockCount();

Future<String> sideSend(
String address,
double amount,
bool subtractFeeFromAmount,
);
Future<String> sideGenerateAddress();
Future<int> sideBlockCount();
Future<double> sideEstimateFee();

Future<BmmResult> refreshBMM(int bidSatoshis);

/// Returns null if there's no current bundle
Expand Down
6 changes: 3 additions & 3 deletions lib/widgets/containers/tabs/dashboard_tab_widgets.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import 'package:sail_ui/widgets/core/sail_text.dart';
import 'package:sidesail/providers/balance_provider.dart';
import 'package:sidesail/providers/transactions_provider.dart';
import 'package:sidesail/routing/router.dart';
import 'package:sidesail/rpc/rpc_sidechain.dart';
import 'package:sidesail/rpc/rpc_testchain.dart';
import 'package:sidesail/widgets/containers/dashboard_action_modal.dart';
import 'package:stacked/stacked.dart';

Expand Down Expand Up @@ -115,7 +115,7 @@ class SendOnSidechainViewModel extends BaseViewModel {
BalanceProvider get _balanceProvider => GetIt.I.get<BalanceProvider>();
TransactionsProvider get _transactionsProvider => GetIt.I.get<TransactionsProvider>();
AppRouter get _router => GetIt.I.get<AppRouter>();
SidechainRPC get _rpc => GetIt.I.get<SidechainRPC>();
TestchainRPC get _rpc => GetIt.I.get<TestchainRPC>();

final bitcoinAddressController = TextEditingController();
final bitcoinAmountController = TextEditingController();
Expand Down Expand Up @@ -312,7 +312,7 @@ class ReceiveOnSidechainAction extends StatelessWidget {
}

class ReceiveOnSidechainViewModel extends BaseViewModel {
SidechainRPC get _rpc => GetIt.I.get<SidechainRPC>();
TestchainRPC get _rpc => GetIt.I.get<TestchainRPC>();
final log = Logger(level: Level.debug);

String? sidechainAddress;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import 'package:sidesail/providers/balance_provider.dart';
import 'package:sidesail/providers/transactions_provider.dart';
import 'package:sidesail/routing/router.dart';
import 'package:sidesail/rpc/rpc_mainchain.dart';
import 'package:sidesail/rpc/rpc_sidechain.dart';
import 'package:sidesail/rpc/rpc_testchain.dart';
import 'package:sidesail/widgets/containers/dashboard_action_modal.dart';
import 'package:stacked/stacked.dart';

Expand Down Expand Up @@ -68,7 +68,7 @@ class PegOutViewModel extends BaseViewModel {
BalanceProvider get _balanceProvider => GetIt.I.get<BalanceProvider>();
TransactionsProvider get _transactionsProvider => GetIt.I.get<TransactionsProvider>();
AppRouter get _router => GetIt.I.get<AppRouter>();
SidechainRPC get _testchain => GetIt.I.get<SidechainRPC>();
TestchainRPC get _testchain => GetIt.I.get<TestchainRPC>();
MainchainRPC get _mainchain => GetIt.I.get<MainchainRPC>();

final bitcoinAddressController = TextEditingController();
Expand Down Expand Up @@ -282,7 +282,7 @@ class PegInAction extends StatelessWidget {
}

class PegInViewModel extends BaseViewModel {
SidechainRPC get _rpc => GetIt.I.get<SidechainRPC>();
TestchainRPC get _rpc => GetIt.I.get<TestchainRPC>();
final log = Logger(level: Level.debug);

String? pegInAddress;
Expand Down
45 changes: 5 additions & 40 deletions test/mocks/rpc_mock_sidechain.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,6 @@ class MockSidechainRPC extends SidechainRPC {
chain = TestSidechain();
}

@override
Future<String> mainGenerateAddress() async {
return 'bc1...?';
}

@override
Future<(double, double)> getBalance() async {
return (1.12345678, 2.24680);
Expand All @@ -22,41 +17,6 @@ class MockSidechainRPC extends SidechainRPC {
return;
}

@override
Future<double> sideEstimateFee() async {
return 0.001;
}

@override
Future<String> sideGenerateAddress() async {
return 's1deadbeef';
}

@override
Future<String> mainSend(String address, double amount, double sidechainFee, double mainchainFee) async {
return 'txidmainbeef';
}

@override
Future<String> sideSend(String address, double amount, bool subtractFeeFromAmount) async {
return 'txidsidebeef';
}

@override
Future<List<CoreTransaction>> listTransactions() async {
return List.empty();
}

@override
Future<int> sideBlockCount() async {
return 1;
}

@override
Future<int> mainBlockCount() async {
return 1;
}

@override
Future<(bool, String?)> testConnection() async {
return (true, null);
Expand All @@ -71,4 +31,9 @@ class MockSidechainRPC extends SidechainRPC {
Future<void> ping() async {
return;
}

@override
Future<List<CoreTransaction>> listTransactions() async {
return List.empty();
}
}

0 comments on commit d805813

Please sign in to comment.