From 331e1ba09c37edacfc39a5a22476d90e56008fba Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Veljko=20Tekelerovi=C4=87?= Date: Thu, 19 Jan 2023 20:06:03 +0100 Subject: [PATCH] Added trasnaction confirmation logic and loader/spinner on the UI side. Closes #59 --- frontend/src/lib/Instructions.svelte | 4 +- frontend/src/lib/Loader.svelte | 73 ++++++++++++ frontend/src/lib/Utilities.ts | 37 ++++-- frontend/src/routes/+page.svelte | 3 +- frontend/src/routes/newquestion/+page.svelte | 68 +++++++---- .../src/routes/questions/[slug]/+page.svelte | 112 +++++++++++------- hardhat.config.ts | 2 +- 7 files changed, 219 insertions(+), 80 deletions(-) create mode 100644 frontend/src/lib/Loader.svelte diff --git a/frontend/src/lib/Instructions.svelte b/frontend/src/lib/Instructions.svelte index 43627e3..a989cdf 100644 --- a/frontend/src/lib/Instructions.svelte +++ b/frontend/src/lib/Instructions.svelte @@ -1,6 +1,6 @@ @@ -9,7 +9,7 @@ -

За више детаља, погледајте упутства за приступање платформи.

+

За више детаља, погледајте видео упутства за приступање платформи...

\ No newline at end of file diff --git a/frontend/src/lib/Utilities.ts b/frontend/src/lib/Utilities.ts index ef6f0f2..ca325a7 100644 --- a/frontend/src/lib/Utilities.ts +++ b/frontend/src/lib/Utilities.ts @@ -72,7 +72,6 @@ class PlatformContract { try { this.platformContract = Provider.fabricateContract(); await this.platformContract.register(); - console.log("New user registered, informing store."); // update the store on the fly PlatformStore.registered(true); return Promise.resolve(true); @@ -88,8 +87,15 @@ class PlatformContract { async addNewQuestion(questionTitle: string, labels: string[]): Promise { try { this.platformContract = Provider.fabricateContract(); - await this.platformContract.addQuestion(questionTitle, labels); - return Promise.resolve(true); + const addQResponse = await this.platformContract.addQuestion(questionTitle, labels); + + // await for response receipt and examine confirmations + const receipt = await addQResponse.wait() + // console.log(receipt); + if(receipt.status === 1) { // check if we're successfull + // console.log("Question added, hash: ", receipt.transactionHash); + return Promise.resolve(true); + } // otherwise, fallthrough and reject } catch (err) { console.log("Creating new question errored. Reason:"); console.log(err); @@ -104,9 +110,9 @@ class PlatformContract { try { this.platformContract = Provider.fabricateContract(); const qInfoArray = await this.platformContract.getAllQuestions(); - // console.log(qInfoArray); - // const questionInfoArray = response[0]; + // for(const qInfo of qInfoArray) { + // construct new questioninfo objects returnSet.push(new QuestionInfoOutput( qInfo.id, qInfo.question, @@ -142,9 +148,14 @@ class PlatformContract { async vote(questionID: number, score: number): Promise { try { this.platformContract = Provider.fabricateContract(); - await this.platformContract.vote(questionID, score); - console.log(`Successfull vote for ${questionID}, score: ${score}`); - return Promise.resolve(true); + const voteResult = await this.platformContract.vote(questionID, score); + // + //wait for confirmations + const receipt = await voteResult.wait(); + if(receipt.status === 1) { + console.log("Vote successfull. Receipt hash: ", receipt.transactionHash); + return Promise.resolve(true); + } } catch (e) { console.log("Error occured: ", e.reason); } @@ -155,8 +166,14 @@ class PlatformContract { async provideExtra(questionID: number, extraScore: number): Promise { try { this.platformContract = Provider.fabricateContract(); - await this.platformContract.voteExtra(questionID, extraScore); - return Promise.resolve(true); + const voteResult = await this.platformContract.voteExtra(questionID, extraScore); + + // wait for confirmations + const receipt = await voteResult.wait(); + if(receipt.status === 1) { + console.log("Vote successfull. Receipt hash: ", receipt.transactionHash); + return Promise.resolve(true); + } } catch (e) { console.log("Error during providing extra options"); } diff --git a/frontend/src/routes/+page.svelte b/frontend/src/routes/+page.svelte index 53dac31..06a5898 100644 --- a/frontend/src/routes/+page.svelte +++ b/frontend/src/routes/+page.svelte @@ -52,6 +52,7 @@ {#await Contract.isRegisteredUser() then success } {#if success} +

Број постављених питања: {$PlatformStore.totalQuestions}

{:else} + + +

Поставите Ваше питање

- - + - + - - + + {#if isLoading} + + {:else} + + {/if} + \ No newline at end of file diff --git a/frontend/src/routes/questions/[slug]/+page.svelte b/frontend/src/routes/questions/[slug]/+page.svelte index 6f73609..b28aa55 100644 --- a/frontend/src/routes/questions/[slug]/+page.svelte +++ b/frontend/src/routes/questions/[slug]/+page.svelte @@ -1,15 +1,24 @@ @@ -104,23 +123,26 @@ {/if} - - + + + {#if isLoading} + + {:else} {#if questionInfo.hasVoted} - Укупно гласова: {questionInfo.totalVoters} +

Укупно гласова: {questionInfo.totalVoters} ({totalVotePercentage}% корисникa платформе)

{:else} {/if} -
-
+ + + {/if} + \ No newline at end of file diff --git a/hardhat.config.ts b/hardhat.config.ts index 637d4fd..c149522 100644 --- a/hardhat.config.ts +++ b/hardhat.config.ts @@ -19,7 +19,7 @@ const config: HardhatUserConfig = { } } }, - defaultNetwork: "matic", //used for testing + defaultNetwork: "hardhat", //used for testing networks: { hardhat: {}, polygon_mumbai: {