feat(useragent): add SDK clients table screen
This commit is contained in:
120
useragent/lib/providers/evm/evm_grants.dart
Normal file
120
useragent/lib/providers/evm/evm_grants.dart
Normal file
@@ -0,0 +1,120 @@
|
||||
import 'package:arbiter/features/connection/evm/grants.dart';
|
||||
import 'package:arbiter/proto/evm.pb.dart';
|
||||
import 'package:arbiter/providers/connection/connection_manager.dart';
|
||||
import 'package:fixnum/fixnum.dart';
|
||||
import 'package:freezed_annotation/freezed_annotation.dart';
|
||||
import 'package:hooks_riverpod/experimental/mutation.dart';
|
||||
import 'package:mtcore/markettakers.dart';
|
||||
import 'package:riverpod_annotation/riverpod_annotation.dart';
|
||||
|
||||
part 'evm_grants.freezed.dart';
|
||||
part 'evm_grants.g.dart';
|
||||
|
||||
final createEvmGrantMutation = Mutation<int>();
|
||||
final revokeEvmGrantMutation = Mutation<void>();
|
||||
|
||||
@freezed
|
||||
abstract class EvmGrantsState with _$EvmGrantsState {
|
||||
const EvmGrantsState._();
|
||||
|
||||
const factory EvmGrantsState({
|
||||
required List<GrantEntry> grants,
|
||||
@Default(false) bool showRevoked,
|
||||
}) = _EvmGrantsState;
|
||||
|
||||
bool get revokedFilterBackedByServer => false;
|
||||
}
|
||||
|
||||
@riverpod
|
||||
class EvmGrants extends _$EvmGrants {
|
||||
@override
|
||||
Future<EvmGrantsState?> build() async {
|
||||
final connection = await ref.watch(connectionManagerProvider.future);
|
||||
if (connection == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
try {
|
||||
final grants = await listEvmGrants(connection);
|
||||
return EvmGrantsState(grants: grants);
|
||||
} catch (e, st) {
|
||||
talker.handle(e, st);
|
||||
rethrow;
|
||||
}
|
||||
}
|
||||
|
||||
void toggleShowRevoked(bool value) {
|
||||
final current = state.asData?.value;
|
||||
if (current == null) {
|
||||
return;
|
||||
}
|
||||
state = AsyncData(current.copyWith(showRevoked: value));
|
||||
}
|
||||
|
||||
Future<void> refresh() async {
|
||||
final connection = await ref.read(connectionManagerProvider.future);
|
||||
if (connection == null) {
|
||||
state = const AsyncData(null);
|
||||
return;
|
||||
}
|
||||
|
||||
final previous = state.asData?.value;
|
||||
state = const AsyncLoading();
|
||||
|
||||
state = await AsyncValue.guard(() async {
|
||||
final grants = await listEvmGrants(connection);
|
||||
return EvmGrantsState(
|
||||
grants: grants,
|
||||
showRevoked: previous?.showRevoked ?? false,
|
||||
);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
Future<int> executeCreateEvmGrant(
|
||||
MutationTarget ref, {
|
||||
required int clientId,
|
||||
required int walletId,
|
||||
required Int64 chainId,
|
||||
DateTime? validFrom,
|
||||
DateTime? validUntil,
|
||||
List<int>? maxGasFeePerGas,
|
||||
List<int>? maxPriorityFeePerGas,
|
||||
TransactionRateLimit? rateLimit,
|
||||
required SpecificGrant specific,
|
||||
}) {
|
||||
return createEvmGrantMutation.run(ref, (tsx) async {
|
||||
final connection = await tsx.get(connectionManagerProvider.future);
|
||||
if (connection == null) {
|
||||
throw Exception('Not connected to the server.');
|
||||
}
|
||||
|
||||
final grantId = await createEvmGrant(
|
||||
connection,
|
||||
clientId: clientId,
|
||||
walletId: walletId,
|
||||
chainId: chainId,
|
||||
validFrom: validFrom,
|
||||
validUntil: validUntil,
|
||||
maxGasFeePerGas: maxGasFeePerGas,
|
||||
maxPriorityFeePerGas: maxPriorityFeePerGas,
|
||||
rateLimit: rateLimit,
|
||||
specific: specific,
|
||||
);
|
||||
|
||||
await tsx.get(evmGrantsProvider.notifier).refresh();
|
||||
return grantId;
|
||||
});
|
||||
}
|
||||
|
||||
Future<void> executeRevokeEvmGrant(MutationTarget ref, {required int grantId}) {
|
||||
return revokeEvmGrantMutation.run(ref, (tsx) async {
|
||||
final connection = await tsx.get(connectionManagerProvider.future);
|
||||
if (connection == null) {
|
||||
throw Exception('Not connected to the server.');
|
||||
}
|
||||
|
||||
await deleteEvmGrant(connection, grantId);
|
||||
await tsx.get(evmGrantsProvider.notifier).refresh();
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user