Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updated test and added get execution logic #1090

Merged
merged 2 commits into from
Sep 20, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion dart_sdk/example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ class MyHomePage extends StatefulWidget {
class _MyHomePageState extends State<MyHomePage> {
GlobalKey<ScaffoldState> scaffoldKey = GlobalKey<ScaffoldState>();

String cookBookId = "cookbook_cry_306";
String cookBookId = "cookbook_cry_308";
String recipeId = "recipe_1";
String ownerId = "pylo1v97v5qj2kvph2d02fzxxlh44wzpfmuc63vpphj";
String itemId = "8MrbcX2hkmm";
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import 'dart:convert';

import 'package:pylons_sdk/src/core/constants/strings.dart';
import 'package:pylons_sdk/src/features/ipc/base/ipc_handler.dart';
import 'package:pylons_sdk/src/features/ipc/responseCompleters.dart';
import 'package:pylons_sdk/src/features/models/sdk_ipc_response.dart';
import 'package:pylons_sdk/src/generated/pylons/execution.pb.dart';

class ExecuteRecipeHandler implements IPCHandler {
@override
void handler(SDKIPCResponse<dynamic> response) {
final defaultResponse = SDKIPCResponse<Execution>(
success: response.success,
action: response.action,
data: Execution.create(),
error: response.error,
errorCode: response.errorCode);
try {
if (response.success) {
defaultResponse.data = Execution.create()
..mergeFromProto3Json(jsonDecode(response.data));
}
} on Exception catch (_) {
defaultResponse.success = false;
defaultResponse.error = 'Execution parsing failed';
defaultResponse.errorCode = Strings.ERR_MALFORMED_EXECUTION;
}
responseCompleters[Strings.TX_EXECUTE_RECIPE]!.complete(defaultResponse);
}
}
2 changes: 2 additions & 0 deletions dart_sdk/lib/src/features/ipc/ipc_handler_factory.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import 'package:pylons_sdk/src/core/constants/strings.dart';
import 'package:pylons_sdk/src/features/ipc/base/ipc_handler.dart';
import 'package:pylons_sdk/src/features/ipc/handlers/create_cookbook_handler.dart';
import 'package:pylons_sdk/src/features/ipc/handlers/create_recipe_handler.dart';
import 'package:pylons_sdk/src/features/ipc/handlers/execute_recipe_handler.dart';
import 'package:pylons_sdk/src/features/ipc/handlers/get_cookbooks_handler.dart';
import 'package:pylons_sdk/src/features/ipc/handlers/get_execution_by_id_handler.dart';
import 'package:pylons_sdk/src/features/ipc/handlers/get_list_items_by_owner_handler.dart';
Expand All @@ -28,6 +29,7 @@ class IPCHandlerFactory {
Strings.GET_PROFILE: GetProfileHandler(),
Strings.TX_CREATE_COOKBOOK: CreateCookbookHandler(),
Strings.TX_CREATE_RECIPE: CreateRecipeHandler(),
Strings.TX_EXECUTE_RECIPE: ExecuteRecipeHandler()
};

/// Fetches and resolves appropriate [IPCHandler] instance for [sdkIpcResponse], or completes
Expand Down
2 changes: 1 addition & 1 deletion dart_sdk/lib/src/pylons_wallet.dart
Original file line number Diff line number Diff line change
Expand Up @@ -376,7 +376,7 @@ abstract class PylonsWallet {
///
/// If the operation fails due to an exception thrown by this library, that
/// exception will be passed directly.
Future<SDKIPCResponse> txExecuteRecipe(
Future<SDKIPCResponse<Execution>> txExecuteRecipe(
{required String cookbookId,
required String recipeName,
required List<String> itemIds,
Expand Down
38 changes: 26 additions & 12 deletions dart_sdk/lib/src/pylons_wallet/pylons_wallet_impl.dart
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ class PylonsWalletImpl implements PylonsWallet {
sdkipcResponse = SDKIPCResponse.fromIPCMessage(getMessage);
IPCHandlerFactory.getHandler(sdkipcResponse);
} catch (e) {
print(e);
print('Something went wrong in parsing');
return;
}
Expand Down Expand Up @@ -217,25 +218,38 @@ class PylonsWalletImpl implements PylonsWallet {
}

@override
Future<SDKIPCResponse> txExecuteRecipe(
Future<SDKIPCResponse<Execution>> txExecuteRecipe(
{required String cookbookId,
required String recipeName,
required List<String> itemIds,
required int coinInputIndex,
required List<PaymentInfo> paymentInfo,
bool requestResponse = true}) async {
return Future.sync(() async {
return await _dispatch(
Strings.TX_EXECUTE_RECIPE,
jsonEncode(MsgExecuteRecipe(
creator: '',
cookbookId: cookbookId,
recipeId: recipeName,
coinInputsIndex: fixnum.Int64(coinInputIndex),
itemIds: itemIds,
paymentInfos: paymentInfo)
.toProto3Json()),
requestResponse: requestResponse);
return Future.sync(() async {
final response = await _dispatch(
Strings.TX_EXECUTE_RECIPE,
jsonEncode(MsgExecuteRecipe(
creator: '',
cookbookId: cookbookId,
recipeId: recipeName,
coinInputsIndex: fixnum.Int64(coinInputIndex),
itemIds: itemIds,
paymentInfos: paymentInfo)
.toProto3Json()),
requestResponse: requestResponse);

if (response is SDKIPCResponse<Execution>) {
return response;
}

if (response is SDKIPCResponse<String> && !requestResponse) {
return SDKIPCResponse.success(Execution.create(),
action: Strings.TX_EXECUTE_RECIPE);
}

throw Exception('Response malformed');
});
});
}

Expand Down
8 changes: 6 additions & 2 deletions dart_sdk/test/pylons_wallet/pylons_wallet_impl_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -445,11 +445,13 @@ void executeRecipeTest() {
.thenAnswer((realInvocation) => Stream<String?>.value('Jawad'));
var pylonsWallet = PylonsWalletImpl(host: MOCK_HOST, uniLink: uniLink);



Future.delayed(Duration(seconds: 1), () {
final sdkResponse = SDKIPCResponse(
success: true,
error: '',
data: '',
data: MOCK_EXECUTION,
errorCode: '',
action: Strings.TX_EXECUTE_RECIPE);
responseCompleters[Strings.TX_EXECUTE_RECIPE]!.complete(sdkResponse);
Expand All @@ -464,6 +466,7 @@ void executeRecipeTest() {

expect(true, response.success);
expect(response.action, Strings.TX_EXECUTE_RECIPE);
expect(response.data, MOCK_EXECUTION);
});

test('should execute recipe in the wallet without redirecting', () async {
Expand All @@ -483,7 +486,8 @@ void executeRecipeTest() {
requestResponse: false);

expect(true, response.success);
expect(response.data, Strings.ACTION_DONE);
expect(response.action, Strings.TX_EXECUTE_RECIPE);

});
}

Expand Down
20 changes: 18 additions & 2 deletions wallet/lib/stores/wallet_store_imp.dart
Original file line number Diff line number Diff line change
Expand Up @@ -298,8 +298,24 @@ class WalletsStoreImp implements WalletsStore {
Future<SdkIpcResponse> executeRecipe(Map json) async {
final msgObj = pylons.MsgExecuteRecipe.create()..mergeFromProto3Json(json);
msgObj.creator = wallets.value.last.publicAddress;
final response = await _signAndBroadcast(msgObj);
return response;

final sdkResponse = await _signAndBroadcast(msgObj);
if (!sdkResponse.success) {
return SdkIpcResponse.failure(error: sdkResponse.error, sender: sdkResponse.sender, errorCode: sdkResponse.errorCode);
}

final executionEither = await repository.getExecutionsByRecipeId(recipeId: json["recipeId"].toString(), cookBookId: json["cookbookId"].toString());

if (executionEither.isLeft()) {
return SdkIpcResponse.failure(error: sdkResponse.error, sender: sdkResponse.sender, errorCode: sdkResponse.errorCode);
}

if (executionEither.toOption().toNullable()!.completedExecutions.isEmpty) {
return SdkIpcResponse.failure(error: sdkResponse.error, sender: sdkResponse.sender, errorCode: sdkResponse.errorCode);
}

return SdkIpcResponse.success(
data: jsonEncode(executionEither.toOption().toNullable()!.completedExecutions.last.toProto3Json()), sender: sdkResponse.sender, transaction: sdkResponse.data.toString());
}

@override
Expand Down