Skip to content

Commit

Permalink
Update contract utility GetL2Output (#18)
Browse files Browse the repository at this point in the history
* Update output of GetL2Output function

* 👌 Applied suggestions
  • Loading branch information
nagdahimanshu authored Jan 26, 2024
1 parent 6a8fa43 commit e35a0d0
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 2 deletions.
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)
}

0 comments on commit e35a0d0

Please sign in to comment.