Skip to content

Commit

Permalink
support transferFrom
Browse files Browse the repository at this point in the history
  • Loading branch information
zxlwdgithub committed Nov 7, 2019
1 parent bab5840 commit 10d5622
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 7 deletions.
25 changes: 19 additions & 6 deletions transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -357,7 +357,9 @@ func GetTransferTx(input *adaptor.GetTransferTxInput, rpcParams *RPCParams, netI
}
data := tx.Data()
transferMethodId := Hex2Bytes("a9059cbb")
if isErc20 && !bytes.HasPrefix(data, transferMethodId) {
transferFromMethodId := Hex2Bytes("23b872dd")
isTransfer := bytes.HasPrefix(data, transferMethodId)
if isErc20 && !isTransfer && !bytes.HasPrefix(data, transferFromMethodId) {
return nil, errors.New("not a transfer method invoke")
}

Expand Down Expand Up @@ -405,11 +407,22 @@ func GetTransferTx(input *adaptor.GetTransferTxInput, rpcParams *RPCParams, netI
receipt, err := client.TransactionReceipt(context.Background(), hash)
if err != nil {
if isErc20 { //for pending erc20 tx
recvAddr := common.BytesToAddress(data[16:36])
result.Tx.ToAddress = recvAddr.String()
tokenValue := new(big.Int)
tokenValue.SetBytes(data[36:])
result.Tx.Amount = adaptor.NewAmountAsset(tokenValue, asset)
dataLen := len(data)
if isTransfer && dataLen == 68 { //transfer 4+32+32 32=(12+20)
recvAddr := common.BytesToAddress(data[16:36])
result.Tx.ToAddress = recvAddr.String()
tokenValue := new(big.Int)
tokenValue.SetBytes(data[36:])
result.Tx.Amount = adaptor.NewAmountAsset(tokenValue, asset)
} else if dataLen == 100 { //transferFrom 4+32+32+32 32=(12+20)
recvAddr := common.BytesToAddress(data[48:68])
result.Tx.ToAddress = recvAddr.String()
tokenValue := new(big.Int)
tokenValue.SetBytes(data[68:])
result.Tx.Amount = adaptor.NewAmountAsset(tokenValue, asset)
} else {
return nil, errors.New("tx data's length is invalid")
}
}
return &result, nil
}
Expand Down
3 changes: 2 additions & 1 deletion transaction_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,8 @@ func TestGetTxBasicInfo(t *testing.T) {
func TestGetTransferTxErc20Transfer(t *testing.T) {
//input := &adaptor.GetTransferTxInput{TxID: Hex2Bytes("51121d1124fb844132f994ef5067ec73f9bbe92b41c12720ae073401f746dc99")} //eth transfer
//input := &adaptor.GetTransferTxInput{TxID: Hex2Bytes("498b634c39fbd19af340d66c8866623c124eb0e2160a45aa433644adc636bedb")} //eth pending transfer
input := &adaptor.GetTransferTxInput{TxID: Hex2Bytes("61cded704bd23d8ff7cbe0ac4b62b940bd76f3709f784db695c95efa8074b7df")} //pannz transfer
//input := &adaptor.GetTransferTxInput{TxID: Hex2Bytes("61cded704bd23d8ff7cbe0ac4b62b940bd76f3709f784db695c95efa8074b7df")} //pannz transfer
input := &adaptor.GetTransferTxInput{TxID: Hex2Bytes("7d8bb81da859ed086ececda6fbc8a69a0d1264d6fb76790db5b7d617e8ca3eb2")} //pannz transfer
//input := &adaptor.GetTransferTxInput{TxID: Hex2Bytes("0325d73d025a8c8aa73c88303a65b13898e88384f4e153c5bc6466e7f7581e35")} //pannz pending transfer
//input := &adaptor.GetTransferTxInput{TxID: Hex2Bytes("4ef356ce0fc244ffb43cc0a941ca293c5b80e91254ad70474ba27acb9eb7b8fd")} //pannz approve

Expand Down

0 comments on commit 10d5622

Please sign in to comment.