diff --git a/Config/DefaultImmutable.ini b/Config/DefaultImmutable.ini new file mode 100644 index 0000000..4c804c3 --- /dev/null +++ b/Config/DefaultImmutable.ini @@ -0,0 +1,6 @@ + + +[/Script/Immutable.ImtblSwapRequest] +BaseUrls=((Sandbox, "https://checkout-playground.sandbox.immutable.com/checkout/swap"),(Production, "https://toolkit.immutable.com/checkout/swap")) +ApiKeys=((Sandbox, "pk_imapik-test-7-hfC5T$W$eEDE8Mc5mp"),(Production, "pk_imapik-WGd9orNd8mLdtTCTb3CP")) + diff --git a/Content/BlueprintSampleContent/ImtblAuthenticatedWidget4_26.uasset b/Content/BlueprintSampleContent/ImtblAuthenticatedWidget4_26.uasset index bf567db..b66362c 100644 Binary files a/Content/BlueprintSampleContent/ImtblAuthenticatedWidget4_26.uasset and b/Content/BlueprintSampleContent/ImtblAuthenticatedWidget4_26.uasset differ diff --git a/Content/BlueprintSampleContent/Marketplace/ImtblSwapWidget4_26.uasset b/Content/BlueprintSampleContent/Marketplace/ImtblSwapWidget4_26.uasset new file mode 100644 index 0000000..7d9cb1b Binary files /dev/null and b/Content/BlueprintSampleContent/Marketplace/ImtblSwapWidget4_26.uasset differ diff --git a/Source/Immutable/Private/Immutable/Marketplace/ImtblSwapRequest.cpp b/Source/Immutable/Private/Immutable/Marketplace/ImtblSwapRequest.cpp new file mode 100644 index 0000000..87bfea1 --- /dev/null +++ b/Source/Immutable/Private/Immutable/Marketplace/ImtblSwapRequest.cpp @@ -0,0 +1,85 @@ +#include "Immutable/Marketplace/ImtblSwapRequest.h" + +#include "GenericPlatform/GenericPlatformHttp.h" + +FString FImtblSwapRequestQueryParams::GetPercentEncodedUrl() const +{ + TArray QueryParams; + + if (!PublishableKey.IsEmpty()) + { + QueryParams.Add(FString::Printf(TEXT("%s=%s"), TEXT("publishableKey"), *FGenericPlatformHttp::UrlEncode(PublishableKey))); + } + + if (!FromTokenAddress.IsEmpty()) + { + QueryParams.Add(FString::Printf(TEXT("%s=%s"), TEXT("fromTokenAddress"), *FGenericPlatformHttp::UrlEncode(FromTokenAddress))); + } + + if (!ToTokenAddress.IsEmpty()) + { + QueryParams.Add(FString::Printf(TEXT("%s=%s"), TEXT("toTokenAddress"), *FGenericPlatformHttp::UrlEncode(ToTokenAddress))); + } + + return FString::Join(QueryParams, TEXT("&")); +} + +bool UImtblSwapRequest::GetBaseUrl(EImtblEnvironment Environment, FString& BaseUrl) const +{ + if (const FString* Find = BaseUrls.Find(Environment)) + { + BaseUrl = *Find; + + return true; + } + + return true; +} + +bool UImtblSwapRequest::GetApiKey(EImtblEnvironment Environment, FString& ApiKey) const +{ + if (const FString* Find = ApiKeys.Find(Environment)) + { + ApiKey = *Find; + + return true; + } + + return false; +} + +bool UImtblSwapRequest::ComputePath(FString& ComputedPath, EImtblEnvironment Environment, FString FromTokenAddress, FString ToTokenAddress) const +{ + FString BaseUrl; + bool bFoundBaseUrl = GetBaseUrl(Environment, BaseUrl); + + if (!bFoundBaseUrl) + { + return false; + } + + FString ApiKey; + bool bFoundApiKey = GetApiKey(Environment, ApiKey); + + if (!bFoundApiKey) + { + return false; + } + + FImtblSwapRequestQueryParams QueryParams; + QueryParams.PublishableKey = ApiKey; + QueryParams.FromTokenAddress = FromTokenAddress; + QueryParams.ToTokenAddress = ToTokenAddress; + + const FString QueryParamsPercentEncodedUrl = QueryParams.GetPercentEncodedUrl(); + + ComputedPath = FString::Printf + ( + TEXT("%s%s%s"), + *BaseUrl, + !QueryParamsPercentEncodedUrl.IsEmpty() ? TEXT("?") : TEXT(""), + *QueryParamsPercentEncodedUrl + ); + + return true; +} \ No newline at end of file diff --git a/Source/Immutable/Public/Immutable/ApplicationConfig.h b/Source/Immutable/Public/Immutable/ApplicationConfig.h index 78f526e..3b96c20 100644 --- a/Source/Immutable/Public/Immutable/ApplicationConfig.h +++ b/Source/Immutable/Public/Immutable/ApplicationConfig.h @@ -96,10 +96,10 @@ class IMMUTABLE_API UApplicationConfig : public UObject { switch (Environment) { - case EPassportEnvironment::Production: + case EImtblEnvironment::Production: return ImmutablePassportEnvironmentConstants::EnvironmentProduction; default: - case EPassportEnvironment::Sandbox: + case EImtblEnvironment::Sandbox: return ImmutablePassportEnvironmentConstants::EnvironmentProduction; } } @@ -159,7 +159,7 @@ class IMMUTABLE_API UApplicationConfig : public UObject * @note The default environment is set to the Sandbox environment. */ UPROPERTY(EditDefaultsOnly, Category = "Passport") - EPassportEnvironment Environment = EPassportEnvironment::Sandbox; + EImtblEnvironment Environment = EImtblEnvironment::Sandbox; /** * (Android, iOS, and macOS only) diff --git a/Source/Immutable/Public/Immutable/ImmutableEnums.h b/Source/Immutable/Public/Immutable/ImmutableEnums.h index ad9f31c..79183b4 100644 --- a/Source/Immutable/Public/Immutable/ImmutableEnums.h +++ b/Source/Immutable/Public/Immutable/ImmutableEnums.h @@ -1,10 +1,11 @@ #pragma once UENUM(BlueprintType) -enum class EPassportEnvironment : uint8 +enum class EImtblEnvironment : uint8 { - Development, Sandbox, Production, + MAX }; +ENUM_RANGE_BY_COUNT(EImtblEnvironment, EImtblEnvironment::MAX) \ No newline at end of file diff --git a/Source/Immutable/Public/Immutable/Marketplace/ImtblSwapRequest.h b/Source/Immutable/Public/Immutable/Marketplace/ImtblSwapRequest.h new file mode 100644 index 0000000..1802326 --- /dev/null +++ b/Source/Immutable/Public/Immutable/Marketplace/ImtblSwapRequest.h @@ -0,0 +1,77 @@ +#pragma once + +#include "ImtblSwapRequest.generated.h" + +enum class EImtblEnvironment : uint8; + +/** + * Swap request query params + */ +USTRUCT(BlueprintType) +struct FImtblSwapRequestQueryParams +{ + GENERATED_BODY() + +public: + FString GetPercentEncodedUrl() const; + +public: + UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Immutable") + FString PublishableKey; + + UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Immutable") + FString FromTokenAddress; + + UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Immutable") + FString ToTokenAddress; +}; + +/** + * Functionality to generate swap requests + */ +UCLASS(BlueprintType, Config = "Immutable", DefaultConfig) +class IMMUTABLE_API UImtblSwapRequest : public UObject +{ + GENERATED_BODY() + +public: + /** + * Finds the base url associated with the environment. + * + * @param Environment The environment that will be used to look the value up. Only sandbox and production environment are supported. + * @param BaseUrl The base url associated with the key. + * @return True if an item was found (False indicates nothing in the map uses the provided environment). + */ + UFUNCTION(BlueprintPure, Category = "Immutable") + bool GetBaseUrl(EImtblEnvironment Environment, FString& BaseUrl) const; + + /** + * Finds the api key associated with the provided key. + * + * @param Environment The environment that will be used to look the value up. Only sandbox and production environment are supported. + * @param ApiKey The api key associated with the key. + * @return True if an item was found (False indicates nothing in the map uses the provided environment). + */ + UFUNCTION(BlueprintPure, Category = "Immutable") + bool GetApiKey(EImtblEnvironment Environment, FString& ApiKey) const; + + /** + * Generates a link for the swap flow. + * + * @param ComputedPath [Out] The generated link. + * @param Environment [In] The environment for which the path is being computed for (e.g. sandbox, production). + * @param ToTokenAddress [Optional] The token address of the token being swapped from. Default value is an empty string. + * @param FromTokenAddress [Optional] The token address of the token being swapped to. Default value is an empty string. + * + * @return True if path was computed (False indicates link failed to compute). + */ + UFUNCTION(BlueprintCallable, BlueprintPure = "False", Category = "Immutable") + bool ComputePath(FString& ComputedPath, EImtblEnvironment Environment, FString FromTokenAddress = TEXT(""), FString ToTokenAddress = TEXT("")) const; + +public: + UPROPERTY(Config, EditDefaultsOnly, BlueprintReadOnly, Category = "Immutable") + TMap BaseUrls; + + UPROPERTY(Config, EditDefaultsOnly, BlueprintReadOnly, Category = "Immutable") + TMap ApiKeys; +}; \ No newline at end of file