fetchCurrentGuardianSet.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. package utils
  2. import (
  3. "context"
  4. "fmt"
  5. "math/big"
  6. "time"
  7. "github.com/certusone/wormhole/node/pkg/common"
  8. "github.com/certusone/wormhole/node/pkg/watchers/evm/connectors/ethabi"
  9. ethAbi "github.com/certusone/wormhole/node/pkg/watchers/evm/connectors/ethabi"
  10. ethBind "github.com/ethereum/go-ethereum/accounts/abi/bind"
  11. eth_common "github.com/ethereum/go-ethereum/common"
  12. ethClient "github.com/ethereum/go-ethereum/ethclient"
  13. ethRpc "github.com/ethereum/go-ethereum/rpc"
  14. )
  15. func GetRpcUrl(network common.Environment) string {
  16. switch network {
  17. case common.MainNet:
  18. return "https://rpc.ankr.com/eth"
  19. case common.TestNet:
  20. return "https://rpc.ankr.com/eth_goerli"
  21. case common.UnsafeDevNet:
  22. return "http://localhost:8545"
  23. case common.GoTest:
  24. return "http://eth-devnet:8545"
  25. default:
  26. return ""
  27. }
  28. }
  29. func FetchLatestBlockNumber(ctx context.Context, network common.Environment) (*big.Int, error) {
  30. rawUrl := GetRpcUrl(network)
  31. if rawUrl == "" {
  32. return nil, fmt.Errorf("unable to get rpc url")
  33. }
  34. return FetchLatestBlockNumberFromUrl(ctx, rawUrl)
  35. }
  36. func FetchLatestBlockNumberFromUrl(ctx context.Context, rawUrl string) (*big.Int, error) {
  37. rawClient, err := ethRpc.DialContext(ctx, rawUrl)
  38. if err != nil {
  39. return nil, fmt.Errorf("unable to dial eth context: %w", err)
  40. }
  41. client := ethClient.NewClient(rawClient)
  42. header, err := client.HeaderByNumber(ctx, nil)
  43. if err != nil {
  44. return nil, fmt.Errorf("unable to fetch latest header: %w", err)
  45. }
  46. return header.Number, nil
  47. }
  48. func FetchCurrentGuardianSet(network common.Environment) (uint32, *ethabi.StructsGuardianSet, error) {
  49. ctx, cancel := context.WithTimeout(context.Background(), time.Second*5)
  50. defer cancel()
  51. rawUrl := GetRpcUrl(network)
  52. if rawUrl == "" {
  53. return 0, nil, fmt.Errorf("unable to get rpc url")
  54. }
  55. var ethContract string
  56. switch network {
  57. case common.MainNet:
  58. ethContract = "0x98f3c9e6E3fAce36bAAd05FE09d375Ef1464288B"
  59. case common.TestNet:
  60. ethContract = "0x706abc4E45D419950511e474C7B9Ed348A4a716c"
  61. case common.UnsafeDevNet:
  62. case common.GoTest:
  63. ethContract = "0xC89Ce4735882C9F0f0FE26686c53074E09B0D550"
  64. default:
  65. return 0, nil, fmt.Errorf("unable to fetch guardian set for unknown network %s", network)
  66. }
  67. contract := eth_common.HexToAddress(ethContract)
  68. rawClient, err := ethRpc.DialContext(ctx, rawUrl)
  69. if err != nil {
  70. return 0, nil, fmt.Errorf("failed to connect to ethereum")
  71. }
  72. client := ethClient.NewClient(rawClient)
  73. caller, err := ethAbi.NewAbiCaller(contract, client)
  74. if err != nil {
  75. return 0, nil, fmt.Errorf("failed to create caller")
  76. }
  77. currentIndex, err := caller.GetCurrentGuardianSetIndex(&ethBind.CallOpts{Context: ctx})
  78. if err != nil {
  79. return 0, nil, fmt.Errorf("error requesting current guardian set index: %w", err)
  80. }
  81. gs, err := caller.GetGuardianSet(&ethBind.CallOpts{Context: ctx}, currentIndex)
  82. if err != nil {
  83. return 0, nil, fmt.Errorf("error requesting current guardian set value: %w", err)
  84. }
  85. return currentIndex, &gs, nil
  86. }