|
| 1 | +import Web3 from 'web3' |
| 2 | +import { EventData } from 'web3-eth-contract' |
| 3 | +import lotteryConfig from '../build/contracts/Lottery.json' |
| 4 | +import emitter from './emitter' |
| 5 | + |
| 6 | +type NetworkSettings = Record<string, { address: string }> |
| 7 | + |
| 8 | +const projectUrl = process.env['NEXT_PUBLIC_PROJECT_URL'] |
| 9 | +const networkId = process.env['NEXT_PUBLIC_NETWORK_ID'] |
| 10 | + |
| 11 | +const networkSettings = lotteryConfig.networks as NetworkSettings |
| 12 | + |
| 13 | +const CONTRACT_ABI = lotteryConfig.abi as unknown as AbiItem |
| 14 | +const CONTRACT_ADDRESS = networkSettings[networkId ?? 5777].address |
| 15 | + |
| 16 | +export const web3 = new Web3(Web3.givenProvider ?? projectUrl) |
| 17 | + |
| 18 | +export const lotteryContract = new web3.eth.Contract( |
| 19 | + CONTRACT_ABI, |
| 20 | + CONTRACT_ADDRESS, |
| 21 | +) |
| 22 | + |
| 23 | +lotteryContract.events.WinnerPicked((error: Error, event: EventData) => { |
| 24 | + if (error) { |
| 25 | + emitter.emit('error-picking-winner', error) |
| 26 | + } else { |
| 27 | + emitter.emit('winner-picked', event) |
| 28 | + } |
| 29 | +}) |
| 30 | + |
| 31 | +export function getContractAddress(): string { |
| 32 | + return CONTRACT_ADDRESS |
| 33 | +} |
| 34 | + |
| 35 | +export async function requestAccounts(): Promise<string[]> { |
| 36 | + return web3.eth.requestAccounts() |
| 37 | +} |
| 38 | + |
| 39 | +export async function getBalance(address: string): Promise<string> { |
| 40 | + const balanceWei = await web3.eth.getBalance(address) |
| 41 | + return web3.utils.fromWei(balanceWei) |
| 42 | +} |
| 43 | + |
| 44 | +export async function getContractBalance(): Promise<string> { |
| 45 | + return getBalance(CONTRACT_ADDRESS) |
| 46 | +} |
| 47 | + |
| 48 | +export async function getContractOwner(): Promise<string> { |
| 49 | + return lotteryContract.methods.owner().call() |
| 50 | +} |
| 51 | + |
| 52 | +export async function enterLottery( |
| 53 | + from: string, |
| 54 | + ether: string, |
| 55 | +): Promise<string> { |
| 56 | + return lotteryContract.methods.enter().send({ |
| 57 | + from, |
| 58 | + value: web3.utils.toWei(ether), |
| 59 | + }) |
| 60 | +} |
| 61 | + |
| 62 | +export async function getPlayers(from: string): Promise<string[]> { |
| 63 | + if (from) { |
| 64 | + return lotteryContract.methods.getPlayers().call({ from }) |
| 65 | + } |
| 66 | + |
| 67 | + return [] |
| 68 | +} |
| 69 | + |
| 70 | +export async function numPlayers(from: string): Promise<number> { |
| 71 | + const players = await getPlayers(from) |
| 72 | + return players.length |
| 73 | +} |
| 74 | + |
| 75 | +export async function pickWinner(from: string): Promise<void> { |
| 76 | + if (from) { |
| 77 | + return lotteryContract.methods.pickWinner().send({ from }) |
| 78 | + } |
| 79 | +} |
| 80 | + |
| 81 | +export async function getContractDetails(address: string) { |
| 82 | + const [balance, players, owner, contractBalance] = await Promise.all([ |
| 83 | + getBalance(address), |
| 84 | + getPlayers(address), |
| 85 | + getContractOwner(), |
| 86 | + getContractBalance(), |
| 87 | + ]) |
| 88 | + |
| 89 | + const participants = players.length |
| 90 | + const hasEntered = players.includes(address) |
| 91 | + const isManager = owner === address |
| 92 | + |
| 93 | + return { |
| 94 | + owner, |
| 95 | + players, |
| 96 | + balance, |
| 97 | + isManager, |
| 98 | + hasEntered, |
| 99 | + participants, |
| 100 | + contractBalance, |
| 101 | + } |
| 102 | +} |
0 commit comments