Skip to content

Commit a50147e

Browse files
committed
fix: attempt to estimate gas for token reservation
1 parent 5cb651e commit a50147e

6 files changed

Lines changed: 43 additions & 13 deletions

File tree

src/LowLevel/LowLevel.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,8 +81,8 @@ a browser, make sure you have MetaMask installed and enabled.`
8181
public getAccount = async () => {
8282
const nodeAccounts = await web3.eth.getAccounts();
8383
const walletAccount = (web3.eth.accounts.wallet as Web3Wallet)[0] || {};
84-
85-
return nodeAccounts[0] || walletAccount.address;
84+
const account = walletAccount.address || nodeAccounts[0];
85+
return account;
8686
};
8787

8888
public initialize = async ({

src/LowLevel/PolyToken.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,15 @@ import { PolyTokenAbi } from './abis/PolyTokenAbi';
44
import { PolyTokenFaucetAbi } from './abis/PolyTokenFaucetAbi';
55
import { Contract } from './Contract';
66
import { Context } from './LowLevel';
7+
import { fromWei, toWei, prepareAndSendTx } from './utils';
8+
79
import {
810
GenericContract,
911
AllowanceArgs,
1012
GetTokensArgs,
1113
BalanceOfArgs,
1214
ApproveArgs,
1315
} from './types';
14-
import { fromWei, toWei } from './utils';
1516

1617
interface PolyTokenContract extends GenericContract {
1718
methods: {
@@ -84,9 +85,10 @@ export class PolyToken extends Contract<PolyTokenContract> {
8485
}
8586

8687
return () =>
87-
this.contract.methods
88-
.approve(spender, amountInWei)
89-
.send({ from: ownerAddress });
88+
await prepareAndSendTx(
89+
this.contract.methods.approve(spender, amountInWei),
90+
{ from: ownerAddress }
91+
);
9092
};
9193

9294
public symbol = async () => {

src/LowLevel/SecurityTokenRegistry.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@ import {
1212
GetTickerDetailsArgs,
1313
IsTickerAvailableArgs,
1414
} from './types';
15-
import { fromWei } from './utils';
15+
import { fromWei, prepareAndSendTx } from './utils';
16+
1617
import { PolymathError } from '../PolymathError';
1718
import { ErrorCodes } from '../types';
1819
import { ZERO_ADDRESS } from './constants';
@@ -73,9 +74,10 @@ export class SecurityTokenRegistry extends Contract<
7374
tokenName,
7475
}: RegisterTickerArgs) => {
7576
return () =>
76-
this.contract.methods
77-
.registerTicker(owner, ticker, tokenName)
78-
.send({ from: this.context.account });
77+
prepareAndSendTx(
78+
this.contract.methods.registerTicker(owner, ticker, tokenName),
79+
{ from: this.context.account }
80+
);
7981
};
8082

8183
public getTickerDetails = async ({ ticker }: GetTickerDetailsArgs) => {

src/LowLevel/utils.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import Web3 from 'web3';
22
import BigNumber from 'bignumber.js';
3+
import { web3 } from '../LowLevel/web3Client';
4+
import { Tx, TransactionObject } from 'web3/eth/types';
35

46
const { utils } = Web3;
57

@@ -63,3 +65,25 @@ export function toAscii(value: string) {
6365
export function isAddress(value: string) {
6466
return utils.isAddress(value);
6567
}
68+
69+
// @TODO add multiplier
70+
// @TODO add docs
71+
72+
export async function prepareAndSendTx(
73+
method: TransactionObject<any>,
74+
options: Tx
75+
) {
76+
const block = await web3.eth.getBlock('latest');
77+
const networkGasLimit = block.gasLimit;
78+
options.gasPrice = options.gasPrice || (await web3.eth.getGasPrice());
79+
if (options.from) {
80+
options.nonce =
81+
options.nonce || (await web3.eth.getTransactionCount(options.from));
82+
}
83+
if (!options.gas) {
84+
const gasLimit = await method.estimateGas(options);
85+
// Do not exceed block gas limit.
86+
if (gasLimit < networkGasLimit) options.gas = gasLimit;
87+
}
88+
return method.send(options);
89+
}

src/Polymath.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -112,17 +112,18 @@ export class Polymath {
112112
polymathRegistryAddress,
113113
httpProvider,
114114
httpProviderUrl,
115+
privateKey,
115116
}: PolymathNetworkParams) => {
116117
let lowLevel: LowLevel;
117118

118119
if (httpProvider) {
119120
this.httpProvider = httpProvider;
120-
lowLevel = new LowLevel({ provider: this.httpProvider });
121+
lowLevel = new LowLevel({ provider: this.httpProvider, privateKey });
121122
} else if (httpProviderUrl) {
122123
this.httpProviderUrl = httpProviderUrl;
123-
lowLevel = new LowLevel({ provider: this.httpProviderUrl });
124+
lowLevel = new LowLevel({ provider: this.httpProviderUrl, privateKey });
124125
} else {
125-
lowLevel = new LowLevel();
126+
lowLevel = new LowLevel({ privateKey });
126127
}
127128

128129
this.lowLevel = lowLevel;

src/types/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ export interface PolymathNetworkParams {
8080
wsProvider?: WebsocketProvider;
8181
wsProviderUrl?: string;
8282
polymathRegistryAddress: string;
83+
privateKey?: string;
8384
}
8485

8586
export enum ProcedureTypes {

0 commit comments

Comments
 (0)