Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: use arbitrator fee only with consensus - read warn #23

Merged
merged 9 commits into from
Dec 7, 2022
Prev Previous commit
Next Next commit
fix: split calculation
  • Loading branch information
EmanuelCampos committed Dec 6, 2022
commit 0f6bf5c8a94371deda99c69bacc55690e4774839
5 changes: 3 additions & 2 deletions contracts/Unicrow.sol
Original file line number Diff line number Diff line change
Expand Up @@ -356,8 +356,8 @@ contract Unicrow is ReentrancyGuard, IUnicrow, Context {
/// @inheritdoc IUnicrow
function splitCalculation(
uint16[5] calldata currentSplit
) external pure override returns (uint16[4] memory) {
uint16[4] memory split;
) external pure override returns (uint16[5] memory) {
uint16[5] memory split;

uint16 calculatedArbitratorFee;

Expand Down Expand Up @@ -392,6 +392,7 @@ contract Unicrow is ReentrancyGuard, IUnicrow, Context {
unchecked {
split[WHO_SELLER] = currentSplit[WHO_SELLER] - split[WHO_PROTOCOL] - split[WHO_MARKETPLACE] - calculatedArbitratorFee;
split[WHO_BUYER] = currentSplit[WHO_BUYER];
split[WHO_ARBITRATOR] = calculatedArbitratorFee;
}

return split;
Expand Down
6 changes: 4 additions & 2 deletions contracts/UnicrowArbitrator.sol
Original file line number Diff line number Diff line change
Expand Up @@ -232,8 +232,8 @@ contract UnicrowArbitrator is IUnicrowArbitrator, Context, ReentrancyGuard {
*/
function arbitrationCalculation(
uint16[5] calldata currentSplit
) public pure returns (uint16[4] memory) {
uint16[4] memory split;
) public pure returns (uint16[5] memory) {
uint16[5] memory split;

uint16 calculatedSellerArbitratorFee;
uint16 calculatedBuyerArbitratorFee;
Expand All @@ -252,6 +252,8 @@ contract UnicrowArbitrator is IUnicrowArbitrator, Context, ReentrancyGuard {
* currentSplit[WHO_SELLER])
/ _100_PCT_IN_BIPS
);

split[WHO_ARBITRATOR] = calculatedBuyerArbitratorFee + calculatedSellerArbitratorFee;
}

// Protocol fee
Expand Down
35 changes: 10 additions & 25 deletions contracts/UnicrowClaim.sol
Original file line number Diff line number Diff line change
Expand Up @@ -99,16 +99,15 @@ contract UnicrowClaim is IUnicrowClaim, Context, ReentrancyGuard {
"0-006"
);

uint16[4] memory calculatedSplits = calculateSplits(
uint16[5] memory calculatedSplits = calculateSplits(
arbitratorData.arbitratorFee,
arbitratorData.arbitrated,
escrow
);

uint256[5] memory payments = calculatePayments(
escrow.amount,
calculatedSplits,
arbitratorData.arbitratorFee
calculatedSplits
);

address[5] memory addresses = [
Expand Down Expand Up @@ -154,7 +153,7 @@ contract UnicrowClaim is IUnicrowClaim, Context, ReentrancyGuard {
);

// Calculate final splits (in bips) from gross splits
uint16[4] memory calculatedSplits = calculateSplits(
uint16[5] memory calculatedSplits = calculateSplits(
arbitratorData.arbitratorFee,
arbitratorData.arbitrated,
escrow
Expand All @@ -163,8 +162,7 @@ contract UnicrowClaim is IUnicrowClaim, Context, ReentrancyGuard {
// Calculate amounts to be sent in the token
uint256[5] memory payments = calculatePayments(
escrow.amount,
calculatedSplits,
arbitratorData.arbitratorFee
calculatedSplits
);

// Prepare list of addresses for the withdrawals
Expand Down Expand Up @@ -210,8 +208,8 @@ contract UnicrowClaim is IUnicrowClaim, Context, ReentrancyGuard {
uint16 arbitratorFee,
bool arbitrated,
Escrow memory escrow
) internal view returns(uint16[4] memory) {
uint16[4] memory split;
) internal view returns(uint16[5] memory) {
uint16[5] memory split;

// The calculation will differ slightly based on whether the payment was decided by an arbitrator or not
if(arbitrated) {
Expand Down Expand Up @@ -246,9 +244,8 @@ contract UnicrowClaim is IUnicrowClaim, Context, ReentrancyGuard {
*/
function calculatePayments(
uint amount,
uint16[4] memory split,
uint16 arbitratorFee
) internal returns(uint256[5] memory) {
uint16[5] memory split
) internal pure returns(uint256[5] memory) {
uint256[5] memory payments;

// Multiply all the splits by the total amount
Expand All @@ -258,21 +255,9 @@ contract UnicrowClaim is IUnicrowClaim, Context, ReentrancyGuard {

// If the arbitrator decided the payment, they get their full fee
// in such case, buyer's or seller split was reduced in the calling function)
payments[WHO_ARBITRATOR] = (uint256(arbitratorFee) * amount) / _100_PCT_IN_BIPS;
payments[WHO_ARBITRATOR] = (uint256(split[WHO_ARBITRATOR]) * amount) / _100_PCT_IN_BIPS;

unchecked {
payments[WHO_PROTOCOL] = amount - payments[WHO_BUYER] - payments[WHO_SELLER] - payments[WHO_MARKETPLACE] - payments[WHO_ARBITRATOR];
}

// console all the payments
console.log("amount", amount);
console.log("payments[WHO_BUYER]", payments[WHO_BUYER]);
console.log("payments[WHO_SELLER]", payments[WHO_SELLER]);
console.log("payments[WHO_MARKETPLACE]", payments[WHO_MARKETPLACE]);
console.log("payments[WHO_ARBITRATOR]", payments[WHO_ARBITRATOR]);
console.log("payments[WHO_ARBITRATOR]", payments[WHO_ARBITRATOR]);

console.log("PAYMENTS[WHO_PROTOCOL]", payments[WHO_PROTOCOL]);
payments[WHO_PROTOCOL] = amount - payments[WHO_BUYER] - payments[WHO_SELLER] - payments[WHO_MARKETPLACE] - payments[WHO_ARBITRATOR];

return payments;
}
Expand Down
2 changes: 1 addition & 1 deletion contracts/interfaces/IUnicrow.sol
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ interface IUnicrow {
*/
function splitCalculation(
uint16[5] calldata currentSplit
) external returns(uint16[4] memory);
) external returns(uint16[5] memory);

/**
* @dev Get the escrow data (without arbitrator or settlement information)
Expand Down
4 changes: 2 additions & 2 deletions test/UnicrowArbitrator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -351,7 +351,7 @@ describe("UnicrowArbitrator", function () {

const escrowFee = await unicrowContract.protocolFee();

const expectedResult = [0, 10000 - 100 - escrowFee, 0, escrowFee];
const expectedResult = [0, 10000 - 100 - escrowFee, 0, escrowFee, 100];

expect(
await unicrowArbitratorContract.arbitrationCalculation(
Expand Down Expand Up @@ -379,7 +379,7 @@ describe("UnicrowArbitrator", function () {

const [b, s, m, c ] = split;

const expectedResult = [4950, 4950, 0, 0];
const expectedResult = [4950, 4950, 0, 0, 100];

expect(
await unicrowArbitratorContract.arbitrationCalculation(
Expand Down