You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/smart-contracts/deploy-a-contract.md
+21-1Lines changed: 21 additions & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -25,9 +25,13 @@ After defining your contract's typed DTOs (as covered in [Contract Interaction](
25
25
dotnet add package Nethereum.Web3
26
26
```
27
27
28
+
You need a funded account to deploy — deployment is a transaction that costs gas.
29
+
28
30
## Using a Code-Generated Deployment Class
29
31
30
-
The recommended approach is to use Nethereum's code generator to create typed deployment classes from your Solidity ABI and bytecode:
32
+
The recommended approach is to use Nethereum's [code generator](./code-generation.md) to create typed deployment classes from your Solidity ABI and bytecode. The generated class inherits from `ContractDeploymentMessage` and contains the bytecode and constructor parameters as properties.
33
+
34
+
Once you have the deployment class, create a `Web3` instance with your account, populate the constructor parameters, and send:
The handler encodes your constructor parameters into the deployment bytecode, estimates gas (unless you set it explicitly), manages the nonce, and waits for the transaction to be mined. The `ContractAddress` on the receipt is the address of your new contract.
58
+
53
59
## Estimating Deployment Gas
54
60
61
+
If you want to check the gas cost before committing to the deployment, call `EstimateGasAsync` first. This simulates the deployment against the current chain state without actually sending a transaction:
Note that `SendRequestAndWaitForReceiptAsync` estimates gas automatically if you don't set `Gas` on the message — so this step is only needed when you want to display the cost to a user or set a specific gas limit.
70
+
61
71
## Deploy Without Code-Generated Classes
62
72
73
+
When prototyping or working with a raw ABI string, you can deploy without a typed class. Pass the ABI, bytecode, sender address, gas limit, and constructor arguments directly:
This approach is less safe — constructor argument types aren't checked at compile time, so a mismatch will fail at runtime. Use the typed approach for production code.
83
+
70
84
## Deploying with Multiple Constructor Parameters
71
85
86
+
Contracts often take multiple constructor arguments. Each parameter maps to a property on the deployment message class:
87
+
72
88
```csharp
73
89
vardeployment=newMyNFTDeployment
74
90
{
@@ -81,8 +97,12 @@ var handler = web3.Eth.GetContractDeploymentHandler<MyNFTDeployment>();
The `[Parameter]` attributes on the deployment class (defined in [Contract Interaction](./guide-smart-contract-interaction)) control how each property maps to the Solidity constructor signature.
101
+
84
102
## Checking Deployment Status
85
103
104
+
The receipt's `Status` field tells you whether the deployment succeeded. A status of `1` means success; `0` means the transaction was mined but the contract creation reverted (often due to a `require` failing in the constructor):
Copy file name to clipboardExpand all lines: docs/smart-contracts/erc20.md
+21-3Lines changed: 21 additions & 3 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -16,10 +16,12 @@ var receipt = await erc20.TransferRequestAndWaitForReceiptAsync(recipient, amoun
16
16
No ABI, no code generation — built-in typed service for any ERC-20 token.
17
17
:::
18
18
19
-
Nethereum has built-in typed services for ERC-20 tokens. No ABI needed, no code generation — just call the methods directly. This is the fastest way to interact with any ERC-20 token and covers the vast majority of use cases.
19
+
Nethereum has built-in typed services for ERC-20 tokens. No ABI needed, no code generation — just call the methods directly. This is the fastest way to interact with any ERC-20 token and covers the vast majority of use cases. For the full list of built-in services (ERC-721, ERC-1155, ENS, and more), see [Built-in Standards](./guide-built-in-standards.md).
20
20
21
21
## Query Token Info
22
22
23
+
Every ERC-20 token exposes metadata — the name, symbol, decimals, and total supply. These are read-only calls (no gas, no signing needed):
The `decimals` value is important — ERC-20 tokens store balances as integers in the smallest unit (like Wei for ETH). Most tokens use 18 decimals, but stablecoins like USDC use 6. Always use `Web3.Convert.FromWei(value, decimals)` to display human-readable amounts.
39
+
36
40
## Check Balance
37
41
42
+
Balances are returned in the token's smallest unit. Use `FromWei` with the token's decimals to get a readable value:
To transfer tokens, you need a `Web3` instance connected with a funded account (as shown in the [Contract Interaction guide](./guide-smart-contract-interaction.md)). The amount must be in the smallest unit — use `ToWei` with the token's decimals:
Gas estimation, nonce management, and EIP-1559 fees are handled automatically.
62
+
53
63
## Approve and TransferFrom
54
64
55
-
The approve/transferFrom pattern allows another address (like a DEX) to spend your tokens:
65
+
The approve/transferFrom pattern allows another address (like a DEX or smart contract) to spend your tokens. This is a two-step process: first you approve a spending limit, then the spender calls `transferFrom`.
56
66
57
67
```csharp
58
68
// Approve the spender to use 1000 tokens
@@ -65,8 +75,12 @@ var allowance = await erc20.AllowanceQueryAsync(myAddress, spenderAddress);
Be careful with approvals — approving a large amount gives the spender permission to transfer up to that amount at any time. Many DApps request unlimited approval (`BigInteger.MaxValue`) for convenience, but it's safer to approve only what's needed.
79
+
68
80
## Listen for Transfer Events
69
81
82
+
ERC-20 transfers emit a `Transfer` event. You can query historical transfers or monitor for new ones. Nethereum ships a built-in `TransferEventDTO` for ERC-20, so you don't need to define your own:
For more advanced filtering (by sender, recipient, block range), see the [Events guide](./guide-events.md).
98
+
83
99
## Historical Queries
84
100
85
-
All query methods accept an optional `BlockParameter`for historical state:
101
+
All query methods accept an optional `BlockParameter`to read state at a specific block. This is useful for checking what a balance was at a past point in time:
0 commit comments