Skip to content
Open
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: 2 additions & 0 deletions internals/orderbook/orderbook.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,9 @@ func (ob *OrderBook) CancelContract(contract models.Contract) error {

// ModifyContract cancels a specific user's contract and then adds a new contract based on the updated modifications.
func (ob *OrderBook) ModifyContract(contract models.Contract) {
contract.RequestType = "delete"
ob.CancelContract(contract)
contract.RequestType = "add"
ob.AddContract(contract)
}

Expand Down
109 changes: 109 additions & 0 deletions internals/orderbook/orderbook_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
package orderbook

import (
"github.com/ChickenWhisky/makeItIntersting/pkg/models"
"testing"
)

func TestAddContract(t *testing.T) {
ob := NewOrderBook()
contract := models.Contract{
OrderType: "buy",
RequestType: "add",
Quantity: 10,
Price: 100,
}

err := ob.AddContract(contract)
if err != nil {
t.Errorf("expected no error, got %v", err)
}

if len(ob.Orders) != 1 {
t.Errorf("expected 1 contract, got %d", len(ob.Orders))
}

}

func TestDeleteContract(t *testing.T) {
ob := NewOrderBook()
contract := models.Contract{
ContractID: "123",
OrderType: "buy",
RequestType: "delete",
Quantity: 10,
Price: 100,
UserID: "user1",
}

ob.AddContract(contract)
contract.RequestType = "delete"
err := ob.CancelContract(contract)
if err != nil {
t.Errorf("expected no error, got %v", err)
}

if len(ob.Orders) != 0 {
t.Errorf("expected 0 contracts, got %d", len(ob.Orders))
}
}

func TestModifyContract(t *testing.T) {
ob := NewOrderBook()
contract := models.Contract{
ContractID: "123",
OrderType: "buy",
RequestType: "add",
Quantity: 10,
Price: 100,
UserID: "user1",
}

ob.AddContract(contract)
modifiedContract := models.Contract{
ContractID: "123",
OrderType: "buy",
Quantity: 20,
Price: 150,
UserID: "user1",
}

ob.ModifyContract(modifiedContract)
if ob.Orders["123"].Quantity != 20 {
t.Errorf("expected quantity 20, got %d", ob.Orders["123"].Quantity)
}

if ob.Orders["123"].Price != 150 {
t.Errorf("expected price 150, got %f", ob.Orders["123"].Price)
}
}

func TestMatchOrders(t *testing.T) {
ob := NewOrderBook()
askContract := models.Contract{
ContractID: "ask1",
OrderType: "sell",
RequestType: "add",
Quantity: 10,
Price: 100,
}
bidContract := models.Contract{
ContractID: "bid1",
OrderType: "buy",
RequestType: "add",
Quantity: 10,
Price: 100,
}

ob.AddContract(askContract)
ob.AddContract(bidContract)
ob.MatchOrders()

if len(ob.LastMatchedPrices) != 1 {
t.Errorf("expected 1 matched trade, got %d", len(ob.LastMatchedPrices))
}

if ob.LastMatchedPrices[0].Price != 100 {
t.Errorf("expected matched price 100, got %f", ob.LastMatchedPrices[0].Price)
}
}
14 changes: 9 additions & 5 deletions pkg/helpers/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,23 @@ package helpers

import (
"crypto/rand"
"encoding/base64"
"log"
"math/big"
"os"
"strconv"
)

func GenerateRandomString(length int) string {
const charset = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
b := make([]byte, length)
_, err := rand.Read(b)
if err != nil {
panic(err)
for i := range b {
num, err := rand.Int(rand.Reader, big.NewInt(int64(len(charset))))
if err != nil {
log.Fatal(err)
}
b[i] = charset[num.Int64()]
}
return base64.StdEncoding.EncodeToString(b)
return string(b)
}
func ConvertStringToInt(s string) int {
lengthFromEnv, err := strconv.Atoi(os.Getenv("CONTRACT_ID_LENGTH"))
Expand Down