Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add getEthResources #261

Merged
merged 1 commit into from
Feb 11, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 48 additions & 0 deletions eth-providers/src/__tests__/e2e/tx.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,54 @@ describe('transaction tests', () => {
await provider.disconnect();
});

describe('test eth gas', () => {
it('getEthResources', async () => {
const randomWallet = Wallet.createRandom().connect(provider);

const amount = '1000000000000000000';
const resources = await provider.getEthResources({
type: 0,
from: wallet3.address,
to: randomWallet.address,
value: BigNumber.from(amount)
});

await wallet3.sendTransaction({
type: 0,
to: randomWallet.address,
value: BigNumber.from(amount),
...resources
});

expect((await randomWallet.getBalance()).toString()).eq(amount);
});

it('getPrice', async () => {
const randomWallet = Wallet.createRandom().connect(provider);

const amount = '1000000000000000000';

const params = await wallet3.populateTransaction({
type: 0,
to: randomWallet.address,
value: BigNumber.from(amount)
});

const data = provider.validSubstrateResources({
gasLimit: params.gasLimit,
gasPrice: params.gasPrice
});

console.log({
gasLimit: data.gasLimit.toString(),
storageLimit: data.storageLimit.toString(),
validUntil: data.validUntil.toString()
});

// expect((await randomWallet.getBalance()).toString()).eq(amount);
});
});

describe('test the error tx', () => {
it('InvalidDecimals', async () => {
await expect(
Expand Down
80 changes: 75 additions & 5 deletions eth-providers/src/base-provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,8 @@ import {
findEvmEvent,
getIndexerMetadata,
filterLog,
toHex
toHex,
calcEthereumTransactionParams
} from './utils';
import { TransactionReceipt as TransactionReceiptGQL } from './utils/gqlTypes';
import { UnfinalizedBlockCache } from './utils/unfinalizedBlockCache';
Expand Down Expand Up @@ -650,6 +651,79 @@ export abstract class BaseProvider extends AbstractProvider {
return resources.gas.add(storageGasLimit);
};

/**
* Get the gas for eth transactions
* @returns The gas used by eth transaction
*/
getEthResources = async (
transaction: Deferrable<TransactionRequest>,
Copy link
Collaborator

@shunjizhan shunjizhan Feb 7, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we can make transaction param optional, so then we will be able to use this method directly to calculate eth gasLimit/gasPrice when we don't know the whole transaction

Copy link
Collaborator

@shunjizhan shunjizhan Feb 7, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

something like

## when don't know whole TX
const ethGasParams = getEthResources({
  transactions: null,
  gasLimit,
  validUntil,
  storageLimit,
})

## when know whole TX
const ethGasParams = getEthResources({
  transactions: null
})

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This api is copied from estimateGas and requires transaction parameters.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the transaction parameter is not known, then a function can be designed separately.

{
gasLimit,
storageLimit,
validUntil
}: {
gasLimit?: BigNumberish;
storageLimit?: BigNumberish;
validUntil?: BigNumberish;
} = {}
): Promise<{
gasPrice: BigNumber;
gasLimit: BigNumber;
}> => {
if (!gasLimit || !storageLimit) {
const { gas, storage } = await this.estimateResources(transaction);
gasLimit = gasLimit ?? gas;
storageLimit = storageLimit ?? storage;
}

if (!validUntil) {
const blockNumber = await this.getBlockNumber();
// Expires after 100 blocks by default
validUntil = blockNumber + 100;
}

const storageByteDeposit = (this.api.consts.evm.storageDepositPerByte as UInt).toBigInt();
const txFeePerGas = (this.api.consts.evm.txFeePerGas as UInt).toBigInt();

const { txGasLimit, txGasPrice } = calcEthereumTransactionParams({
gasLimit,
storageLimit,
validUntil,
storageByteDeposit,
txFeePerGas
});

return {
gasLimit: txGasLimit,
gasPrice: txGasPrice
};
};

/**
* Validate substrate transaction parameters
*/
validSubstrateResources = ({
gasLimit,
gasPrice
}: {
gasLimit: BigNumberish;
gasPrice: BigNumberish;
}): {
gasLimit: BigNumber;
storageLimit: BigNumber;
validUntil: BigNumber;
} => {
const storageByteDeposit = (this.api.consts.evm.storageDepositPerByte as UInt).toBigInt();
const txFeePerGas = (this.api.consts.evm.txFeePerGas as UInt).toBigInt();

return calcSubstrateTransactionParams({
txGasPrice: gasPrice,
txGasLimit: gasLimit,
storageByteDeposit,
txFeePerGas
});
};

/**
* Estimate resources for a transaction.
* @param transaction The transaction to estimate the resources of
Expand All @@ -664,10 +738,6 @@ export abstract class BaseProvider extends AbstractProvider {
}> => {
const ethTx = await this._getTransactionRequest(transaction);

if (!ethTx.from) {
return logger.throwArgumentError('missing from address', 'transaction', ethTx);
}

const { from, to, data, value } = ethTx;

const extrinsic = !to
Expand Down