示例#1
0
文件: models.go 项目: aryaya/ifundmgr
func AddReq(gid, gwallet string, t int, u *User, currency string, amount, fees float64) (*Request, error) {
	gbas := Gconf.GBAs
	var gba *GateBankAccount
	for _, g := range gbas {
		if strings.Contains(gid, g.BankId) {
			gba = &g
			break
		}
	}

	req := &Request{
		CTime:        time.Now(),
		UName:        u.UName,
		UWallet:      u.UWallet,
		UBankName:    u.UBankName,
		UBankId:      u.UBankId,
		UContact:     u.UContact,
		UCertificate: u.UCertificate,
		Currency:     currency,
		Amount:       amount,
		Fees:         fees,
		Type:         t,
	}
	req.InvoiceId = getInvoiceId(req)

	if gba == nil && len(gbas) > 0 {
		gba = &gbas[0]
	}

	tm := time.Unix(0, 0) //time.Date(1979, 1, 1, 0, 0, 0, 0, time.UTC)

	rec := &Recoder{
		R:       req,
		FTime:   tm,
		MTime:   tm,
		ATime:   tm,
		GWallet: gwallet,
	}
	// 因为取款和回收会产生多个记录, 所以只有交易完成时, 才变为C*K(提交状态)
	// 所以这里只设置存款和发行的C*K状态
	if t == Deposit || t == Issue {
		rec.Status = C*K
	}
	if gba != nil {
		// req.GName = gba.Name
		// req.GBankName = gba.BankName
		rec.GBankId = gba.BankId
	}
	// log.Printf("%#v", rec)
	req.R = rec
	_, err := Gorm.Insert(rec)
	if err != nil {
		glog.Fatal(err)
	}
	_, err = Gorm.Insert(req)
	if err != nil {
		glog.Fatal(err)
	}
	return req, nil
}
示例#2
0
文件: main.go 项目: 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)
	}
}
示例#3
0
文件: main.go 项目: aryaya/ifundgate
func main() {
	glog.SetLogDirs(".")
	glog.SetLogToStderr(true)
	conf := controllers.Gconf
	beego.Get("/federation", func(ctx *context.Context) {
		federation(ctx, conf)
	})

	beego.Get("/ripple.txt", func(ctx *context.Context) {
		f, err := os.Open("ripple.txt")
		if err != nil {
			glog.Fatal(err)
		}
		io.Copy(ctx.ResponseWriter, f)
		f.Close()
	})

	beego.Get("/quote", func(ctx *context.Context) {
		u := "http://" + conf.Host + "/api/quote?" + ctx.Request.URL.RawQuery
		glog.Infoln(u)
		r, err := http.Get(u)
		if err != nil {
			glog.Errorln(err)
			return
		}
		io.Copy(ctx.ResponseWriter, r.Body)
		r.Body.Close()
	})

	beego.Run()
}
示例#4
0
文件: models.go 项目: aryaya/ifundmgr
func Init() {
	orm.RegisterDataBase("default", "sqlite3", "icloud.db")
	orm.RegisterModel(new(Recoder), new(Request))
	orm.RegisterModel(new(Role), new(Log))
	orm.RunSyncdb("default", false, true)
	Gorm = orm.NewOrm()
	Gorm.Using("default")
	initConf()
	for _, r := range Gconf.Roles {
		r.Password = PassHash(r.Password)
		_, _, err := Gorm.ReadOrCreate(&r, "Username")
		if err != nil {
			glog.Fatal(err)
		}
	}
	accs := make([]string, len(Gconf.HoltWallet))
	for i, hw := range Gconf.HoltWallet {
		accs[i] = hw.AccountId
	}
	accs = append(accs, Gconf.ColdWallet)
	go monitor(Gconf.ServerAddr, accs)
}
示例#5
0
文件: quote.go 项目: aryaya/ifundmgr
// https://ripplecn.com/bridge?type=quote&amount=1%2FCNY&destination=z&address=ra5tSyQ2cvJUHfAvEdmC89HKSKZTn7xXMw&alipay_account=aa&full_name=bb&contact_info=cc
func (c *MainController) ApiQuote() {
	sa := c.Ctx.Request.URL.Query().Get("amount")
	address := c.Ctx.Request.URL.Query().Get("address")
	bank_name := c.Ctx.Request.URL.Query().Get("bank_name")
	card_number := c.Ctx.Request.URL.Query().Get("card_number")
	full_name := c.Ctx.Request.URL.Query().Get("full_name")
	contact_info := c.Ctx.Request.URL.Query().Get("contact_info")

	a, err := data.NewAmount(sa)
	if err != nil {
		glog.Error(err)
		resp := quoteErrorResp("the query amount err")
		sendResp(resp, c.Ctx)
		return
	}
	glog.Info("ApiQuote:", address, bank_name, card_number, full_name, contact_info, a, a.IsNative())
	sv := a.Value.String()
	am, err := strconv.ParseFloat(sv, 64)
	if err != nil {
		glog.Error(err)
		resp := quoteErrorResp("the query amount err2")
		sendResp(resp, c.Ctx)
		return
	}

	if a.IsNative() {
		pv, _ := data.NewNativeValue(1e6)
		a.Value, _ = a.Value.Divide(*pv)
	}

	currency := a.Currency.String()
	fee := am * models.Gconf.Fees.Rate
	min := models.Gconf.Fees.FeeMap[currency][0]
	max := models.Gconf.Fees.FeeMap[currency][1]
	if fee < min {
		fee = min
	}
	if fee > max {
		fee = max
	}
	acc, err := data.NewAccountFromAddress(models.Gconf.ColdWallet)
	if err != nil {
		glog.Fatal(err)
	}
	a.Issuer = *acc

	u := &models.User{
		UName:     full_name,
		UWallet:   address,
		UBankName: bank_name,
		UBankId:   card_number,
		UContact:  contact_info,
	}

	t := models.Withdrawal
	if a.IsNative() {
		t = models.Redeem
	}
	req, err := models.AddReq("", models.Gconf.ColdWallet, t, u, a.Currency.String(), am/1e6, fee)
	if err != nil {
		// glog.Error(err)
		return
	}

	fv, err := data.NewValue(fmt.Sprintf("%f", fee), a.IsNative())
	if err != nil {
		glog.Fatal(err)
	}

	a.Value, err = a.Value.Add(*fv)
	if err != nil {
		glog.Fatal(err)
	}

	if a.IsNative() {
		pv, _ := data.NewNativeValue(1e6)
		a.Value, _ = a.Value.Divide(*pv)
		a.Currency, _ = data.NewCurrency("ICC")
	}

	quote := &Quote{
		Address:        models.Gconf.ColdWallet,
		DestinationTag: 2147483647,
		Send:           []data.Amount{*a},
		InvoiceID:      req.InvoiceId,
	}
	resp := &QuoteResp{
		Result:    "success",
		QuoteJson: quote,
	}
	glog.Info("Quote:", address, full_name, "OK")
	sendResp(resp, c.Ctx)
}
示例#6
0
func (c *MainController) verify(isOut bool) error {
	r := c.getRole()
	if r == nil {
		return PDErr
	}
	sid := c.Ctx.Request.URL.Query().Get("id")
	id, err := strconv.Atoi(sid)
	if err != nil {
		return err
	}
	// rec := &models.Recoder{Id: int64(id)}
	var req models.Request
	err = models.Gorm.QueryTable("Request").Filter("R__id", id).RelatedSel().One(&req)
	if err != nil {
		return err
	}
	rec := req.R
	newStatus := canVerify(r.Type, rec.Status, 0 /*rec.R.Type*/)
	if newStatus == -1 {
		return PDErr
	}
	if r.Type == models.RoleF {
		if rec.GWallet == "" || rec.GBankId == "" {
			err = errors.New("HotWallet OR GateBandId config error")
			glog.Error(err)
			return err
		}
		rec.FId = r.Username
		rec.FTime = time.Now()
		rec.Status = models.FOK
	} else if r.Type == models.RoleM {
		rec.MId = r.Username
		rec.MTime = time.Now()
		rec.Status = models.MOK
	} else if r.Type == models.RoleA {
		rec.AId = r.Username
		rec.ATime = time.Now()
		// 会计审批, 直接发送
		if isOut {
			if rec.R == nil {
				return PDErr
			}
			sender := ""
			if strings.Contains(rec.GWallet, ":") {
				sender = strings.Split(rec.GWallet, ":")[1]
			}
			if sender == "" {
				return errors.New("HotWallet error")
			}
			rec.R.Currency = getCurrencyID(rec.R.Currency)
			err := models.Payment(rec.R, sender)
			if err != nil {
				return err
			}
			rec.Status = models.AOK
		} else { // 回收和取款
			rec.Status = models.OKC // 转账完成则整个记录完成
		}
	} else {
		glog.Fatal("can't go here")
	}
	_, err = models.Gorm.Update(rec)
	return err
}