This repository has been archived by the owner on Jan 22, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 925
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Default commitment to
confirmed
when not explicitly specified
- Loading branch information
1 parent
2962a3f
commit cb7702c
Showing
6 changed files
with
435 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
121 changes: 121 additions & 0 deletions
121
packages/rpc-core/src/__tests__/default-commitment-test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
import { Commitment } from '@solana/rpc-types'; | ||
|
||
import { applyDefaultCommitment } from '../default-commitment'; | ||
|
||
const MOCK_COMMITMENT_PROPERTY_NAME = 'commitmentProperty'; | ||
|
||
describe('applyDefaultCommitment', () => { | ||
describe.each([0, 1, 2])('in relation to a method whose commitment config is argument #%s', expectedPosition => { | ||
it('adds the default commitment when absent from the call', () => { | ||
expect.assertions(1); | ||
expect( | ||
applyDefaultCommitment({ | ||
commitmentPropertyName: MOCK_COMMITMENT_PROPERTY_NAME, | ||
optionsObjectPositionInParams: expectedPosition, | ||
overrideCommitment: 'processed', | ||
params: [], | ||
}), | ||
).toEqual([ | ||
...new Array(expectedPosition).map(() => expect.anything()), | ||
{ [MOCK_COMMITMENT_PROPERTY_NAME]: 'processed' }, | ||
]); | ||
}); | ||
describe.each(['confirmed', 'finalized', 'processed'] as Commitment[])( | ||
'when the default commitment is set to `%s`', | ||
defaultCommitment => { | ||
describe.each(['confirmed', 'processed'])( | ||
'and the params already specify a commitment of `%s`', | ||
existingCommitment => { | ||
it('does not overwrite it', () => { | ||
const params = [ | ||
...new Array(expectedPosition), | ||
{ [MOCK_COMMITMENT_PROPERTY_NAME]: existingCommitment }, | ||
]; | ||
expect( | ||
applyDefaultCommitment({ | ||
commitmentPropertyName: MOCK_COMMITMENT_PROPERTY_NAME, | ||
optionsObjectPositionInParams: expectedPosition, | ||
overrideCommitment: defaultCommitment, | ||
params, | ||
}), | ||
).toBe(params); | ||
}); | ||
}, | ||
); | ||
describe.each(['finalized', undefined])( | ||
'and the params already specify a commitment of `%s`', | ||
existingCommitment => { | ||
it('removes the commitment property when there are other properties in the config object', () => { | ||
expect.assertions(1); | ||
const params = [ | ||
...new Array(expectedPosition), | ||
{ [MOCK_COMMITMENT_PROPERTY_NAME]: existingCommitment, other: 'property' }, | ||
]; | ||
expect( | ||
applyDefaultCommitment({ | ||
commitmentPropertyName: MOCK_COMMITMENT_PROPERTY_NAME, | ||
optionsObjectPositionInParams: expectedPosition, | ||
overrideCommitment: defaultCommitment, | ||
params, | ||
}), | ||
).toStrictEqual([ | ||
...new Array(expectedPosition).map(() => expect.anything()), | ||
{ other: 'property' }, | ||
]); | ||
}); | ||
it('sets the config object to `undefined` when there are no other properties left and the config object is not the last param', () => { | ||
expect.assertions(1); | ||
const params = [ | ||
...new Array(expectedPosition), | ||
{ [MOCK_COMMITMENT_PROPERTY_NAME]: existingCommitment }, | ||
'someParam', | ||
]; | ||
expect( | ||
applyDefaultCommitment({ | ||
commitmentPropertyName: MOCK_COMMITMENT_PROPERTY_NAME, | ||
optionsObjectPositionInParams: expectedPosition, | ||
overrideCommitment: defaultCommitment, | ||
params, | ||
}), | ||
).toStrictEqual([ | ||
...new Array(expectedPosition).map(() => expect.anything()), | ||
undefined, | ||
'someParam', | ||
]); | ||
}); | ||
it('truncates the params when there are no other properties left and the config object is the last param', () => { | ||
expect.assertions(1); | ||
const params = [ | ||
...new Array(expectedPosition), | ||
{ [MOCK_COMMITMENT_PROPERTY_NAME]: existingCommitment }, | ||
]; | ||
expect( | ||
applyDefaultCommitment({ | ||
commitmentPropertyName: MOCK_COMMITMENT_PROPERTY_NAME, | ||
optionsObjectPositionInParams: expectedPosition, | ||
overrideCommitment: defaultCommitment, | ||
params, | ||
}), | ||
).toStrictEqual([...new Array(expectedPosition).map(() => expect.anything())]); | ||
}); | ||
}, | ||
); | ||
}, | ||
); | ||
it.each([null, 1, '1', 1n, [1, 2, 3]])( | ||
"does not overwrite the existing param when it's a non-object like `%s`", | ||
paramInConfigPosition => { | ||
expect.assertions(1); | ||
const params = [...new Array(expectedPosition), paramInConfigPosition]; | ||
expect( | ||
applyDefaultCommitment({ | ||
commitmentPropertyName: MOCK_COMMITMENT_PROPERTY_NAME, | ||
optionsObjectPositionInParams: expectedPosition, | ||
overrideCommitment: 'processed', | ||
params, | ||
}), | ||
).toBe(params); | ||
}, | ||
); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
import { Commitment } from '@solana/rpc-types'; | ||
|
||
type Config = Readonly<{ | ||
params: unknown[]; | ||
commitmentPropertyName: string; | ||
optionsObjectPositionInParams: number; | ||
overrideCommitment?: Commitment; | ||
}>; | ||
|
||
export function applyDefaultCommitment({ | ||
commitmentPropertyName, | ||
params, | ||
optionsObjectPositionInParams, | ||
overrideCommitment, | ||
}: Config) { | ||
const paramInTargetPosition = params[optionsObjectPositionInParams]; | ||
if ( | ||
// There's no config. | ||
paramInTargetPosition === undefined || | ||
// There is a config object. | ||
(paramInTargetPosition && typeof paramInTargetPosition === 'object' && !Array.isArray(paramInTargetPosition)) | ||
) { | ||
if ( | ||
// The config object already has a commitment set. | ||
paramInTargetPosition && | ||
commitmentPropertyName in paramInTargetPosition | ||
) { | ||
if ( | ||
!paramInTargetPosition[commitmentPropertyName as keyof typeof paramInTargetPosition] || | ||
paramInTargetPosition[commitmentPropertyName as keyof typeof paramInTargetPosition] === 'finalized' | ||
) { | ||
// Delete the commitment property; `finalized` is already the server default. | ||
const nextParams = [...params]; | ||
const { | ||
[commitmentPropertyName as keyof typeof paramInTargetPosition]: _, // eslint-disable-line @typescript-eslint/no-unused-vars | ||
...rest | ||
} = paramInTargetPosition; | ||
if (Object.keys(rest).length > 0) { | ||
nextParams[optionsObjectPositionInParams] = rest; | ||
} else { | ||
if (optionsObjectPositionInParams === nextParams.length - 1) { | ||
nextParams.length--; | ||
} else { | ||
nextParams[optionsObjectPositionInParams] = undefined; | ||
} | ||
} | ||
return nextParams; | ||
} | ||
} else if (overrideCommitment !== 'finalized') { | ||
// Apply the default commitment. | ||
const nextParams = [...params]; | ||
nextParams[optionsObjectPositionInParams] = { | ||
...paramInTargetPosition, | ||
[commitmentPropertyName]: overrideCommitment, | ||
}; | ||
return nextParams; | ||
} | ||
} | ||
return params; | ||
} |
Oops, something went wrong.