This repository provides a starting point for building Ethereum smart contracts with Solidity. It includes a sample contract (MyContract
), as well as setup for deployment scripts, testing with Chai, and Truffle configuration.
- Install Node.js and npm.
- Clone this repo to your local machine using
git clone git@github.com:0xKyran/solidity-truffle-template.git
. - Navigate to the project directory and run
npm install
. - Install Truffle globally with
npm install -g truffle
. - Install Ganache - this provides a personal Ethereum blockchain which you can use to run tests, execute commands, and inspect state while controlling how the chain operates.
-
./migrations/1_deploy_contracts.js
: Contains the deployment script for theMyContract
smart contract. It deploys the contract with an initial name of "ExampleContract". -
./src/contracts/MyContract.sol
: Contains theMyContract
smart contract. The contract has a name property, which can be changed and fetched via thechangeName
andgetName
functions respectively. -
./test/test.js
: Contains the tests for theMyContract
smart contract. Uses the Chai assertion library for testing. -
truffle-config.js
: Contains the Truffle configuration for the project, including compiler options and network definitions.
To create your own contract, edit the MyContract.sol
file in the ./src/contracts/
directory. Use MyContract
as a starting point for your contract.
Before deploying or running tests, you'll need to compile your contract. The Truffle suite will handle this for you. To compile your contracts, navigate to your project directory and run:
truffle compile
This command will compile all contracts in the ./src/contracts/ directory and output the results (including the ABI and bytecode) to the ./src/abis/ directory.
Keep in mind that every time you modify your contract, you will need to recompile it before running tests or deploying it to a network.
To write tests for your contract, modify the test.js file in the ./test/ directory. The tests use the Chai assertion library.
To run the tests, make sure you have a development blockchain running (like Ganache), and then execute:
truflle test
This will compile your contracts, deploy them to the development network, and run your tests.
In order to deploy your contract, replace the placeholder values in truffle-config.js
with your actual API key. Then run:
also replace
truffle migrate --network <network-name>
Note: if you omit the
--network
flag, Truffle will deploy to the development network by default.
Replace with the name of the network you want to deploy to (e.g., mainnet, rinkeby, ropsten, or development).
This project is licensed under the MIT License. See the LICENSE file for details.
This project is based on the solidity-truffle-template GitHub template.
- Node.js (recommended version: 14.x.x)
- Truffle (recommended version: 5.x.x)
- Solidity (recommended version: 0.8.x)
- Clone the repository:
git clone <repo>
- Install the dependencies:
npm install
- Compile the contracts:
npm run compile
- Run the tests:
truffle test
- Deploy the contracts to your desired Ethereum network:
npm run migrate --network <network-name>
Note: if you omit the --network flag, Truffle will deploy to the development network by default. Replace with the name of the network you want to deploy to (e.g., mainnet, rinkeby, ropsten, or development).
<contract functions + explanation>
This project is licensed under the MIT License. See the LICENSE file for details.