@obolnetwork/obol-sdk 2.3.0 → 2.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,36 @@
1
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
2
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
3
+ return new (P || (P = Promise))(function (resolve, reject) {
4
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
5
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
6
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
7
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
8
+ });
9
+ };
10
+ import { Contract } from 'ethers';
11
+ import { MerkleDistributorABI } from './abi/MerkleDistributorWithDeadline';
12
+ import { getProvider } from './utils';
13
+ export const claimIncentivesFromMerkleDistributor = (incentivesData) => __awaiter(void 0, void 0, void 0, function* () {
14
+ try {
15
+ const contract = new Contract(incentivesData.contractAddress, MerkleDistributorABI.abi, incentivesData.signer);
16
+ const tx = yield contract.claim(BigInt(incentivesData.index), incentivesData.operatorAddress, BigInt(incentivesData.amount), incentivesData.merkleProof);
17
+ const receipt = yield tx.wait();
18
+ return { txHash: receipt.hash };
19
+ }
20
+ catch (error) {
21
+ console.log('Error claiming incentives:', error);
22
+ throw new Error(`Failed to claim incentives: ${error.message}`);
23
+ }
24
+ });
25
+ export const isClaimedFromMerkleDistributor = (chainId, contractAddress, index) => __awaiter(void 0, void 0, void 0, function* () {
26
+ try {
27
+ const provider = getProvider(chainId);
28
+ const contract = new Contract(contractAddress, MerkleDistributorABI.abi, provider);
29
+ const claimed = yield contract.isClaimed(BigInt(index));
30
+ return claimed;
31
+ }
32
+ catch (error) {
33
+ console.log('Error checking claim status:', error);
34
+ throw new Error(`Failed to check claim status: ${error.message}`);
35
+ }
36
+ });
@@ -17,6 +17,7 @@ import { validatePayload } from './ajv.js';
17
17
  import { definitionSchema, operatorPayloadSchema, rewardsSplitterPayloadSchema, totalSplitterPayloadSchema, } from './schema.js';
18
18
  import { deploySplitterContract, formatSplitRecipients, handleDeployOWRAndSplitter, predictSplitterAddress, getOWRTranches, } from './splitHelpers.js';
19
19
  import { isContractAvailable } from './utils.js';
20
+ import { Incentives } from './incentives.js';
20
21
  export * from './types.js';
21
22
  export * from './services.js';
22
23
  export * from './verification/signature-validator.js';
@@ -38,6 +39,7 @@ export class Client extends Base {
38
39
  constructor(config, signer) {
39
40
  super(config);
40
41
  this.signer = signer;
42
+ this.incentives = new Incentives(this.signer, this.chainId, this.request.bind(this));
41
43
  }
42
44
  /**
43
45
  * Accepts Obol terms and conditions to be able to create or update data.
@@ -353,17 +355,4 @@ export class Client extends Base {
353
355
  return lock;
354
356
  });
355
357
  }
356
- /**
357
- * @param address - Operator address
358
- * @returns {Promise<Incentives>} The matched incentives from DB
359
- * @throws On not found if address not found.
360
- */
361
- getIncentivesByAddress(address) {
362
- return __awaiter(this, void 0, void 0, function* () {
363
- const incentives = yield this.request(`/${DEFAULT_BASE_VERSION}/address/incentives/${address}`, {
364
- method: 'GET',
365
- });
366
- return incentives;
367
- });
368
- }
369
358
  }
@@ -0,0 +1,149 @@
1
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
2
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
3
+ return new (P || (P = Promise))(function (resolve, reject) {
4
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
5
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
6
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
7
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
8
+ });
9
+ };
10
+ var _a, _b;
11
+ import { ethers, JsonRpcProvider } from 'ethers';
12
+ import { Client } from '../src/index';
13
+ import * as utils from '../src/utils';
14
+ import * as incentivesHelpers from '../src/incentivesHalpers';
15
+ import { DEFAULT_BASE_VERSION } from '../src/constants';
16
+ const mnemonic = (_b = (_a = ethers.Wallet.createRandom().mnemonic) === null || _a === void 0 ? void 0 : _a.phrase) !== null && _b !== void 0 ? _b : '';
17
+ const privateKey = ethers.Wallet.fromPhrase(mnemonic).privateKey;
18
+ const provider = new JsonRpcProvider('https://ethereum-holesky.publicnode.com');
19
+ const wallet = new ethers.Wallet(privateKey, provider);
20
+ const mockSigner = wallet.connect(provider);
21
+ const baseUrl = 'https://obol-api-dev.gcp.obol.tech';
22
+ global.fetch = jest.fn();
23
+ describe('Client.incentives', () => {
24
+ let clientInstance;
25
+ const mockIncentivesData = {
26
+ contractAddress: '0x1234567890abcdef1234567890abcdef12345678',
27
+ index: 5,
28
+ operatorAddress: '0xabcdef1234567890abcdef1234567890abcdef12',
29
+ amount: 1000000000000000000,
30
+ merkleProof: [
31
+ '0x1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef',
32
+ '0xabcdef1234567890abcdef1234567890abcdef1234567890abcdef1234567890',
33
+ ],
34
+ };
35
+ beforeEach(() => {
36
+ jest.clearAllMocks();
37
+ clientInstance = new Client({ baseUrl, chainId: 17000 }, mockSigner);
38
+ global.fetch.mockReset();
39
+ });
40
+ test('claimIncentives should throw an error without signer', () => __awaiter(void 0, void 0, void 0, function* () {
41
+ const clientWithoutSigner = new Client({
42
+ baseUrl,
43
+ chainId: 17000,
44
+ });
45
+ yield expect(clientWithoutSigner.incentives.claimIncentives(mockIncentivesData)).rejects.toThrow('Signer is required in claimIncentives');
46
+ }));
47
+ test('claimIncentives should throw an error if contract is not available', () => __awaiter(void 0, void 0, void 0, function* () {
48
+ jest
49
+ .spyOn(utils, 'isContractAvailable')
50
+ .mockImplementation(() => __awaiter(void 0, void 0, void 0, function* () { return yield Promise.resolve(false); }));
51
+ yield expect(clientInstance.incentives.claimIncentives(mockIncentivesData)).rejects.toThrow(`Merkle Distributor contract is not available at address ${mockIncentivesData.contractAddress}`);
52
+ }));
53
+ test('claimIncentives should return txHash on successful claim', () => __awaiter(void 0, void 0, void 0, function* () {
54
+ const mockTxHash = '0x0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef';
55
+ jest
56
+ .spyOn(utils, 'isContractAvailable')
57
+ .mockImplementation(() => __awaiter(void 0, void 0, void 0, function* () { return yield Promise.resolve(true); }));
58
+ jest
59
+ .spyOn(incentivesHelpers, 'claimIncentivesFromMerkleDistributor')
60
+ .mockImplementation(() => __awaiter(void 0, void 0, void 0, function* () { return yield Promise.resolve({ txHash: mockTxHash }); }));
61
+ const result = yield clientInstance.incentives.claimIncentives(mockIncentivesData);
62
+ expect(result).toEqual({ txHash: mockTxHash });
63
+ expect(incentivesHelpers.claimIncentivesFromMerkleDistributor).toHaveBeenCalledWith({
64
+ signer: mockSigner,
65
+ contractAddress: mockIncentivesData.contractAddress,
66
+ index: mockIncentivesData.index,
67
+ operatorAddress: mockIncentivesData.operatorAddress,
68
+ amount: mockIncentivesData.amount,
69
+ merkleProof: mockIncentivesData.merkleProof,
70
+ });
71
+ }));
72
+ test('claimIncentives should throw an error if helper function fails', () => __awaiter(void 0, void 0, void 0, function* () {
73
+ jest
74
+ .spyOn(utils, 'isContractAvailable')
75
+ .mockImplementation(() => __awaiter(void 0, void 0, void 0, function* () { return yield Promise.resolve(true); }));
76
+ jest
77
+ .spyOn(incentivesHelpers, 'claimIncentivesFromMerkleDistributor')
78
+ .mockImplementation(() => __awaiter(void 0, void 0, void 0, function* () {
79
+ throw new Error('Helper function error');
80
+ }));
81
+ yield expect(clientInstance.incentives.claimIncentives(mockIncentivesData)).rejects.toThrow('Failed to claim incentives: Helper function error');
82
+ }));
83
+ test('incentives should be initialized with the same chainId as client', () => {
84
+ const customChainId = 5;
85
+ const clientWithCustomChain = new Client({ baseUrl, chainId: customChainId }, mockSigner);
86
+ expect(clientWithCustomChain.incentives.chainId).toBe(customChainId);
87
+ });
88
+ test('isClaimed should return true when incentive is claimed', () => __awaiter(void 0, void 0, void 0, function* () {
89
+ jest
90
+ .spyOn(incentivesHelpers, 'isClaimedFromMerkleDistributor')
91
+ .mockImplementation(() => __awaiter(void 0, void 0, void 0, function* () { return yield Promise.resolve(true); }));
92
+ const result = yield clientInstance.incentives.isClaimed(mockIncentivesData.contractAddress, mockIncentivesData.index);
93
+ expect(result).toBe(true);
94
+ expect(incentivesHelpers.isClaimedFromMerkleDistributor).toHaveBeenCalledWith(clientInstance.incentives.chainId, mockIncentivesData.contractAddress, mockIncentivesData.index);
95
+ }));
96
+ test('isClaimed should return false when incentive is not claimed', () => __awaiter(void 0, void 0, void 0, function* () {
97
+ jest
98
+ .spyOn(incentivesHelpers, 'isClaimedFromMerkleDistributor')
99
+ .mockImplementation(() => __awaiter(void 0, void 0, void 0, function* () { return yield Promise.resolve(false); }));
100
+ const result = yield clientInstance.incentives.isClaimed(mockIncentivesData.contractAddress, mockIncentivesData.index);
101
+ expect(result).toBe(false);
102
+ }));
103
+ test('isClaimed should throw an error if helper function fails', () => __awaiter(void 0, void 0, void 0, function* () {
104
+ jest
105
+ .spyOn(incentivesHelpers, 'isClaimedFromMerkleDistributor')
106
+ .mockImplementation(() => __awaiter(void 0, void 0, void 0, function* () {
107
+ throw new Error('Helper function error');
108
+ }));
109
+ yield expect(clientInstance.incentives.isClaimed(mockIncentivesData.contractAddress, mockIncentivesData.index)).rejects.toThrow('Helper function error');
110
+ }));
111
+ test('isClaimed should work with a client without signer', () => __awaiter(void 0, void 0, void 0, function* () {
112
+ // Create a client without a signer
113
+ const clientWithoutSigner = new Client({
114
+ baseUrl,
115
+ chainId: 17000,
116
+ });
117
+ jest
118
+ .spyOn(incentivesHelpers, 'isClaimedFromMerkleDistributor')
119
+ .mockImplementation(() => __awaiter(void 0, void 0, void 0, function* () { return yield Promise.resolve(true); }));
120
+ const result = yield clientWithoutSigner.incentives.isClaimed(mockIncentivesData.contractAddress, mockIncentivesData.index);
121
+ expect(result).toBe(true);
122
+ expect(incentivesHelpers.isClaimedFromMerkleDistributor).toHaveBeenCalledWith(clientWithoutSigner.incentives.chainId, mockIncentivesData.contractAddress, mockIncentivesData.index);
123
+ }));
124
+ test('getIncentivesByAddress should make the correct API request', () => __awaiter(void 0, void 0, void 0, function* () {
125
+ const mockAddress = '0x1234567890abcdef1234567890abcdef12345678';
126
+ const mockIncentives = {
127
+ operator_address: '0x8c00157cae72c4ed6a1f8bfb60205601f0252e26',
128
+ amount: '100',
129
+ index: 1,
130
+ merkle_proof: ['hash1', 'hash2'],
131
+ contract_address: '0xContract',
132
+ };
133
+ global.fetch.mockResolvedValueOnce({
134
+ ok: true,
135
+ status: 200,
136
+ json: () => __awaiter(void 0, void 0, void 0, function* () { return mockIncentives; }),
137
+ headers: new Headers(),
138
+ });
139
+ const result = yield clientInstance.incentives.getIncentivesByAddress(mockAddress);
140
+ expect(result).toEqual(mockIncentives);
141
+ expect(global.fetch).toHaveBeenCalledWith(`${baseUrl}/${DEFAULT_BASE_VERSION}/address/incentives/${mockAddress}`, expect.objectContaining({ method: 'GET' }));
142
+ }));
143
+ test('getIncentivesByAddress should handle API errors', () => __awaiter(void 0, void 0, void 0, function* () {
144
+ const mockAddress = '0x1234567890abcdef1234567890abcdef12345678';
145
+ global.fetch.mockRejectedValue(new Error('Network error'));
146
+ yield expect(clientInstance.incentives.getIncentivesByAddress(mockAddress)).rejects.toThrow();
147
+ expect(global.fetch).toHaveBeenCalled();
148
+ }));
149
+ });
@@ -386,47 +386,3 @@ describe('createObolTotalSplit', () => {
386
386
  });
387
387
  }));
388
388
  });
389
- describe('getIncentivesByAddress', () => {
390
- let clientInstance;
391
- beforeAll(() => {
392
- jest
393
- .spyOn(utils, 'isContractAvailable')
394
- .mockImplementation(() => __awaiter(void 0, void 0, void 0, function* () { return yield Promise.resolve(true); }));
395
- jest
396
- .spyOn(splitsHelpers, 'predictSplitterAddress')
397
- .mockImplementation(() => __awaiter(void 0, void 0, void 0, function* () { return yield Promise.resolve('0xPredictedAddress'); }));
398
- jest
399
- .spyOn(splitsHelpers, 'deploySplitterContract')
400
- .mockImplementation(() => __awaiter(void 0, void 0, void 0, function* () { return yield Promise.resolve('0xSplitterAddress'); }));
401
- clientInstance = new Client({ baseUrl: 'https://obol-api-dev.gcp.obol.tech', chainId: 17000 }, mockSigner);
402
- });
403
- test('should return incentives for a valid address', () => __awaiter(void 0, void 0, void 0, function* () {
404
- const mockAddress = '0x1234567890abcdef1234567890abcdef12345678';
405
- const mockIncentives = {
406
- operator_address: '0x8c00157cae72c4ed6a1f8bfb60205601f0252e26',
407
- amount: '100',
408
- index: 1,
409
- merkle_proof: ['hash1', 'hash2'],
410
- contract_address: '0xContract',
411
- };
412
- clientInstance['request'] = jest
413
- .fn()
414
- .mockReturnValue(Promise.resolve(mockIncentives));
415
- const incentives = yield clientInstance.getIncentivesByAddress(mockAddress);
416
- expect(incentives).toEqual(mockIncentives);
417
- }));
418
- test('should throw an error if address is not found', () => __awaiter(void 0, void 0, void 0, function* () {
419
- const invalidAddress = '0x0000000000000000000000000000000000000000';
420
- clientInstance['request'] = jest
421
- .fn()
422
- .mockRejectedValue(new Error('Not found'));
423
- yield expect(clientInstance.getIncentivesByAddress(invalidAddress)).rejects.toThrow('Not found');
424
- }));
425
- test('should throw an error if request fails', () => __awaiter(void 0, void 0, void 0, function* () {
426
- const mockAddress = '0x1234567890abcdef1234567890abcdef12345678';
427
- clientInstance['request'] = jest
428
- .fn()
429
- .mockRejectedValue(new Error('Network error'));
430
- yield expect(clientInstance.getIncentivesByAddress(mockAddress)).rejects.toThrow('Network error');
431
- }));
432
- });
@@ -0,0 +1,91 @@
1
+ export declare const MerkleDistributorABI: {
2
+ abi: readonly [{
3
+ readonly inputs: readonly [{
4
+ readonly internalType: "address";
5
+ readonly name: "token_";
6
+ readonly type: "address";
7
+ }, {
8
+ readonly internalType: "bytes32";
9
+ readonly name: "merkleRoot_";
10
+ readonly type: "bytes32";
11
+ }];
12
+ readonly stateMutability: "nonpayable";
13
+ readonly type: "constructor";
14
+ }, {
15
+ readonly anonymous: false;
16
+ readonly inputs: readonly [{
17
+ readonly indexed: false;
18
+ readonly internalType: "uint256";
19
+ readonly name: "index";
20
+ readonly type: "uint256";
21
+ }, {
22
+ readonly indexed: false;
23
+ readonly internalType: "address";
24
+ readonly name: "account";
25
+ readonly type: "address";
26
+ }, {
27
+ readonly indexed: false;
28
+ readonly internalType: "uint256";
29
+ readonly name: "amount";
30
+ readonly type: "uint256";
31
+ }];
32
+ readonly name: "Claimed";
33
+ readonly type: "event";
34
+ }, {
35
+ readonly inputs: readonly [{
36
+ readonly internalType: "uint256";
37
+ readonly name: "index";
38
+ readonly type: "uint256";
39
+ }, {
40
+ readonly internalType: "address";
41
+ readonly name: "account";
42
+ readonly type: "address";
43
+ }, {
44
+ readonly internalType: "uint256";
45
+ readonly name: "amount";
46
+ readonly type: "uint256";
47
+ }, {
48
+ readonly internalType: "bytes32[]";
49
+ readonly name: "merkleProof";
50
+ readonly type: "bytes32[]";
51
+ }];
52
+ readonly name: "claim";
53
+ readonly outputs: readonly [];
54
+ readonly stateMutability: "nonpayable";
55
+ readonly type: "function";
56
+ }, {
57
+ readonly inputs: readonly [{
58
+ readonly internalType: "uint256";
59
+ readonly name: "index";
60
+ readonly type: "uint256";
61
+ }];
62
+ readonly name: "isClaimed";
63
+ readonly outputs: readonly [{
64
+ readonly internalType: "bool";
65
+ readonly name: "";
66
+ readonly type: "bool";
67
+ }];
68
+ readonly stateMutability: "view";
69
+ readonly type: "function";
70
+ }, {
71
+ readonly inputs: readonly [];
72
+ readonly name: "merkleRoot";
73
+ readonly outputs: readonly [{
74
+ readonly internalType: "bytes32";
75
+ readonly name: "";
76
+ readonly type: "bytes32";
77
+ }];
78
+ readonly stateMutability: "view";
79
+ readonly type: "function";
80
+ }, {
81
+ readonly inputs: readonly [];
82
+ readonly name: "token";
83
+ readonly outputs: readonly [{
84
+ readonly internalType: "address";
85
+ readonly name: "";
86
+ readonly type: "address";
87
+ }];
88
+ readonly stateMutability: "view";
89
+ readonly type: "function";
90
+ }];
91
+ };
@@ -0,0 +1,49 @@
1
+ import { type Signer } from 'ethers';
2
+ import { type Incentives as IncentivesType, type ETH_ADDRESS } from './types';
3
+ export declare class Incentives {
4
+ private readonly signer;
5
+ chainId: number;
6
+ private readonly request;
7
+ constructor(signer: Signer | undefined, chainId: number, request: (endpoint: string, options?: RequestInit) => Promise<any>);
8
+ /**
9
+ * Claims obol incentives from a Merkle Distributor contract.
10
+ *
11
+ * @remarks
12
+ * **⚠️ Important:** If you're storing the private key in an `.env` file, ensure it is securely managed
13
+ * and not pushed to version control.
14
+ *
15
+ * @param {Object} incentivesData - The incentives data needed for claiming.
16
+ * @param {string} incentivesData.contractAddress - The address of the Merkle Distributor contract.
17
+ * @param {number} incentivesData.index - The index in the Merkle tree.
18
+ * @param {string} incentivesData.operatorAddress - The address of the operator.
19
+ * @param {number} incentivesData.amount - The amount to claim.
20
+ * @param {string[]} incentivesData.merkleProof - The Merkle proof.
21
+ * @returns {Promise<{ txHash: string }>} The transaction hash of the claim transaction.
22
+ * @throws Will throw an error if the contract is not available or the claim fails.
23
+ *
24
+ */
25
+ claimIncentives(incentivesData: {
26
+ contractAddress: ETH_ADDRESS;
27
+ index: number;
28
+ operatorAddress: ETH_ADDRESS;
29
+ amount: number;
30
+ merkleProof: string[];
31
+ }): Promise<{
32
+ txHash: string;
33
+ }>;
34
+ /**
35
+ * Read isClaimed.
36
+ *
37
+ * @param {ETH_ADDRESS} contractAddress - Address of the Merkle Distributor Contract
38
+ * @param {ETH_ADDRESS} index - operator index in merkle tree
39
+ * @returns {Promise<boolean>} true if incentives are already claime
40
+ *
41
+ */
42
+ isClaimed(contractAddress: ETH_ADDRESS, index: number): Promise<boolean>;
43
+ /**
44
+ * @param address - Operator address
45
+ * @returns {Promise<IncentivesType>} The matched incentives from DB
46
+ * @throws On not found if address not found.
47
+ */
48
+ getIncentivesByAddress(address: string): Promise<IncentivesType>;
49
+ }
@@ -0,0 +1,13 @@
1
+ import { type ETH_ADDRESS } from './types';
2
+ import { type Signer } from 'ethers';
3
+ export declare const claimIncentivesFromMerkleDistributor: (incentivesData: {
4
+ signer: Signer;
5
+ contractAddress: ETH_ADDRESS;
6
+ index: number;
7
+ operatorAddress: ETH_ADDRESS;
8
+ amount: number;
9
+ merkleProof: string[];
10
+ }) => Promise<{
11
+ txHash: string;
12
+ }>;
13
+ export declare const isClaimedFromMerkleDistributor: (chainId: number, contractAddress: ETH_ADDRESS, index: number) => Promise<boolean>;
@@ -1,6 +1,7 @@
1
1
  import { type Signer } from 'ethers';
2
2
  import { Base } from './base.js';
3
- import { type RewardsSplitPayload, type ClusterDefinition, type ClusterLock, type ClusterPayload, type OperatorPayload, type TotalSplitPayload, type ClusterValidator, type ETH_ADDRESS, type OWRTranches, type Incentives } from './types.js';
3
+ import { type RewardsSplitPayload, type ClusterDefinition, type ClusterLock, type ClusterPayload, type OperatorPayload, type TotalSplitPayload, type ClusterValidator, type ETH_ADDRESS, type OWRTranches } from './types.js';
4
+ import { Incentives } from './incentives.js';
4
5
  export * from './types.js';
5
6
  export * from './services.js';
6
7
  export * from './verification/signature-validator.js';
@@ -10,6 +11,7 @@ export * from './verification/common.js';
10
11
  */
11
12
  export declare class Client extends Base {
12
13
  private readonly signer;
14
+ incentives: Incentives;
13
15
  /**
14
16
  * @param config - Client configurations
15
17
  * @param config.baseUrl - obol-api url
@@ -112,10 +114,4 @@ export declare class Client extends Base {
112
114
  * [getObolClusterLock](https://github.com/ObolNetwork/obol-sdk-examples/blob/main/TS-Example/index.ts#L89)
113
115
  */
114
116
  getClusterLock(configHash: string): Promise<ClusterLock>;
115
- /**
116
- * @param address - Operator address
117
- * @returns {Promise<Incentives>} The matched incentives from DB
118
- * @throws On not found if address not found.
119
- */
120
- getIncentivesByAddress(address: string): Promise<Incentives>;
121
117
  }
@@ -0,0 +1 @@
1
+ export {};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@obolnetwork/obol-sdk",
3
- "version": "2.3.0",
3
+ "version": "2.4.0",
4
4
  "description": "A package for creating Distributed Validators using the Obol API.",
5
5
  "bugs": {
6
6
  "url": "https://github.com/obolnetwork/obol-sdk/issues"
@@ -15,7 +15,7 @@
15
15
  "compile": "tsc -b ./tsconfig.cjs.json ./tsconfig.esm.json ./tsconfig.types.json",
16
16
  "build:clean": "rm -rf ./dist",
17
17
  "build": "npm-run-all build:clean compile",
18
- "test": "jest ./test/methods.test.ts",
18
+ "test": "jest ./test/*.test.ts",
19
19
  "generate-typedoc": "typedoc",
20
20
  "npm:publish": "npm publish --tag latest",
21
21
  "release": "release-it",
@@ -0,0 +1,70 @@
1
+ export const MerkleDistributorABI = {
2
+ abi: [
3
+ {
4
+ inputs: [
5
+ { internalType: 'address', name: 'token_', type: 'address' },
6
+ { internalType: 'bytes32', name: 'merkleRoot_', type: 'bytes32' },
7
+ ],
8
+ stateMutability: 'nonpayable',
9
+ type: 'constructor',
10
+ },
11
+ {
12
+ anonymous: false,
13
+ inputs: [
14
+ {
15
+ indexed: false,
16
+ internalType: 'uint256',
17
+ name: 'index',
18
+ type: 'uint256',
19
+ },
20
+ {
21
+ indexed: false,
22
+ internalType: 'address',
23
+ name: 'account',
24
+ type: 'address',
25
+ },
26
+ {
27
+ indexed: false,
28
+ internalType: 'uint256',
29
+ name: 'amount',
30
+ type: 'uint256',
31
+ },
32
+ ],
33
+ name: 'Claimed',
34
+ type: 'event',
35
+ },
36
+ {
37
+ inputs: [
38
+ { internalType: 'uint256', name: 'index', type: 'uint256' },
39
+ { internalType: 'address', name: 'account', type: 'address' },
40
+ { internalType: 'uint256', name: 'amount', type: 'uint256' },
41
+ { internalType: 'bytes32[]', name: 'merkleProof', type: 'bytes32[]' },
42
+ ],
43
+ name: 'claim',
44
+ outputs: [],
45
+ stateMutability: 'nonpayable',
46
+ type: 'function',
47
+ },
48
+ {
49
+ inputs: [{ internalType: 'uint256', name: 'index', type: 'uint256' }],
50
+ name: 'isClaimed',
51
+ outputs: [{ internalType: 'bool', name: '', type: 'bool' }],
52
+ stateMutability: 'view',
53
+ type: 'function',
54
+ },
55
+ {
56
+ inputs: [],
57
+ name: 'merkleRoot',
58
+ outputs: [{ internalType: 'bytes32', name: '', type: 'bytes32' }],
59
+ stateMutability: 'view',
60
+ type: 'function',
61
+ },
62
+ {
63
+ inputs: [],
64
+ name: 'token',
65
+ outputs: [{ internalType: 'address', name: '', type: 'address' }],
66
+ stateMutability: 'view',
67
+ type: 'function',
68
+ },
69
+ ] as const,
70
+ };
@@ -0,0 +1,116 @@
1
+ import { type Provider, type Signer } from 'ethers';
2
+ import { isContractAvailable } from './utils';
3
+ import { type Incentives as IncentivesType, type ETH_ADDRESS } from './types';
4
+ import {
5
+ claimIncentivesFromMerkleDistributor,
6
+ isClaimedFromMerkleDistributor,
7
+ } from './incentivesHalpers';
8
+ import { DEFAULT_BASE_VERSION } from './constants';
9
+
10
+ export class Incentives {
11
+ private readonly signer: Signer | undefined;
12
+ public chainId: number;
13
+ private readonly request: (
14
+ endpoint: string,
15
+ options?: RequestInit,
16
+ ) => Promise<any>;
17
+
18
+ constructor(
19
+ signer: Signer | undefined,
20
+ chainId: number,
21
+ request: (endpoint: string, options?: RequestInit) => Promise<any>,
22
+ ) {
23
+ this.signer = signer;
24
+ this.chainId = chainId;
25
+ this.request = request;
26
+ }
27
+
28
+ /**
29
+ * Claims obol incentives from a Merkle Distributor contract.
30
+ *
31
+ * @remarks
32
+ * **⚠️ Important:** If you're storing the private key in an `.env` file, ensure it is securely managed
33
+ * and not pushed to version control.
34
+ *
35
+ * @param {Object} incentivesData - The incentives data needed for claiming.
36
+ * @param {string} incentivesData.contractAddress - The address of the Merkle Distributor contract.
37
+ * @param {number} incentivesData.index - The index in the Merkle tree.
38
+ * @param {string} incentivesData.operatorAddress - The address of the operator.
39
+ * @param {number} incentivesData.amount - The amount to claim.
40
+ * @param {string[]} incentivesData.merkleProof - The Merkle proof.
41
+ * @returns {Promise<{ txHash: string }>} The transaction hash of the claim transaction.
42
+ * @throws Will throw an error if the contract is not available or the claim fails.
43
+ *
44
+ */
45
+ async claimIncentives(incentivesData: {
46
+ contractAddress: ETH_ADDRESS;
47
+ index: number;
48
+ operatorAddress: ETH_ADDRESS;
49
+ amount: number;
50
+ merkleProof: string[];
51
+ }): Promise<{ txHash: string }> {
52
+ if (!this.signer) {
53
+ throw new Error('Signer is required in claimIncentives');
54
+ }
55
+ try {
56
+ const isContractDeployed = await isContractAvailable(
57
+ incentivesData.contractAddress,
58
+ this.signer.provider as Provider,
59
+ );
60
+
61
+ if (!isContractDeployed) {
62
+ throw new Error(
63
+ `Merkle Distributor contract is not available at address ${incentivesData.contractAddress}`,
64
+ );
65
+ }
66
+
67
+ const { txHash } = await claimIncentivesFromMerkleDistributor({
68
+ signer: this.signer,
69
+ contractAddress: incentivesData.contractAddress,
70
+ index: incentivesData.index,
71
+ operatorAddress: incentivesData.operatorAddress,
72
+ amount: incentivesData.amount,
73
+ merkleProof: incentivesData.merkleProof,
74
+ });
75
+
76
+ return { txHash };
77
+ } catch (error: any) {
78
+ console.log('Error claiming incentives:', error);
79
+ throw new Error(`Failed to claim incentives: ${error.message}`);
80
+ }
81
+ }
82
+
83
+ /**
84
+ * Read isClaimed.
85
+ *
86
+ * @param {ETH_ADDRESS} contractAddress - Address of the Merkle Distributor Contract
87
+ * @param {ETH_ADDRESS} index - operator index in merkle tree
88
+ * @returns {Promise<boolean>} true if incentives are already claime
89
+ *
90
+ */
91
+ async isClaimed(
92
+ contractAddress: ETH_ADDRESS,
93
+ index: number,
94
+ ): Promise<boolean> {
95
+ return await isClaimedFromMerkleDistributor(
96
+ this.chainId,
97
+ contractAddress,
98
+ index,
99
+ );
100
+ }
101
+
102
+ /**
103
+ * @param address - Operator address
104
+ * @returns {Promise<IncentivesType>} The matched incentives from DB
105
+ * @throws On not found if address not found.
106
+ */
107
+ async getIncentivesByAddress(address: string): Promise<IncentivesType> {
108
+ const incentives: IncentivesType = await this.request(
109
+ `/${DEFAULT_BASE_VERSION}/address/incentives/${address}`,
110
+ {
111
+ method: 'GET',
112
+ },
113
+ );
114
+ return incentives;
115
+ }
116
+ }