Skip to content

Commit

Permalink
Merge pull request #274 from eLearningDAO/litigation-transactions
Browse files Browse the repository at this point in the history
Transactions in litigation
  • Loading branch information
huzaifa-99 committed Jan 20, 2023
2 parents 24dc8ff + d338c10 commit 34527f4
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 9 deletions.
11 changes: 11 additions & 0 deletions app/web-frontend/src/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,18 @@ const API_BASE_URL = 'https://pocre-api.herokuapp.com/v1/';

const POCRE_WALLET_ADDRESS = 'addr_test1qr0nvz3xurstmkj3h3a32knxsgpzvz4g8z3lvhhya9ffzh74uhu2hd3kjx8v9p906g4sejyj3w7q76zqwsgt4w9drfnsp8jhz7'; // preview testnet address || IMPORTANT: dont make real transactions

// charges in ADA
const CHARGES = {
CREATION: {
PUBLISHING_ON_IPFS: 9,
FINALIZING_ON_CHAIN: 1,
},
RECOGNITION_ACCEPT: 5,
LITIGATION: {
START: 10,
VOTE: 1,
REDEEM: 5,
},
};

const TRANSACTION_PURPOSES = {
Expand All @@ -16,6 +22,11 @@ const TRANSACTION_PURPOSES = {
FINALIZING_ON_CHAIN: 'CREATION_FINALIZED',
},
RECOGNITION_ACCEPT: 'RECOGNITION_ACCEPT',
LITIGATION: {
START: 'LITIGATION_START',
VOTE: 'LITIGATION_VOTE',
REDEEM: 'LITIGATED_ITEM_REDEEM',
},
};

const IPFS_BASE_URL = 'https://gateway.pinata.cloud/ipfs/';
Expand Down
28 changes: 23 additions & 5 deletions app/web-frontend/src/pages/Litigations/Create/useCreate.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import {
import useSuggestions from 'hooks/useSuggestions';
import { useState } from 'react';
import authUser from 'utils/helpers/authUser';
import { CHARGES, TRANSACTION_PURPOSES } from 'config';
import { transactADAToPOCRE } from 'utils/helpers/wallet';

// get auth user
const user = authUser.getUser();
Expand Down Expand Up @@ -38,6 +40,20 @@ const useCreate = () => {
isLoading: isCreatingLitigation,
} = useMutation({
mutationFn: async (litigationBody = {}) => {
// make transaction
const txHash = await transactADAToPOCRE({
amountADA: CHARGES.LITIGATION.START,
purposeDesc: TRANSACTION_PURPOSES.LITIGATION.START,
walletName: authUser.getUser()?.selectedWallet,
metaData: {
claimed_entity: litigationBody.material ? 'MATERIAL' : 'CREATION',
creation_id: litigationBody.creation,
material_id: litigationBody.material,
},
});

if (!txHash) throw new Error('Failed to make transaction');

// make a new litigation
const response = await Litigation.create({
litigation_title: litigationBody.title.trim(),
Expand All @@ -49,11 +65,13 @@ const useCreate = () => {
});

// get data about recognized judges
response.recognitions = await Promise.all(response.recognitions.map(async (recognitionId) => {
const recognition = await Recognition.getById(recognitionId);
recognition.recognition_for = await User.getById(recognition.recognition_for);
return recognition;
}));
response.recognitions = await Promise.all(response.recognitions.map(
async (recognitionId) => {
const recognition = await Recognition.getById(recognitionId);
recognition.recognition_for = await User.getById(recognition.recognition_for);
return recognition;
},
));

setNewLitigation(response);

Expand Down
18 changes: 18 additions & 0 deletions app/web-frontend/src/pages/Litigations/Details/useDetails.jsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import { useMutation, useQuery, useQueryClient } from '@tanstack/react-query';
import { Decision, Litigation } from 'api/requests';
import { CHARGES, TRANSACTION_PURPOSES } from 'config';
import moment from 'moment';
import { useState } from 'react';
import statusTypes from 'utils/constants/statusTypes';
import authUser from 'utils/helpers/authUser';
import { transactADAToPOCRE } from 'utils/helpers/wallet';

const user = authUser.getUser();

Expand Down Expand Up @@ -127,6 +129,22 @@ const useDetails = () => {
// check if vote is to be updated
if (voteStatus === litigation.voteStatus) return;

// make transaction
const txHash = await transactADAToPOCRE({
amountADA: CHARGES.LITIGATION.VOTE,
purposeDesc: TRANSACTION_PURPOSES.LITIGATION.VOTE,
walletName: authUser.getUser()?.selectedWallet,
metaData: {
claimed_entity: litigation.material ? 'MATERIAL' : 'CREATION',
creation_id: litigation.creation_id,
material_id: litigation.material_id,
vote: voteStatus,
author_id: user?.user_id,
},
});

if (!txHash) throw new Error('Failed to make transaction');

const updatedDecisions = await (async () => {
const decisions = [...litigation.decisions];

Expand Down
23 changes: 20 additions & 3 deletions app/web-frontend/src/pages/Litigations/Home/useHome.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import moment from 'moment';
import { useState } from 'react';
import statusTypes from 'utils/constants/statusTypes';
import authUser from 'utils/helpers/authUser';
import { transactADAToPOCRE } from 'utils/helpers/wallet';
import { CHARGES, TRANSACTION_PURPOSES } from 'config';

const user = authUser.getUser();

Expand Down Expand Up @@ -190,16 +192,31 @@ const useHome = () => {
reset: resetTransferOwnershipStatus,
} = useMutation({
mutationFn: async (id) => {
// make api call to edit the litigation
await Litigation.update(id, { ownership_transferred: true });

// update litigation
const updatedLitigations = { ...litigations };
const foundLitigation = updatedLitigations?.closed?.find(
(x) => x.litigation_id === id,
);
foundLitigation.ownership_transferred = true;

// make transaction
const txHash = await transactADAToPOCRE({
amountADA: CHARGES.LITIGATION.REDEEM,
purposeDesc: TRANSACTION_PURPOSES.LITIGATION.REDEEM,
walletName: authUser.getUser()?.selectedWallet,
metaData: {
redeemed_by: user?.user_id,
litigation_id: foundLitigation.litigation_id,
creation_id: foundLitigation.creation_id,
material_id: foundLitigation.material_id,
},
});

if (!txHash) throw new Error('Failed to make transaction');

// make api call to edit the litigation
await Litigation.update(id, { ownership_transferred: true });

// update queries
queryClient.cancelQueries({ queryKey: ['litigations'] });
queryClient.setQueryData(['litigations'], () => ({ ...updatedLitigations }));
Expand Down
6 changes: 5 additions & 1 deletion app/web-frontend/src/utils/helpers/wallet.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,12 +58,16 @@ const transactADAToPOCRE = async ({
// submit transaction
return await wallet.submitTx(signedTx);
} catch (error) {
const errorMessage = error?.message;
const errorMessage = error?.message?.toLowerCase();

if (error === 'null not allowed in metadata') {
throw new Error('Invalid transaction details');
}

if (errorMessage.includes('an error occurred during build: utxo balance insufficient')) {
throw new Error('Insufficient balance');
}

if (errorMessage.includes('"code":-3')) {
throw new Error('Refused to make transaction');
}
Expand Down

0 comments on commit 34527f4

Please sign in to comment.