When the token contract is deployed, it assigns the total balance to the contract owner, as we showed in the previous section. However, during the token sale, we only want to sell a portion of the total number of tokens, keeping the remainder under ownership of the project owner. To achieve this, we want to transfer a portion of the tokens to the address of the token sale contract.
When we wrote the sample test file for our token sale contract in testing our token sale contract, one of the first steps was to provision the contract with the number of tokens defined by tokensToSell. We defined this as 500,000, or 50% of the total supply. We will provision the same proportion of tokens to our publicly-deployed token sale contract.
First, get a reference to our deployed token sale contract:
truffle(rinkeby)> PacktTokenSale.deployed().then(function(saleInstance) { tokenSaleInstance = saleInstance; })
undefined
We can then get the deployed address using tokenSaleInstance.address.
Before transferring the tokens, ensure that the account is unlocked as described in the Configuring Truffle to Work with Geth section. We can then transfer the tokens by submitting a transaction with the following command:
truffle(rinkeby)> tokenInstance.transfer(tokenSaleInstance.address, 500000, { from: web3.eth.accounts[0] })
This should take about 15 to 30 seconds to complete, after which a transaction receipt will be returned in the console. From here, we can check that the balances have been updated:
truffle(rinkeby)> tokenInstance.balanceOf("<your_account_address>").then(function(balance) { ownerBalance = balance; })
Undefined
truffle(rinkeby)> ownerBalance.toNumber()
500000
truffle(rinkeby)> tokenInstance.balanceOf(tokenSaleInstance.address).then(function(balance) { saleBalance = balance; })
Undefined
truffle(rinkeby)> saleBalance.toNumber()
500000
Our token sale contract is now provisioned and ready to sell its tokens!