Skip to content

Commit

Permalink
Fix: consider the extra 1 Wei for networkFee
Browse files Browse the repository at this point in the history
  • Loading branch information
dit7ya committed Apr 8, 2022
1 parent 364f914 commit b9b156d
Showing 1 changed file with 11 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -119,19 +119,26 @@ const supRenderAvatar = (address: Address, item: ItemDataType<AnyUser>) => (
<UserAvatar address={address} user={item} size="xs" notSet={false} />
);

// NOTE: The equation to calculate totalToPay is as following (in Wei)
// totalToPay = (receivedAmount + 1) * (feeInverse / (feeInverse -1))
// The network adds 1 wei extra fee after the percentage calculation
// For more info check out
// https://github.com/JoinColony/colonyNetwork/blob/806e4d5750dc3a6b9fa80f6e007773b28327c90f/contracts/colony/ColonyFunding.sol#L656

export const calculateFee = (
receivedAmount: string, // amount that the recipient finally receives
feeInverse: string,
decimals: number,
): { feesInWei: string; totalToPay: string } => {
const amountInWei = moveDecimal(receivedAmount, decimals);
const totalToPay = bigNumberify(amountInWei)
const totalToPayInWei = bigNumberify(amountInWei)
.add(1)
.mul(feeInverse)
.div(bigNumberify(feeInverse).sub(1)); // this formula is easily derivable
const feesInWei = totalToPay.sub(amountInWei);
.div(bigNumberify(feeInverse).sub(1));
const feesInWei = totalToPayInWei.sub(amountInWei);
return {
feesInWei: feesInWei.toString(),
totalToPay: moveDecimal(totalToPay, -1 * decimals),
totalToPay: moveDecimal(totalToPayInWei, -1 * decimals),
}; // NOTE: seems like moveDecimal does not have strict typing
};

Expand Down

0 comments on commit b9b156d

Please sign in to comment.