-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
6 changed files
with
191 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import { toJson } from './utils/serializer'; | ||
import { getNetworkFromChainId } from './utils/snapUtils'; | ||
import { getKeysFromAddress, estimateFeeBulk } from './utils/starknetUtils'; | ||
import { ApiParams, EstimateFeesRequestParams } from './types/snapApi'; | ||
import { logger } from './utils/logger'; | ||
|
||
export async function estimateFees(params: ApiParams) { | ||
try { | ||
const { state, keyDeriver, requestParams } = params; | ||
const requestParamsObj = requestParams as EstimateFeesRequestParams; | ||
|
||
logger.log(`estimateFees params: ${toJson(requestParamsObj, 2)}`); | ||
|
||
const senderAddress = requestParamsObj.senderAddress; | ||
const network = getNetworkFromChainId(state, requestParamsObj.chainId); | ||
const { privateKey: senderPrivateKey } = await getKeysFromAddress(keyDeriver, network, state, senderAddress); | ||
|
||
const fees = await estimateFeeBulk( | ||
network, | ||
senderAddress, | ||
senderPrivateKey, | ||
requestParamsObj.invocations, | ||
requestParamsObj.invocationDetails ? requestParamsObj.invocationDetails : undefined, | ||
); | ||
|
||
return fees.map((fee) => ({ | ||
overall_fee: fee.overall_fee.toString(10) || '0', | ||
gas_consumed: fee.gas_consumed.toString(10) || '0', | ||
gas_price: fee.gas_price.toString(10) || '0', | ||
suggestedMaxFee: fee.suggestedMaxFee.toString(10) || '0', | ||
})); | ||
} catch (err) { | ||
logger.error(`Problem found: ${err}`); | ||
throw err; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
import chai, { expect } from 'chai'; | ||
import sinon from 'sinon'; | ||
import sinonChai from 'sinon-chai'; | ||
import { WalletMock } from '../wallet.mock.test'; | ||
import { SnapState } from '../../src/types/snapState'; | ||
import { estimateFees } from '../../src/estimateFees'; | ||
import { STARKNET_TESTNET_NETWORK } from '../../src/utils/constants'; | ||
import { account2, estimateDeployFeeResp2, estimateDeployFeeResp3, getBip44EntropyStub } from '../constants.test'; | ||
import { getAddressKeyDeriver } from '../../src/utils/keyPair'; | ||
import * as utils from '../../src/utils/starknetUtils'; | ||
import { Mutex } from 'async-mutex'; | ||
import { ApiParams } from '../../src/types/snapApi'; | ||
import { TransactionType } from 'starknet'; | ||
chai.use(sinonChai); | ||
const sandbox = sinon.createSandbox(); | ||
|
||
describe('Test function: estimateFees', function () { | ||
this.timeout(5000); | ||
const walletStub = new WalletMock(); | ||
const state: SnapState = { | ||
accContracts: [account2], | ||
erc20Tokens: [], | ||
networks: [STARKNET_TESTNET_NETWORK], | ||
transactions: [], | ||
}; | ||
const apiParams: ApiParams = { | ||
state, | ||
requestParams: {}, | ||
wallet: walletStub, | ||
saveMutex: new Mutex(), | ||
}; | ||
|
||
beforeEach(async function () { | ||
walletStub.rpcStubs.snap_getBip44Entropy.callsFake(getBip44EntropyStub); | ||
apiParams.keyDeriver = await getAddressKeyDeriver(walletStub); | ||
}); | ||
|
||
afterEach(function () { | ||
walletStub.reset(); | ||
sandbox.restore(); | ||
}); | ||
|
||
it('should estimate the fee including deploy txn correctly', async function () { | ||
const feeResult = [estimateDeployFeeResp2, estimateDeployFeeResp3]; | ||
sandbox.stub(utils, 'estimateFeeBulk').callsFake(async () => { | ||
return feeResult; | ||
}); | ||
apiParams.requestParams = { | ||
senderAddress: account2.address, | ||
chainId: STARKNET_TESTNET_NETWORK.chainId, | ||
invocations: [ | ||
{ | ||
type: TransactionType.INVOKE, | ||
payload: { | ||
entrypoint: 'transfer', | ||
contractAddress: '0x049d36570d4e46f48e99674bd3fcc84644ddd6b96f7c741b1562b82f9e004dc7', | ||
calldata: ['1697416752243704114657612983658108968471303240361660550219082009242042413588', '1', '0'], | ||
}, | ||
}, | ||
], | ||
}; | ||
const expectedResult = feeResult.map((fee) => ({ | ||
overall_fee: fee.overall_fee.toString(10) || '0', | ||
gas_consumed: fee.gas_consumed.toString(10) || '0', | ||
gas_price: fee.gas_price.toString(10) || '0', | ||
suggestedMaxFee: fee.suggestedMaxFee.toString(10) || '0', | ||
})); | ||
const result = await estimateFees(apiParams); | ||
expect(result).to.eql(expectedResult); | ||
}); | ||
|
||
it('should throw error if estimateFee failed', async function () { | ||
sandbox.stub(utils, 'getSigner').callsFake(async () => { | ||
return account2.publicKey; | ||
}); | ||
sandbox.stub(utils, 'estimateFeeBulk').throws(new Error()); | ||
apiParams.requestParams = { | ||
senderAddress: account2.address, | ||
chainId: STARKNET_TESTNET_NETWORK.chainId, | ||
invocations: [ | ||
{ | ||
type: TransactionType.INVOKE, | ||
payload: { | ||
entrypoint: 'transfer', | ||
contractAddress: '0x049d36570d4e46f48e99674bd3fcc84644ddd6b96f7c741b1562b82f9e004dc7', | ||
calldata: ['1697416752243704114657612983658108968471303240361660550219082009242042413588', '1', '0'], | ||
}, | ||
}, | ||
], | ||
}; | ||
|
||
let result; | ||
try { | ||
await estimateFees(apiParams); | ||
} catch (err) { | ||
result = err; | ||
} finally { | ||
expect(result).to.be.an('Error'); | ||
} | ||
}); | ||
}); |