import {ethers} from "ethers";
import {queryHelperAbi} from "@/blockchain/abi.js";


export function vaultAddress( factory, vaultInitCodeHash, owner, num=0) {
    if( !owner || !factory || !vaultInitCodeHash )
        return null
    const salt = ethers.solidityPackedKeccak256(['address','uint8'],[owner,num])
    return ethers.getCreate2Address(factory, salt, vaultInitCodeHash)
}


export function contractOrNull(addr,abi,provider) {
    try {
        return new ethers.Contract(addr,abi,provider)
    }
    catch (e) {
        return null
    }
}

export async function queryHelperContract(helper, provider) {
    return contractOrNull(helper, queryHelperAbi, provider)
}

