Links
🍬

Testing with Truffle

A step-by-step guide for creating a Truffle project and deploying it to your private StealthTest environment.
Before you begin, please make sure to create your StealthTest environment and have access to the RPC URL and chain ID.
This document is intended to act as a quick-start integration guide for new Truffle projects. If you already have an established Truffle project, skip to step 3.
For more information on Truffle, please access the documentation here: Truffle documentation
We will never ask you for your private key, nor will it ever be required at any step in the process of creating a StealthTest environment.
  1. 1.
    Install Truffle
    1. 1.
      npm install -g truffle
  2. 2.
    Create a new Truffle project
    1. 1.
      truffle unbox metacoin truffle-tutorial
    2. 2.
      This is an example project that creates a new token called MetaCoin
    3. 3.
      Important directories include
      1. 1.
        contracts/: Directory for Solidity contracts
      2. 2.
        migrations/: Directory for scriptable deployment files
      3. 3.
        test/: Directory for test files for testing your application and contracts
      4. 4.
        truffle-config.js: Truffle configuration file
      5. 5.
        Output from creating your new Truffle project:
truffle unbox metacoin truffle-tutorial
Starting unbox...
✓ Preparing to download box ✓ Downloading ✓ Cleaning up temporary files ✓ Setting up box
Unbox successful, sweet!
Commands:
Compile contracts: truffle compile
Migrate contracts: truffle migrate
Test contracts: truffle test
Your new Truffle project will have the above file structure
  1. 3.
    Ensure your contracts are working properly by running the test suite
    1. 1.
      truffle test
    2. 2.
      Output from successful tests
TestMetaCoin
✔ testInitialBalanceUsingDeployedContract
✔ testInitialBalanceWithNewMetaCoin
Contract: MetaCoin
✔ should put 10000 MetaCoin in the first account
✔ should call a function that depends on a linked library
✔ should send coin correctly (52ms)ode
  1. 4.
    Compile your contracts
    1. 1.
      truffle compile
Compiling your contracts...
===========================
> Compiling ./contracts/ConvertLib.sol
> Compiling ./contracts/MetaCoin.sol
> Artifacts written to /workspace/truffle-tutorial/build/contracts
> Compiled successfully using:
- solc: 0.8.13+commit.abaa5c0e.Emscripten.clang
  1. 5.
    Configure your Truffle project
    1. 1.
      npm i @truffle/hdwallet-provider
    2. 2.
      Using your StealthTest URL, update truffle-config.js
      1. 1.
        * will match any network ID
      2. 2.
        Use any private key provided by StealthTest and remove the 0x prefix.
      3. 3.
        Note: Each StealthTest environment comes with 5 pre-funded wallets, but please never use these on mainnet. You may also quickly fund your own wallet with our Faucet but please never share your private key with us or anyone you don't know.
    3. 3.
      Example truffle-config.js below
const HDWalletProvider = require("@truffle/hdwallet-provider");
const privateKey = "996491130195a3dea4d3bad7dc252606cf77087ee82e905d9ba6d1d1f3f4cc9b"; // Remove 0x prefix
module.exports = {
networks: {
development: {
provider: function() {
return new HDWalletProvider(privateKey, "https://18d22e6b-3d6f-4d6a-9ca3-32c135ee98a4.ethereum.staging-42.nameless.io");
},
network_id: "*",
},
},
compilers: {
solc: {
version: "0.8.13",
}
}
};
  1. 6.
    Deploy your contracts
    1. 1.
      truffle migrate
    2. 2.
      Contract addresses will be output in the console.
Starting migrations...
======================
> Network name: 'development'
> Network id: 148073
> Block gas limit: 30000000 (0x1c9c380)
1_deploy_contracts.js
=====================
Replacing 'ConvertLib'
----------------------
> transaction hash: 0xb87152784dc8c9a1e6727ab9b0cde8abc9d6be55dc2151ff928dd614a3bb29d2
> Blocks: 0 Seconds: 4
> contract address: 0x253fADFDbc3356B85332FFcBFdD148DefF854f30
> block number: 491
> block timestamp: 1683048102
> account: 0xe2E6Eb5BcCBC10Fe7cE002E4b91D709250139b8F
> balance: 904625697166532776746648320380374280103671755200316906058.258445736816203202
> gas used: 157568 (0x26780)
> gas price: 2.500000007 gwei
> value sent: 0 ETH
> total cost: 0.000393920001102976 ETH
Linking
-------
* Contract: MetaCoin <--> Library: ConvertLib (at address: 0x253fADFDbc3356B85332FFcBFdD148DefF854f30)
Replacing 'MetaCoin'
--------------------
> transaction hash: 0x6e99bb6ea33efa49b5b1a357d321e115aa4b2ba27513795e66eaa6c5b78bdadf
> Blocks: 0 Seconds: 8
> contract address: 0x33ef020f130c5dC023b85340D4826e56aC069762
> block number: 492
> block timestamp: 1683048114
> account: 0xe2E6Eb5BcCBC10Fe7cE002E4b91D709250139b8F
> balance: 904625697166532776746648320380374280103671755200316906058.257404251813287044
> gas used: 416594 (0x65b52)
> gas price: 2.500000007 gwei
> value sent: 0 ETH
> total cost: 0.001041485002916158 ETH
> Saving artifacts
-------------------------------------
> Total cost: 0.001435405004019134 ETH
Summary
=======
> Total deployments: 2
> Final cost: 0.001435405004019134 ETH

And that's it! You're now testing in private using StealthTest!
💪