Lib for interacting with the ethlotto smart contract
npm install --save ethlottolib
import React, { Component } from 'react'
import Contract from 'ethlottolib'
class Example extends Component {
state = {status: ''}
connect = async () => {
await Contract.connect(statusCallback=>{
this.setState({status: statusCallback.status})
});
this.setState({status: 'connected'})
}
render() {
return (
<div>
<button onClick={this.connect}>Connect to Ethereum</button>
<div>{this.state.status}</div>
</div>
}
}
See /example/App.js for more a more comprehensive example.
Check that the browser has an Etherium provider (for now Metamask on desktop browser and Trust Wallet on Android devices):
let hasProvider = await Contract.hasProvider();
/*
If false - help the user install metamask.
*/
Call the connect method to connect to Metamask, and fetch information from the contract.
await Contract.connect(statusCallback=>{
//an optional callback to provide status info
if(statusCallback.error) {
//deal with error
return;
}
this.setState({status: statusCallback.status})
})
Once the connection process is complete, the following information is available:
Contract.newEntry //Is this user a completey new user (true) or have they had previous entries
/*In the example come you'll see we desplay different information for existing entries*/
Contract.children //userIDs of the direct downline of the given node (depending on the referralID)
Contract.pot //The minimum pot amount for the next game
Contract.newGameReward //The reward for succesfully running the next draw (5% of the pot)
Contract.feeWei //the entry fee in WEI
Contract.fee //entry fee in ETH
Contract.balance //balance of the current metamask wallet
Contract.entriesUntilNextDraw //the number of entries before the next draw can be initiated
Contract.paidOut //the total amount paid out to members so far
//The following only applies if Contract.newEntry is true:
Contract.earned //the amount this user has earned that hasn't yet been withdrawn
Contract.referralID //refferal ID for this user to invite people with
Contract.accounts //A list of all the entries, as well as their value in earnings for this user
The following methods are also available:
//assuming the context of within an async function:
let success = await Contract.enter() //creates a new entry. Returns true if succesful
let success = await Contract.withdrawEarnings() //withdraws earnings if earnings > 0 and this is not a new entry
let success = await Contract.runNextDraw()
//attempts to run the next draw - if Contract.entriesUntilNextDraw==0 and this is an existing member
In order to connect with the smart contract an Ethereum provider is needed. In this case, the providers from Metamask (desktop) and Trustwallet (mobile - android) are used.
In the case of Desktop - check if Metamask exists. If not - direct the user to downloading metamask. If it exists - show a button to connect to Ethereum etc.
In the case of mobile you don't know if a user has trust wallet or not (It's a separate App, not a chrome plugin), so explain to the user that Trust wallet is required, and then provide a deep link into the DAPP browser of trustwallet (eg. https://link.trustwallet.com/open_url?url=https://wallet.ethlotto.club?u=x ). Trust wallet will either take the user through the download process, or take the user to the wallet DAPP. Best practice seems to be providing a 'connect to Ethereum' button, but in this case the user has explicitly asked to be taken to trust wallet already - so from a UX pov. it seems more efficient to just connect as the user hits't the site.
(Still need to figure out licensing - but for now, feel free to use the lib, as long as the contract addresses remain the same.)