stack icon indicating copy to clipboard operation
stack copied to clipboard

chore: Add "orderengine" Library to go-libs 1

Open agourdel opened this issue 1 year ago • 0 comments

This library allows configuring complex transaction orders independently of any ledger implementation

Key Features

Order Creation:

Enables users to create orders with complex transactions. An order can contain a global condition of execution and also one or multiple transactions, each with specific conditions.

Logical and Numerical Expressions:

Supports logical expressions (LogicalExpression) and numerical expressions (ValueExpression). Logical expressions evaluate boolean conditions, while numerical expressions evaluate dynamic values.

Conditional Transactions:

Allows defining conditional transactions where a transaction is executed only if certain conditions are met. Conditions can be based on account balances, aggregated balances, or other criteria defined by logical and numerical expressions.

Variable Amounts:

Supports VariableAmount, allowing the definition of dynamic amounts based on numerical expressions. Ideal for scenarios where the amount to be transferred depends on multiple factors.

Example Conditional Transaction in Numscript

In this example, the user wants to create a Conditional Transaction using Numscript. If the aggregated balance of all bank accounts of the player LeslieKnope is less than 500 COINs, then the Central Bank should perform a transaction of 25 COINs to her main account and 75 COINs to her Chest account.

See ./ledgerorder/examples for more...


func CondExample1(){
	
    /*
		
		FICTIVE EXAMPLE BASED ON FUTUR PROBABLE FEATURES
		Conditionnal Transaction

		send [COIN 100] (
			condition = {
				$lt : { AggBalance[@player:leslieknope], 500 }	
			}

  			source = @centralbank
 			destination = {
    			25% to @player:leslieknope
    			remaining to @player:leslieknope:chest
  			}
		)


	*/

	source1 := AccountIdentifier("orga_schema", "ledger-01", "@centralbank")
	destination1 := AccountIdentifier("orga_schema", "ledger-01","@player:leslieknope")
	destination2 := AccountIdentifier("orga_schema", "ledger-01", "@player:leslieknope:chest")

	aggIdentifier := AccountIdentifier("orga_schema", "ledger-01","@player:leslieknope")


	aggBalance1 := VariableAmount.New("aggBalance1",
				ValueExpression.New(Operators.AggBalance, aggIdentifier))

	condTrans1 := ConditionTransaction.With(LogicalExpression.New(Operators.Lt, aggBalance1, MicroAmount.FromValue(500)))

	orderDraft := Order()

	orderDraft.AddVariableAmount(aggBalance1)
	
	transaction1 := Transaction().
			SetSource(source1).
			SetDestination(destination1).
			SetAsset("COIN").
		SetAmount(MicroAmount.FromValue(25))

	transaction2 := Transaction(). 
			SetSource(source1). 
			SetDestination(destination2). 
			SetAsset("COIN"). 
			SetAmount(MicroAmount.FromValue(75))
	
	orderDraft.AddTransactionWithCondition(transaction1, condTrans1)
	orderDraft.AddTransactionWithCondition(transaction2, condTrans1)

	// OrderDraft.Commit()

}

agourdel avatar Jun 20 '24 18:06 agourdel