Polygon
هل أنت مستعد لاستدعاء هذا في الإنتاج؟
الخطة المجانية تغطي المشاريع الشخصية. الدفع حسب الاستخدام يتوسع دون بطاقة.
Polygon
الخطة المجانية تغطي المشاريع الشخصية. الدفع حسب الاستخدام يتوسع دون بطاقة.
go-ethereum (geth) is the official Go implementation of the Ethereum protocol and, by extension, the most complete and up-to-date EVM library available to Go developers. Since Polygon PoS is EVM-equivalent, geth's ethclient package connects to https://polygon.therpc.io/YOUR_API_KEY exactly as it would to any Ethereum node — chain ID 137 is just another EVM network from the client's point of view. Pull it in with go get github.com/ethereum/go-ethereum. It is maintained by the Ethereum Foundation, so it tracks protocol changes closely.
go-ethereum is the official Go client maintained by the Ethereum Foundation, and it gives Go developers building on Polygon the most complete and up-to-date EVM implementation — the same battle-tested code that runs Ethereum mainnet nodes, pointed at chain ID 137.
Connect with ethclient.Dial, passing the Polygon HTTP endpoint https://polygon.therpc.io/YOUR_API_KEY; it returns a client you can share across goroutines. Wrapping that client in your own struct, as below, keeps RPC logic organized and makes it easy to swap in a mock client for tests instead of hitting the live Polygon network.
abigen-generated type-safe contract bindingsThe idiomatic way to work with a Polygon contract in Go is to run abigen against its Solidity ABI to generate type-safe Go bindings, giving you compile-time-checked method calls instead of stringly-typed RPC. Under the hood those bindings — and the lower-level example here — rely on bind.BoundContract, which wraps the parsed ABI and lets you Call read-only methods or Transact to send state-changing transactions that consume MATIC for gas.
SubscribeNewHead and other live subscriptions require a WebSocket connection — the HTTP Polygon endpoint cannot push events, so for streaming you must dial the WebSocket variant of the endpoint rather than the HTTPS one shown above. Once subscribed, run the read loop in a goroutine with a select that handles both incoming block headers and the subscription's error channel, so a dropped connection on chain 137 surfaces cleanly instead of silently stalling.
Polygon values are denominated in wei, where 1 MATIC equals 10^18 wei — far beyond the precision of float64. Always use big.Int for raw wei amounts and big.Float for conversions; reaching for native floats will silently round and corrupt balances and transfer amounts. The helpers below convert between wei and MATIC and validate addresses without losing precision.