-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
143e829
commit 2c523d9
Showing
12 changed files
with
266 additions
and
188 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
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 |
---|---|---|
@@ -0,0 +1,77 @@ | ||
package types | ||
|
||
import ( | ||
fmt "fmt" | ||
"math/big" | ||
"strings" | ||
|
||
"github.com/ethereum/go-ethereum/accounts/abi" | ||
"github.com/ethereum/go-ethereum/common" | ||
) | ||
|
||
// GmpDecoder is the payload sent from Axelar to IBC middleware. | ||
// It needs to be decoded using the ABI. | ||
type GmpDecoder struct { | ||
AssetNames [][32]byte | ||
ContractAddress common.Address | ||
CommandSelector [4]byte | ||
CommandParams []byte | ||
Timestamp *big.Int | ||
AbiEncodedData []byte | ||
} | ||
|
||
// abiSpec is the ABI specification for the GMP data. | ||
var decoderSpec = abi.Arguments{ | ||
{ | ||
Type: assetNamesType, | ||
}, | ||
{ | ||
Type: contractAddressType, | ||
}, | ||
{ | ||
Type: commandSelectorType, | ||
}, | ||
{ | ||
Type: commandParamsType, | ||
}, | ||
{ | ||
Type: timestampType, | ||
}, | ||
} | ||
|
||
// NewGmpDecoder decodes a payload from GMP given a byte array | ||
func NewGmpDecoder(payload []byte) (GmpDecoder, error) { | ||
args, err := decoderSpec.Unpack(payload) | ||
if err != nil { | ||
return GmpDecoder{}, err | ||
} | ||
|
||
// check to make sure each argument is the correct type | ||
if assetNames, ok := args[0].([][32]byte); !ok { | ||
return GmpDecoder{}, fmt.Errorf("invalid asset names type: %T", args[0]) | ||
} else if contractAddress, ok := args[1].(common.Address); !ok { | ||
return GmpDecoder{}, fmt.Errorf("invalid contract address type: %T", args[1]) | ||
} else if commandSelector, ok := args[2].([4]byte); !ok { | ||
return GmpDecoder{}, fmt.Errorf("invalid command selector type: %T", args[2]) | ||
} else if commandParams, ok := args[3].([]byte); !ok { | ||
return GmpDecoder{}, fmt.Errorf("invalid command params type: %T", args[3]) | ||
} else if timestamp, ok := args[4].(*big.Int); !ok { | ||
return GmpDecoder{}, fmt.Errorf("invalid timestamp type: %T", args[4]) | ||
} else { | ||
return GmpDecoder{ | ||
AssetNames: assetNames, | ||
ContractAddress: contractAddress, | ||
CommandSelector: commandSelector, | ||
CommandParams: commandParams, | ||
Timestamp: timestamp, | ||
}, nil | ||
} | ||
} | ||
|
||
func (g GmpDecoder) GetDenoms() []string { | ||
denoms := make([]string, len(g.AssetNames)) | ||
for i, name := range g.AssetNames { | ||
denoms[i] = strings.TrimRight(string(name[:]), "\x00") | ||
} | ||
return denoms | ||
} |
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,53 @@ | ||
package types | ||
|
||
import ( | ||
"math/big" | ||
"testing" | ||
|
||
"github.com/ethereum/go-ethereum/common" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
// TestGmpData tests the GmpData struct by encoding and decoding it. | ||
func TestGmpData(t *testing.T) { | ||
g := GmpDecoder{ | ||
AssetNames: [][32]byte{{1}}, | ||
ContractAddress: common.HexToAddress("0x0000001"), | ||
CommandSelector: [4]byte{1}, | ||
CommandParams: []byte{1}, | ||
Timestamp: big.NewInt(1), | ||
} | ||
payload, err := decoderSpec.Pack( | ||
g.AssetNames, | ||
g.ContractAddress, | ||
g.CommandSelector, | ||
g.CommandParams, | ||
g.Timestamp, | ||
) | ||
require.NoError(t, err) | ||
newGmpData, err := NewGmpDecoder(payload) | ||
require.NoError(t, err) | ||
|
||
require.Equal(t, g, newGmpData) | ||
} | ||
|
||
func TestGetDenoms(t *testing.T) { | ||
assetNamesString := []string{ | ||
"BTC", | ||
"ETH", | ||
"USDT", | ||
"BNB", | ||
"ADA", | ||
"FOOBARFOOBARFOOBARFOOBARFOOBARFO", // maxmimum allowed length | ||
} | ||
assetNamesAsBytes := make([][32]byte, len(assetNamesString)) | ||
for i, name := range assetNamesString { | ||
copy(assetNamesAsBytes[i][:], name) | ||
} | ||
|
||
g := GmpDecoder{ | ||
AssetNames: assetNamesAsBytes, | ||
} | ||
denoms := g.GetDenoms() | ||
require.Equal(t, assetNamesString, denoms) | ||
} |
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,56 @@ | ||
package types | ||
|
||
import ( | ||
"math/big" | ||
|
||
"github.com/ethereum/go-ethereum/accounts/abi" | ||
"github.com/ethereum/go-ethereum/common" | ||
) | ||
|
||
// GmpEncoder is the struct we use to encode the data we want to send to the GMP. | ||
type GmpEncoder struct { | ||
PriceData []PriceData | ||
AssetNames [32]byte | ||
ContractAddress common.Address | ||
CommandSelector [4]byte | ||
CommandParams []byte | ||
} | ||
|
||
// MedianData is the struct that represents the MedianData tuple in Solidity. | ||
type MedianData struct { | ||
BlockNums []*big.Int | ||
Medians []*big.Int | ||
Deviations []*big.Int | ||
} | ||
|
||
// PriceData is the struct that represents the PriceData tuple in Solidity. | ||
type PriceData struct { | ||
AssetName [32]byte | ||
Price *big.Int | ||
ResolveTime *big.Int | ||
MedianData []MedianData | ||
} | ||
|
||
// encoderSpec is the ABI specification for the GMP data. | ||
var encoderSpec = abi.Arguments{ | ||
{ | ||
Type: priceDataType, | ||
}, | ||
{ | ||
Type: assetNameType, | ||
}, | ||
{ | ||
Type: contractAddressType, | ||
}, | ||
{ | ||
Type: commandSelectorType, | ||
}, | ||
{ | ||
Type: commandParamsType, | ||
}, | ||
} | ||
|
||
// GMPEncode encodes the GMP data into a byte array. | ||
func (g GmpEncoder) GMPEncode() ([]byte, error) { | ||
return encoderSpec.Pack(g.PriceData, g.AssetNames, g.ContractAddress, g.CommandSelector, g.CommandParams) | ||
} |
Oops, something went wrong.