Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update contract utility GetL2Output #18

Merged
merged 2 commits into from
Jan 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 22 additions & 2 deletions pkg/chain/contracts.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,23 @@ import (
"fmt"
"math/big"

"github.com/LiskHQ/op-fault-detector/pkg/encoding"
"github.com/ethereum/go-ethereum/accounts/abi/bind"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/ethereum/go-ethereum/ethclient"

"github.com/ethereum-optimism/optimism/op-bindings/bindings"
)

// L2Output is the output of GetL2Output.
type L2Output struct {
OutputRoot string
L1Timestamp uint64
L2BlockNumber uint64
L2OutputIndex uint64
}

// OracleAccessor binds oracle contract to an instance for querying data.
type OracleAccessor struct {
contractInstance *bindings.L2OutputOracle
Expand Down Expand Up @@ -68,6 +78,16 @@ func (oc *OracleAccessor) GetNextOutputIndex() (*big.Int, error) {
}

// GetL2Output returns L2 output at given index.
func (oc *OracleAccessor) GetL2Output(index *big.Int) (bindings.TypesOutputProposal, error) {
return oc.contractInstance.GetL2Output(&bind.CallOpts{}, index)
func (oc *OracleAccessor) GetL2Output(index *big.Int) (L2Output, error) {
l2Output, err := oc.contractInstance.GetL2Output(&bind.CallOpts{}, index)
if err != nil {
return L2Output{}, err
}

return L2Output{
OutputRoot: hexutil.Encode(l2Output.OutputRoot[:]),
L1Timestamp: encoding.MustConvertBigIntToUint64(l2Output.Timestamp),
L2BlockNumber: encoding.MustConvertBigIntToUint64(l2Output.L2BlockNumber),
L2OutputIndex: encoding.MustConvertBigIntToUint64(index),
}, nil
}
16 changes: 16 additions & 0 deletions pkg/encoding/encoding.go
Original file line number Diff line number Diff line change
@@ -1,2 +1,18 @@
// Package encoding implements all the utils/helpers for encoding incoming/outgoing RPC requests
package encoding

import "math/big"

// MustConvertBigIntToUint64 converts big integer to integer.
func MustConvertBigIntToUint64(value *big.Int) uint64 {
valueToString, ok := new(big.Int).SetString(value.String(), 10)
if !ok {
panic("error while converting big integer to integer")
}
return valueToString.Uint64()
}

// MustConvertUint64ToBigInt converts integer to big integer.
func MustConvertUint64ToBigInt(value uint64) *big.Int {
return new(big.Int).SetUint64(value)
}
26 changes: 26 additions & 0 deletions pkg/encoding/encoding_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package encoding

import (
"math/big"
"testing"

"github.com/stretchr/testify/assert"
)

func TestMustConvertBigIntToUint64(t *testing.T) {
assert := assert.New(t)

bigIntInput := big.NewInt(1000)
const expectedOutput uint64 = 1000
result := MustConvertBigIntToUint64(bigIntInput)
assert.Equal(expectedOutput, result)
}

func TestMustConvertUint64ToBigInt(t *testing.T) {
assert := assert.New(t)

const uint64Output uint64 = 1000
expectedOutput := big.NewInt(1000)
result := MustConvertUint64ToBigInt(uint64Output)
assert.Equal(expectedOutput, result)
}
Loading