-
Notifications
You must be signed in to change notification settings - Fork 67
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
17 changed files
with
266 additions
and
13 deletions.
There are no files selected for viewing
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package main | ||
|
||
func(cl *openCxClient) OrderCommand(args []string) error { | ||
return nil | ||
} | ||
|
||
func(cl *openCxClient) ViewOrderbook(args []string) error { | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package cxrpc | ||
|
||
import ( | ||
"github.com/mit-dci/opencx/match" | ||
) | ||
|
||
// SubmitOrderArgs holds the args for the submitorder command | ||
type SubmitOrderArgs struct { | ||
BuyOrder *match.Order | ||
SellOrder *match.Order | ||
} | ||
|
||
// SubmitOrderReply holds the args for the submitorder command | ||
type SubmitOrderReply struct { | ||
// TODO empty for now | ||
} | ||
|
||
// SubmitOrder submits an order to the order book or throws an error | ||
func(cl *OpencxRPC) SubmitOrder(args SubmitOrderArgs, reply *SubmitOrderReply) error { | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package ocxredis | ||
|
||
// unfinished | ||
|
||
// Buyer is a struct containing information needed for a buyer | ||
type Buyer struct { | ||
username string | ||
haveAsset byte | ||
// I think 51 bits is enough to store all the satoshis cause 21,000,000 * 10^8 = a 51 bit number | ||
amountHave int64 | ||
} | ||
|
||
// Seller is a struct containing information needed for a seller | ||
type Seller struct { | ||
username string | ||
haveAsset byte | ||
// I think 51 bits is enough to store all the satoshis cause 21,000,000 * 10^8 = a 51 bit number | ||
amountHave int64 | ||
} | ||
|
||
// ExchangeCoins exchanges coins between a buyer and a seller (with a fee of course) | ||
func (db *DB) ExchangeCoins(buyer Buyer, seller Seller) error { | ||
return nil | ||
} | ||
|
||
// InitializeAccount initializes all database values for an account with username 'username' | ||
func (db *DB) InitializeAccount(username string) error { | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package ocxsql | ||
|
||
// this is just here so it can be "implemented" | ||
|
||
// CreateAccount creates an account | ||
func(db *DB) CreateAccount(username string, password string) (bool, error) { | ||
// TODO later | ||
return true, nil | ||
} | ||
|
||
// CheckCredentials checks users username and passwords | ||
func(db *DB) CheckCredentials(username string, password string) (bool, error) { | ||
// TODO later | ||
return true, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package ocxsql | ||
|
||
// CreateStoreToken creates a token for username and stores it for a certain amount of time | ||
func(db *DB) CreateStoreToken(username string) ([]byte, error) { | ||
// TODO later | ||
return nil, nil | ||
} | ||
|
||
// CheckToken checks the token assigned to a user | ||
func(db *DB) CheckToken(username string, token []byte) (bool, error) { | ||
// TODO maybe never if I use signed stuff | ||
return true, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package ocxsql | ||
|
||
import ( | ||
"github.com/mit-dci/opencx/match" | ||
) | ||
|
||
// ExchangeCoins exchanges coins between a buyer and a seller (with a fee of course) | ||
func (db *DB) ExchangeCoins(buyOrder *match.Order, sellOrder *match.Order) error { | ||
// check balances | ||
// if balances check out then make the trade, update balances | ||
|
||
return nil | ||
} | ||
|
||
// InitializeAccount initializes all database values for an account with username 'username' | ||
func (db *DB) InitializeAccount(username string) error { | ||
// Balances table, username is one column and balances are all the other columns | ||
// SELECT username FROM balances... | ||
// tx, err := db.Begin() | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
package ocxsql | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
"log" | ||
"io" | ||
"database/sql" | ||
|
||
// mysql is just the driver, always interact with database/sql api | ||
_ "github.com/go-sql-driver/mysql" | ||
) | ||
|
||
// turn into config options | ||
var ( | ||
defaultUsername = "localhost" | ||
defaultPassword = "" | ||
balanceSchema = "balances" | ||
) | ||
|
||
// DB contains the sql DB type as well as a logger | ||
type DB struct { | ||
DBHandler *sql.DB | ||
logger *log.Logger | ||
} | ||
|
||
// SetupClient sets up the mysql client and driver | ||
func(db *DB) SetupClient() error { | ||
|
||
// open db handle | ||
dbHandle, err := sql.Open("mysql", "") | ||
if err != nil { | ||
return fmt.Errorf("Error opening database: \n%s", err) | ||
} | ||
|
||
db.DBHandler = dbHandle | ||
|
||
err = db.DBHandler.Ping() | ||
if err != nil { | ||
return fmt.Errorf("Could not ping the database, is it running: \n%s", err) | ||
} | ||
|
||
// check schema | ||
// if schema not there make it | ||
_, err = db.DBHandler.Exec("CREATE SCHEMA IF NOT EXISTS " + balanceSchema) | ||
if err != nil { | ||
return fmt.Errorf("Could not create balance schema: \n%s", err) | ||
} | ||
|
||
// if schema there then we're good | ||
_, err = db.DBHandler.Exec("USE " + balanceSchema) | ||
if err != nil { | ||
return fmt.Errorf("Could not use balance schema: \n%s", err) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
// SetLogPath sets the log path for the database, and tells it to also print to stdout. This should be changed in the future so only verbose clients log to stdout | ||
func (db *DB) SetLogPath(logPath string) error { | ||
logFile, err := os.OpenFile(logPath, os.O_CREATE|os.O_APPEND|os.O_RDWR, 0666) | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
mw := io.MultiWriter(os.Stdout, logFile) | ||
db.logger = log.New(mw, "OPENCX DATABASE: ", log.LstdFlags) | ||
db.LogPrintf("Logger has been set up at %s\n", logPath) | ||
return nil | ||
} | ||
|
||
// These methods can be removed, but these are used frequently so maybe the | ||
// time spent writing these cuts down on the time spent writing logger | ||
|
||
// LogPrintf is like printf but you don't have to go db.logger every time | ||
func (db *DB) LogPrintf(format string, v ...interface{}) { | ||
db.logger.Printf(format, v...) | ||
} | ||
|
||
// LogPrintln is like println but you don't have to go db.logger every time | ||
func (db *DB) LogPrintln(v ...interface{}) { | ||
db.logger.Println(v...) | ||
} | ||
|
||
// LogPrint is like print but you don't have to go db.logger every time | ||
func (db *DB) LogPrint(v ...interface{}) { | ||
db.logger.Print(v...) | ||
} | ||
|
||
// LogErrorf is like printf but with error at the beginning | ||
func (db *DB) LogErrorf(format string, v ...interface{}) { | ||
db.logger.Printf("ERROR: "+format, v...) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,6 @@ | ||
module github.com/mit-dci/opencx | ||
|
||
require github.com/go-redis/redis v6.15.1+incompatible | ||
require ( | ||
github.com/go-redis/redis v6.15.1+incompatible | ||
github.com/go-sql-driver/mysql v1.4.1 | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package match | ||
|
||
// Pair is a struct that represents a trading pair | ||
type Pair struct { | ||
AssetWant byte `json:"assetWant"` | ||
// the AssetHave asset will always be the asset whose balance is checked | ||
AssetHave byte `json:"assetHave"` | ||
} | ||
|
||
// Order is a struct that represents a stored side of a trade | ||
type Order struct { | ||
Client string `json:"username"` | ||
Side string `json:"side"` | ||
TradingPair Pair `json:"pair"` | ||
// amount of assetHave the user would like to trade | ||
AmountHave int64 `json:"amount"` | ||
// amount of assetWant the user wants for their assetHave | ||
AmountWant int64 `json:"price"` | ||
} | ||
|
||
func(o *Order) isBuySide() bool { | ||
return o.Side == "buy" | ||
} | ||
|
||
func(o *Order) isSellSide() bool { | ||
return o.Side == "sell" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters