Skip to content

Commit

Permalink
Update Deploy and Interact with a Solidity Contract (#198)
Browse files Browse the repository at this point in the history
* Update `Deploy and Interact with a Solidity Contract`

* Fix the link of quick_start

* Update run result in deploy_solidity.md

Co-authored-by: sunchengzhu <[email protected]>

---------

Co-authored-by: sunchengzhu <[email protected]>
  • Loading branch information
Flouse and sunchengzhu authored Sep 18, 2023
1 parent ff80f5e commit 049038e
Showing 1 changed file with 16 additions and 61 deletions.
77 changes: 16 additions & 61 deletions docs/getting-started/for-dapp-devs/deploy_solidity.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,87 +9,42 @@ import useBaseUrl from "@docusaurus/useBaseUrl";
# Deploy and Interact with a Solidity Contract

## Deploy a Solidity contract
Given Axon’s full compatibility with the EVM, contract deployment on Axon closely resembles the process on Ethereum. You can follow the [Quick Start](https://hardhat.org/hardhat-runner/docs/getting-started#quick-start) for guidance. The only distinction is the network, Axon. To make the adjustment, you need to edit the [hardhat.config.ts](https://hardhat.org/hardhat-runner/docs/config) file as follows, which is done on the internal Axon’s testnet.
Given Axon’s full compatibility with the EVM, contract deployment on Axon closely resembles the process on Ethereum. You can follow the [Hardhat Getting Started](https://hardhat.org/hardhat-runner/docs/getting-started) for guidance. The only distinction is the network configuration.

This document assumes that you have followed [the Axon quick start](./quick_start.md) to run an Axon node.

When the project is created by `npx hardhat`, you just need to edit the [hardhat.config.ts](https://hardhat.org/hardhat-runner/docs/config) file as follows:

```javascript
import { HardhatUserConfig } from "hardhat/config";
import "@nomicfoundation/hardhat-toolbox";

// Axon genesis account configrued on the local / test network.
const AXON_PRIVATE_KEY = "0x37aa0f893d05914a4def0460c0a984d3611546cfb26924d7a7ca6e0db9950a2d";

const config: HardhatUserConfig = {
solidity: "0.8.19",
networks: {
axon: {
chainId: 2022,
url: "The axon net URL"
accounts: [AXON_PRIVATE_KEY],
url: "http://127.0.0.1:8000", // The RPC URL of Axon devnet
// Axon devnet's accounts since the genesis block
// See https://github.com/axonweb3/axon/blob/88c9a913/devtools/chain/specs/single_node/chain-spec.toml#L18C3-L21
accounts: {
mnemonic: "test test test test test test test test test test test junk",
count: 10, // the number of accounts to derive
},
},
},
};

export default config;
```

Then deploy the contract:
Now you can connect Hardhat to this Axon node, for example to run a deployment script against it, you simply need to run it using `--network axon`:
```shell
$ npx hardhat run scripts/deploy.ts --network axon

Lock with 0.001ETH and unlock timestamp 1692058859 deployed to 0x7CcECF6cc5E022F7D582deF5d5b53fD179f9A368
# Result
Lock with 0.001ETH and unlock timestamp 1694761966 deployed to 0x5FbDB2315678afecb367f032d93F642f64180aa3
```
You can follow [this](https://docs.axonweb3.io/getting-started/for-dapp-devs/send_transactions_on_axon_via_metaMask/#11-local-setup) instruction to set up an Axon node locally, and replace the `url` in the `hardhat.config.ts` file with `http://127.0.0.1:8000`.

## Interact With the Deployed Contract
Interacting with Axon contracts is the same as with Ethereum contracts. You can refer to [Connecting to Existing Contracts](https://docs.ethers.org/v4/api-contract.html#connecting-to-existing-contracts) for more details.

Below is an example:

```javascript
import { ethers } from "ethers";

// Copy from 'solidity-contract/artifacts/contracts/Lock.sol/Lock.json'
import lockContract from "./Lock.json";

// Address of the deployed contract
const contractAddress = "0x7CcECF6cc5E022F7D582deF5d5b53fD179f9A368";

async function main() {
// Connect to the network
let provider = new ethers.JsonRpcProvider(AXON_NET_URL);
Interacting with Axon contracts is the same as with Ethereum contracts. You can refer to https://docs.ethers.org for more details.

// A Signer from a private key
const signer = new ethers.Wallet(AXON_PRIVATE_KEY, provider);

// Create a new instance of the Contract with a Signer, which allows
// update methods
const contract = new ethers.Contract(
contractAddress,
lockContract.abi,
signer,
);

console.log(
"before calling the contract: ",
await provider.getBalance(contract.target),
);

// Call a Contract's non-constant method
let tx = await contract.withdraw();
console.log("tx hash: ", tx.hash);

// The operation is NOT complete yet; we must wait until it is mined
await tx.wait();

console.log(
"after calling the contract: ",
await provider.getBalance(contract.target),
);

// Call Contract's read-only constant methods
console.log("unlock time: ", await contract.unlockTime());
console.log("contract owner: ", await contract.owner());
}

main();
```

0 comments on commit 049038e

Please sign in to comment.