Skip to content

Commit 9268b40

Browse files
authored
Allow lib user set buffer for request price to pay and remove hardcoded buffer from EIP-1559 gas pricing (#23)
* allow lib user set buffer for request price to pay * update tests
1 parent 2289820 commit 9268b40

4 files changed

Lines changed: 16 additions & 10 deletions

File tree

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "randomness-js",
3-
"version": "1.0.3",
3+
"version": "1.0.4",
44
"description": "A library for consuming, verifying and using randomness from the dcipher network",
55
"source": "src/index.ts",
66
"main": "./dist/cjs/index.cjs",

src/index.ts

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ export type RandomnessVerificationConfig = {
4848

4949
type RequestRandomnessParams = {
5050
callbackGasLimit: bigint
51+
bufferPercent: bigint
5152
timeoutMs: number
5253
confirmations: number
5354
pollingIntervalMs: number
@@ -66,6 +67,7 @@ export class Randomness {
6667
this.contract = RandomnessSender__factory.connect(this.networkConfig.contractAddress, rpc)
6768
this.defaultRequestParams = {
6869
callbackGasLimit: networkConfig.callbackGasLimitDefault,
70+
bufferPercent: 50n,
6971
timeoutMs: defaultRequestTimeoutMs,
7072
confirmations: 1,
7173
pollingIntervalMs: 500,
@@ -99,7 +101,7 @@ export class Randomness {
99101
const maxPriorityFeePerGas = feeData.maxPriorityFeePerGas!;
100102

101103
// 2. Use EIP-1559 pricing
102-
const txGasPrice = (maxFeePerGas + maxPriorityFeePerGas) * 10n;
104+
const txGasPrice = maxFeePerGas + maxPriorityFeePerGas;
103105

104106
// 3. Estimate request price using the selected txGasPrice
105107
const requestPrice = await this.contract.estimateRequestPriceNative(
@@ -108,7 +110,9 @@ export class Randomness {
108110
);
109111

110112
// 4. Apply buffer (e.g. 100% = 2× total)
111-
const bufferPercent = isFilecoin(Number(chainId)) ? 300n : 50n;
113+
const bufferPercent = config.bufferPercent !== undefined
114+
? config.bufferPercent
115+
: (isFilecoin(Number(chainId)) ? 300n : 100n);
112116
const valueToSend = requestPrice + (requestPrice * bufferPercent) / 100n;
113117

114118
// 5. Estimate gas
@@ -191,7 +195,7 @@ export class Randomness {
191195
* @param callbackGasLimit The callbackGasLimit to use when fulfilling the request with a decryption key.
192196
* @returns The estimated request price and the transaction gas price used
193197
*/
194-
async calculateRequestPriceNative(callbackGasLimit: bigint): Promise<[bigint, bigint]> {
198+
async calculateRequestPriceNative(callbackGasLimit: bigint, config: Partial<RequestRandomnessParams> = this.defaultRequestParams): Promise<[bigint, bigint]> {
195199
if (this.rpc.provider == null) {
196200
throw Error("RPC requires a provider to request randomness")
197201
}
@@ -209,7 +213,7 @@ export class Randomness {
209213
const maxPriorityFeePerGas = feeData.maxPriorityFeePerGas!;
210214

211215
// 2. Use EIP-1559 pricing
212-
const txGasPrice = (maxFeePerGas + maxPriorityFeePerGas) * 10n;
216+
const txGasPrice = maxFeePerGas + maxPriorityFeePerGas;
213217

214218
// 3. Estimate request price using the selected txGasPrice
215219
const requestPrice = await this.contract.estimateRequestPriceNative(
@@ -218,7 +222,9 @@ export class Randomness {
218222
);
219223

220224
// 4. Apply buffer (e.g. 100% = 2× total)
221-
const bufferPercent = isFilecoin(Number(chainId)) ? 300n : 100n;
225+
const bufferPercent = config.bufferPercent !== undefined
226+
? config.bufferPercent
227+
: (isFilecoin(Number(chainId)) ? 300n : 100n);
222228
const valueToSend = requestPrice + (requestPrice * bufferPercent) / 100n;
223229

224230
return [valueToSend, txGasPrice];

test/randomness.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ describe("randomness", () => {
4747
const randomness = Randomness.createBaseSepolia(wallet)
4848
expect(randomness).not.toEqual(null)
4949

50-
const response = await randomness.requestRandomness({ callbackGasLimit: 100_000n })
50+
const response = await randomness.requestRandomness({ callbackGasLimit: 100_000n, bufferPercent: 10n })
5151
expect(await randomness.verify(response)).toBeTruthy()
5252

5353
rpc.destroy()
@@ -73,7 +73,7 @@ describe("randomness", () => {
7373
const randomness = Randomness.createPolygonPos(wallet)
7474
expect(randomness).not.toEqual(null)
7575

76-
const response = await randomness.requestRandomness({ callbackGasLimit: 100_000n })
76+
const response = await randomness.requestRandomness({ callbackGasLimit: 100_000n, bufferPercent: 10n })
7777
expect(await randomness.verify(response)).toBeTruthy()
7878

7979
rpc.destroy()

0 commit comments

Comments
 (0)