safe-core-sdk
safe-core-sdk copied to clipboard
How do I get the balance of an undeployed predictedSafeAddress
Hi I've send ethers to an predictedSafeAddress and want to know balance on the predictedSafeAddress before deploy it. Please. Thanks
You can use the protocolKit instance for that.
const protocolKit = await Safe.create({ ethAdapter, predictedSafe })
const balance = await protocolKit.getBalance()
Safe.create() will not deploy the Safe right away, this will happen the first time you execute a transaction.
HI @dasanra I got the error below when i try to get the balance of an AA address that is not yet deployed (an predictedSafeAddress).
60 | customContractAddress: customSafeAddress ?? customContracts?.safeSingletonAddress, 61 | customContractAbi: customContracts?.safeSingletonAbi 62 | }); 63 | const isContractDeployed = await ethAdapter.isContractDeployed(await safeContract.getAddress()); 64 | if (!isContractDeployed) { 65 | throw new Error('SafeProxy contract is not deployed on the current network'); ^ error: SafeProxy contract is not deployed on the current network
Could you please share the versions of the packages you are using?
Also could you share the code snippet? With the code I shared above, if you are using the latest versions you shouldn't be having such error.
installed [email protected] installed @safe-global/[email protected] installed @safe-global/[email protected] installed @safe-global/[email protected] installed [email protected]
@francelwebdev could you also share the code fragment and also the chain you are using?
Thank you
`import { ethers } from 'ethers' import Safe, { EthersAdapter, SafeFactory, SafeAccountConfig } from '@safe-global/protocol-kit' import SafeApiKit from '@safe-global/api-kit' import { MetaTransactionData } from '@safe-global/safe-core-sdk-types'
export class SafeProtocolKitService {
static async init() {
try {
// Initialize Signers, Providers, and EthAdapter
const RPC_URL = 'https://eth-sepolia.public.blastapi.io'
const provider = new ethers.JsonRpcProvider(RPC_URL)
const owner1Signer = new ethers.Wallet("EOA_PRIVATE_KEY", provider)
const ethAdapterOwner1 = new EthersAdapter({
ethers,
signerOrProvider: owner1Signer
})
// Initialize Signers, Providers, and EthAdapter
// Initialize the API Kit
const apiKit = new SafeApiKit({
chainId: BigInt(1)
})
// Initialize the API Kit
// Initialize the Protocol Kit
const safeFactory = await SafeFactory.create({ ethAdapter: ethAdapterOwner1 })
// Initialize the Protocol Kit
const safeAccountConfig: SafeAccountConfig = {
owners: [
await owner1Signer.getAddress()
],
threshold: 1,
// ... (Optional params)
}
// predictSafeAddress
const predictedSafeAddress = await safeFactory.predictSafeAddress(safeAccountConfig, "2")
console.log('predictedSafeAddress', predictedSafeAddress)
// predictSafeAddress
return predictedSafeAddress
} catch (error) {
console.error("Error generating new safe account:", error);
throw error;
}
}
static async getBalance() {
const RPC_URL = 'https://eth-sepolia.public.blastapi.io'
const provider = new ethers.JsonRpcProvider(RPC_URL)
const owner1Signer = new ethers.Wallet("EOA_PRIVATE_KEY", provider)
const ethAdapterOwner1 = new EthersAdapter({
ethers,
signerOrProvider: owner1Signer
})
const protocolKit = await Safe.create({ ethAdapter: ethAdapterOwner1, safeAddress: "PREDICTED_SAFE_ADDRESS" })
console.log('protocolKit', protocolKit)
const balance = await protocolKit.getBalance()
console.log('balance', balance)
return balance
}
}
(async () => { // await SafeProtocolKitService.init() await SafeProtocolKitService.getBalance() })()`
Ok, I see the issue now.
The initialization you are using inside getBalance is fine for a deployed Safe, but not for a Safe that wasn't deployed yet.
If you check carefully in the docs in the Safe.create() you need to set a predictedSafe property instead of a safeAddress for those Safes that aren't deployed yet.
This predictedSafe will look like the safeAccountConfig you used above for the SafeFactory
const safeAccountConfig: SafeAccountConfig = {
owners: [
await owner1Signer.getAddress()
],
threshold: 1,
// ... (Optional params)
}
const protocolKit = await Safe.create({ ethAdapter, predictedSafe: safeAccountConfig })
const balance = await protocolKit.getBalance()
This will make it work.
Also check that you are creating an instance of the SafeApiKit in the init function, which is not in use, but it's trying to use mainnet while you configured a sepolia RPC. This will cause problems also, so either remove it or set the correct chainId
Also as another optimization, to get the predicted Safe address you can directly use the protocolKit instead of going through SafeFactory. If you use Safe.create() with predictedSafe as I shared above and then you use:
const predictedSafeAddress = await protocolKit.getAddress()
OK, I see. When I re-try, I got the balance below.
balance 1000000000000000n
This balance is the balance of predictedSafeAddress ? chainId is : 11155111.
I will send some ether test coin to the predictedSafeAddress and re-check balance.
I successfully send some test ether to the predictedSafeAddress and when I check balance, I got : balance 1000000000000000n . It seem not show the exact balance of predictedSafeAddress.
https://sepolia.etherscan.io/tx/0xb945107d95c5dc1b7d007486b62e429e729f3e06d5fd5573f226709305393265
I'm not really sure if it's the expected balance.
1000000000000000n is the value in wei, so it equals to 0.001which it was the value in the account at some moment
As no other feedback was given we will consider this issue as closed.
Feel free to open a new one if you have issues.