Skip to content

Commit

Permalink
Merge pull request #1054 from Pylons-tech/fix/create_cookbook_transac…
Browse files Browse the repository at this point in the history
…tion_returns_cookbook

SDK will now return cookbook
  • Loading branch information
faddat committed Sep 15, 2022
2 parents a225d84 + d2f9791 commit 99b60b9
Show file tree
Hide file tree
Showing 24 changed files with 136 additions and 32 deletions.
20 changes: 20 additions & 0 deletions .github/workflows/flutter.yml
Original file line number Diff line number Diff line change
Expand Up @@ -56,5 +56,25 @@ jobs:
flutter pub get
flutter analyze
flutter build appbundle
flutter test
dart_sdk:
name: SDK
runs-on: ubuntu-latest
permissions:
actions: read
contents: read
security-events: write

steps:
- uses: actions/checkout@v3
- uses: subosito/flutter-action@v2
with:
channel: "stable" # or: 'beta', 'dev' or 'master'
- run: |
flutter --version
cd dart_sdk
flutter pub get
flutter analyze
flutter test
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_305";
String cookBookId = "cookbook_cry_306";
String recipeId = "recipe_1";
String ownerId = "pylo1v97v5qj2kvph2d02fzxxlh44wzpfmuc63vpphj";
String itemId = "8MrbcX2hkmm";
Expand Down
1 change: 1 addition & 0 deletions dart_sdk/lib/pylons_sdk.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,5 @@ export 'src/generated/pylons/cookbook.pb.dart';
export 'src/generated/pylons/recipe.pb.dart';
export 'src/generated/pylons/trade.pb.dart';
export 'src/features/helper/dec_string.dart';
export 'src/features/models/sdk_ipc_response.dart';
export 'src/generated/cosmos/base/v1beta1/coin.pb.dart' show Coin;
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/cookbook.pb.dart';

class CreateCookbookHandler implements IPCHandler {
@override
void handler(SDKIPCResponse<dynamic> response) {
final defaultResponse = SDKIPCResponse<Cookbook>(
success: response.success,
action: response.action,
data: Cookbook.create(),
error: response.error,
errorCode: response.errorCode);
try {
if (response.success) {
defaultResponse.data = Cookbook.create()
..mergeFromProto3Json(jsonDecode(response.data));
}
} on Exception catch (_) {
defaultResponse.success = false;
defaultResponse.error = 'Cookbook parsing failed';
defaultResponse.errorCode = Strings.ERR_MALFORMED_COOKBOOK;
}
responseCompleters[Strings.TX_CREATE_COOKBOOK]!.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
@@ -1,5 +1,6 @@
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/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 @@ -24,6 +25,7 @@ class IPCHandlerFactory {
Strings.GET_EXECUTION_BY_ID: GetExecutionByIdHandler(),
Strings.GET_TRADES: GetTradesHandler(),
Strings.GET_PROFILE: GetProfileHandler(),
Strings.TX_CREATE_COOKBOOK: CreateCookbookHandler(),
};

/// Fetches and resolves appropriate [IPCHandler] instance for [sdkIpcResponse], or completes
Expand Down
4 changes: 2 additions & 2 deletions dart_sdk/lib/src/features/models/sdk_ipc_response.dart
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@ class SDKIPCResponse<T> {
success: jsonMap['success']);
}

factory SDKIPCResponse.success(T data) {
factory SDKIPCResponse.success(T data, {String action = ''}) {
return SDKIPCResponse(
error: '', errorCode: '', action: '', data: data, success: true);
error: '', errorCode: '', action: action, data: data, success: true);
}

String toBas64Hash() => base64Url.encode(utf8.encode(toJson()));
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 @@ -298,7 +298,7 @@ abstract class PylonsWallet {
///
/// If the operation fails due to an exception thrown by this library, that
/// exception will be passed directly.
Future<SDKIPCResponse> txCreateCookbook(Cookbook cookbook,
Future<SDKIPCResponse<Cookbook>> txCreateCookbook(Cookbook cookbook,
{bool requestResponse = true});

/// Async: Creates a transaction to create the provided [Recipe] on the Pylons
Expand Down
15 changes: 13 additions & 2 deletions dart_sdk/lib/src/pylons_wallet/pylons_wallet_impl.dart
Original file line number Diff line number Diff line change
Expand Up @@ -175,12 +175,23 @@ class PylonsWalletImpl implements PylonsWallet {
}

@override
Future<SDKIPCResponse> txCreateCookbook(Cookbook cookbook,
Future<SDKIPCResponse<Cookbook>> txCreateCookbook(Cookbook cookbook,
{bool requestResponse = true}) {
return Future.sync(() async {
return await _dispatch(
final response = await _dispatch(
Strings.TX_CREATE_COOKBOOK, jsonEncode(cookbook.toProto3Json()),
requestResponse: requestResponse);


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

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

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

Expand Down
9 changes: 8 additions & 1 deletion dart_sdk/test/mocks/mock_constants.dart
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
import 'package:fixnum/fixnum.dart';
import 'package:pylons_sdk/pylons_sdk.dart';
import 'package:pylons_sdk/src/features/data/models/profile.dart';
import 'package:pylons_sdk/src/features/helper/dec_string.dart';
import 'package:pylons_sdk/src/generated/cosmos/base/v1beta1/coin.pb.dart';
import 'package:pylons_sdk/src/generated/pylons/cookbook.pb.dart';


import 'package:pylons_sdk/src/generated/pylons/execution.pb.dart';
import 'package:pylons_sdk/src/generated/pylons/item.pb.dart';
import 'package:pylons_sdk/src/generated/pylons/recipe.pb.dart';
import 'package:pylons_sdk/src/generated/pylons/trade.pb.dart';

var MOCK_COOKBOOK = '''{
"creator": "pylo1akzpu26f36pgxr636uch8evdtdjepu93v5y9s2",
Expand Down
21 changes: 18 additions & 3 deletions dart_sdk/test/pylons_wallet/pylons_wallet_impl_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,16 @@ import 'dart:convert';
import 'package:flutter/services.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:mockito/mockito.dart';
import 'package:pylons_sdk/pylons_sdk.dart';
import 'package:pylons_sdk/src/core/constants/strings.dart';
import 'package:pylons_sdk/src/features/ipc/ipc_constants.dart';
import 'package:pylons_sdk/src/features/ipc/responseCompleters.dart';
import 'package:pylons_sdk/src/features/models/execution_list_by_recipe_response.dart';
import 'package:pylons_sdk/src/features/models/sdk_ipc_response.dart';
import 'package:pylons_sdk/src/generated/pylons/cookbook.pb.dart';
import 'package:pylons_sdk/src/generated/pylons/execution.pb.dart';
import 'package:pylons_sdk/src/generated/pylons/item.pb.dart';
import 'package:pylons_sdk/src/generated/pylons/recipe.pb.dart';
import 'package:pylons_sdk/src/generated/pylons/trade.pb.dart';
import 'package:pylons_sdk/src/pylons_wallet/pylons_wallet_impl.dart';
import '../mocks/mock_constants.dart';
import '../mocks/mock_uni_link.dart';
Expand Down Expand Up @@ -406,6 +409,9 @@ void mockChannelHandler() {
var linuxChannel =
const MethodChannel('plugins.flutter.io/url_launcher_linux');

var macOSChannel =
const MethodChannel('plugins.flutter.io/url_launcher_macos');

// Register the mock handler.
channel.setMockMethodCallHandler((MethodCall methodCall) async {
if (methodCall.method == 'canLaunch') {
Expand All @@ -420,6 +426,13 @@ void mockChannelHandler() {
}
return null;
});

macOSChannel.setMockMethodCallHandler((MethodCall methodCall) async {
if (methodCall.method == 'canLaunch') {
return true;
}
return null;
});
}

void executeRecipeTest() {
Expand Down Expand Up @@ -489,7 +502,7 @@ void createCookBookTest() {
final sdkResponse = SDKIPCResponse(
success: true,
error: '',
data: '',
data: cookBook,
errorCode: '',
action: Strings.TX_CREATE_COOKBOOK);
responseCompleters[Strings.TX_CREATE_COOKBOOK]!.complete(sdkResponse);
Expand All @@ -499,6 +512,7 @@ void createCookBookTest() {

expect(true, response.success);
expect(response.action, Strings.TX_CREATE_COOKBOOK);
expect(response.data.id, cookBook.id);
});

test('should create cookbook in the wallet without redirecting back',
Expand All @@ -517,7 +531,8 @@ void createCookBookTest() {
await pylonsWallet.txCreateCookbook(cookBook, requestResponse: false);

expect(true, response.success);
expect(response.data, Strings.ACTION_DONE);
expect(response.action, Strings.TX_CREATE_COOKBOOK);
expect(response.data.id, cookBook.id);
});
}

Expand Down
3 changes: 2 additions & 1 deletion dart_sdk/test/src/features/core/error/exception_test.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import 'package:flutter_test/flutter_test.dart';
import 'package:pylons_sdk/pylons_sdk.dart';
import 'package:pylons_sdk/src/core/error/exceptions.dart';


import '../../../../mocks/mock_constants.dart';

Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import 'dart:convert';

import 'package:flutter_test/flutter_test.dart';
import 'package:pylons_sdk/pylons_sdk.dart';

import 'package:pylons_sdk/src/core/constants/strings.dart';
import 'package:pylons_sdk/src/features/ipc/handlers/get_item_by_id_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/item.pb.dart';

import '../../../../mocks/mock_constants.dart';

Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import 'dart:convert';

import 'package:flutter_test/flutter_test.dart';
import 'package:pylons_sdk/pylons_sdk.dart';

import 'package:pylons_sdk/src/core/constants/strings.dart';
import 'package:pylons_sdk/src/features/ipc/handlers/get_list_items_by_owner_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/item.pb.dart';

import '../../../../mocks/mock_constants.dart';

Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import 'dart:convert';

import 'package:flutter_test/flutter_test.dart';
import 'package:pylons_sdk/pylons_sdk.dart';
import 'package:pylons_sdk/src/core/constants/strings.dart';
import 'package:pylons_sdk/src/features/ipc/handlers/get_recipe_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/recipe.pb.dart';

import '../../../../mocks/mock_constants.dart';

Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import 'package:flutter_test/flutter_test.dart';
import 'package:pylons_sdk/pylons_sdk.dart';

import 'package:pylons_sdk/src/core/constants/strings.dart';
import 'package:pylons_sdk/src/features/ipc/handlers/get_recipes_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/recipe.pb.dart';

import '../../../../mocks/mock_constants.dart';

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@ import 'package:pylons_sdk/pylons_sdk.dart';
import 'package:pylons_sdk/src/core/constants/strings.dart';
import 'package:pylons_sdk/src/features/ipc/handlers/get_trades_handler.dart';
import 'package:pylons_sdk/src/features/ipc/responseCompleters.dart';
import 'package:pylons_sdk/src/features/models/sdk_ipc_response.dart';

import '../../../../mocks/mock_constants.dart';



void main() {
test('should complete the get trades future', () {
initResponseCompleter(Strings.GET_TRADES);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import 'package:fixnum/fixnum.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:pylons_sdk/pylons_sdk.dart';
import 'package:pylons_sdk/src/core/error/exceptions.dart';
import 'package:pylons_sdk/src/features/helper/dec_string.dart';
import 'package:pylons_sdk/src/features/validations/validate_recipe.dart';
import 'package:pylons_sdk/src/generated/cosmos/base/v1beta1/coin.pb.dart';
import 'package:pylons_sdk/src/generated/pylons/recipe.pb.dart';

void main() {
test('should throw error on empty cookbook name ', () {
Expand Down
3 changes: 2 additions & 1 deletion easel/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,8 @@ dependencies:

provider: ^6.0.1

pylons_sdk: 0.1.3
pylons_sdk:
path: ../dart_sdk

google_fonts: ^3.0.1

Expand Down
8 changes: 4 additions & 4 deletions wallet/lib/ipc/models/sdk_ipc_response.dart
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import 'dart:convert';

class SdkIpcResponse {
class SdkIpcResponse<T>{
bool success;
String errorCode;
String error;
dynamic data;
T? data;
String sender;
String action;

Expand All @@ -17,7 +17,7 @@ class SdkIpcResponse {
required this.action});

factory SdkIpcResponse.success(
{required dynamic data,
{required T data,
required String sender,
required String transaction}) {
return SdkIpcResponse(
Expand All @@ -35,7 +35,7 @@ class SdkIpcResponse {
required String errorCode}) {
return SdkIpcResponse(
sender: sender,
data: '',
data: null,
success: false,
error: error,
errorCode: errorCode,
Expand Down
2 changes: 1 addition & 1 deletion wallet/lib/stores/wallet_store.dart
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ abstract class WalletsStore {
/// This method creates the cookbook
/// Input : [Map] containing the info related to the creation of cookbook
/// Output : [String] response
Future<SdkIpcResponse> createCookbook(Map json);
Future<SdkIpcResponse<String>> createCookbook(Map json);

/// This method is for create recipe
/// MsgCreateRecipe proto
Expand Down
2 changes: 1 addition & 1 deletion wallet/lib/stores/wallet_store_dummy_inventory.dart
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@ class WalletsStoreDummyInventory extends WalletsStore {
}

@override
Future<SdkIpcResponse> createCookbook(Map json) {
Future<SdkIpcResponse<String>> createCookbook(Map json) {
return _baseInstance.createCookbook(json);
}

Expand Down
17 changes: 13 additions & 4 deletions wallet/lib/stores/wallet_store_imp.dart
Original file line number Diff line number Diff line change
Expand Up @@ -237,10 +237,21 @@ class WalletsStoreImp implements WalletsStore {
/// Input : [Map] containing the info related to the creation of cookbook
/// Output : [TransactionHash] hash of the transaction
@override
Future<SdkIpcResponse> createCookbook(Map json) async {
Future<SdkIpcResponse<String>> createCookbook(Map json) async {
final msgObj = pylons.MsgCreateCookbook.create()..mergeFromProto3Json(json);
msgObj.creator = wallets.value.last.publicAddress;
return _signAndBroadcast(msgObj);
final sdkResponse = await _signAndBroadcast(msgObj);
if (!sdkResponse.success) {
return SdkIpcResponse.failure(error: sdkResponse.error, sender: sdkResponse.sender, errorCode: sdkResponse.errorCode);
}

final cookBookResponseEither = await repository.getCookbookBasedOnId(cookBookId: json["id"].toString());

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

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

@override
Expand Down Expand Up @@ -577,8 +588,6 @@ class WalletsStoreImp implements WalletsStore {

final userName = getUsernameBasedOnAddress.getOrElse(() => '');



final creds = AlanPrivateAccountCredentials(
publicInfo: AccountPublicInfo(
chainId: baseEnv.chainId,
Expand Down
Loading

0 comments on commit 99b60b9

Please sign in to comment.