Skip to content

Commit

Permalink
feat(GasEstimator): touched account
Browse files Browse the repository at this point in the history
  • Loading branch information
franciscotobar committed Sep 6, 2024
1 parent facacd7 commit f85e4dc
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/gasEstimator/gasEstimator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ const estimateRelayMaxPossibleGas = async (
* the costs are included in all of them. These costs can be subtract by using the following constants:
* - POST_RELAY_DEPLOY_GAS_COST: transfer back cost is always included since we cannot know if the smart wallet is going
* to have some balance after the execution
* - OWNER_ALREADY_TOUCHED: Owner already touched, while we are doing the transfer back the owner may or not be a new account
* - OWNER_ALREADY_TOUCHED: Owner already touched, the owner of the smart wallet was previously touched
* @param relayRequest
* @param signer
* @param options
Expand Down
1 change: 1 addition & 0 deletions src/gasEstimator/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ export {
} from './gasEstimator';
export {
standardMaxPossibleGasEstimation,
touchedAccount,
PRE_RELAY_GAS_COST,
POST_RELAY_DEPLOY_GAS_COST,
OWNER_ALREADY_TOUCHED,
Expand Down
9 changes: 9 additions & 0 deletions src/gasEstimator/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,18 @@ const resolveSmartWalletAddress = async (
: callForwarder;
};

const touchedAccount = async (address: string) => {
const provider = getProvider();
const ownerBalance = await provider.getBalance(address);
const ownerTx = await provider.getTransactionCount(address);

return !ownerBalance.isZero() || ownerTx > 0;
};

export {
standardMaxPossibleGasEstimation,
resolveSmartWalletAddress,
touchedAccount,
PRE_RELAY_GAS_COST,
POST_RELAY_DEPLOY_GAS_COST,
POST_DEPLOY_EXECUTION,
Expand Down
37 changes: 37 additions & 0 deletions test/gasEstimator/gasEstimator.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import {
PRE_RELAY_GAS_COST,
resolveSmartWalletAddress,
standardMaxPossibleGasEstimation,
touchedAccount,
} from '../../src/gasEstimator/utils';
import { createRandomAddress } from '../utils';
import type { EnvelopingTxRequest } from '../../src';
Expand Down Expand Up @@ -445,4 +446,40 @@ describe('GasEstimator', function () {
expect(smartWalletAddress).to.be.equal(expectedSmartWalletAddress);
});
});

describe('touchedAccount', function () {
let providerStub: SinonStubbedInstance<providers.BaseProvider>;

beforeEach(function () {
providerStub = sandbox.createStubInstance(providers.BaseProvider);
sandbox.stub(clientConfiguration, 'getProvider').returns(providerStub);
providerStub.getBalance.resolves(BigNumber.from(1));
providerStub.getTransactionCount.resolves(1);
});

afterEach(function () {
sandbox.restore();
});

it('should return true if balance is not zero', async function () {
const touched = await touchedAccount(createRandomAddress());

expect(touched).to.be.true;
});

it('should return true if transaction count is greater than 0', async function () {
const touched = await touchedAccount(createRandomAddress());

expect(touched).to.be.true;
});

it('should return false if balance is zero and transaction count is 0', async function () {
providerStub.getBalance.resolves(BigNumber.from(0));
providerStub.getTransactionCount.resolves(0);

const touched = await touchedAccount(createRandomAddress());

expect(touched).to.be.false;
});
});
});

0 comments on commit f85e4dc

Please sign in to comment.