Skip to content

Commit

Permalink
eth bridge mainnet support
Browse files Browse the repository at this point in the history
  • Loading branch information
gmajor-encrypt committed Jun 24, 2024
1 parent 2c2ebd5 commit c68a4fb
Show file tree
Hide file tree
Showing 8 changed files with 128 additions and 33 deletions.
16 changes: 8 additions & 8 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
module github.com/gmajor-encrypt/xcm-tools

go 1.18
go 1.20

require (
github.com/itering/scale.go v1.8.1
github.com/itering/substrate-api-rpc v0.6.1
github.com/shopspring/decimal v1.3.1
github.com/itering/scale.go v1.8.4
github.com/itering/substrate-api-rpc v0.7.0
github.com/shopspring/decimal v1.4.0
github.com/stretchr/testify v1.8.4
github.com/urfave/cli v1.22.14
)
Expand All @@ -18,13 +18,13 @@ require (
github.com/gorilla/websocket v1.4.2 // indirect
github.com/gtank/merlin v0.1.1-0.20191105220539-8318aed1a79f // indirect
github.com/gtank/ristretto255 v0.1.2 // indirect
github.com/huandu/xstrings v1.4.0 // indirect
github.com/huandu/xstrings v1.5.0 // indirect
github.com/jpillora/backoff v1.0.0 // indirect
github.com/mimoo/StrobeGo v0.0.0-20181016162300-f8f6d4d2b643 // indirect
github.com/pierrec/xxHash v0.1.5 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/russross/blackfriday/v2 v2.1.0 // indirect
golang.org/x/crypto v0.19.0 // indirect
golang.org/x/sys v0.17.0 // indirect
golang.org/x/crypto v0.24.0 // indirect
golang.org/x/sys v0.21.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
)
35 changes: 35 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ3
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/gorilla/websocket v1.4.2 h1:+/TMaTYc4QFitKJxsQ7Yye35DkWvkdLcvGKqM+x0Ufc=
github.com/gorilla/websocket v1.4.2/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
github.com/gtank/merlin v0.1.1-0.20191105220539-8318aed1a79f h1:8N8XWLZelZNibkhM1FuF+3Ad3YIbgirjdMiVA0eUkaM=
Expand All @@ -18,11 +19,17 @@ github.com/gtank/ristretto255 v0.1.2/go.mod h1:Ph5OpO6c7xKUGROZfWVLiJf9icMDwUeIv
github.com/huandu/xstrings v1.3.1/go.mod h1:y5/lhBue+AyNmUVz9RLU9xbLR0o4KIIExikq4ovT0aE=
github.com/huandu/xstrings v1.4.0 h1:D17IlohoQq4UcpqD7fDk80P7l+lwAmlFaBHgOipl2FU=
github.com/huandu/xstrings v1.4.0/go.mod h1:y5/lhBue+AyNmUVz9RLU9xbLR0o4KIIExikq4ovT0aE=
github.com/huandu/xstrings v1.5.0 h1:2ag3IFq9ZDANvthTwTiqSSZLjDc+BedvHPAp5tJy2TI=
github.com/huandu/xstrings v1.5.0/go.mod h1:y5/lhBue+AyNmUVz9RLU9xbLR0o4KIIExikq4ovT0aE=
github.com/itering/scale.go v1.7.1/go.mod h1:fmksrbfD7WRKkP9cr+ewz6qDFWYWQ/2O7pOsP9m2jJA=
github.com/itering/scale.go v1.8.1 h1:gtRFv+iupFchOVljSd0ILQDQiJRxAS5d3OEGMd/c0XY=
github.com/itering/scale.go v1.8.1/go.mod h1:mEyWsfihftdLjhpeAo13wnGEM7XE42ZPvFVpC5Lsgqo=
github.com/itering/scale.go v1.8.4 h1:1VH0yehRHxio6J70AHN5TLupr5ZGSkBFj4+Tiw5ha+Y=
github.com/itering/scale.go v1.8.4/go.mod h1:mEyWsfihftdLjhpeAo13wnGEM7XE42ZPvFVpC5Lsgqo=
github.com/itering/substrate-api-rpc v0.6.1 h1:2PWytIaESvC2hV8XxLAfn/M4Z29eEi+whc0nctAWbpU=
github.com/itering/substrate-api-rpc v0.6.1/go.mod h1:HQYTVw/EHUhJnfd/MIBcd8YFTE78FEAfKrhJ9Z0LLBA=
github.com/itering/substrate-api-rpc v0.7.0 h1:rxaU6EAAYpI8ODMhAFrrAeUVCbMzdDwnuOJl/jYOiS8=
github.com/itering/substrate-api-rpc v0.7.0/go.mod h1:hhpUowcP+DROhsF791R3Dk2tEIN/53uAbg4Q8PpO5BQ=
github.com/jpillora/backoff v1.0.0 h1:uvFg412JmmHBHw7iwprIxkPMI+sGQ4kzOWsMeHnm2EA=
github.com/jpillora/backoff v1.0.0/go.mod h1:J/6gKK9jxlEcS3zixgDgUAsiuZ7yrSoa/FX5e0EB2j4=
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
Expand All @@ -48,6 +55,8 @@ github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQD
github.com/shopspring/decimal v1.2.0/go.mod h1:DKyhrW/HYNuLGql+MJL6WCR6knT2jwCFRcu2hWCYk4o=
github.com/shopspring/decimal v1.3.1 h1:2Usl1nmF/WZucqkFZhnfFYxxxu8LG21F6nPQBE5gKV8=
github.com/shopspring/decimal v1.3.1/go.mod h1:DKyhrW/HYNuLGql+MJL6WCR6knT2jwCFRcu2hWCYk4o=
github.com/shopspring/decimal v1.4.0 h1:bxl37RwXBklmTi0C79JfXCEBD1cqqHt0bbgBAGFp81k=
github.com/shopspring/decimal v1.4.0/go.mod h1:gawqmDU56v4yIKSwfBSFip1HdCCXN8/+DMd9qYNcwME=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
Expand All @@ -65,19 +74,32 @@ golang.org/x/crypto v0.0.0-20191206172530-e9b2fee46413/go.mod h1:LzIPMQfyMNhhGPh
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
golang.org/x/crypto v0.6.0/go.mod h1:OFC/31mSvZgRz0V1QTNCzfAI1aIRzbiufJtkMIlEp58=
golang.org/x/crypto v0.12.0/go.mod h1:NF0Gs7EO5K4qLn+Ylc+fih8BSTeIjAP05siRnAh98yw=
golang.org/x/crypto v0.13.0/go.mod h1:y6Z2r+Rw4iayiXXAIxJIDAJ1zMW4yaTpebo8fPOliYc=
golang.org/x/crypto v0.19.0 h1:ENy+Az/9Y1vSrlrvBSyna3PITt4tiZLf7sgCjZBX7Wo=
golang.org/x/crypto v0.19.0/go.mod h1:Iy9bg/ha4yyC70EfRS8jz+B6ybOBKMaSxLj6P6oBDfU=
golang.org/x/crypto v0.23.0/go.mod h1:CKFgDieR+mRhux2Lsu27y0fO304Db0wZe70UKqHu0v8=
golang.org/x/crypto v0.24.0 h1:mnl8DM0o513X8fdIkmyFE/5hTYxbwYOjDS/+rK6qpRI=
golang.org/x/crypto v0.24.0/go.mod h1:Z1PMYSOR5nyMcyAVAIQSKCDwalqy85Aqn1x3Ws4L5DM=
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4=
golang.org/x/mod v0.8.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs=
golang.org/x/mod v0.12.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs=
golang.org/x/mod v0.15.0/go.mod h1:hTbmBsO62+eylJbnUtE2MGJUyE7QWk4xUqPFrRgJ+7c=
golang.org/x/mod v0.17.0/go.mod h1:hTbmBsO62+eylJbnUtE2MGJUyE7QWk4xUqPFrRgJ+7c=
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c=
golang.org/x/net v0.6.0/go.mod h1:2Tu9+aMcznHK/AK1HMvgo6xiTLG5rD5rZLDS+rp2Bjs=
golang.org/x/net v0.10.0/go.mod h1:0qNGK6F8kojg2nk9dLZ2mShWaEBan6FAoqfSigmmuDg=
golang.org/x/net v0.15.0/go.mod h1:idbUs1IY1+zTqbi8yxTbhexhEEk5ur9LInksu6HrEpk=
golang.org/x/net v0.21.0/go.mod h1:bIjVDfnllIU7BJ2DNgfnXvpSvtn8VRwhlsaeUTyUS44=
golang.org/x/net v0.25.0/go.mod h1:JkAGAh7GEvH74S6FOH42FLoXpXbE/aqXSrIQjXgsiwM=
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.1.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.3.0/go.mod h1:FU7BRWz2tNW+3quACPkgCx/L+uEAv1htQ0V83Z9Rj+Y=
golang.org/x/sync v0.6.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
golang.org/x/sync v0.7.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
Expand All @@ -87,25 +109,38 @@ golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBc
golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.8.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.11.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.12.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.17.0 h1:25cE3gD+tdBA7lp7QfhuV+rJiE9YXTcS3VG1SqssI/Y=
golang.org/x/sys v0.17.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/sys v0.20.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/sys v0.21.0 h1:rF+pYz3DAGSQAxAu1CbC7catZg4ebC4UIeIhKxBZvws=
golang.org/x/sys v0.21.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/telemetry v0.0.0-20240228155512-f48c80bd79b2/go.mod h1:TeRTkGYfJXctD9OcfyVLyj2J3IxLnKwHJR8f4D8a3YE=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
golang.org/x/term v0.5.0/go.mod h1:jMB1sMXY+tzblOD4FWmEbocvup2/aLOaQEp7JmGp78k=
golang.org/x/term v0.8.0/go.mod h1:xPskH00ivmX89bAKVGSKKtLOWNx2+17Eiy94tnKShWo=
golang.org/x/term v0.11.0/go.mod h1:zC9APTIj3jG3FdV/Ons+XE1riIZXG4aZ4GTHiPZJPIU=
golang.org/x/term v0.12.0/go.mod h1:owVbMEjm3cBLCHdkQu9b1opXd4ETQWc3BhuQGKgXgvU=
golang.org/x/term v0.17.0/go.mod h1:lLRBjIVuehSbZlaOtGMbcMncT+aqLLLmKrsjNrUguwk=
golang.org/x/term v0.20.0/go.mod h1:8UkIAJTvZgivsXaD6/pH6U9ecQzZ45awqEOzuCvwpFY=
golang.org/x/term v0.21.0/go.mod h1:ooXLefLobQVslOqselCNF4SxFAaoS6KujMbsGzSDmX0=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
golang.org/x/text v0.7.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8=
golang.org/x/text v0.9.0/go.mod h1:e1OnstbJyHTd6l/uOt8jFFHp6TRDWZR/bV3emEE/zU8=
golang.org/x/text v0.12.0/go.mod h1:TvPlkZtksWOMsz7fbANvkp4WM8x/WCo/om8BMLbz+aE=
golang.org/x/text v0.13.0/go.mod h1:TvPlkZtksWOMsz7fbANvkp4WM8x/WCo/om8BMLbz+aE=
golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU=
golang.org/x/text v0.15.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU=
golang.org/x/text v0.16.0/go.mod h1:GhwF1Be+LQoKShO3cGOHzqOgRrGaYc9AvblQOmPVHnI=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc=
golang.org/x/tools v0.6.0/go.mod h1:Xwgl3UAJ/d3gWutnCtw505GrjyAbvKui8lOU390QaIU=
golang.org/x/tools v0.13.0/go.mod h1:HvlwmtVNQAhOuCjW7xxvovg8wbNq7LwfXh/k7wXUl58=
golang.org/x/tools v0.21.1-0.20240508182429-e35e4ccd0d2d/go.mod h1:aiJjzUbINMkxbQROHiO6hDPo2LHcIPhhQsa9DLh0yGk=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
Expand Down
43 changes: 35 additions & 8 deletions tracker/ethereum.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,36 @@ type TrackBridgeMessageOptions struct {
OriginEndpoint string
// relay chain websocket endpoint
RelayEndpoint string

// bridge hub name
BridgeHubName string
}

var (
bridgeHubName = "bridgehub-rococo" // Subscan api endpoint name, for example: bridgehub-rococo,bridgehub-polkadot
bridgeContract = "0x5B4909cE6Ca82d2CE23BD46738953c7959E710Cd" // bridge contract address
bridgeHubRococo = "bridgehub-rococo"
bridgeHubWestend = "bridgehub-westend"
bridgeHubPolkadot = "bridgehub-polkadot"
bridgeHubKusama = "bridgehub-kusama"
)

// Subscan api endpoint name, for example: bridgehub-rococo,bridgehub-polkadot =

func (t *TrackBridgeMessageOptions) bridgeHubName() string {
const defaultBridgeHubName = "bridgehub-rococo"
if t.BridgeHubName != "" {
if !util.InSlice(t.BridgeHubName, []string{bridgeHubRococo, bridgeHubWestend, bridgeHubPolkadot, bridgeHubKusama}) {
panic(fmt.Sprintf("bridgeHubName %s is not supported", t.BridgeHubName))
}
return t.BridgeHubName
}
return defaultBridgeHubName
}

var (
bridgeContract = map[string]string{
bridgeHubPolkadot: "0x27ca963C279c93801941e1eB8799c23f407d68e7", // polkadot bridge contract address
bridgeHubRococo: "0x5B4909cE6Ca82d2CE23BD46738953c7959E710Cd", // rococo bridge contract address
}

extrinsicIndexEmptyError = fmt.Errorf("extrinsicIndex is empty")
bridgeHubEndpointEmptyError = fmt.Errorf("bridgeHubEndpoint is empty")
Expand All @@ -41,16 +66,18 @@ var (
func TrackBridgeMessage(ctx context.Context, opt *TrackBridgeMessageOptions) (*Event, error) {
// ethereum -> polkadot
// if tx is not empty, will track ethereum => polkadot message
isPolkadot := opt.bridgeHubName() == bridgeHubPolkadot

if opt.Tx != "" {
// Get transaction receipt
receipt, err := util.EthGetTransactionReceipt(ctx, opt.Tx)
receipt, err := util.EthGetTransactionReceipt(ctx, isPolkadot, opt.Tx)
if err != nil {
return nil, err
}
blockNum := util.HexToUint64(receipt.BlockNumber)

// Get block timestamp
block, err := util.EthGetBlockByNum(ctx, blockNum)
block, err := util.EthGetBlockByNum(ctx, isPolkadot, blockNum)
if err != nil {
return nil, err
}
Expand All @@ -74,7 +101,7 @@ func TrackBridgeMessage(ctx context.Context, opt *TrackBridgeMessageOptions) (*E

log.Println("Get ethereum message Id", messageId, "timestamp", timestamp, "blockNum", blockNum)

polkadotBlock, err := util.SubscanGetBlockByTime(ctx, bridgeHubName, uint(timestamp))
polkadotBlock, err := util.SubscanGetBlockByTime(ctx, opt.bridgeHubName(), uint(timestamp))
if err != nil {
log.Printf("SubscanGetBlockByTime get err %v\n", err)
return nil, err
Expand All @@ -94,7 +121,7 @@ func TrackBridgeMessage(ctx context.Context, opt *TrackBridgeMessageOptions) (*E
const maxEndBlockNum = 99999999
for {
eventReqParams.BlockRange = fmt.Sprintf("%d-%d", startCrawlNum, maxEndBlockNum)
events, err := util.SubscanGetEvents(ctx, bridgeHubName, &eventReqParams)
events, err := util.SubscanGetEvents(ctx, opt.bridgeHubName(), &eventReqParams)
if len(events) == 0 {
return nil, errors.New("not found message")
}
Expand Down Expand Up @@ -161,13 +188,13 @@ func TrackBridgeMessage(ctx context.Context, opt *TrackBridgeMessageOptions) (*E
if err != nil {
return nil, err
}
etherStartBlockNum, err := util.EtherscanGetBlockByTime(ctx, event.BlockTime)
etherStartBlockNum, err := util.EtherscanGetBlockByTime(ctx, isPolkadot, event.BlockTime)
if err != nil {
return nil, err
}
log.Println("Get etherStartBlockNum", etherStartBlockNum)

logs, err := util.EtherscanGetLogs(ctx, uint64(etherStartBlockNum), bridgeContract, InboundMessageDispatchedTopic, 1, 1000)
logs, err := util.EtherscanGetLogs(ctx, isPolkadot, uint64(etherStartBlockNum), bridgeContract[opt.BridgeHubName], InboundMessageDispatchedTopic, 1, 1000)
if err != nil {
return nil, err
}
Expand Down
2 changes: 1 addition & 1 deletion tx/tx_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
)

func Test_Client(t *testing.T) {
client := NewClient("wss://polkadot-rpc.dwellir.com")
client := NewClient("wss://rpc.polkadot.io")

assert.NotNil(t, GetModule("Balances", client.m))
assert.Nil(t, GetModule("xtokens", client.m))
Expand Down
30 changes: 18 additions & 12 deletions util/etherscan.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,16 @@ import (
)

const (
EtherscanAPIBaseURL = "https://api-sepolia.etherscan.io/api"
EtherscanAPIKey = "5D91FZ48V3XPNV58ZSNAYBBWS14K3GGSZC"
EtherscanAPIKey = "5D91FZ48V3XPNV58ZSNAYBBWS14K3GGSZC"
)

func etherscanAPIBaseURL(isMainnet bool) string {
if isMainnet {
return "https://api.etherscan.io/api"
}
return "https://api-sepolia.etherscan.io/api"
}

type EtherscanProxyRes[T any] struct {
Jsonrpc string `json:"jsonrpc"`
Id int `json:"id"`
Expand Down Expand Up @@ -42,8 +48,8 @@ type EtherscanTransaction struct {

// EthGetTransactionByHash returns the transaction details by hash
// eth_getTransactionByHash
func EthGetTransactionByHash(ctx context.Context, hash string) (*EtherscanTransaction, error) {
var endpoint = EtherscanAPIBaseURL + "?module=proxy&action=eth_getTransactionByHash&txhash=" + hash + "&apikey=" + EtherscanAPIKey
func EthGetTransactionByHash(ctx context.Context, isMainnet bool, hash string) (*EtherscanTransaction, error) {
var endpoint = etherscanAPIBaseURL(isMainnet) + "?module=proxy&action=eth_getTransactionByHash&txhash=" + hash + "&apikey=" + EtherscanAPIKey
body, err := HttpGet(ctx, endpoint)
if err != nil {
return nil, err
Expand Down Expand Up @@ -86,8 +92,8 @@ type EthReceiptLog struct {

// EthGetTransactionReceipt returns the transaction receipt by hash
// eth_getTransactionReceipt
func EthGetTransactionReceipt(ctx context.Context, hash string) (*EtherscanTransactionReceipt, error) {
var endpoint = EtherscanAPIBaseURL + "?module=proxy&action=eth_getTransactionReceipt&txhash=" + hash + "&apikey=" + EtherscanAPIKey
func EthGetTransactionReceipt(ctx context.Context, isMainnet bool, hash string) (*EtherscanTransactionReceipt, error) {
var endpoint = etherscanAPIBaseURL(isMainnet) + "?module=proxy&action=eth_getTransactionReceipt&txhash=" + hash + "&apikey=" + EtherscanAPIKey
body, err := HttpGet(ctx, endpoint)
if err != nil {
return nil, err
Expand Down Expand Up @@ -121,9 +127,9 @@ type EtherscanBlock struct {
Uncles []interface{} `json:"uncles"`
}

func EthGetBlockByNum(ctx context.Context, blockNum uint64) (*EtherscanBlock, error) {
func EthGetBlockByNum(ctx context.Context, isMainnet bool, blockNum uint64) (*EtherscanBlock, error) {
// https://api.etherscan.io/api?module=proxy&action=eth_getBlockByNumber&tag=0x10d4f&boolean=false&apikey=YourApiKeyToken
var endpoint = EtherscanAPIBaseURL + "?module=proxy&action=eth_getBlockByNumber&tag=0x" + fmt.Sprintf("%x", blockNum) + "&boolean=false&apikey=" + EtherscanAPIKey
var endpoint = etherscanAPIBaseURL(isMainnet) + "?module=proxy&action=eth_getBlockByNumber&tag=0x" + fmt.Sprintf("%x", blockNum) + "&boolean=false&apikey=" + EtherscanAPIKey
body, err := HttpGet(ctx, endpoint)
if err != nil {
return nil, err
Expand All @@ -141,9 +147,9 @@ type EtherscanRes[T any] struct {
Result T `json:"result"`
}

func EtherscanGetBlockByTime(ctx context.Context, timestamp int64) (uint, error) {
func EtherscanGetBlockByTime(ctx context.Context, isMainnet bool, timestamp int64) (uint, error) {
// https://api-sepolia.etherscan.io/api?module=block&action=getblocknobytime&timestamp=1708921620&closest=after&apikey=5D91FZ48V3XPNV58ZSNAYBBWS14K3GGSZC
var endpoint = EtherscanAPIBaseURL + "?module=block&action=getblocknobytime&timestamp=" + fmt.Sprintf("%d", timestamp) + "&closest=after&apikey=" + EtherscanAPIKey
var endpoint = etherscanAPIBaseURL(isMainnet) + "?module=block&action=getblocknobytime&timestamp=" + fmt.Sprintf("%d", timestamp) + "&closest=after&apikey=" + EtherscanAPIKey
body, err := HttpGet(ctx, endpoint)
if err != nil {
return 0, err
Expand All @@ -170,9 +176,9 @@ type EtherscanLog struct {
TransactionIndex string `json:"transactionIndex"`
}

func EtherscanGetLogs(ctx context.Context, fromBlock uint64, address, topic0 string, page uint64, offset uint64) ([]EtherscanLog, error) {
func EtherscanGetLogs(ctx context.Context, isMainnet bool, fromBlock uint64, address, topic0 string, page uint64, offset uint64) ([]EtherscanLog, error) {
// https://api.etherscan.io/api?module=logs&action=getLogs&fromBlock=12878196&topic0=0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef&page=1&offset=1&apikey=YourApiKeyToken
var endpoint = EtherscanAPIBaseURL + "?module=logs&action=getLogs&fromBlock=" + fmt.Sprintf("%d", fromBlock) + "&address=" + address + "&topic0=" + topic0 + "&page=" + fmt.Sprintf("%d", page) + "&offset=" + fmt.Sprintf("%d", offset) + "&apikey=" + EtherscanAPIKey
var endpoint = etherscanAPIBaseURL(isMainnet) + "?module=logs&action=getLogs&fromBlock=" + fmt.Sprintf("%d", fromBlock) + "&address=" + address + "&topic0=" + topic0 + "&page=" + fmt.Sprintf("%d", page) + "&offset=" + fmt.Sprintf("%d", offset) + "&apikey=" + EtherscanAPIKey
body, err := HttpGet(ctx, endpoint)
if err != nil {
return nil, err
Expand Down
Loading

0 comments on commit c68a4fb

Please sign in to comment.