/
claim.go
124 lines (112 loc) · 5.28 KB
/
claim.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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
package types
import (
"encoding/json"
"fmt"
"strconv"
"github.com/cosmos/cosmos-sdk/codec"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
"github.com/sifchain/peggy/x/oracle"
)
// EthBridgeClaim is a structure that contains all the data for a particular bridge claim
type EthBridgeClaim struct {
EthereumChainID int `json:"ethereum_chain_id" yaml:"ethereum_chain_id"`
BridgeContractAddress EthereumAddress `json:"bridge_registry_contract_address" yaml:"bridge_registry_contract_address"`
Nonce int `json:"nonce" yaml:"nonce"`
Symbol string `json:"symbol" yaml:"symbol"`
TokenContractAddress EthereumAddress `json:"token_contract_address" yaml:"token_contract_address"`
EthereumSender EthereumAddress `json:"ethereum_sender" yaml:"ethereum_sender"`
CosmosReceiver sdk.AccAddress `json:"cosmos_receiver" yaml:"cosmos_receiver"`
ValidatorAddress sdk.ValAddress `json:"validator_address" yaml:"validator_address"`
Amount int64 `json:"amount" yaml:"amount"`
ClaimType ClaimType `json:"claim_type" yaml:"claim_type"`
}
// NewEthBridgeClaim is a constructor function for NewEthBridgeClaim
func NewEthBridgeClaim(ethereumChainID int, bridgeContract EthereumAddress,
nonce int, symbol string, tokenContact EthereumAddress, ethereumSender EthereumAddress,
cosmosReceiver sdk.AccAddress, validator sdk.ValAddress, amount int64, claimType ClaimType,
) EthBridgeClaim {
return EthBridgeClaim{
EthereumChainID: ethereumChainID,
BridgeContractAddress: bridgeContract,
Nonce: nonce,
Symbol: symbol,
TokenContractAddress: tokenContact,
EthereumSender: ethereumSender,
CosmosReceiver: cosmosReceiver,
ValidatorAddress: validator,
Amount: amount,
ClaimType: claimType,
}
}
// OracleClaimContent is the details of how the content of the claim for each validator will be stored in the oracle
type OracleClaimContent struct {
CosmosReceiver sdk.AccAddress `json:"cosmos_receiver" yaml:"cosmos_receiver"`
Amount int64 `json:"amount" yaml:"amount"`
Symbol string `json:"symbol" yaml:"symbol"`
TokenContractAddress EthereumAddress `json:"token_contract_address" yaml:"token_contract_address"`
ClaimType ClaimType `json:"claim_type" yaml:"claim_type"`
}
// NewOracleClaimContent is a constructor function for OracleClaim
func NewOracleClaimContent(
cosmosReceiver sdk.AccAddress, amount int64, symbol string, tokenContractAddress EthereumAddress, claimType ClaimType,
) OracleClaimContent {
return OracleClaimContent{
CosmosReceiver: cosmosReceiver,
Amount: amount,
Symbol: symbol,
TokenContractAddress: tokenContractAddress,
ClaimType: claimType,
}
}
// CreateOracleClaimFromEthClaim converts a specific ethereum bridge claim to a general oracle claim to be used by
// the oracle module. The oracle module expects every claim for a particular prophecy to have the same id, so this id
// must be created in a deterministic way that all validators can follow.
// For this, we use the Nonce an Ethereum Sender provided,
// as all validators will see this same data from the smart contract.
func CreateOracleClaimFromEthClaim(cdc *codec.Codec, ethClaim EthBridgeClaim) (oracle.Claim, error) {
oracleID := strconv.Itoa(ethClaim.EthereumChainID) + strconv.Itoa(ethClaim.Nonce) + ethClaim.EthereumSender.String()
claimContent := NewOracleClaimContent(ethClaim.CosmosReceiver, ethClaim.Amount,
ethClaim.Symbol, ethClaim.TokenContractAddress, ethClaim.ClaimType)
claimBytes, err := json.Marshal(claimContent)
if err != nil {
return oracle.Claim{}, err
}
claimString := string(claimBytes)
claim := oracle.NewClaim(oracleID, ethClaim.ValidatorAddress, claimString)
return claim, nil
}
// CreateEthClaimFromOracleString converts a string
// from any generic claim from the oracle module into an ethereum bridge specific claim.
func CreateEthClaimFromOracleString(
ethereumChainID int, bridgeContract EthereumAddress, nonce int,
ethereumAddress EthereumAddress, validator sdk.ValAddress, oracleClaimString string,
) (EthBridgeClaim, error) {
oracleClaim, err := CreateOracleClaimFromOracleString(oracleClaimString)
if err != nil {
return EthBridgeClaim{}, err
}
return NewEthBridgeClaim(
ethereumChainID,
bridgeContract,
nonce,
oracleClaim.Symbol,
oracleClaim.TokenContractAddress,
ethereumAddress,
oracleClaim.CosmosReceiver,
validator,
oracleClaim.Amount,
oracleClaim.ClaimType,
), nil
}
// CreateOracleClaimFromOracleString converts a JSON string into an OracleClaimContent struct used by this module.
// In general, it is expected that the oracle module will store claims in this JSON format
// and so this should be used to convert oracle claims.
func CreateOracleClaimFromOracleString(oracleClaimString string) (OracleClaimContent, error) {
var oracleClaimContent OracleClaimContent
bz := []byte(oracleClaimString)
if err := json.Unmarshal(bz, &oracleClaimContent); err != nil {
return OracleClaimContent{}, sdkerrors.Wrap(ErrJSONMarshalling, fmt.Sprintf("failed to parse claim: %s", err.Error()))
}
return oracleClaimContent, nil
}