Skip to content

Commit

Permalink
Merge pull request #32 from Assassyn/fix/improve-code-coverage
Browse files Browse the repository at this point in the history
Fix/improve code coverage
  • Loading branch information
Assassyn committed Jun 1, 2023
2 parents 318ef0b + da6a070 commit bc26e0f
Show file tree
Hide file tree
Showing 28 changed files with 264 additions and 334 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,7 @@ To allow slightly more advanced aproach to amont which have to be transfer there
},
"actions": [
{
"name": "CustomActionName",
"username": "universal-bot",
"activeKey": "<activeKey here>",
"postingKey": "<postingKey here>",
Expand Down
10 changes: 9 additions & 1 deletion src/Functional.UniversalBot.CLI/Logging.fs
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,21 @@ open Functional.ETL.Pipeline
open PipelineResult
open Lamar

let writeToConsole message =
printfn "%O" message

let logger (entity: PipelineProcessData<PipelineResult.UniversalHiveBotResutls>) =
if entity.results |> Seq.length > 0
then
let lastMessage = entity.results.Head
printfn "%O" lastMessage
writeToConsole lastMessage
entity

let logConfigurationFound (config: Types.Configuration) =
let actionsMessage = sprintf "Found actions %i to process" (config.actions |> Seq.length)
writeToConsole actionsMessage
config

type ActionRegistry () as self =
inherit ServiceRegistry ()
do
Expand Down
5 changes: 3 additions & 2 deletions src/Functional.UniversalBot.CLI/Program.fs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ let config = getConfiguration ()
printfn "Starting UniveralHiveBot processs"

config
|> Scheduler.bind
|> Scheduler.start
|> Logging.logConfigurationFound
|> Scheduler.bind Logging.writeToConsole
|> Scheduler.start Logging.writeToConsole

Loop.executeLoop ()
24 changes: 20 additions & 4 deletions src/Functional.UniversalBot.CLI/Scheduler.fs
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,29 @@ let options =
opt.IncludingSeconds <- true
opt

let bind configuration =
let private bindAction logger name pipeline =
new Action<CancellationToken>(fun ct -> logger name; processPipeline pipeline |> ignore)

let bind logger configuration =
let crontab = new Scheduler()
createScheduledPipelines configuration
|> Seq.map (fun (trigger, pipeline) -> (CrontabSchedule.Parse(trigger, options), pipeline))
|> Seq.map (fun (cron, pipeline) -> (cron, new Action<CancellationToken>(fun ct -> processPipeline pipeline |> ignore)))
|> Seq.map (fun (name, trigger, pipeline) -> (name, CrontabSchedule.Parse(trigger, options), pipeline))
|> Seq.map (fun (name, cron, pipeline) -> (cron, bindAction logger name pipeline))
|> Seq.iter (fun (cron, action) -> crontab.AddTask(cron, action) |> ignore)
crontab

let start (scheduler: Scheduler) =
let start logger (scheduler: Scheduler) =
scheduler.Start()

let message =
let occurences =
scheduler.GetNextOccurrences ()
|> Seq.map (fun x -> x.ToTuple())
|> Seq.map (fun (date, _) -> date)
|> Seq.sortBy (fun x -> x)
//let nextOccurence = occurences |> Seq.head
//let (dateOfOccurence, _) = (nextOccurence.ToTuple())
let dateOfOccurence = occurences |> Seq.head
sprintf "Next action on: %s" (dateOfOccurence.ToString("dd/MM/yyyy HH:mm:ss"))

logger message
14 changes: 14 additions & 0 deletions src/Functional.UniversalBot.Core/Action.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
module Action

open PipelineResult
open Functional.ETL.Pipeline
open Functional.ETL.Pipeline.PipelineProcessData

let bindAction moduleName userbasedAction (entity: PipelineProcessData<UniversalHiveBotResutls>) =
let userDetails: (string * string * string) option = PipelineProcessData.readPropertyAsType entity "userdata"

match userDetails with
| Some (username, _, _) when username <> "" ->
userbasedAction username entity
| _ ->
NoUserDetails moduleName |> PipelineProcessData.withResult entity
8 changes: 1 addition & 7 deletions src/Functional.UniversalBot.Core/Configuration.fs
Original file line number Diff line number Diff line change
Expand Up @@ -66,19 +66,13 @@ let private bindTransfomers url (config: UserActionsDefinition) =
|> List.map (fun item -> binder item)
|> List.fold (fun state next -> state >> next >> actionDecorator) Transformer.defaultTransformer<PipelineResult.UniversalHiveBotResutls>

//type ScheduledPipeline =
// {
// schedule: string
// pipeline: Pipeline<PipelineResult.UniversalHiveBotResutls>
// }

let private bindScheduledPipeline urls (config: UserActionsDefinition) =
let reader = container.GetInstance<UserActionReader>()
let transforms = bindTransfomers urls config

let pipeline = Pipeline.bind (reader [config]) transforms

(config.Trigger, pipeline)
(config.Name, config.Trigger, pipeline)

let private bindPipeline urls (config: UserActionsDefinition) =
let reader = container.GetInstance<UserActionReader>()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
<Compile Include="SeriesToActionsRewriter.fs" />
<Compile Include="Configuration.Types.fs" />
<Compile Include="Configuration.fs" />
<Compile Include="Action.fs" />
</ItemGroup>

<ItemGroup>
Expand Down
9 changes: 9 additions & 0 deletions src/Functional.UniversalBot.Core/Types.fs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ let private defaultTrigger = "0 0 */1 * * *"

type UserActionsDefinition () =
let mutable username = ""
let mutable name = ""
let mutable activeKey = ""
let mutable postingKey = ""
let mutable trigger = defaultTrigger
Expand All @@ -34,6 +35,14 @@ type UserActionsDefinition () =
member this.Username
with get () = username
and set (value) = username <- value
member this.Name
with get () =
if System.String.IsNullOrEmpty(name)
then
username
else
name
and set (value) = name <- value
member this.ActiveKey
with get () = activeKey
and set (value) = activeKey <- value
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,44 +32,38 @@ let private scheduleTokenToPoolTransfer username tokenPair baseQuantity quoteQua
|> buildCustomJson username "ssc-mainnet-hive"
|> scheduleActiveOperation ModuleName tokenPair

let action hive hiveEngineUrl tokenPair leftAmountCalculator rightAmountCalculator (entity: PipelineProcessData<UniversalHiveBotResutls>) =
let userDetails: (string * string * string) option = PipelineProcessData.readPropertyAsType entity "userdata"

match userDetails with
| Some (username, _, _) when username <> "" ->
let marketPosition = HiveEngine.getAvailableMarketPools hiveEngineUrl tokenPair
let (leftToken, rightToken) = splitPair tokenPair
let action hive hiveEngineUrl tokenPair leftAmountCalculator rightAmountCalculator username (entity: PipelineProcessData<UniversalHiveBotResutls>) =
let marketPosition = HiveEngine.getAvailableMarketPools hiveEngineUrl tokenPair
let (leftToken, rightToken) = splitPair tokenPair

let leftTokenPrecision = TokenInfo.getTokenPrecision entity leftToken
let rightTokenPrecision = TokenInfo.getTokenPrecision entity rightToken
let leftTokenPrecision = TokenInfo.getTokenPrecision entity leftToken
let rightTokenPrecision = TokenInfo.getTokenPrecision entity rightToken

let leftTokenBaseAmount =
readPropertyAsDecimal entity leftToken
|> defaultWhenNone 0M
|> leftAmountCalculator
|> roundToPrecision leftTokenPrecision
let leftTokenQuoteAmount = (leftTokenBaseAmount * marketPosition.basePrice) |> roundToPrecision rightTokenPrecision
let leftTokenBaseAmount =
readPropertyAsDecimal entity leftToken
|> defaultWhenNone 0M
|> leftAmountCalculator
|> roundToPrecision leftTokenPrecision
let leftTokenQuoteAmount = (leftTokenBaseAmount * marketPosition.basePrice) |> roundToPrecision rightTokenPrecision

let rightTokenBaseAmount =
readPropertyAsDecimal entity rightToken
|> defaultWhenNone 0M
|> rightAmountCalculator
|> roundToPrecision rightTokenPrecision
let rightTokenQuoteAmount = (rightTokenBaseAmount * marketPosition.quotePrice) |> roundToPrecision leftTokenPrecision
let rightTokenBaseAmount =
readPropertyAsDecimal entity rightToken
|> defaultWhenNone 0M
|> rightAmountCalculator
|> roundToPrecision rightTokenPrecision
let rightTokenQuoteAmount = (rightTokenBaseAmount * marketPosition.quotePrice) |> roundToPrecision leftTokenPrecision


match (leftTokenBaseAmount, leftTokenQuoteAmount, rightTokenBaseAmount, rightTokenQuoteAmount) with
| (leftBase, leftQuote, _, _) when leftBase > 0M && leftQuote > 0M && leftBase <= leftTokenBaseAmount && leftQuote <= rightTokenBaseAmount ->
scheduleTokenToPoolTransfer username tokenPair leftBase leftQuote |> withResult entity
| (_, _, rightBase, rightQuote) when rightBase > 0M && rightQuote > 0M && rightBase <= rightTokenBaseAmount && rightQuote <= leftTokenBaseAmount ->
scheduleTokenToPoolTransfer username tokenPair rightQuote rightBase |> withResult entity
| _ ->
TokenBalanceTooLow (ModuleName, username, tokenPair) |> withResult entity
match (leftTokenBaseAmount, leftTokenQuoteAmount, rightTokenBaseAmount, rightTokenQuoteAmount) with
| (leftBase, leftQuote, _, _) when leftBase > 0M && leftQuote > 0M && leftBase <= leftTokenBaseAmount && leftQuote <= rightTokenBaseAmount ->
scheduleTokenToPoolTransfer username tokenPair leftBase leftQuote |> withResult entity
| (_, _, rightBase, rightQuote) when rightBase > 0M && rightQuote > 0M && rightBase <= rightTokenBaseAmount && rightQuote <= leftTokenBaseAmount ->
scheduleTokenToPoolTransfer username tokenPair rightQuote rightBase |> withResult entity
| _ ->
NoUserDetails ModuleName |> PipelineProcessData.withResult entity
TokenBalanceTooLow (ModuleName, username, tokenPair) |> withResult entity

let bind (urls: Urls) (parameters: Map<string, string>) =
let tokenPair = parameters.["tokenPair"]
let leftAmount = parameters.["leftAmount"] |> AmountCalator.bind
let rightAmount = parameters.["rightAmount"] |> AmountCalator.bind
action urls.hiveNodeUrl urls.hiveEngineNodeUrl tokenPair leftAmount rightAmount
Action.bindAction ModuleName (action urls.hiveNodeUrl urls.hiveEngineNodeUrl tokenPair leftAmount rightAmount)
Original file line number Diff line number Diff line change
Expand Up @@ -12,32 +12,27 @@ open Functional.ETL.Pipeline.PipelineProcessData
[<Literal>]
let ModuleName = "DelegateStake"

let action tokenSymbol delegateTo amountCalcualtor (entity: PipelineProcessData<UniversalHiveBotResutls>) =
let userDetails: (string * string * string) option = readPropertyAsType entity "userdata"
let action tokenSymbol delegateTo amountCalcualtor username (entity: PipelineProcessData<UniversalHiveBotResutls>) =
let precision = TokenInfo.getTokenPrecision entity tokenSymbol
let tokenBalance =
sprintf "%s_stake" tokenSymbol
|> readPropertyAsDecimal entity
|> defaultWhenNone 0M
|> amountCalcualtor
|> roundToPrecision precision

match userDetails with
| Some (username, activeKey, _) ->
let precision = TokenInfo.getTokenPrecision entity tokenSymbol
let tokenBalance =
sprintf "%s_stake" tokenSymbol
|> readPropertyAsDecimal entity
|> defaultWhenNone 0M
|> amountCalcualtor
|> roundToPrecision precision

if tokenBalance > 0M
then
bindCustomJson "tokens" "delegate" {|``to`` = delegateTo;symbol = tokenSymbol;quantity = String.asString tokenBalance|}
|> buildCustomJson username "ssc-mainnet-hive"
|> scheduleActiveOperation ModuleName tokenSymbol
|> withResult entity
else
TokenBalanceTooLow (ModuleName, username, tokenSymbol) |> withResult entity
| _ ->
NoUserDetails ModuleName |> withResult entity
if tokenBalance > 0M
then
bindCustomJson "tokens" "delegate" {|``to`` = delegateTo;symbol = tokenSymbol;quantity = String.asString tokenBalance|}
|> buildCustomJson username "ssc-mainnet-hive"
|> scheduleActiveOperation ModuleName tokenSymbol
|> withResult entity
else
TokenBalanceTooLow (ModuleName, username, tokenSymbol) |> withResult entity

let bind urls (parameters: Map<string, string>) =
let token = parameters.["token"]
let delegateTo = parameters.["delegateTo"]
let amount = parameters.["amount"] |> AmountCalator.bind
action token delegateTo amount

Action.bindAction ModuleName (action token delegateTo amount)
38 changes: 16 additions & 22 deletions src/Functional.UniversalBot.Plugin.HiveEngine/Actions/SellToken.fs
Original file line number Diff line number Diff line change
Expand Up @@ -18,30 +18,24 @@ let private getTokenPrice hiveEngineUrl tokenSymbol quantityToSell =
|> Seq.find (fun marketBook -> marketBook.quantity >= quantityToSell)
priceItem.price

let action hive hiveEngineUrl tokenSymbol amountCalcualtor (entity: PipelineProcessData<UniversalHiveBotResutls>) =
let userDetails: (string * string * string) option = PipelineProcessData.readPropertyAsType entity "userdata"
let action hive hiveEngineUrl tokenSymbol amountCalcualtor username (entity: PipelineProcessData<UniversalHiveBotResutls>) =
let amountToSell =
tokenSymbol
|> readPropertyAsDecimal entity
|> defaultWhenNone 0M
|> amountCalcualtor

match userDetails with
| Some (username, _, _) when username <> "" ->
let amountToSell =
tokenSymbol
|> readPropertyAsDecimal entity
|> defaultWhenNone 0M
|> amountCalcualtor

if amountToSell > 0M
then
let tokenPrice = getTokenPrice hiveEngineUrl tokenSymbol amountToSell
bindCustomJson "market" "sell" {| symbol = tokenSymbol; quantity = String.asString amountToSell; price = String.asString tokenPrice; |}
|> buildCustomJson username "ssc-mainnet-hive"
|> scheduleActiveOperation ModuleName tokenSymbol
|> withResult entity
else
TokenBalanceTooLow (ModuleName, username, tokenSymbol) |> PipelineProcessData.withResult entity
| _ ->
NoUserDetails ModuleName |> PipelineProcessData.withResult entity
if amountToSell > 0M
then
let tokenPrice = getTokenPrice hiveEngineUrl tokenSymbol amountToSell
bindCustomJson "market" "sell" {| symbol = tokenSymbol; quantity = String.asString amountToSell; price = String.asString tokenPrice; |}
|> buildCustomJson username "ssc-mainnet-hive"
|> scheduleActiveOperation ModuleName tokenSymbol
|> withResult entity
else
TokenBalanceTooLow (ModuleName, username, tokenSymbol) |> PipelineProcessData.withResult entity

let bind (urls: Urls) (parameters: Map<string, string>) =
let token = parameters.["token"]
let amountToSell = parameters.["amountToSell"] |> AmountCalator.bind
action urls.hiveNodeUrl urls.hiveEngineNodeUrl token amountToSell
Action.bindAction ModuleName (action urls.hiveNodeUrl urls.hiveEngineNodeUrl token amountToSell)
36 changes: 15 additions & 21 deletions src/Functional.UniversalBot.Plugin.HiveEngine/Actions/StakeToken.fs
Original file line number Diff line number Diff line change
Expand Up @@ -10,28 +10,22 @@ open Functional.ETL.Pipeline.PipelineProcessData
[<Literal>]
let ModuleName = "Stake"

let action tokenSymbol amountCalcualtor (entity: PipelineProcessData<UniversalHiveBotResutls>) =
let userDetails: (string * string * string) option = PipelineProcessData.readPropertyAsType entity "userdata"

match userDetails with
| Some (username, _, _) when username <> "" ->
let tokenBalance =
tokenSymbol
|> readPropertyAsDecimal entity
|> defaultWhenNone 0M
|> amountCalcualtor
if tokenBalance > 0M
then
bindCustomJson "tokens" "stake" {| ``to`` = username;symbol = tokenSymbol;quantity = String.asString tokenBalance|}
|> buildCustomJson username "ssc-mainnet-hive"
|> scheduleActiveOperation ModuleName tokenSymbol
|> withResult entity
else
TokenBalanceTooLow (ModuleName, username, tokenSymbol) |> PipelineProcessData.withResult entity
| _ ->
NoUserDetails ModuleName |> PipelineProcessData.withResult entity
let action tokenSymbol amountCalcualtor username (entity: PipelineProcessData<UniversalHiveBotResutls>) =
let tokenBalance =
tokenSymbol
|> readPropertyAsDecimal entity
|> defaultWhenNone 0M
|> amountCalcualtor
if tokenBalance > 0M
then
bindCustomJson "tokens" "stake" {| ``to`` = username;symbol = tokenSymbol;quantity = String.asString tokenBalance|}
|> buildCustomJson username "ssc-mainnet-hive"
|> scheduleActiveOperation ModuleName tokenSymbol
|> withResult entity
else
TokenBalanceTooLow (ModuleName, username, tokenSymbol) |> PipelineProcessData.withResult entity

let bind urls (parameters: Map<string, string>) =
let token = parameters.["token"]
let amount = parameters.["amount"] |> AmountCalator.bind
action token amount
Action.bindAction ModuleName (action token amount)

0 comments on commit bc26e0f

Please sign in to comment.