LLC Crowdsale — Full Walkthrough with Truffle Console

TechJD
4 min readMar 21, 2022

This is part 2 of my series in which I show you how to tokenize an LLC in order to raise capital for your business.

The first part walks you through the frontend. Part 2 is for those interested in learning how to execute your contract functions on the backend (e.g. withdrawing funds from the crowdsale, whitelisting members, pausing the sale, etc.)

Tools used:

Truffle v5.4.33 (core: 5.4.33)Solidity - 0.6.2 (solc-js)Node v12.18.0Web3.js v1.5.3

First, you need to have node installed. Then you must install Truffle. Truffle is notoriously tricky to install because it’s not compatible with all versions of node.

With Homebrew for Mac it’s simple:

brew install node

But, sometimes the latest version won’t be compatible with Truffle. So instead, I would recommend following this tutorial to install the node version manager(nvm), which will allow you to download and manage several versions of node.

Once nvm is installed, you should be able to change versions with this command:

nvm use [version]

For example:

nvm use 12.18.0

Open VS Code or your favorite editor. Open a new terminal and then:

npm install truffle
truffle init

This will create a new truffle project, complete with a truffle-config.js file, a Migrations.Sol contract, and a deploy script ready to go.

Alternatively, you can clone my git repo for the project:

git clone https://github.com/thetechjd/zelda-gold-rupee.git

But if you’re building from scratch then there are a few other dependencies you need to install:

npm install @openzeppelin/contractsnpm install @truffle/hdwallet-provider

Before you launch the truffle console, run npm install one more time just to make sure all the modules you need are set up. You should see a “node_modules” folder in your explorer.

Next, you obviously need to add your Solidity contracts to the “contracts” folder. Again, if you’re building from scratch, just copy and paste each one into a new file, along with the deploy script.

In the truffle-config.js file, you need to set up an Infura account. In your Settings, select the dropdown and choose Rinkeby network. Then copy

Your mnemonic is the twelve word password for your Metamask wallet. Put it in your .env file like so:

MNEMONIC=much skull helmet visit drip orchard unable artist chimney net six same

Run:

truffle compile

Then migrate your contracts:

truffle migrate --network rinkeby

Start the console:

truffle console --network rinkeby

If done successfully, it should look like this:

truffle(development) >>

Check to see if your Rinkeby test account is linked:

accounts

In the next part, we will work directly with an instance of your deployed Solidity contract.

let token = await Token.deployed()

If not:

truffle deploy

You can then access the properties of your token:

token.address
token.name()
token.symbol()

Create the instances of your Solidity contracts:

let networkId = await web3.eth.net.getId()

You can do other cool things like check the balance of your account:

await web3.eth.getBalance(accounts[0])

Create an instance of your token contract:

TokenInstance = new web3.eth.Contract(Token.abi,Token.networks[networkId] && Token.networks[networkId].address);

If done successfully, Token.networks[networkId].address should return the same value as token.address.

Create an instance of your crowdsale:

TokenSaleInstance = new web3.eth.Contract(MyTokenSale.abi,MyTokenSale.networks[networkId] && MyTokenSale.networks[networkId].address);

If done correctly, the contract should show up in the console. You can look over it to view the functions you can access, the same way you would using something like Remix IDE.

For an example of a call function. Let’s call weiRaised:

let weiRaised = await TokenSaleInstance.methods.weiRaised().call();

Access the variable:

weiRaised

It should show how much the crowdsale contract was funded.

Now, let’s try a send function. This will cost us some gas, so make sure you’ve got some test ETH in your account. The crowdsale contract has a buyTokens function. Let’s try that one:

let transferAmount = <Amount you want to transfer>

transferAmount should be a small number if you don’t have that much ETH. Then:

await TokenSaleInstance.methods.buyTokens(this.accounts[0]).send({ from: this.accounts[0], value: Math.round(transferAmount * Math.pow(10, 18)) });

Note: Transactions are performed in wei. 1 ETH = 1*10¹⁸ wei

If the transaction was successful, you should see the transaction hash, the “from” wallet, the contract address, among a lot of other bits of info. Check Rinkeby Etherscan with the transaction hash to verify.

Lastly, check how many of your new token you have in your wallet:

let userTokens = await TokenInstance.methods.balanceOf(this.accounts[0]).call();

That’s it! You know know how to operate your crowdsale directly from Truffle Console! If the tutorial was still hard to follow, I’ve got a full walkthrough video on my channel here.

--

--

TechJD

Law, programming, and everything in-between! Coming up with fun coding projects with real-world application.