-
Notifications
You must be signed in to change notification settings - Fork 6
/
processtx.go
64 lines (59 loc) · 1.67 KB
/
processtx.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
package btc
import (
"github.com/fsn-dev/crossChain-Bridge/log"
"github.com/fsn-dev/crossChain-Bridge/tokens"
"github.com/fsn-dev/crossChain-Bridge/tokens/tools"
)
func (b *Bridge) processTransaction(txid string) {
_ = b.processSwapin(txid)
_ = b.processP2shSwapin(txid)
}
func (b *Bridge) processSwapin(txid string) error {
if tools.IsSwapinExist(txid) {
return nil
}
swapInfo, err := b.VerifyTransaction(txid, true)
if !tokens.ShouldRegisterSwapForError(err) {
return err
}
err = tools.RegisterSwapin(txid, swapInfo.Bind)
if err != nil {
log.Trace("[scan] processSwapin", "txid", txid, "err", err)
}
return err
}
func (b *Bridge) processP2shSwapin(txid string) error {
if tools.IsSwapinExist(txid) {
return nil
}
swapInfo, err := b.checkP2shTransaction(txid, true)
if !tokens.ShouldRegisterSwapForError(err) {
return err
}
err = tools.RegisterP2shSwapin(txid, swapInfo.Bind)
if err != nil {
log.Trace("[scan] processP2shSwapin", "txid", txid, "err", err)
}
return err
}
func (b *Bridge) checkP2shTransaction(txHash string, allowUnstable bool) (*tokens.TxSwapInfo, error) {
tx, err := b.GetTransactionByHash(txHash)
if err != nil {
log.Debug(b.TokenConfig.BlockChain+" Bridge::GetTransaction fail", "tx", txHash, "err", err)
return nil, tokens.ErrTxNotFound
}
var bindAddress, p2shAddress string
for _, output := range tx.Vout {
if *output.ScriptpubkeyType == p2shType {
p2shAddress = *output.ScriptpubkeyAddress
bindAddress = tools.GetP2shBindAddress(p2shAddress)
if bindAddress != "" {
break
}
}
}
if bindAddress == "" {
return nil, tokens.ErrTxWithWrongReceiver
}
return b.VerifyP2shTransaction(txHash, bindAddress, allowUnstable)
}