This repository has been archived by the owner on Mar 16, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 229
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Tezos] Implement block api from rpc (#1354)
* implement block api from tezos rpc * parse delegation operation
- Loading branch information
1 parent
37ed4f7
commit 3a772c4
Showing
9 changed files
with
1,464 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,118 @@ | ||
package tezos | ||
|
||
import "github.com/trustwallet/golibs/types" | ||
import ( | ||
"encoding/json" | ||
"errors" | ||
|
||
"github.com/trustwallet/golibs/coin" | ||
"github.com/trustwallet/golibs/types" | ||
|
||
"github.com/itchyny/timefmt-go" | ||
) | ||
|
||
func (p *Platform) CurrentBlockNumber() (int64, error) { | ||
return p.client.GetCurrentBlock() | ||
return p.rpcClient.GetBlockHead() | ||
} | ||
|
||
func (p *Platform) GetBlockByNumber(num int64) (*types.Block, error) { | ||
txTypes := []string{TxTypeTransaction, TxTypeDelegation} | ||
srcTxs, err := p.client.GetBlockByNumber(num, txTypes) | ||
block, err := p.rpcClient.GetBlockByNumber(num) | ||
if err != nil { | ||
return nil, err | ||
} | ||
txs := NormalizeTxs(srcTxs, "") | ||
return &types.Block{ | ||
Number: num, | ||
Txs: txs, | ||
return NormalizeRpcBlock(block) | ||
} | ||
|
||
func NormalizeRpcBlock(block RpcBlock) (*types.Block, error) { | ||
txs := []types.Tx{} | ||
|
||
for _, ops := range block.Operations { | ||
for _, op := range ops { | ||
for _, content := range op.Contents { | ||
if tx, err := mapTransaction(content); err == nil { | ||
if normalized, err := NormalizeRpcTransaction(tx, block.Header); err == nil { | ||
normalized.ID = op.Hash | ||
txs = append(txs, normalized) | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
return &types.Block{Number: block.Header.Level, Txs: txs}, nil | ||
} | ||
|
||
func NormalizeRpcTransaction(tx RpcTransaction, header RpcBlockHeader) (types.Tx, error) { | ||
coin := coin.Tezos() | ||
|
||
var metadata interface{} | ||
var to string | ||
var txType types.TransactionType | ||
switch tx.Kind { | ||
case TxTypeTransaction: | ||
to = tx.Destination | ||
txType = types.TxTransfer | ||
metadata = types.Transfer{ | ||
Value: types.Amount(tx.Amount), | ||
Symbol: coin.Symbol, | ||
Decimals: coin.Decimals, | ||
} | ||
case TxTypeDelegation: | ||
var title types.KeyTitle | ||
if len(tx.Delegate) == 0 { | ||
title = types.AnyActionUndelegation | ||
} else { | ||
title = types.AnyActionDelegation | ||
to = tx.Delegate | ||
} | ||
txType = types.TxAnyAction | ||
metadata = types.AnyAction{ | ||
Coin: coin.ID, | ||
Title: title, | ||
Key: types.KeyStakeDelegate, | ||
Name: coin.Name, | ||
Symbol: coin.Symbol, | ||
Decimals: coin.Decimals, | ||
} | ||
default: | ||
return types.Tx{}, errors.New("not supported operation kind: " + tx.Kind) | ||
} | ||
|
||
date, err := timefmt.Parse(header.Timestamp, "%Y-%m-%dT%H:%M:%SZ") | ||
if err != nil { | ||
return types.Tx{}, err | ||
} | ||
|
||
var status types.Status | ||
if tx.Metadata.OperationResult.Status == TxStatusApplied { | ||
status = types.StatusCompleted | ||
} else { | ||
status = types.StatusError | ||
} | ||
|
||
return types.Tx{ | ||
Coin: coin.ID, | ||
From: tx.Source, | ||
To: to, | ||
Fee: types.Amount(tx.Fee), | ||
Date: date.Unix(), | ||
Block: uint64(header.Level), | ||
Status: status, | ||
Type: txType, | ||
Meta: metadata, | ||
}, nil | ||
} | ||
|
||
func mapTransaction(content interface{}) (RpcTransaction, error) { | ||
bytes, err := json.Marshal(content) | ||
if err != nil { | ||
return RpcTransaction{}, err | ||
} | ||
|
||
var tx RpcTransaction | ||
err = json.Unmarshal(bytes, &tx) | ||
if err != nil { | ||
return RpcTransaction{}, err | ||
} | ||
|
||
return tx, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,146 @@ | ||
package tezos | ||
|
||
import ( | ||
"fmt" | ||
"reflect" | ||
"testing" | ||
|
||
"github.com/trustwallet/golibs/mock" | ||
"github.com/trustwallet/golibs/types" | ||
) | ||
|
||
func TestNormalizeRpcBlock(t *testing.T) { | ||
type args struct { | ||
filename string | ||
} | ||
tests := []struct { | ||
name string | ||
args args | ||
want *types.Block | ||
wantErr bool | ||
}{ | ||
{ | ||
name: "Test normalize block 1292516", | ||
args: args{ | ||
filename: "rpc_block_1292516.json", | ||
}, | ||
want: &types.Block{ | ||
Number: 1292516, | ||
Txs: []types.Tx{ | ||
{ | ||
ID: "oo25sEdAT3YDb83WNdMSxRv4E6V2Rt6Jc8msgTio7R4FBnAiFmj", | ||
Coin: 1729, | ||
From: "tz1SiPXX4MYGNJNDsRc7n8hkvUqFzg8xqF9m", | ||
To: "tz1LGrjCS3Jj3YZyRx3mHMmEXRJVQeVnoYYi", | ||
Fee: "2940", | ||
Date: 1610065014, | ||
Block: 1292516, | ||
Status: "completed", | ||
Sequence: 0, | ||
Type: "transfer", | ||
Memo: "", | ||
Meta: types.Transfer{ | ||
Value: "5278500000", | ||
Symbol: "XTZ", | ||
Decimals: 6, | ||
}, | ||
}, | ||
{ | ||
ID: "ooPykS2pw28FveDV3FojeXmThtuCATDJdn93iFjrRxFaEytait2", | ||
Coin: 1729, | ||
From: "tz1MKCMt9dQDccykzripUGk5439BwEWthqx5", | ||
To: "tz1S1Aew75hMrPUymqenKfHo8FspppXKpW7h", | ||
Fee: "9008", | ||
Date: 1610065014, | ||
Block: 1292516, | ||
Status: "completed", | ||
Sequence: 0, | ||
Type: "any_action", | ||
Memo: "", | ||
Meta: types.AnyAction{ | ||
Coin: 1729, | ||
Title: "Delegation", | ||
Key: "stake_delegate", | ||
Name: "Tezos", | ||
Symbol: "XTZ", | ||
Decimals: 6, | ||
}, | ||
}, | ||
{ | ||
ID: "ooFFpMbVAJMkxNYFqxERDumptXS2kSEJQqTwoovVPinayJehB8f", | ||
Coin: 1729, | ||
From: "tz1YbzpfsfRtSiYJWcvpWEDHPNe3kkqEvY56", | ||
To: "tz1L1c5YoQMaciYGB8gpzWoHbscBmtsTsknF", | ||
Fee: "30000", | ||
Date: 1610065014, | ||
Block: 1292516, | ||
Status: "completed", | ||
Sequence: 0, | ||
Type: "transfer", | ||
Memo: "", | ||
Meta: types.Transfer{ | ||
Value: "290691675", | ||
Symbol: "XTZ", | ||
Decimals: 6, | ||
}, | ||
}, | ||
{ | ||
ID: "opLbuEsuf9NmzgikvAG4sVaS7NJedzXHcz53iQPon6QurLP8Ztv", | ||
Coin: 1729, | ||
From: "tz1XbmS2Z3ya36JDqo1P1y3VU8t4RU2LJW6J", | ||
To: "", | ||
Fee: "2500", | ||
Date: 1610065014, | ||
Block: 1292516, | ||
Status: "completed", | ||
Sequence: 0, | ||
Type: "any_action", | ||
Memo: "", | ||
Meta: types.AnyAction{ | ||
Coin: 1729, | ||
Title: "Undelegation", | ||
Key: "stake_delegate", | ||
Name: "Tezos", | ||
Symbol: "XTZ", | ||
Decimals: 6, | ||
}, | ||
}, | ||
{ | ||
ID: "opNgBb87Cnpjr29Uc4CRuXpWgTfhPx2h76S5txrPJELa5XYiidZ", | ||
Coin: 1729, | ||
From: "tz1c5wM9826YcUNQ8a17z9eUYpKQ3oW3zfmJ", | ||
To: "KT19kgnqC5VWoxktLRdRUERbyUPku9YioE8W", | ||
Fee: "824", | ||
Date: 1610065014, | ||
Block: 1292516, | ||
Status: "completed", | ||
Sequence: 0, | ||
Type: "transfer", | ||
Memo: "", | ||
Meta: types.Transfer{ | ||
Value: "0", | ||
Symbol: "XTZ", | ||
Decimals: 6, | ||
}, | ||
}, | ||
}, | ||
}, | ||
wantErr: false, | ||
}, | ||
} | ||
for _, tt := range tests { | ||
var block RpcBlock | ||
_ = mock.JsonModelFromFilePath("mocks/"+tt.args.filename, &block) | ||
t.Run(tt.name, func(t *testing.T) { | ||
got, err := NormalizeRpcBlock(block) | ||
fmt.Println(got) | ||
if (err != nil) != tt.wantErr { | ||
t.Errorf("NormalizeRpcBlock() error = %v, wantErr %v", err, tt.wantErr) | ||
return | ||
} | ||
if !reflect.DeepEqual(got, tt.want) { | ||
t.Errorf("NormalizeRpcBlock() = %v, \nwant %v", got, tt.want) | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.