Skip to content

Commit 7b26e61

Browse files
committed
feat: get default gas in testnets using eth_gasPrice
1 parent d1936ca commit 7b26e61

File tree

1 file changed

+48
-24
lines changed

1 file changed

+48
-24
lines changed

src/Polymath.ts

Lines changed: 48 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -89,33 +89,9 @@ export class Polymath {
8989
});
9090
}
9191

92-
// we use 5 gwei as a sensible default for testnets
93-
let defaultGasPrice = new BigNumber(5000000000);
94-
95-
switch (speed) {
96-
case TransactionSpeed.Slow: {
97-
defaultGasPrice = defaultGasPrice.dividedBy(5);
98-
break;
99-
}
100-
case TransactionSpeed.Medium: {
101-
defaultGasPrice = defaultGasPrice.dividedBy(3 / 5);
102-
break;
103-
}
104-
case TransactionSpeed.Fast: {
105-
break;
106-
}
107-
default: {
108-
throw new PolymathError({
109-
code: ErrorCode.FatalError,
110-
message: 'Invalid transaction speed parameter',
111-
});
112-
}
113-
}
114-
11592
let contractWrappers = new PolymathBase({
11693
provider,
11794
polymathRegistryAddress,
118-
defaultGasPrice,
11995
});
12096

12197
const isTestnet = await contractWrappers.isTestnet();
@@ -158,6 +134,54 @@ export class Polymath {
158134
} catch (err) {
159135
// if the request fails, we simply use the previous default
160136
}
137+
} else {
138+
let defaultGasPrice = await new Promise<BigNumber>((resolve, reject) => {
139+
provider.sendAsync(
140+
{
141+
jsonrpc: '2.0',
142+
id: new Date().getTime(),
143+
params: [],
144+
method: 'eth_gasPrice',
145+
},
146+
(err, resp) => {
147+
if (err) {
148+
reject(err);
149+
} else if (!resp) {
150+
// we use 1 gwei as a sensible slow default for testnets
151+
resolve(new BigNumber(1000000000));
152+
} else {
153+
const price = parseInt(resp.result, 16);
154+
resolve(new BigNumber(price));
155+
}
156+
}
157+
);
158+
});
159+
160+
switch (speed) {
161+
case TransactionSpeed.Slow: {
162+
break;
163+
}
164+
case TransactionSpeed.Medium: {
165+
defaultGasPrice = defaultGasPrice.multipliedBy(3);
166+
break;
167+
}
168+
case TransactionSpeed.Fast: {
169+
defaultGasPrice = defaultGasPrice.multipliedBy(5);
170+
break;
171+
}
172+
default: {
173+
throw new PolymathError({
174+
code: ErrorCode.FatalError,
175+
message: 'Invalid transaction speed parameter',
176+
});
177+
}
178+
}
179+
180+
contractWrappers = new PolymathBase({
181+
provider,
182+
polymathRegistryAddress,
183+
defaultGasPrice,
184+
});
161185
}
162186

163187
this.context = new Context({

0 commit comments

Comments
 (0)