Skip to content

Commit

Permalink
add: getDepositInfo and getBalanceOf
Browse files Browse the repository at this point in the history
  • Loading branch information
sherifahmed990 committed Jan 6, 2025
1 parent 6680855 commit 9eca1fe
Show file tree
Hide file tree
Showing 4 changed files with 119 additions and 2 deletions.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -58,5 +58,6 @@
"lint-staged": {
"*.js": "eslint --cache --fix",
"*.--write": "prettier --ignore-unknown --write"
}
},
"packageManager": "[email protected]+sha512.a6b2f7906b721bba3d67d4aff083df04dad64c399707841b7acf00f6b133b7ac24255f2652fa22ae3534329dc6180534e98d17432037ff6fd140556e2bb3137e"
}
5 changes: 4 additions & 1 deletion src/abstractionkit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,10 @@ export {
fetchAccountNonce,
calculateUserOperationMaxGasCost,
sendJsonRpcRequest,
fetchGasPrice
fetchGasPrice,
DepositInfo,
getDepositInfo,
getBalanceOf
} from "./utils";

export {
Expand Down
84 changes: 84 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<bigint> {
const depositInfo = await getDepositInfo(
nodeRpcUrl, address, entrypointAddress
)
return depositInfo.deposit;
}

export async function getDepositInfo(
nodeRpcUrl: string,
address: string,
entrypointAddress: string,
): Promise<DepositInfo> {
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;
Expand Down
29 changes: 29 additions & 0 deletions test/entrypoint.test.js
Original file line number Diff line number Diff line change
@@ -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");
});
});
});

0 comments on commit 9eca1fe

Please sign in to comment.