Skip to content

Commit

Permalink
fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
HananINouman committed Sep 9, 2024
1 parent 8c8d283 commit ad86567
Show file tree
Hide file tree
Showing 10 changed files with 415 additions and 88 deletions.
9 changes: 5 additions & 4 deletions src/ajv.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import Ajv, { type ErrorObject } from 'ajv';
import { parseUnits } from 'ethers';
import { RewardsSplitPayload, SplitRecipient } from './types';
import { DEFAULT_RETROACTIVE_FUNDING_REWARDS_ONLY_SPLIT } from './constants';
import { RewardsSplitPayload, SplitRecipient, TotalSplitPayload } from './types';
import { DEFAULT_RETROACTIVE_FUNDING_REWARDS_ONLY_SPLIT, DEFAULT_RETROACTIVE_FUNDING_TOTAL_SPLIT } from './constants';

const validDepositAmounts = (data: boolean, deposits: string[]): boolean => {
let sum = 0;
Expand Down Expand Up @@ -30,13 +30,14 @@ const validDepositAmounts = (data: boolean, deposits: string[]): boolean => {

const validateSplitRecipients = (
_: boolean,
data: RewardsSplitPayload,
data: RewardsSplitPayload | TotalSplitPayload,
): boolean => {
const splitPercentage = data.splitRecipients.reduce(
(acc: number, curr: SplitRecipient) => acc + curr.percentAllocation,
0,
);
return splitPercentage + (data.ObolRAFSplit || DEFAULT_RETROACTIVE_FUNDING_REWARDS_ONLY_SPLIT) === 100;
const ObolRAFSplitParam = data.ObolRAFSplit || ('principalRecipient' in data ? DEFAULT_RETROACTIVE_FUNDING_REWARDS_ONLY_SPLIT : DEFAULT_RETROACTIVE_FUNDING_TOTAL_SPLIT)
return splitPercentage + ObolRAFSplitParam === 100;
};

export function validatePayload(
Expand Down
2 changes: 2 additions & 0 deletions src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -159,3 +159,5 @@ export const CHAIN_CONFIGURATION = {
};

export const DEFAULT_RETROACTIVE_FUNDING_REWARDS_ONLY_SPLIT= 1

export const DEFAULT_RETROACTIVE_FUNDING_TOTAL_SPLIT = 0.1;
108 changes: 103 additions & 5 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {
AVAILABLE_SPLITTER_CHAINS,
CHAIN_CONFIGURATION,
DEFAULT_RETROACTIVE_FUNDING_REWARDS_ONLY_SPLIT,
DEFAULT_RETROACTIVE_FUNDING_TOTAL_SPLIT,
} from './constants.js';
import { ConflictError } from './errors.js';
import {
Expand All @@ -25,17 +26,20 @@ import {
type ClusterPayload,
type OperatorPayload,
type SplitterReturnedType,
TotalSplitPayload,
} from './types.js';
import { clusterConfigOrDefinitionHash } from './verification/common.js';
import { validatePayload } from './ajv.js';
import {
definitionSchema,
operatorPayloadSchema,
rewardsSplitterPayloadSchema,
totalSplitterPayloadSchema,
} from './schema.js';
import {
deploySplitterContract,
formatSplitRecipients,
handleDeployRewardsSplitter,
handleDeployOWRAndSplitter,
predictSplitterAddress,
} from './splitHelpers.js';
import { isContractAvailable } from './utils.js';
Expand Down Expand Up @@ -124,7 +128,6 @@ export class Client extends Base {
ObolRAFSplit = DEFAULT_RETROACTIVE_FUNDING_REWARDS_ONLY_SPLIT,
distributorFee = 0,
controllerAddress = ZeroAddress,

}: RewardsSplitPayload): Promise<SplitterReturnedType> {
// This method doesnt require T&C signature
if (!this.signer) {
Expand All @@ -150,7 +153,6 @@ export class Client extends Base {
);
}

// Double check the deployed addresses (not sure if needed)
const checkSplitMainAddress = await isContractAvailable(
CHAIN_CONFIGURATION[this.chainId].SPLITMAIN_ADDRESS,
this.signer.provider as Provider,
Expand Down Expand Up @@ -187,19 +189,25 @@ export class Client extends Base {
const { accounts, percentAllocations } = formatSplitRecipients(
copiedSplitRecipients,
);

const predictedSplitterAddress = await predictSplitterAddress({
signer: this.signer,
accounts,
percentAllocations,
chainId: this.chainId,
distributorFee
distributorFee,
controllerAddress
});


const isSplitterDeployed = await isContractAvailable(
predictedSplitterAddress,
this.signer.provider as Provider,
);


const { withdrawalAddress, feeRecipientAddress } =
await handleDeployRewardsSplitter({
await handleDeployOWRAndSplitter({
signer: this.signer,
isSplitterDeployed: !!isSplitterDeployed,
predictedSplitterAddress,
Expand All @@ -215,6 +223,96 @@ export class Client extends Base {
return { withdrawalAddress, feeRecipientAddress };
}

/**
* Deploys Splitter Proxy.
* @param {TotalSplitPayload} totalSplitPayload - Data needed to deploy splitter if it doesnt exist.
* @returns {Promise<SplitterReturnedType>} splitter address as withdrawal address and splitter as fee recipient too
*/
// add the example reference
async createObolTotalSplit({
splitRecipients,
ObolRAFSplit = DEFAULT_RETROACTIVE_FUNDING_TOTAL_SPLIT,
distributorFee = 0,
controllerAddress = ZeroAddress,

}: TotalSplitPayload): Promise<SplitterReturnedType> {
// This method doesnt require T&C signature
if (!this.signer) {
throw new Error('Signer is required in createObolTotalSplit');
}

validatePayload(
{
splitRecipients,
ObolRAFSplit,
distributorFee,
controllerAddress
},
totalSplitterPayloadSchema,
);

// Check if we allow splitters on this chainId
if (!AVAILABLE_SPLITTER_CHAINS.includes(this.chainId)) {
throw new Error(
`Splitter configuration is not supported on ${this.chainId} chain`,
);
}

const checkSplitMainAddress = await isContractAvailable(
CHAIN_CONFIGURATION[this.chainId].SPLITMAIN_ADDRESS,
this.signer.provider as Provider,
)

if (
!checkSplitMainAddress
) {
throw new Error(
'Something isn not working as expected, check this issue with obol-sdk team',
);
}

const retroActiveFundingRecipient = {
account: CHAIN_CONFIGURATION[this.chainId].RETROACTIVE_FUNDING_ADDRESS,
percentAllocation: ObolRAFSplit,
};

const copiedSplitRecipients = [...splitRecipients];
copiedSplitRecipients.push(retroActiveFundingRecipient);

const { accounts, percentAllocations } = formatSplitRecipients(
copiedSplitRecipients,
);
const predictedSplitterAddress = await predictSplitterAddress({
signer: this.signer,
accounts,
percentAllocations,
chainId: this.chainId,
distributorFee,
controllerAddress
});

const isSplitterDeployed = await isContractAvailable(
predictedSplitterAddress,
this.signer.provider as Provider,
);

if (!isSplitterDeployed) {
const splitterAddress =
await deploySplitterContract({
signer: this.signer,
accounts,
percentAllocations,
chainId: this.chainId,
distributorFee,
controllerAddress
});
return { withdrawalAddress: splitterAddress, feeRecipientAddress: splitterAddress };

}

return { withdrawalAddress: predictedSplitterAddress, feeRecipientAddress: predictedSplitterAddress };
}

/**
* Creates a cluster definition which contains cluster configuration.
* @param {ClusterPayload} newCluster - The new unique cluster.
Expand Down
35 changes: 24 additions & 11 deletions src/schema.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { DEFAULT_RETROACTIVE_FUNDING_REWARDS_ONLY_SPLIT, DEFAULT_RETROACTIVE_FUNDING_TOTAL_SPLIT } from "./constants";

export const operatorPayloadSchema = {
type: 'object',
properties: {
Expand Down Expand Up @@ -63,7 +65,7 @@ export const definitionSchema = {
required: ['name', 'operators', 'validators'],
};

export const rewardsSplitterPayloadSchema = {
export const totalSplitterPayloadSchema = {
type: 'object',
properties: {
splitRecipients: {
Expand All @@ -82,17 +84,9 @@ export const rewardsSplitterPayloadSchema = {
required: ['account', 'percentAllocation'],
},
},
principalRecipient: {
type: 'string',
minLength: 42,
maxLength: 42,
},
validatorsSize: {
type: 'number',
},
ObolRAFSplit: {
type: 'number',
minimum: 1
minimum: DEFAULT_RETROACTIVE_FUNDING_TOTAL_SPLIT
},
distributorFee: {
type: "number",
Expand All @@ -106,5 +100,24 @@ export const rewardsSplitterPayloadSchema = {
validateSplitRecipients: true,

},
required: ['splitRecipients', 'principalRecipient', 'validatorsSize'],
required: ['splitRecipients'],
}

export const rewardsSplitterPayloadSchema = {
...totalSplitterPayloadSchema,
properties: {
...totalSplitterPayloadSchema.properties,
ObolRAFSplit: {
type: 'number',
minimum: DEFAULT_RETROACTIVE_FUNDING_REWARDS_ONLY_SPLIT
},
validatorsSize: {
type: 'number',
},
principalRecipient: {
type: 'string',
pattern: "^0x[a-fA-F0-9]{40}$"
}
},
required: ['splitRecipients', 'principalRecipient', 'validatorsSize'],
}
Loading

0 comments on commit ad86567

Please sign in to comment.