Skip to content

Commit

Permalink
Merge pull request #262 from JoinColony/feat/2642-simple-payment-moti…
Browse files Browse the repository at this point in the history
…on-amount-show-exclude-network-fee

Feat: Update action when simple payment motion finalized to include networkFee and amountLessFee
  • Loading branch information
iamsamgibbs authored Aug 7, 2024
2 parents dba678a + e9a1e4c commit 268f816
Show file tree
Hide file tree
Showing 4 changed files with 112 additions and 16 deletions.
39 changes: 23 additions & 16 deletions src/graphql/generated.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,21 +71,6 @@ export type BridgeCreateBankAccountReturn = {
success?: Maybe<Scalars['Boolean']>;
};

export type BridgeDrain = {
__typename?: 'BridgeDrain';
amount: Scalars['String'];
createdAt: Scalars['String'];
currency: Scalars['String'];
id: Scalars['String'];
receipt: BridgeDrainReceipt;
state: Scalars['String'];
};

export type BridgeDrainReceipt = {
__typename?: 'BridgeDrainReceipt';
url: Scalars['String'];
};

export type BridgeIbanAccountInput = {
account_number: Scalars['String'];
bic: Scalars['String'];
Expand Down Expand Up @@ -131,6 +116,16 @@ export type BridgeXyzBankAccount = {
usAccount?: Maybe<BridgeUsBankAccount>;
};

export type BridgeXyzDrain = {
__typename?: 'BridgeXYZDrain';
amount?: Maybe<Scalars['String']>;
created_at?: Maybe<Scalars['String']>;
deposit_tx_hash?: Maybe<Scalars['String']>;
id?: Maybe<Scalars['String']>;
receipt?: Maybe<DrainReceipt>;
state?: Maybe<Scalars['String']>;
};

export type BridgeXyzMutationAccountInput = {
account_number: Scalars['String'];
routing_number: Scalars['String'];
Expand Down Expand Up @@ -187,6 +182,7 @@ export type BridgeXyzQueryInput = {

export type BridgeXyzQueryReturn = {
__typename?: 'BridgeXYZQueryReturn';
drains?: Maybe<Array<Maybe<BridgeXyzDrain>>>;
success?: Maybe<Scalars['Boolean']>;
transactionFee?: Maybe<Scalars['String']>;
};
Expand Down Expand Up @@ -2028,6 +2024,12 @@ export type DomainMetadataChangelogInput = {
transactionHash: Scalars['String'];
};

export type DrainReceipt = {
__typename?: 'DrainReceipt';
destination_currency?: Maybe<Scalars['String']>;
url?: Maybe<Scalars['String']>;
};

export type Expenditure = {
__typename?: 'Expenditure';
actions?: Maybe<ModelColonyActionConnection>;
Expand Down Expand Up @@ -5696,7 +5698,6 @@ export type ProfileMetadataInput = {
/** Root query type */
export type Query = {
__typename?: 'Query';
bridgeGetDrainsHistory?: Maybe<Array<BridgeDrain>>;
/** Fetch from the Bridge XYZ API */
bridgeXYZQuery?: Maybe<BridgeXyzQueryReturn>;
getActionByExpenditureId?: Maybe<ModelColonyActionConnection>;
Expand Down Expand Up @@ -9925,6 +9926,9 @@ export type GetColonyActionByMotionIdQuery = {
__typename?: 'ColonyAction';
id: string;
colonyDecisionId?: string | null;
amount?: string | null;
networkFee?: string | null;
type: ColonyActionType;
pendingDomainMetadata?: {
__typename?: 'DomainMetadata';
name: string;
Expand Down Expand Up @@ -11115,6 +11119,9 @@ export const GetColonyActionByMotionIdDocument = gql`
...ColonyMetadata
}
colonyDecisionId
amount
networkFee
type
}
}
}
Expand Down
3 changes: 3 additions & 0 deletions src/graphql/queries/motions.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ query GetColonyActionByMotionId($motionId: ID!) {
...ColonyMetadata
}
colonyDecisionId
amount
networkFee
type
}
}
}
Expand Down
80 changes: 80 additions & 0 deletions src/handlers/motions/motionFinalized/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {
} from '~utils';
import { query, mutate } from '~amplifyClient';
import {
ColonyActionType,
ColonyMetadata,
ColonyMotion,
CreateDomainMetadataDocument,
Expand All @@ -28,11 +29,15 @@ import {
GetDomainMetadataQuery,
GetDomainMetadataQueryVariables,
StakerReward,
UpdateColonyActionDocument,
UpdateColonyActionMutation,
UpdateColonyActionMutationVariables,
UpdateColonyDocument,
UpdateColonyMetadataDocument,
UpdateDomainMetadataDocument,
} from '~graphql';
import { parseAction } from '../motionCreated/helpers';
import { getAmountLessFee, getNetworkInverseFee } from '~utils/networkFee';

export const getStakerReward = async (
motionId: string,
Expand Down Expand Up @@ -353,3 +358,78 @@ export const updateColonyUnclaimedStakes = async (
});
}
};

/*
* When a Simple Payment motion is finalized, we want to update the action in the database
* to include the networkFee and the amount excluding the fee at the time of finalization
*/
export const updateAmountToExcludeNetworkFee = async (
action: string,
colonyAddress: string,
finalizedMotion: ColonyMotion,
): Promise<void> => {
const colonyClient = await getCachedColonyClient(colonyAddress);
const oneTxPaymentClient =
(await colonyClient?.getExtensionClient(Extension.OneTxPayment)) ?? null;

const parsedAction = parseAction(action, {
colonyClient,
oneTxPaymentClient,
});

if (!parsedAction) {
return;
}

if (parsedAction.name !== ColonyOperations.MakePaymentFundedFromDomain) {
return;
}

const { data } =
(await query<
GetColonyActionByMotionIdQuery,
GetColonyActionByMotionIdQueryVariables
>(GetColonyActionByMotionIdDocument, {
motionId: finalizedMotion.id,
})) ?? {};

const colonyAction = data?.getColonyActionByMotionId?.items[0];

if (!colonyAction) {
return;
}

if (colonyAction.type === ColonyActionType.PaymentMotion) {
if (colonyAction.networkFee) {
return;
}

if (!colonyAction.amount) {
return;
}

const networkInverseFee = await getNetworkInverseFee();
if (!networkInverseFee) {
output(
'Network inverse fee not found. This is a bug and should be investigated.',
);
return;
}

const amountWithFee = colonyAction.amount;

const amountLessFee = getAmountLessFee(amountWithFee, networkInverseFee);
const networkFee = BigNumber.from(amountWithFee).sub(amountLessFee);

await mutate<
UpdateColonyActionMutation,
UpdateColonyActionMutationVariables
>(UpdateColonyActionDocument, {
input: {
id: colonyAction.id,
amount: amountLessFee.toString(),
networkFee: networkFee.toString(),
},
});
}
};
6 changes: 6 additions & 0 deletions src/handlers/motions/motionFinalized/motionFinalized.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {
getStakerReward,
linkPendingMetadata,
updateColonyUnclaimedStakes,
updateAmountToExcludeNetworkFee,
} from './helpers';

export const handleMotionFinalized: EventHandler = async (event, listener) => {
Expand Down Expand Up @@ -57,6 +58,11 @@ export const handleMotionFinalized: EventHandler = async (event, listener) => {

if (yayWon) {
await linkPendingMetadata(action, colonyAddress, finalizedMotion);
await updateAmountToExcludeNetworkFee(
action,
colonyAddress,
finalizedMotion,
);
}

const updatedStakerRewards = await Promise.all(
Expand Down

0 comments on commit 268f816

Please sign in to comment.