diff --git a/Frontend/src/components/cardLotteryToken/cardLotteryToken.component.html b/Frontend/src/components/cardLotteryToken/cardLotteryToken.component.html index cccc922..947eca7 100644 --- a/Frontend/src/components/cardLotteryToken/cardLotteryToken.component.html +++ b/Frontend/src/components/cardLotteryToken/cardLotteryToken.component.html @@ -10,10 +10,6 @@

Token Balance: {{ tokenBalance ? tokenBalance : "0" }} LTO

- diff --git a/Frontend/src/components/ownerInteractions/ownerInteractions.component.html b/Frontend/src/components/ownerInteractions/ownerInteractions.component.html index 9589009..147175c 100644 --- a/Frontend/src/components/ownerInteractions/ownerInteractions.component.html +++ b/Frontend/src/components/ownerInteractions/ownerInteractions.component.html @@ -1 +1,44 @@ -

OWNER INTERACTIONS!

\ No newline at end of file +
+
+
+
Owner Information
+
+ {{ + walletAddress + }} +
+ + +

Bets Open: {{ betsOpen }}

+

Are you the owner: {{ isOwner }}

+
+

Open Bets

+ +
+ +
+

Closing Time: {{ betsClosingTime }}

+
+
+
diff --git a/Frontend/src/components/ownerInteractions/ownerInteractions.component.ts b/Frontend/src/components/ownerInteractions/ownerInteractions.component.ts index 80c5116..6ea3af9 100644 --- a/Frontend/src/components/ownerInteractions/ownerInteractions.component.ts +++ b/Frontend/src/components/ownerInteractions/ownerInteractions.component.ts @@ -1,12 +1,101 @@ import { Component } from '@angular/core'; +import { BigNumber, Contract, ethers } from 'ethers'; +import lotteryJson from '../../assets/Lottery.json'; + +const LOTTERY_CONTRACT_ADDRESS = '0x6354dA7FbCc203D49cEC938727A9A97a4c80bDE7'; @Component({ selector: 'owner-interactions', templateUrl: './ownerInteractions.component.html', - providers: [] + providers: [], }) export class OwnerInteractions { + walletSigner: ethers.providers.JsonRpcSigner | undefined; + provider: ethers.providers.Web3Provider | undefined; + lotteryContract: Contract | undefined; + lotteryAddress: string | undefined; + betsOpen: boolean | undefined; + betsClosingTime: number | undefined; + contractOwner: string | undefined; + walletAddress: string | undefined; + isOwner: boolean | undefined; constructor() { + this.provider = new ethers.providers.Web3Provider(window.ethereum, 'any'); + } + + async ngOnInit() { + this.lotteryAddress = LOTTERY_CONTRACT_ADDRESS; + this.lotteryContract = new Contract( + this.lotteryAddress, + lotteryJson.abi, + this.walletSigner ?? this.provider + ); + + this.connectWallet(); + this.checkOwner(); + // Are bets open? + this.lotteryContract['betsOpen']().then((isOpen: boolean) => { + this.betsOpen = isOpen; + }); + + // Bets closing time + this.lotteryContract['betsClosingTime']().then((closingTime: number) => { + const betsClosingTimeStr = ethers.utils.formatEther(closingTime); + this.betsClosingTime = parseFloat(betsClosingTimeStr); + }); + } + + async connectWallet(): Promise { + if (typeof window.ethereum !== 'undefined') { + try { + await window.ethereum + .request({ method: 'eth_requestAccounts' }) + .then((address: any) => { + this.walletAddress = address[0]; + this.walletSigner = this.provider!.getSigner(this.walletAddress); + }) + .catch((error: any) => { + if (error.code === 4001) { + console.log('User rejected'); + } + return console.log('we found our error', error); + }); + } catch {} + } + } + + async isConnected() { + const accounts = await window.ethereum.request({ method: 'eth_accounts' }); + if (accounts.length) { + this.walletAddress = accounts[0]; + this.walletSigner = this.provider!.getSigner(accounts[0]); + } else { + this.connectWallet(); + } + } + + async openBets(value: string): Promise { + if (this.lotteryContract) { + const currentBlock = this.provider?.getBlock('latest'); + if (currentBlock) { + this.lotteryContract['openBets']().then( + (await currentBlock).timestamp + Number(value) + ); + } + } + } + + checkOwner() { + if (this.lotteryContract) { + this.lotteryContract['owner']().then((ownerAddres: string) => { + this.contractOwner = ownerAddres; + }); + if (this.walletAddress === this.contractOwner) { + this.isOwner = true; + } else { + this.isOwner = false; + } + } } }