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 and Alpha Mainnet.
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 and Alpha Mainnet 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'], }, alpha: { url: 'https://rpc.goat.network', chainId: 2345, 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 to either testnet3 or alpha mainnet:
npx hardhat run scripts/deploy.js --network goat
or...
npx hardhat run scripts/deploy.js --network alpha
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)
Contract Addresses
Testnet3 Core Protocol Contracts
Alpha Mainnet Core Protocol Contracts
Contract Name | Mainnet Address | Description |
---|---|---|
WGBTC | 0xbC10000000000000000000000000000000000000 (opens in a new tab) | Native wrapped Bitcoin token |
Bitcoin.sol | 0xbc10000000000000000000000000000000000005 (opens in a new tab) | Bitcoin block header verification |
Multicall3 | 0xcA11bde05977b3631167028862bE2a173976CA11 (opens in a new tab) | Multicall aggregator |
Permit2 | 0x000000000022D473030F116dDEE9F6B43aC78BA3 (opens in a new tab) | Gasless approvals |
Bridge | 0xBC10000000000000000000000000000000000003 (opens in a new tab) | Cross-chain bridge |
Locking | 0xbC10000000000000000000000000000000000004 (opens in a new tab) | Token locking mechanism |
GOAT Token | 0xbC10000000000000000000000000000000000001 (opens in a new tab) | Native utility token |
Alpha Mainnet is now live! For complete contract addresses and integrations, visit our Contracts Reference.