Fund an asset crowdsale

Now, that our "hello world" application has created a deposit account (i.e. token address) and provided the terms for the asset crowdsale, investors can fund the purchase of the coffee machine. Once the asset crowdsale is finished, and investors own "shares" in the coffee machine, the revenue generated by this asset is provided to investors in the form dividend issuance.

Our "hello world" application will decompose the funding in two asynchronous functions: contribute() and fundCoffee() . The first relates to the investment in the asset. The second executes the funding process end to end, including the execution of setOperator() createAsset() and the issuance of dividends on the asset. It outputs our first "hello world" application result.

For full detail on fundCoffee() function see example.

Contribute to an asset crowdsale

To contribute to a crowdsale the investor calls asynchronous function contribute (account, amount) which parses the investor account and contribution amount. The application checks via the API if the crowdsale is finalised for the assetID

crowdsaleFinalized = await api.crowdsaleFinalized(assetID);

If crowdsale is not finalised:

Then, approveBurn(account) function takes the investor address as its parameter. It then sends burn approval to the deployed MyBitToken contract from that investor's address.

await Network.approveBurn(account);

Then, the application calls the fundAsset() function to finally contribute to the asset crowdsale and pass the following object:

await Network.fundAsset({
assetID: assetID,
amount: amount,
address: account});

Once contributed, the console confirms contribution and amount:

console.log('Contributed ', amount);

Else, if crowdsale is finalised it prints:

console.log('Crowdsale already finished!');

Fund asset and issue dividends

This fundCoffee() function provides a live example end to end of an investment crowdsale in our coffee machine asset by two investors. The console prints out each stage of the execution with the associated result. It provides a series of get functions connecting to MyBit network to obtain the state of funding progress, investors' accounts and operators accounts before and after dividend distribution.

Examples:

Set operator

operatorID = await setOperator();
console.log('Operator ID: ', operatorID);

Start crowdsale

var response = await startCrowdsale();

Get time left to fund in the crowdsale

var timeleft = await Network.getFundingTimeLeft(assetID);
console.log('Time left: ', timeleft);

Get crwodsale funding goal

var fundingGoal = await Network.getFundingGoal(assetID);
console.log('Funding goal: ', fundingGoal);

Two investors contribute to asset crowdsale

await contribute(accounts[3], 30000000000000000);
await contribute(accounts[4], 40000000000000000);

Operator address balance before investments

console.log('Operator ether before: ', await web3.eth.getBalance(
operatorAddress));

Operator address balance after investments

console.log('Operator ether after: ', await web3.eth.getBalance(
operatorAddress));

Issue Dividends

One of the most important asynchronous calls relates to issuance of dividends to investors from the operator address:

await Network.issueDividends(assetID, operatorAddress, 10000000000000000);
console.log('Dividends Issued...');

And the subsequent confirmation of dividends received by taking the balance from investors' accounts:

console.log('Investor 1 ether after: ', await web3.eth.getBalance(
accounts[3]));
console.log('Investor 2 ether after: ', await web3.eth.getBalance(
accounts[4]));