-
Notifications
You must be signed in to change notification settings - Fork 0
/
asset.go
98 lines (92 loc) · 3.55 KB
/
asset.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
// Copyright Fuzamei Corp. 2018 All Rights Reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package executor
import (
"encoding/hex"
"github.com/bnchain/bnchain/account"
"github.com/bnchain/bnchain/common/address"
"github.com/bnchain/bnchain/common/db"
"github.com/bnchain/bnchain/types"
pt "github.com/bnchain/plugin/plugin/dapp/paracross/types"
"github.com/pkg/errors"
)
func (a *action) assetTransfer(transfer *types.AssetsTransfer) (*types.Receipt, error) {
isPara := types.IsPara()
//主链处理分支
if !isPara {
accDB, err := createAccount(a.db, transfer.Cointoken)
if err != nil {
return nil, errors.Wrap(err, "assetTransferToken call account.NewAccountDB failed")
}
execAddr := address.ExecAddress(pt.ParaX)
fromAcc := accDB.LoadExecAccount(a.fromaddr, execAddr)
if fromAcc.Balance < transfer.Amount {
return nil, errors.Wrap(types.ErrNoBalance, "assetTransfer")
}
toAddr := address.ExecAddress(string(a.tx.Execer))
clog.Debug("paracross.AssetTransfer not isPara", "execer", string(a.tx.Execer),
"txHash", hex.EncodeToString(a.tx.Hash()))
return accDB.ExecTransfer(a.fromaddr, toAddr, execAddr, transfer.Amount)
}
//平行链处理分支
paraTitle, err := getTitleFrom(a.tx.Execer)
if err != nil {
return nil, errors.Wrap(err, "assetTransferCoins call getTitleFrom failed")
}
var paraAcc *account.DB
if transfer.Cointoken == "" {
paraAcc, err = NewParaAccount(string(paraTitle), "coins", "bty", a.db)
} else {
paraAcc, err = NewParaAccount(string(paraTitle), "token", transfer.Cointoken, a.db)
}
if err != nil {
return nil, errors.Wrap(err, "assetTransferCoins call NewParaAccount failed")
}
clog.Debug("paracross.AssetTransfer isPara", "execer", string(a.tx.Execer),
"txHash", hex.EncodeToString(a.tx.Hash()))
return assetDepositBalance(paraAcc, transfer.To, transfer.Amount)
}
func (a *action) assetWithdraw(withdraw *types.AssetsWithdraw, withdrawTx *types.Transaction) (*types.Receipt, error) {
isPara := types.IsPara()
//主链处理分支
if !isPara {
accDB, err := createAccount(a.db, withdraw.Cointoken)
if err != nil {
return nil, errors.Wrap(err, "assetWithdrawCoins call account.NewAccountDB failed")
}
fromAddr := address.ExecAddress(string(withdrawTx.Execer))
execAddr := address.ExecAddress(pt.ParaX)
clog.Debug("Paracross.Exec", "AssettWithdraw", withdraw.Amount, "from", fromAddr,
"to", withdraw.To, "exec", execAddr, "withdrawTx execor", string(withdrawTx.Execer))
return accDB.ExecTransfer(fromAddr, withdraw.To, execAddr, withdraw.Amount)
}
//平行链处理分支
paraTitle, err := getTitleFrom(a.tx.Execer)
if err != nil {
return nil, errors.Wrap(err, "assetWithdrawCoins call getTitleFrom failed")
}
var paraAcc *account.DB
if withdraw.Cointoken == "" {
paraAcc, err = NewParaAccount(string(paraTitle), "coins", "bty", a.db)
} else {
paraAcc, err = NewParaAccount(string(paraTitle), "token", withdraw.Cointoken, a.db)
}
if err != nil {
return nil, errors.Wrap(err, "assetWithdrawCoins call NewParaAccount failed")
}
clog.Debug("paracross.assetWithdrawCoins isPara", "execer", string(a.tx.Execer),
"txHash", hex.EncodeToString(a.tx.Hash()), "from", a.fromaddr, "amount", withdraw.Amount)
return assetWithdrawBalance(paraAcc, a.fromaddr, withdraw.Amount)
}
func createAccount(db db.KV, symbol string) (*account.DB, error) {
var accDB *account.DB
var err error
if symbol == "" {
accDB = account.NewCoinsAccount()
accDB.SetDB(db)
} else {
accDB, err = account.NewAccountDB("token", symbol, db)
}
return accDB, err
}