Beispiel #1
0
Datei: main.go Projekt: wangch/tx
func main() {
	flag.Parse()
	glog.SetLogToStderr(true)
	if *server == "" {
		usage()
		return
	}
	if *sender == "" || len(*sender) != 34 || (*sender)[0] != 'i' {
		usage()
		return
	}
	if *secret == "" || len(*secret) != 29 || (*secret)[0] != 's' {
		usage()
		return
	}
	if *recipient == "" || len(*recipient) != 34 || (*recipient)[0] != 'i' {
		usage()
		return
	}
	if *amount <= 0 {
		glog.Errorln(*amount, "发送金额必须>0")
		return
	}

	ws, err := websockets.NewRemote(*server)
	if err != nil {
		glog.Fatal(err)
	}

	issuer := "iN8sGowQCg1qptWcJG1WyTmymKX7y9cpmr"
	err = payment(ws, *secret, *sender, issuer, *recipient, *currency, "", *amount)
	if err != nil {
		glog.Fatal(err)
	}
}
Beispiel #2
0
func main() {
	if len(os.Args) == 1 {
		showUsage()
	}
	flags.Parse(os.Args[2:])
	matches := argumentRegex.FindStringSubmatch(os.Args[1])
	r, err := websockets.NewRemote(*host)
	checkErr(err)
	glog.Infoln("Connected to: ", *host)
	switch {
	case len(matches) == 0:
		showUsage()
	case len(matches[1]) > 0:
		hash, err := data.NewHash256(matches[1])
		checkErr(err)
		fmt.Println("Getting transaction: ", hash.String())
		result, err := r.Tx(*hash)
		checkErr(err)
		explain(&result.TransactionWithMetaData, terminal.Default)
	case len(matches[2]) > 0:
		seq, err := strconv.ParseUint(matches[2], 10, 32)
		checkErr(err)
		ledger, err := r.Ledger(seq, true)
		checkErr(err)
		fmt.Println("Getting transactions for: ", seq)
		for _, txm := range ledger.Ledger.Transactions {
			explain(txm, terminal.Default)
		}
	case len(matches[3]) > 0:
		account, err := data.NewAccountFromAddress(matches[3])
		checkErr(err)
		fmt.Println("Getting transactions for: ", account.String())
		for txm := range r.AccountTx(*account, *pageSize) {
			explain(txm, terminal.ShowLedgerSequence)
		}
	case len(matches[4]) > 0:
		r := bufio.NewReader(os.Stdin)
		for line, err := r.ReadString('\n'); err == nil; line, err = r.ReadString('\n') {
			// TODO: Accept nodeid:nodedata format
			b, err := hex.DecodeString(line[:len(line)-1])
			checkErr(err)
			var nodeid data.Hash256
			v, err := data.ReadPrefix(bytes.NewReader(b), nodeid)
			checkErr(err)
			terminal.Println(v, terminal.Default)
		}
	}
}
Beispiel #3
0
func main() {
	flag.Parse()
	r, err := websockets.NewRemote(*host)
	checkErr(err, true)

	// Subscribe to all streams
	_, err = r.Subscribe(false, false, false, false, []string{*account})
	checkErr(err, true)
	// terminal.Println(fmt.Sprint("Subscribed at: ", confirmation.LedgerSequence), terminal.Default)

	// Consume messages as they arrive
	for {
		msg, ok := <-r.Incoming
		if !ok {
			return
		}

		switch msg := msg.(type) {
		case *websockets.LedgerStreamMsg:
			terminal.Println(msg, terminal.Default)
		case *websockets.TransactionStreamMsg:
			terminal.Println(&msg.Transaction, terminal.Indent)
			for _, path := range msg.Transaction.PathSet() {
				terminal.Println(path, terminal.DoubleIndent)
			}
			trades, err := msg.Transaction.Trades()
			checkErr(err, false)
			for _, trade := range trades {
				terminal.Println(trade, terminal.DoubleIndent)
			}
			balances, err := msg.Transaction.Balances()
			checkErr(err, false)
			for _, balance := range balances {
				terminal.Println(balance, terminal.DoubleIndent)
			}
			// log.Println(msg.Transaction.GetType())
			// b, err := json.MarshalIndent(msg, "", "  ")
			// log.Println(string(b))

		case *websockets.ServerStreamMsg:
			terminal.Println(msg, terminal.Default)
		}
	}
}
Beispiel #4
0
func monitor(serverAddr string, wallets []string) error {
	for {
		ws, err := websockets.NewRemote(serverAddr)
		gws = ws
		if err != nil {
			glog.Error(err)
			time.Sleep(time.Second * 1)
			continue
		}

		_, err = ws.Subscribe(false, false, false, false, wallets)
		if err != nil {
			glog.Error(err)
			time.Sleep(time.Second * 1)
			continue
		}

		for {
			msg, ok := <-ws.Incoming
			if !ok {
				glog.Warning("ws.Incoming chan closed.")
				break
			}

			switch msg := msg.(type) {
			case *websockets.TransactionStreamMsg:
				// the transaction must be validated
				// and only watch payments
				b, err := json.MarshalIndent(msg, "", "  ")
				if err != nil {
					glog.Error(err)
				}
				glog.Info(string(b))
				if !msg.EngineResult.Success() {
					glog.Warning("the transaction NOT success")
					break
				}

				if msg.Transaction.GetType() == "Payment" {
					paymentTx := msg.Transaction.Transaction.(*data.Payment)
					out := isOut(paymentTx.Account.String(), wallets)
					if paymentTx.InvoiceID == nil {
						glog.Warning("paymentTx.InvoiceID == nil")
						break
					}
					// query the paymen tx InvoiceId in database and update tx hash
					invid := paymentTx.InvoiceID.String()
					r := &Request{}
					Gorm.QueryTable("request").Filter("invoice_id", invid).RelatedSel().One(r)
					if r.R == nil {
						glog.Warning("the payment invoiceID " + invid + "is NOT in database")
						// must be cold wallet send to hotwallet
						break
					}

					r.R.TxHash = paymentTx.Hash.String()
					if out { // 存款 or 发行ICC
						r.R.Status = OKC
					} else { // 取款 or 回收ICC
						r.R.Status = C*K
					}
					_, err = Gorm.Update(r.R)
					if err != nil {
						// have error in database
						// must report the error msg on web
						glog.Error(err)
					}
				}
			}
		}
	}
}