Developing Smart Contracts with Solidity
Solidity is the primary language for developing smart contracts on the GOAT Network. This guide will walk you through the process of writing, testing, and deploying Solidity contracts on Testnet3.
Prerequisites
- Solidity Compiler: Install
solc
or use an IDE like Remix or Visual Studio Code with Solidity extensions. - Node.js and NPM: For using development frameworks like Truffle or Hardhat.
- Wallet Setup: Configure MetaMask or another wallet with Testnet3 settings.
Writing Your First Contract
Here's a simple HelloWorld
contract:
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
contract HelloWorld {
string public greeting = "Hello, GOAT Network!";
function setGreeting(string memory _greeting) public {
greeting = _greeting;
}
}
Configuring Hardhat
-
Initialize Hardhat Project:
npx hardhat init
-
Install Dependencies:
npm install --save-dev @nomiclabs/hardhat-ethers ethers
-
Update
hardhat.config.js
:require('@nomiclabs/hardhat-ethers'); module.exports = { defaultNetwork: 'goat', networks: { goat: { url: 'https://rpc.testnet3.goat.network', chainId: 48816, accounts: ['YOUR_PRIVATE_KEY'], }, }, solidity: { version: '0.8.0', }, };
Deploying Your Contract
Create a deployment script:
async function main() {
const HelloWorld = await ethers.getContractFactory('HelloWorld');
const hello = await HelloWorld.deploy();
console.log('Contract deployed to:', hello.address);
}
main()
.then(() => process.exit(0))
.catch(error => {
console.error(error);
process.exit(1);
});
Deploy the contract:
npx hardhat run scripts/deploy.js --network goat
Interacting with the Contract
Use ethers.js within your scripts or front-end application:
const provider = new ethers.providers.JsonRpcProvider('https://rpc.testnet3.goat.network');
const signer = provider.getSigner();
const helloWorldContract = new ethers.Contract(CONTRACT_ADDRESS, ABI, signer);
// Get the greeting
const greeting = await helloWorldContract.greeting();
console.log(greeting);
// Set a new greeting
const tx = await helloWorldContract.setGreeting('Hello, Testnet3!');
await tx.wait();
Testing Your Contract
Create tests using Hardhat's testing framework:
const { expect } = require('chai');
describe('HelloWorld Contract', function () {
it('Should return the initial greeting', async function () {
const HelloWorld = await ethers.getContractFactory('HelloWorld');
const hello = await HelloWorld.deploy();
await hello.deployed();
expect(await hello.greeting()).to.equal('Hello, GOAT Network!');
});
it('Should update the greeting', async function () {
const HelloWorld = await ethers.getContractFactory('HelloWorld');
const hello = await HelloWorld.deploy();
await hello.deployed();
await hello.setGreeting('Hello, Testnet3!');
expect(await hello.greeting()).to.equal('Hello, Testnet3!');
});
});
Run tests:
npx hardhat test
Best Practices
- Security Audits: Always audit your contracts or have them reviewed.
- Use SafeMath: Although Solidity 0.8.x has built-in overflow checks, be cautious with arithmetic operations.
- Gas Optimization: Write efficient code to reduce gas costs, leveraging EIP-1559 where applicable.
- Documentation: Comment your code for better maintainability.
Additional Resources
- Solidity Documentation: docs.soliditylang.org (opens in a new tab)
- GOAT Network Developer Help: Developer Chat (opens in a new tab)