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

feat: add getIncentivesByAddress method to fetch incentives for a given address #77

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import {
type ClusterValidator,
type ETH_ADDRESS,
type OWRTranches,
type Incentives,
} from './types.js';
import { clusterConfigOrDefinitionHash } from './verification/common.js';
import { validatePayload } from './ajv.js';
Expand Down Expand Up @@ -519,4 +520,19 @@ export class Client extends Base {
);
return lock;
}

/**
* @param address - Operator address
* @returns {Promise<Incentives>} The matched incentives from DB
* @throws On not found if address not found.
*/
async getIncentivesByAddress(address: string): Promise<Incentives> {
const incentives: Incentives = await this.request(
`/${DEFAULT_BASE_VERSION}/address/incentives/${address}`,
{
method: 'GET',
},
);
return incentives;
}
}
20 changes: 20 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,26 @@ export type ClusterLock = {
node_signatures?: string[];
};

/**
* Incentives
*/
export type Incentives = {
/** Operator Address. */
operator_address: string;

/** The amount the recipient is entitled to. */
amount: string;

/** The recipient’s index in the Merkle tree. */
index: number;

/** The Merkle proof (an array of hashes) generated for the recipient. */
merkle_proof: string[];

/** The MerkleDistributor contract address. */
contract_address: string;
};

/**
* String expected to be Ethereum Address
*/
Expand Down
73 changes: 73 additions & 0 deletions test/methods.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -539,3 +539,76 @@ describe('createObolTotalSplit', () => {
});
});
});

describe('getIncentivesByAddress', () => {
let mockSigner: ethers.Signer | ethers.Wallet | undefined,
clientInstance: Client;
beforeAll(() => {
jest
.spyOn(utils, 'isContractAvailable')
.mockImplementation(async () => await Promise.resolve(true));
jest
.spyOn(splitsHelpers, 'predictSplitterAddress')
.mockImplementation(
async () => await Promise.resolve('0xPredictedAddress'),
);
jest
.spyOn(splitsHelpers, 'deploySplitterContract')
.mockImplementation(
async () => await Promise.resolve('0xSplitterAddress'),
);

const mnemonic = ethers.Wallet.createRandom().mnemonic?.phrase ?? '';
const privateKey = ethers.Wallet.fromPhrase(mnemonic).privateKey;
const provider = new JsonRpcProvider(
'https://ethereum-holesky.publicnode.com',
);
const wallet = new ethers.Wallet(privateKey, provider);
mockSigner = wallet.connect(provider);

clientInstance = new Client(
{ baseUrl: 'https://obol-api-dev.gcp.obol.tech', chainId: 17000 },
mockSigner,
);
});

test('should return incentives for a valid address', async () => {
const mockAddress = '0x1234567890abcdef1234567890abcdef12345678';
const mockIncentives = {
operator_address: '0x8c00157cae72c4ed6a1f8bfb60205601f0252e26',
amount: '100',
index: 1,
merkle_proof: ['hash1', 'hash2'],
contract_address: '0xContract',
};

clientInstance['request'] = jest
.fn()
.mockReturnValue(Promise.resolve(mockIncentives));

const incentives = await clientInstance.getIncentivesByAddress(mockAddress);
expect(incentives).toEqual(mockIncentives);
});

test('should throw an error if address is not found', async () => {
const invalidAddress = '0x0000000000000000000000000000000000000000';
clientInstance['request'] = jest
.fn()
.mockRejectedValue(new Error('Not found'));

await expect(
clientInstance.getIncentivesByAddress(invalidAddress),
).rejects.toThrow('Not found');
});

test('should throw an error if request fails', async () => {
const mockAddress = '0x1234567890abcdef1234567890abcdef12345678';
clientInstance['request'] = jest
.fn()
.mockRejectedValue(new Error('Network error'));

await expect(
clientInstance.getIncentivesByAddress(mockAddress),
).rejects.toThrow('Network error');
});
});
Loading