Skip to content

Commit

Permalink
Cleanup: Remove all references to ColonyStake model
Browse files Browse the repository at this point in the history
  • Loading branch information
jakubcolony committed Jun 17, 2024
1 parent d9e4323 commit ff6f5b6
Show file tree
Hide file tree
Showing 8 changed files with 6 additions and 441 deletions.
281 changes: 2 additions & 279 deletions src/graphql/generated.ts

Large diffs are not rendered by default.

23 changes: 0 additions & 23 deletions src/graphql/mutations/user.graphql

This file was deleted.

5 changes: 0 additions & 5 deletions src/graphql/queries/user.graphql

This file was deleted.

7 changes: 0 additions & 7 deletions src/handlers/motions/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -139,10 +139,3 @@ export const getMessageKey = (
): string => {
return `${transactionHash}${logIndex}`;
};

export const getColonyStakeId = (
userAddress: string,
colonyAddress: string,
): string => {
return `${userAddress}_${colonyAddress}`;
};
64 changes: 1 addition & 63 deletions src/handlers/motions/motionRewardClaimed/helpers.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,16 @@
import { BigNumber } from 'ethers';
import { mutate, query } from '~amplifyClient';
import { mutate } from '~amplifyClient';
import {
GetColonyStakeDocument,
GetColonyStakeQuery,
GetColonyStakeQueryVariables,
StakerReward,
UpdateColonyDocument,
UpdateColonyMutation,
UpdateColonyMutationVariables,
UpdateColonyStakeDocument,
UpdateColonyStakeMutation,
UpdateColonyStakeMutationVariables,
UpdateUserStakeDocument,
UpdateUserStakeMutation,
UpdateUserStakeMutationVariables,
UserMotionStakes,
} from '~graphql';
import { getColonyFromDB, output } from '~utils';
import { getUserStakeDatabaseId } from '~utils/stakes';

import { getColonyStakeId } from '../helpers';

export const getUpdatedStakerRewards = (
stakerRewards: StakerReward[],
stakerAddress: string,
Expand Down Expand Up @@ -106,42 +96,8 @@ export const updateColonyUnclaimedStakes = async (
*/
export const reclaimUserStake = async (
userAddress: string,
colonyAddress: string,
reclaimAmount: BigNumber,
motionTransactionHash: string,
): Promise<void> => {
const colonyStakeId = getColonyStakeId(userAddress, colonyAddress);
const { data } =
(await query<GetColonyStakeQuery, GetColonyStakeQueryVariables>(
GetColonyStakeDocument,
{
colonyStakeId,
},
)) ?? {};

if (!data?.getColonyStake) {
output(
`Could not find user stake for user ${userAddress} in colony ${colonyAddress}. This is a bug and should be investigated.`,
);
return;
}

const totalAmount = data.getColonyStake.totalAmount;
let updatedAmount = BigNumber.from(totalAmount).sub(reclaimAmount);

// Should never be negative, but just in case.
if (updatedAmount.isNegative()) {
updatedAmount = BigNumber.from(0);
}

await mutate<UpdateColonyStakeMutation, UpdateColonyStakeMutationVariables>(
UpdateColonyStakeDocument,
{
colonyStakeId,
totalAmount: updatedAmount.toString(),
},
);

// Update user stake status
await mutate<UpdateUserStakeMutation, UpdateUserStakeMutationVariables>(
UpdateUserStakeDocument,
Expand All @@ -153,21 +109,3 @@ export const reclaimUserStake = async (
},
);
};

export const getUserMotionStake = (
usersStakes: UserMotionStakes[],
userAddress: string,
): BigNumber => {
const userStakes = usersStakes.find(({ address }) => address === userAddress);

if (!userStakes) {
output(
`Could not find the stakes for user ${userAddress}. This is a bug and should be investigated.`,
);
return BigNumber.from(0);
}

return BigNumber.from(userStakes.stakes.raw.yay).add(
userStakes.stakes.raw.nay,
);
};
11 changes: 2 additions & 9 deletions src/handlers/motions/motionRewardClaimed/motionRewardClaimed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import {
updateColonyUnclaimedStakes,
reclaimUserStake,
getUpdatedStakerRewards,
getUserMotionStake,
} from './helpers';
import { ColonyMotion } from '~graphql';
import { ExtensionEventListener } from '~eventListeners';
Expand Down Expand Up @@ -41,9 +40,8 @@ export const handleMotionRewardClaimed: EventHandler = async (
const claimedMotion = await getMotionFromDB(motionDatabaseId);

if (claimedMotion) {
const { stakerRewards, usersStakes } = claimedMotion;
const { stakerRewards } = claimedMotion;

const userStake = getUserMotionStake(usersStakes, staker);
const updatedStakerRewards = getUpdatedStakerRewards(stakerRewards, staker);

const newMotionMessages = [
Expand All @@ -66,11 +64,6 @@ export const handleMotionRewardClaimed: EventHandler = async (
motionDatabaseId,
updatedStakerRewards,
);
await reclaimUserStake(
staker,
colonyAddress,
userStake,
updatedMotionData.transactionHash,
);
await reclaimUserStake(staker, updatedMotionData.transactionHash);
}
};
53 changes: 1 addition & 52 deletions src/handlers/motions/motionStaked/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,31 +2,22 @@ import { BigNumber, constants } from 'ethers';
import { MotionVote, MotionEvents } from '~types';
import {
ColonyMotion,
CreateColonyStakeDocument,
CreateColonyStakeMutation,
CreateColonyStakeMutationVariables,
CreateMotionMessageInput,
CreateUserStakeDocument,
CreateUserStakeMutation,
CreateUserStakeMutationVariables,
GetColonyStakeDocument,
GetColonyStakeQuery,
GetColonyStakeQueryVariables,
GetUserStakeDocument,
GetUserStakeQuery,
GetUserStakeQueryVariables,
MotionStakes,
UpdateColonyStakeDocument,
UpdateColonyStakeMutation,
UpdateColonyStakeMutationVariables,
UpdateUserStakeDocument,
UpdateUserStakeMutation,
UpdateUserStakeMutationVariables,
UserMotionStakes,
} from '~graphql';
import { mutate, query } from '~amplifyClient';
import { getUserStakeDatabaseId } from '~utils/stakes';
import { getMotionSide, getColonyStakeId } from '../helpers';
import { getMotionSide } from '../helpers';

export const getRequiredStake = (
skillRep: BigNumber,
Expand Down Expand Up @@ -297,48 +288,6 @@ export const getUpdatedMessages = ({
return updatedMessages;
};

/**
* If it's the first time a user has staked in a colony, we create a Colony Stake record for the user,
* else we update the amount they've currently got staked in the colony.
*/
export const updateUserColonyStake = async (
userAddress: string,
colonyAddress: string,
stakeAmount: BigNumber,
): Promise<void> => {
const colonyStakeId = getColonyStakeId(userAddress, colonyAddress);
const { data } =
(await query<GetColonyStakeQuery, GetColonyStakeQueryVariables>(
GetColonyStakeDocument,
{
colonyStakeId,
},
)) ?? {};

if (data?.getColonyStake) {
const { totalAmount } = data?.getColonyStake;
const updatedTotal = BigNumber.from(totalAmount).add(stakeAmount);

await mutate<UpdateColonyStakeMutation, UpdateColonyStakeMutationVariables>(
UpdateColonyStakeDocument,
{
totalAmount: updatedTotal.toString(),
colonyStakeId,
},
);
} else {
await mutate<CreateColonyStakeMutation, CreateColonyStakeMutationVariables>(
CreateColonyStakeDocument,
{
totalAmount: stakeAmount.toString(),
colonyStakeId,
colonyAddress,
userAddress,
},
);
}
};

/**
* Function to update the user stake following staking on a motion
*/
Expand Down
3 changes: 0 additions & 3 deletions src/handlers/motions/motionStaked/motionStaked.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import {
getUpdatedMessages,
updateMotionInDB,
getMessageKey,
updateUserColonyStake,
updateUserStake,
} from '../helpers';

Expand All @@ -29,8 +28,6 @@ export const handleMotionStaked: EventHandler = async (
} = event;
const { colonyAddress } = listener as ExtensionEventListener;

await updateUserColonyStake(staker, colonyAddress, amount);

const votingClient = await getVotingClient(colonyAddress);

if (!votingClient) {
Expand Down

0 comments on commit ff6f5b6

Please sign in to comment.