0

I'm trying to build a payment gateway and I generated address of a contract before deploying it with create2. how to deploy the contract by sending ether to new generated address?

pragma solidity 0.8.14;

contract A{
    receive() payable external{
        set = "hello";
    }
    string public set;
    constructor() payable {
        set = "world";
    }

}

contract B{

    event deploy(address addr);
    function Deploy(uint _salt) external{
        A me = new A{salt: bytes32(_salt)}();
        emit deploy(address(me));
    }

    function getaddress(uint _salt) public view returns(address){
    bytes32 hash = keccak256(abi.encodePacked(bytes1(0xFF),address(this),_salt , keccak256(getBytecode(msg.sender))));
    return address(uint160(uint(hash)));
    }

    function getBytecode(address _owner) public pure returns(bytes memory){
    bytes memory bytecode = type(A).creationCode;
    return abi.encodePacked(bytecode , abi.encode(_owner));
    }

}

here is the code. i know the address of contract A before deploying it using getaddress function , i want to send ether to contract A using the calculated address and then contract A deploy and trigger something like emit an event that contract received the funds.

4
  • 1
    is the contract already deployed? you can deploy with payments by making the constructor payable. you would also be able to send funds to the contract after deployment if it has a payable fallback function and/or payable receive function
    – sola24
    Commented Jun 14, 2022 at 16:06
  • no it's not deployed but i calculated the address and sent it some funds it received the funds but it didn't deploy. i tried payable constructor , fallback and receive functions.
    – Meysam
    Commented Jun 14, 2022 at 16:28
  • ok. just wondering as I wasn't sure where you were at with it. you can send funds to the address in advance of deploying the contract like you said. What I mentioned was just so you could do the same at deployment time and subsequently if more convenient. I don't understand what you mean by it received the funds but didn't deploy. The contract didn't deploy? Could you clarify what you mean and post the errors/output you get when the contract fails to deploy? Is this on a localhost blockchain / testnet / mainnet? The more info the better
    – sola24
    Commented Jun 14, 2022 at 20:45
  • i edited the question
    – Meysam
    Commented Jun 15, 2022 at 10:16

1 Answer 1

1

based on your latest comment ... there are a few moving parts and choices / ways to skin the cat, but ...

you'd want to monitor the predetermined addresses off-chain and could use a node like alchemy + javascript + ethers to check the predetermined addresses for non-zero balances. When a non-zero balance is found your deploy script is called passing the target address to deploy to.

here's some rough demo code that checks addresses for a balance. uses: nodejs, ethers, dotenv (just for the alchemy URL)

// todo: schedule this task or loop on timer or something

require("dotenv").config();
const NODE_URL = process.env.NODE_URL
var ethers = require('ethers');
var url = NODE_URL;
var provider = new ethers.providers.JsonRpcProvider(url);

const TARGET_ADDRESS1 = "0x3C283D39436c8F5B48E84740e938C745a45c2Ab1";
const TARGET_ADDRESS2 = "0x0eE4A43F8E4eaf59886E1118f61c919F9815f59B";
const TARGET_ADDRESS3 = "0x97ed92E744C10FdD5D403A756239C4069e415e79";
const addresses = [TARGET_ADDRESS1, TARGET_ADDRESS2, TARGET_ADDRESS3];

for(let address of addresses) getBalance(address);

async function getBalance(address) {
    const balance = await provider.getBalance(address);
    console.log(`balance at ${address}: ${balance}`);

    if(balance > 0) {
        console.log(`deploying contract to : ${address}`)
        // pseudo code
        // run deploy script passing deployment address
    }
}

output:

$ node monitor.js
balance at 0x3C283D39436c8F5B48E84740e938C745a45c2Ab1: 0
balance at 0x0eE4A43F8E4eaf59886E1118f61c919F9815f59B: 0
balance at 0x97ed92E744C10FdD5D403A756239C4069e415e79: 80000000000000000
deploying contract to : 0x97ed92E744C10FdD5D403A756239C4069e415e79
1
  • i know how event works but my problem is i want if someone send ether to contract A then the contract deploys i dont want to deploy it before sending ether. i want to the deploying process be automatic
    – Meysam
    Commented Jun 15, 2022 at 11:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.