How to write off chain Method for predictDeterministicAddress in clones.sol

I want to write off chain aka javascript method for same.

        address implementation,
        bytes32 salt,
        address deployer
    ) internal pure returns (address predicted) {
        assembly {
            let ptr := mload(0x40)
            mstore(ptr, 0x3d602d80600a3d3981f3363d3d373d3d3d363d73000000000000000000000000)
            mstore(add(ptr, 0x14), shl(0x60, implementation))
            mstore(add(ptr, 0x28), 0x5af43d82803e903d91602b57fd5bf3ff00000000000000000000000000000000)
            mstore(add(ptr, 0x38), shl(0x60, deployer))
            mstore(add(ptr, 0x4c), salt)
            mstore(add(ptr, 0x6c), keccak256(ptr, 0x37))
            predicted := keccak256(add(ptr, 0x37), 0x55)
        }
    }```
2 Likes

This should work:

import { stripHexPrefix } from "ethereumjs-util";

const implementation = stripHexPrefix(implementationContract.address.toLowerCase()).padStart(40, "0");
const initCode = `0x3d602d80600a3d3981f3363d3d373d3d3d363d73${implementation}5af43d82803e903d91602b57fd5bf3`;
const initCodeHash = keccak256(initCode);

const calculatedAddress = getCreate2Address(from, salt, initCodeHash);

const predictedAddress = await Clones.predictDeterministicAddress(implementationContract.address, salt);

expect(predictedAddress).to.equal(calculatedAddress);