-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_exchange.go
142 lines (120 loc) · 3.1 KB
/
test_exchange.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
package exchange
import (
"errors"
"io/ioutil"
"sync"
"github.com/stampjohnny/mttv/config"
"github.com/stampjohnny/mttv/e"
"github.com/stampjohnny/mttv/logging"
"github.com/stampjohnny/mttv/utils"
"golang.org/x/xerrors"
)
const exchangeName = "Test exchange"
type TestExchange struct {
cryptoBalance float64
moneyBalance float64
cryptoAmountToBuy float64
price float64
lock sync.RWMutex
freezed bool
}
func (t *TestExchange) Copy() *TestExchange {
newExchange := TestExchange{
cryptoBalance: t.GetCryptoBalance(),
moneyBalance: t.GetMoneyBalance(),
cryptoAmountToBuy: t.GetAmount(),
price: t.GetPrice(),
freezed: true,
}
return &newExchange
}
func (t *TestExchange) isFreezed() bool {
return t.freezed
}
func (t *TestExchange) GetAmount() float64 {
logging.Debug("amount %v", t.cryptoAmountToBuy)
return t.cryptoAmountToBuy
}
func (t *TestExchange) saveAmount(amount float64) {
t.cryptoAmountToBuy = amount
}
func (t *TestExchange) Buy(amount float64) (interface{}, error) {
logging.Debug("amount=%v", amount)
if t.isFreezed() {
return nil, e.Err("context is freezed")
}
t.lock.Lock()
defer t.lock.Unlock()
t.saveAmount(amount)
if err := t.SetMoneyBalance(t.GetMoneyBalance() - t.getMoneyToPay()); err != nil {
return nil, xerrors.Errorf("catn't update money balance: %v", err)
}
if err := t.SetCryptoBalance(t.GetCryptoBalance() + t.GetAmount()); err != nil {
return nil, xerrors.Errorf("catn't update crypto balance: %v", err)
}
return t.Copy(), nil
}
func (t *TestExchange) getMoneyToPay() float64 {
moneyToPay := t.cryptoAmountToBuy * t.price
logging.Debug("amountToBuy(%v) * price(%v) return %v", t.cryptoAmountToBuy, t.price, moneyToPay)
return moneyToPay
}
func (t *TestExchange) SetuptTestEnv() {
if t.isFreezed() {
utils.Crash("context is freezed")
}
tmpDir, err := ioutil.TempDir("", "mttv")
if err != nil {
panic(err)
}
config.BaseLogDir = tmpDir
}
func (t *TestExchange) SetDefault() {
if t.isFreezed() {
utils.Crash("context is freezed")
}
t.cryptoBalance = float64(100)
t.moneyBalance = float64(100000)
t.price = 5600
}
func (t *TestExchange) GetCryptoBalance() float64 {
return t.cryptoBalance
}
func (t *TestExchange) SetCryptoBalance(amount float64) error {
if t.isFreezed() {
return e.Err("context is freezed")
}
if amount <= 0 {
return errors.New("can't set negative amount")
}
t.cryptoBalance = amount
return nil
}
func (t *TestExchange) GetMoneyBalance() float64 {
logging.Debug("moneyBalance = %v", t.moneyBalance)
return t.moneyBalance
}
func (t *TestExchange) SetMoneyBalance(amount float64) error {
logging.Debug("set money balance: %v", amount)
if t.isFreezed() {
return e.Err("context is freezed")
}
if amount <= 0 {
return errors.New("can't set negative amount")
}
t.moneyBalance = amount
return nil
}
func (t *TestExchange) SetPrice(price float64) error {
if t.isFreezed() {
return e.Err("context is freezed")
}
t.price = price
return nil
}
func (t *TestExchange) GetPrice() float64 {
return t.price
}
func (t *TestExchange) Save() error {
return nil
}