Polygon
Pronto para usar isso em produção?
O plano gratuito cobre projetos pessoais. O pay-as-you-go escala sem cartão de crédito.
Polygon
O plano gratuito cobre projetos pessoais. O pay-as-you-go escala sem cartão de crédito.
Nethereum is the primary .NET library for talking to Polygon. Because Polygon PoS is EVM-equivalent, the same Nethereum APIs you would use against any Ethereum node work unchanged when you point them at https://polygon.therpc.io/YOUR_API_KEY. It targets .NET Standard 2.0+, so the same code runs on .NET Core, .NET 5/6/7+, and the older .NET Framework, and it is compatible with Unity3D, Xamarin, ASP.NET Core, and Blazor. Add it to your project with dotnet add package Nethereum.Web3.
Nethereum targets .NET Standard 2.0+ and is compatible with Unity3D, Xamarin, ASP.NET Core, and Blazor, so a single Polygon integration can be shared across a game client, a mobile app, and a web backend. Install it from NuGet with dotnet add package Nethereum.Web3.
To connect, instantiate new Web3(url) with your Polygon endpoint URL — https://polygon.therpc.io/YOUR_API_KEY — and reuse that single instance across your service. Every IO method in Nethereum returns an async Task, so always await them. Avoid blocking on .Result or .Wait(): in ASP.NET and Unity contexts that can deadlock the synchronization context and freeze your app.
To work with a deployed Polygon contract, get a handle from _web3.Eth.GetContract(abi, contractAddress), passing the contract's ABI JSON and its address on chain 137. From there, GetFunction(name) resolves a callable function: use CallAsync<T>(...) for read-only views, which cost no MATIC, and SendTransactionAsync(...) for state-changing writes, which submit a signed transaction and consume gas.
In an ASP.NET Core app, register EthereumService in the dependency injection container in Program.cs (or Startup.cs) — typically as a singleton so the single Polygon Web3 connection is reused across requests — and let the framework inject it into your controllers. Inside each action, wrap the async call in a try/catch so a failed RPC request to the Polygon endpoint returns BadRequest with the error message instead of bubbling up as an unhandled 500.
To watch contract events on Polygon, define an event DTO matching your Solidity event, then use GetEvent<TEventDTO>(contractAddress) together with CreateFilterInput() to build a log filter. Polling that filter with GetFilterChanges returns the matching logs as they are mined, letting you react to on-chain activity such as token transfers without manually decoding raw log data.
For a Unity3D game, initialize Web3 once in Start() or Awake() with the Polygon endpoint https://polygon.therpc.io/YOUR_API_KEY, since Polygon's low fees make it a popular choice for on-chain game assets paid in MATIC. Use async void only for top-level lifecycle hooks like Start(); for everything else prefer Task-returning helper methods so exceptions can be caught and awaited cleanly, rather than scattering async void across MonoBehaviour callbacks where errors are easy to lose.