How is the implementation contract instance created in the proxy?

It seems to me like the implementation contract instance is created within the creation of the proxy contract when deployed I use the upgrades.deployProxy() function as creation of the implementation contract is an internal tx in the proxy. But I can't seem to find where the creation of the implementation contract was actually being created within the proxy.

How is this done and in which function and line is this done in the ERC1967UpgradeUpgradeable?

:computer: Environment

Hardhat, OZ upgrades plugin.

Hey @atom! The upgrades.deployProxy command in the Hardhat upgrades plugin actually can send more than one transaction under the hood: if the implementation contract has not been yet deployed, it will deploy it, and after that it will send another tx deploy the proxy pointing to it.

If you're interested in the internals, here is where the implementation is deployed:

And here the proxy:

Thanks @spalladino! I still don't understand how the plugin achieved both the creation of the implementation contract and the proxy contract itself and then update the implementation address all in a single transaction.

If I didn't interpret the code wrongly, it seems to me that the code you have highlighted will perform 2 separate transactions, one for the deployment of the proxy contract and the other for the implementation contract. However, I have noticed that there is only a single transaction on etherscan for the deployment of the proxy contract. Within the proxy contract though, it has an internal transaction that does the deployment of the implementation contract, which I presume would mean the proxy contract might have created the implementation contract. But how is this achieved?