func TestInitDir(t *testing.T) { tmpDir := os.TempDir() dir := tmpDir + "/.skycoin-exchange/account" account.InitDir(dir) if _, err := os.Stat(dir); os.IsNotExist(err) { t.Error("InitDir faild") return } // clear the dir os.RemoveAll(filepath.Dir(dir)) }
// New create new server func New(cfg *Config) engine.Exchange { // init the data dir path := initDataDir(cfg.DataDir) // init the account dir account.InitDir(filepath.Join(path, "account")) // init the order book dir. order.InitDir(filepath.Join(path, "orderbook")) var ( acntMgr account.Manager err error ) // load account manager if exist. acntMgr, err = account.LoadManager() if err != nil { if os.IsNotExist(err) { // create new account manager. acntMgr = account.NewManager() } else { panic(err) } } wltItems := []walletItem{ {bitcoin.Type, cfg.Seed}, {skycoin.Type, cfg.Seed}, } // init wallets in server. wlts, err := makeWallets(filepath.Join(path, "wallet"), wltItems) if err != nil { panic(err) } // create bitcoin utxo manager btcWatchAddrs, err := wlts.GetAddresses(bitcoin.Type) if err != nil { panic(err) } btcum := bitcoin.NewUtxoManager(cfg.UtxoPoolSize, btcWatchAddrs) // create skycoin utxo manager skyWatchAddrs, err := wlts.GetAddresses(skycoin.Type) if err != nil { panic(err) } skyum := skycoin.NewUtxoManager(cfg.NodeAddresses[skycoin.Type], cfg.UtxoPoolSize, skyWatchAddrs) // load or create order books. var orderManager *order.Manager orderManager, err = order.LoadManager() if err != nil { if os.IsNotExist(err) { orderManager = order.NewManager() orderManager.AddBook("bitcoin/skycoin", &order.Book{}) } else { panic(err) } } s := &ExchangeServer{ cfg: *cfg, wallets: wlts, Manager: acntMgr, btcum: btcum, skyum: skyum, orderManager: orderManager, coins: make(map[string]coin.Gateway), orderHandlers: map[string]chan order.Order{ "bitcoin/skycoin": make(chan order.Order, 100), }, } return s }