Skip to content

Commit

Permalink
Merge branch 'refs/heads/master' into feature/lit-4031-new-listener-sdk
Browse files Browse the repository at this point in the history
  • Loading branch information
FedericoAmura committed Dec 9, 2024
2 parents 751b1dd + 6ab28bd commit dcf0f07
Show file tree
Hide file tree
Showing 33 changed files with 370 additions and 91 deletions.
2 changes: 1 addition & 1 deletion lerna.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@
"$schema": "node_modules/lerna/schemas/lerna-schema.json",
"useNx": true,
"useWorkspaces": true,
"version": "7.0.0"
"version": "7.0.2"
}
9 changes: 4 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@
"@cosmjs/proto-signing": "0.30.1",
"@cosmjs/stargate": "0.30.1",
"@dotenvx/dotenvx": "^1.6.4",
"@lit-protocol/accs-schemas": "^0.0.19",
"@lit-protocol/contracts": "^0.0.63",
"@lit-protocol/accs-schemas": "^0.0.20",
"@lit-protocol/contracts": "^0.0.74",
"@metamask/eth-sig-util": "5.0.2",
"@mysten/sui.js": "^0.37.1",
"@openagenda/verror": "^3.1.4",
Expand Down Expand Up @@ -84,7 +84,7 @@
"@nx/plugin": "17.3.0",
"@nx/react": "17.3.0",
"@nx/web": "17.3.0",
"@solana/web3.js": "^1.95.3",
"@solana/web3.js": "1.95.3",
"@types/depd": "^1.1.36",
"@types/events": "^3.0.3",
"@types/jest": "27.4.1",
Expand Down Expand Up @@ -126,6 +126,5 @@
},
"workspaces": [
"packages/*"
],
"packageManager": "[email protected]"
]
}
2 changes: 1 addition & 1 deletion packages/access-control-conditions/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
"tags": [
"universal"
],
"version": "7.0.0",
"version": "7.0.2",
"main": "./dist/src/index.js",
"typings": "./dist/src/index.d.ts"
}
5 changes: 3 additions & 2 deletions packages/auth-browser/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,13 @@
"tweetnacl": "^1.0.3",
"tweetnacl-util": "^0.13.3",
"util": "^0.12.4",
"web-vitals": "^3.0.4"
"web-vitals": "^3.0.4",
"@lit-protocol/contracts": "^0.0.74"
},
"tags": [
"browser"
],
"version": "7.0.0",
"version": "7.0.2",
"main": "./dist/src/index.js",
"typings": "./dist/src/index.d.ts"
}
2 changes: 1 addition & 1 deletion packages/auth-helpers/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
"crypto": false,
"stream": false
},
"version": "7.0.0",
"version": "7.0.2",
"main": "./dist/src/index.js",
"typings": "./dist/src/index.d.ts"
}
2 changes: 1 addition & 1 deletion packages/constants/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
"tags": [
"universal"
],
"version": "7.0.0",
"version": "7.0.2",
"main": "./dist/src/index.js",
"typings": "./dist/src/index.d.ts"
}
86 changes: 81 additions & 5 deletions packages/constants/src/lib/constants/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -726,6 +726,71 @@ export const LIT_CHAINS: LITChain<LITEVMChain> = {
type: null,
vmType: 'EVM',
},
skaleCalypsoTestnet: {
contractAddress: null,
chainId: 974399131,
name: 'SKALE Calypso Hub Testnet',
symbol: 'sFUEL',
decimals: 18,
rpcUrls: ['https://testnet.skalenodes.com/v1/giant-half-dual-testnet'],
blockExplorerUrls: [
'https://giant-half-dual-testnet.explorer.testnet.skalenodes.com/',
],
type: null,
vmType: 'EVM',
},
skaleEuropa: {
contractAddress: null,
chainId: 2046399126,
name: 'SKALE Europa DeFI Hub',
symbol: 'sFUEL',
decimals: 18,
rpcUrls: ['https://mainnet.skalenodes.com/v1/elated-tan-skat'],
blockExplorerUrls: [
'https://elated-tan-skat.explorer.mainnet.skalenodes.com/',
],
type: null,
vmType: 'EVM',
},
skaleEuropaTestnet: {
contractAddress: null,
chainId: 1444673419,
name: 'SKALE Europa DeFi Hub Testnet',
symbol: 'sFUEL',
decimals: 18,
rpcUrls: ['https://testnet.skalenodes.com/v1/juicy-low-small-testnet'],
blockExplorerUrls: [
'https://juicy-low-small-testnet.explorer.testnet.skalenodes.com/',
],
type: null,
vmType: 'EVM',
},
skaleTitan: {
contractAddress: null,
chainId: 1350216234,
name: 'SKALE Titan AI Hub',
symbol: 'sFUEL',
decimals: 18,
rpcUrls: ['https://mainnet.skalenodes.com/v1/parallel-stormy-spica'],
blockExplorerUrls: [
'https://parallel-stormy-spica.explorer.mainnet.skalenodes.com/',
],
type: null,
vmType: 'EVM',
},
skaleTitanTestnet: {
contractAddress: null,
chainId: 1020352220,
name: 'SKALE Titan AI Hub Testnet',
symbol: 'sFUEL',
decimals: 18,
rpcUrls: ['https://testnet.skalenodes.com/v1/aware-fake-trim-testnet'],
blockExplorerUrls: [
'https://aware-fake-trim-testnet.explorer.testnet.skalenodes.com/',
],
type: null,
vmType: 'EVM',
},
fhenixHelium: {
contractAddress: null,
chainId: 8008135,
Expand Down Expand Up @@ -759,14 +824,14 @@ export const LIT_CHAINS: LITChain<LITEVMChain> = {
type: null,
vmType: 'EVM',
},
storyIlaid: {
storyOdyssey: {
contractAddress: null,
chainId: 1513,
name: 'Story Iliad Testnet',
chainId: 1516,
name: 'Story Odyssey',
symbol: 'IP',
decimals: 18,
rpcUrls: ['https://testnet.storyrpc.io'],
blockExplorerUrls: ['https://testnet.storyscan.xyz'],
rpcUrls: ['https://rpc.odyssey.storyrpc.io'],
blockExplorerUrls: ['https://odyssey.storyscan.xyz'],
type: null,
vmType: 'EVM',
},
Expand Down Expand Up @@ -803,6 +868,17 @@ export const LIT_CHAINS: LITChain<LITEVMChain> = {
type: null,
vmType: 'EVM',
},
matchain: {
contractAddress: null,
chainId: 698,
name: 'Matchain',
symbol: 'BNB',
decimals: 18,
rpcUrls: ['https://rpc.matchain.io'],
blockExplorerUrls: ['https://matchscan.io'],
type: null,
vmType: 'EVM',
},
};

/**
Expand Down
2 changes: 1 addition & 1 deletion packages/constants/src/lib/version.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export const version = '7.0.0';
export const version = '7.0.2';
2 changes: 1 addition & 1 deletion packages/contracts-sdk/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
"tags": [
"universal"
],
"version": "7.0.0",
"version": "7.0.2",
"main": "./dist/src/index.js",
"typings": "./dist/src/index.d.ts"
}
1 change: 1 addition & 0 deletions packages/core/jest.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ export default {
'ts-jest': {
tsconfig: '<rootDir>/tsconfig.spec.json',
},
fetch: global.fetch,
},
transform: {
'^.+\\.[t]s$': 'ts-jest',
Expand Down
2 changes: 1 addition & 1 deletion packages/core/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@lit-protocol/core",
"version": "7.0.0",
"version": "7.0.2",
"type": "commonjs",
"license": "MIT",
"homepage": "https://github.com/Lit-Protocol/js-sdk",
Expand Down
177 changes: 177 additions & 0 deletions packages/core/src/lib/lit-core.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,177 @@
import { InvalidEthBlockhash } from '@lit-protocol/constants';

import { LitCore } from './lit-core';

describe('LitCore', () => {
let core: LitCore;

describe('getLatestBlockhash', () => {
let originalFetch: typeof fetch;
let originalDateNow: typeof Date.now;
const mockBlockhashUrl =
'https://block-indexer-url.com/get_most_recent_valid_block';

beforeEach(() => {
core = new LitCore({
litNetwork: 'custom',
});
core['_blockHashUrl'] = mockBlockhashUrl;
originalFetch = fetch;
originalDateNow = Date.now;
});

afterEach(() => {
global.fetch = originalFetch;
Date.now = originalDateNow;
jest.clearAllMocks();
});

it('should return cached blockhash if still valid', async () => {
// Setup
const mockBlockhash = '0x1234';
const currentTime = 1000000;
core.latestBlockhash = mockBlockhash;
core.lastBlockHashRetrieved = currentTime;
Date.now = jest.fn().mockReturnValue(currentTime + 15000); // 15 seconds later
global.fetch = jest.fn();

// Execute
const result = await core.getLatestBlockhash();

// Assert
expect(result).toBe(mockBlockhash);
expect(global.fetch).not.toHaveBeenCalled();
});

it('should fetch new blockhash when cache is expired', async () => {
// Setup
const mockBlockhash = '0x5678';
const currentTime = 1000000;
core.latestBlockhash = '0x1234';
core.lastBlockHashRetrieved = currentTime - 31000; // 31 seconds ago currentTime
const blockNumber = 12345;
global.fetch = jest.fn().mockResolvedValue({
ok: true,
json: () =>
Promise.resolve({
blockhash: mockBlockhash,
timestamp: currentTime,
blockNumber,
}),
});
Date.now = jest.fn().mockReturnValue(currentTime);

// Execute
const result = await core.getLatestBlockhash();

// Assert
expect(result).toBe(mockBlockhash);
expect(fetch).toHaveBeenCalledWith(mockBlockhashUrl);
});

it('should throw error when blockhash is not available', async () => {
// Setup
core.latestBlockhash = null;
core.lastBlockHashRetrieved = null;
global.fetch = jest.fn().mockResolvedValue({
ok: false,
});
core['_getProviderWithFallback'] = jest.fn(() => Promise.resolve(null));

// Execute & Assert
await expect(core.getLatestBlockhash()).rejects.toThrow(
InvalidEthBlockhash
);
});

it('should handle fetch failure and use fallback RPC', async () => {
// Setup
const mockBlockhash = '0xabc';
const currentTime = 1000000;
Date.now = jest.fn().mockReturnValue(currentTime);
global.fetch = jest.fn().mockRejectedValue(new Error('Fetch failed'));
const mockProvider = {
getBlockNumber: jest.fn().mockResolvedValue(12345),
getBlock: jest.fn().mockResolvedValue({
hash: mockBlockhash,
number: 12345,
timestamp: currentTime,
}),
};
jest.spyOn(core as any, '_getProviderWithFallback').mockResolvedValue({
...mockProvider,
});

// Execute
const result = await core.getLatestBlockhash();

// Assert
expect(fetch).toHaveBeenCalledWith(mockBlockhashUrl);
expect(mockProvider.getBlock).toHaveBeenCalledWith(-1); // safety margin
expect(result).toBe(mockBlockhash);
});

it('should handle empty blockhash response with fallback RPC URLs', async () => {
// Setup
const mockBlockhash = '0xabc';
const currentTime = 1000000;
Date.now = jest.fn().mockReturnValue(currentTime);
global.fetch = jest.fn().mockResolvedValue({
ok: true,
json: () =>
Promise.resolve({
blockhash: null,
blockNumber: null,
}),
});
const mockProvider = {
getBlockNumber: jest.fn().mockResolvedValue(12345),
getBlock: jest.fn().mockResolvedValue({
hash: mockBlockhash,
number: 12345,
timestamp: currentTime,
}),
};
jest.spyOn(core as any, '_getProviderWithFallback').mockResolvedValue({
...mockProvider,
});

// Execute
const result = await core.getLatestBlockhash();

// Assert
expect(fetch).toHaveBeenCalledWith(mockBlockhashUrl);
expect(mockProvider.getBlock).toHaveBeenCalledWith(-1); // safety margin
expect(result).toBe(mockBlockhash);
});

it('should handle network timeouts gracefully', async () => {
// Setup
const currentTime = 1000000;
Date.now = jest.fn().mockReturnValue(currentTime);

global.fetch = jest
.fn()
.mockImplementation(
() =>
new Promise((_, reject) =>
setTimeout(() => reject(new Error('Network timeout')), 1000)
)
);

const mockProvider = {
getBlockNumber: jest.fn().mockResolvedValue(12345),
getBlock: jest.fn().mockResolvedValue(null), // Provider also fails
};

jest.spyOn(core as any, '_getProviderWithFallback').mockResolvedValue({
...mockProvider,
});

// Execute & Assert
await expect(() => core.getLatestBlockhash()).rejects.toThrow(
InvalidEthBlockhash
);
});
});
});
Loading

0 comments on commit dcf0f07

Please sign in to comment.