Skip to content

Commit

Permalink
Merge pull request #173 from oasisprotocol/CU-86785j9hu_Dont-return-h…
Browse files Browse the repository at this point in the history
…ard-coded-gas-estimation-in-Sapphire-Client-Library_Xi-Zhang

Utilize gas estimation in client libraries
  • Loading branch information
aefhm authored Nov 4, 2023
2 parents 45530e6 + 3af70fd commit 9ccaf13
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 38 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/contracts-test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ on:
branches:
- main
jobs:
hardhat-test:
contracts-test:
runs-on: ubuntu-latest
services:
sapphire-dev-ci:
Expand Down Expand Up @@ -59,7 +59,7 @@ jobs:
- name: Build hardhat integration
working-directory: integrations/hardhat
run: pnpm build
- name: hardhat test contracts
- name: Test contracts with Hardhat
working-directory: contracts
run: pnpm hardhat test --network sapphire-dev-ci
- name: Build docs
Expand Down
20 changes: 5 additions & 15 deletions clients/go/compat.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,7 @@ import (
)

const (
DefaultGasPrice = 100_000_000_000
// DefaultGasLimit is set on all transactions without explicit gas limit to avoid being set on signed queries by the web3 gateway.
DefaultGasLimit = 30_000_000
DefaultGasPrice = 100_000_000_000
DefaultBlockRange = 15
)

Expand Down Expand Up @@ -76,7 +74,7 @@ func PackCall(msg ethereum.CallMsg, cipher Cipher) (*ethereum.CallMsg, error) {

// PackSignedCall prepares `msg` in-place for being sent to Sapphire. The call will be end-to-end encrypted and a signature will be used to authenticate the `from` address.
func PackSignedCall(msg ethereum.CallMsg, cipher Cipher, sign SignerFn, chainID big.Int, leash Leash) (*ethereum.CallMsg, error) {
dataPack, err := NewDataPack(sign, chainID.Uint64(), msg.From[:], msg.To[:], DefaultGasLimit, msg.GasPrice, msg.Value, msg.Data, leash)
dataPack, err := NewDataPack(sign, chainID.Uint64(), msg.From[:], msg.To[:], 0, msg.GasPrice, msg.Value, msg.Data, leash)
if err != nil {
return nil, fmt.Errorf("failed to create signed call data back: %w", err)
}
Expand Down Expand Up @@ -158,7 +156,7 @@ func (b WrappedBackend) Transactor(from common.Address) *bind.TransactOpts {
From: from,
Signer: signFn,
GasPrice: big.NewInt(DefaultGasPrice),
GasLimit: DefaultGasLimit,
GasLimit: 0,
}
}

Expand Down Expand Up @@ -230,16 +228,8 @@ func (b WrappedBackend) SuggestGasTipCap(ctx context.Context) (*big.Int, error)

// EstimateGas implements ContractTransactor.
func (b WrappedBackend) EstimateGas(ctx context.Context, call ethereum.CallMsg) (gas uint64, err error) {
return DefaultGasLimit, nil
// TODO(#39)
// header, err := b.backend.HeaderByNumber(ctx, blockNumber)
// if err != nil {
// return nil, err
// }
// if err = packContractCall(header, &call, b.ChainID.Uint64(), b.Signer, b.Cipher); err != nil {
// return nil, err
// }
// return b.backend.EstimateGas(ctx, call)
// Assume latest round in oasis-web3-gateway RPC call
return b.backend.EstimateGas(ctx, call)
}

func txNeedsPacking(tx *types.Transaction) bool {
Expand Down
22 changes: 2 additions & 20 deletions clients/js/src/compat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,9 +90,6 @@ export type SapphireAnnex = {
};
};

/** If a gas limit is not provided, the runtime will produce a very confusing error message, so we set a default limit. This one is very high, but solves the problem. This should be lowered once error messages are better or gas estimation is enabled. */
const DEFAULT_GAS = 10_000_000;

/**
* Wraps an upstream ethers/web3/EIP-1193 provider to speak the Sapphire format.
*
Expand Down Expand Up @@ -165,7 +162,7 @@ export function wrap<U extends UpstreamProvider>(
cipher,
),
call: hookEthers6Call(signer, 'call', cipher),
estimateGas: async () => BigInt(DEFAULT_GAS),
estimateGas: hookEthers6Call(signer, 'estimateGas', cipher),
connect(provider: ethers6.Provider) {
return wrap(
signer.connect(provider) as unknown as ethers6.Signer,
Expand Down Expand Up @@ -200,13 +197,7 @@ export function wrap<U extends UpstreamProvider>(
cipher,
),
call: hookEthers5Call(signer, 'call', cipher),
// TODO(#39): replace with original once resolved
estimateGas: async () => BigNumber.from(DEFAULT_GAS),
// estimateGas: hookEthersCall(
// signer.estimateGas.bind(signer),
// cipher,
// signer,
// ),
estimateGas: hookEthers5Call(signer, 'estimateGas', cipher),
connect(provider: AbstractProvider) {
return wrap(
signer.connect(provider) as unknown as Ethers5Signer,
Expand Down Expand Up @@ -438,15 +429,13 @@ function hookEthers5Send(send: Ethers5Call, cipher: Cipher): Ethers5Call {
return async (tx: Deferrable<TransactionRequest>, ...rest) => {
const data = await tx.data;
tx.data = cipher.encryptEncode(data);
if (!tx.gasLimit) tx.gasLimit = DEFAULT_GAS;
return send(tx, ...rest);
};
}

function hookEthers6Send(send: Ethers6Call, cipher: Cipher): Ethers6Call {
return async (tx: ethers6.TransactionRequest, ...rest) => {
if (tx.data) tx.data = await cipher.encryptEncode(tx.data);
if (!tx.gasLimit) tx.gasLimit = DEFAULT_GAS;
return send(tx, ...rest);
};
}
Expand Down Expand Up @@ -482,8 +471,6 @@ function hookExternalSigner(
cipher: Cipher,
): EIP1193Provider['request'] {
return async (args: Web3ReqArgs) => {
if (args.method === 'eth_estimateGas')
return BigNumber.from(DEFAULT_GAS).toHexString(); // TODO(#39)
const { method, params } = await prepareRequest(args, signer, cipher);
const res = await signer.provider.send(method, params ?? []);
if (method === 'eth_call') return cipher.decryptEncoded(res);
Expand All @@ -496,11 +483,8 @@ function hookExternalProvider(
cipher: Cipher,
): EIP1193Provider['request'] {
return async ({ method, params }: Web3ReqArgs) => {
if (method === 'eth_estimateGas')
return BigNumber.from(DEFAULT_GAS).toHexString(); // TODO(#39)
if (method === 'eth_call' && params) {
params[0].data = await cipher.encryptEncode(params[0].data);
if (!params[0].gasLimit) params[0].gasLimit = DEFAULT_GAS;
return provider.send(method, params);
}
return provider.send(method, params ?? []);
Expand Down Expand Up @@ -546,7 +530,6 @@ async function prepareRequest(

if (/^eth_((send|sign)Transaction|call|estimateGas)$/.test(method)) {
params[0].data = await cipher.encryptEncode(params[0].data);
if (!params[0].gasLimit) params[0].gasLimit = DEFAULT_GAS;
return { method, params };
}

Expand Down Expand Up @@ -594,7 +577,6 @@ async function repackRawTx(
value: q(tx.value),
chainId: Number(tx.chainId),
};
if (!parsed.gasLimit) parsed.gasLimit = q(BigInt(DEFAULT_GAS)); // TODO(39)
try {
return signer!.signTransaction({
...parsed,
Expand Down
2 changes: 1 addition & 1 deletion clients/js/src/signed_calls.ts
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ async function makeLeash(
const [nonce, block] = await Promise.all([nonceP, blockP]);
const blockRange = overrides?.blockRange ?? DEFAULT_BLOCK_RANGE;

// check wether we should use cached leashes
// check whether we should use cached leashes
if (overrides?.nonce === undefined && overrides?.block === undefined) {
if (!signer.provider)
throw new Error(
Expand Down

0 comments on commit 9ccaf13

Please sign in to comment.