diff --git a/package.json b/package.json index 9886e54..c01ccf3 100644 --- a/package.json +++ b/package.json @@ -58,5 +58,6 @@ "lint-staged": { "*.js": "eslint --cache --fix", "*.--write": "prettier --ignore-unknown --write" - } + }, + "packageManager": "yarn@1.22.22+sha512.a6b2f7906b721bba3d67d4aff083df04dad64c399707841b7acf00f6b133b7ac24255f2652fa22ae3534329dc6180534e98d17432037ff6fd140556e2bb3137e" } diff --git a/src/abstractionkit.ts b/src/abstractionkit.ts index d6d4398..09fbadc 100644 --- a/src/abstractionkit.ts +++ b/src/abstractionkit.ts @@ -28,7 +28,10 @@ export { fetchAccountNonce, calculateUserOperationMaxGasCost, sendJsonRpcRequest, - fetchGasPrice + fetchGasPrice, + DepositInfo, + getDepositInfo, + getBalanceOf } from "./utils"; export { diff --git a/src/utils.ts b/src/utils.ts index 74ce198..5634b37 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -446,6 +446,90 @@ export function calculateUserOperationMaxGasCost( } } +export type DepositInfo = { + deposit: bigint; + staked: boolean; + stake:bigint; + unstakeDelaySec: bigint; + withdrawTime: bigint; +}; + +export async function getBalanceOf( + nodeRpcUrl: string, + address: string, + entrypointAddress: string, +): Promise { + const depositInfo = await getDepositInfo( + nodeRpcUrl, address, entrypointAddress + ) + return depositInfo.deposit; +} + +export async function getDepositInfo( + nodeRpcUrl: string, + address: string, + entrypointAddress: string, +): Promise { + const getDepositInfoSelector = "0x5287ce12"; //"getDepositInfo(address)" + const getDepositInfoCallData = createCallData( + getDepositInfoSelector, + ["address"], + [address], + ); + + const params = { + from: "0x0000000000000000000000000000000000000000", + to: entrypointAddress, + data: getDepositInfoCallData, + }; + + try { + const recoveryRequestResult = await sendEthCallRequest( + nodeRpcUrl, params, "latest"); + + const abiCoder = AbiCoder.defaultAbiCoder(); + const decodedCalldata = abiCoder.decode( + ["uint256", "bool", "uint112", "uint32", "uint48"], recoveryRequestResult); + + + if (decodedCalldata.length === 5) { + try { + return { + deposit:BigInt(decodedCalldata[0]), + staked:Boolean(decodedCalldata[1]), + stake:BigInt(decodedCalldata[2]), + unstakeDelaySec:BigInt(decodedCalldata[3]), + withdrawTime:BigInt(decodedCalldata[4]), + }; + } catch (err) { + const error = ensureError(err); + + throw new AbstractionKitError( + "BAD_DATA", + "getNonce returned ill formed data", + { + cause: error, + }, + ); + } + } else { + throw new AbstractionKitError( + "BAD_DATA", + "getNonce returned ill formed data", + { + context: JSON.stringify(decodedCalldata), + }, + ); + } + } catch (err) { + const error = ensureError(err); + + throw new AbstractionKitError("BAD_DATA", "getNonce failed", { + cause: error, + }); + } +} + type EthCallTransaction = { from?: string; to: string; diff --git a/test/entrypoint.test.js b/test/entrypoint.test.js new file mode 100644 index 0000000..4a7b52f --- /dev/null +++ b/test/entrypoint.test.js @@ -0,0 +1,29 @@ +const accountAbstractionkit = require('../dist/index.umd'); +require('dotenv').config() + +jest.setTimeout(300000); +const address=process.env.PUBLIC_ADDRESS1 +const jsonRpcNodeProvider=process.env.JSON_RPC_NODE_PROVIDER + +const entrypoints = [ + "0x0000000071727de22e5e9d8baf0edac6f37da032", + "0x5ff137d4b0fdcd49dca30c7cf57e578a026d2789" +] + +describe('deposit info and balance of address', () => { + entrypoints.forEach((entrypoint) => { + test('check deposit info and balance are equal and types for entrypoint: ' + entrypoint, async () => { + const depositInfo = await accountAbstractionkit.getDepositInfo( + jsonRpcNodeProvider, address, entrypoint); + const balance = await accountAbstractionkit.getBalanceOf( + jsonRpcNodeProvider, address, entrypoint); + + expect(depositInfo["deposit"]).toStrictEqual(balance); + expect(typeof depositInfo["deposit"]).toBe("bigint"); + expect(typeof depositInfo["staked"]).toBe("boolean"); + expect(typeof depositInfo["stake"]).toBe("bigint"); + expect(typeof depositInfo["unstakeDelaySec"]).toBe("bigint"); + expect(typeof depositInfo["withdrawTime"]).toBe("bigint"); + }); + }); +});