Skip to content

Instantly share code, notes, and snippets.

@SKYBITDev3
Last active October 8, 2023 18:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save SKYBITDev3/943f6389862d1ba68d02ddad72a8c208 to your computer and use it in GitHub Desktop.
Save SKYBITDev3/943f6389862d1ba68d02ddad72a8c208 to your computer and use it in GitHub Desktop.
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.21;
import "@openzeppelin/contracts/token/ERC20/ERC20.sol";
import "@openzeppelin/contracts/token/ERC20/extensions/ERC20Permit.sol";
import "@openzeppelin/contracts/access/AccessControl.sol";
// import "hardhat/console.sol";
struct Point {
uint x;
uint y;
}
contract TESTERC20 is ERC20, ERC20Permit, AccessControl {
Point public point;
bytes public b;
constructor(
string memory name,
string memory symbol,
uint initialSupply,
address[] memory initialHolders,
Point memory _point,
bytes memory _b
) ERC20(name, symbol) ERC20Permit(name) {
_mint(
initialHolders[0],
(10 * (initialSupply * 10 ** decimals())) / 100
);
_mint(
initialHolders[1],
(90 * (initialSupply * 10 ** decimals())) / 100
);
point = _point;
b = _b;
// _grantRole(DEFAULT_ADMIN_ROLE, msg.sender); // if deploying this contract via a factory then msg.sender is factory's address
_grantRole(
DEFAULT_ADMIN_ROLE,
tx.origin
);
}
function privilegedFunction()
public
view
onlyRole(DEFAULT_ADMIN_ROLE)
returns (uint8)
{
return 1;
}
}
async function main() {
const { ethers, network } = require(`hardhat`)
const [ wallet1, wallet2 ] = await ethers.getSigners()
const walletToUse = wallet1 // switch between wallet1 & wallet2 to see the difference in addresses
// const walletToUse = wallet2 // switch between wallet1 & wallet2 to see the difference in addresses
console.log(`Using network: ${network.name} (${network.config.chainId}), account: ${walletToUse.address} having ${await ethers.formatUnits(await ethers.provider.getBalance(walletToUse.address), `ether`)} of native currency, RPC url: ${network.config.url}`)
// Deploy Arachnid's factory
const addressOfFactory = `0x4e59b44847b379578588920ca78fbf26c0b4956c` // See https://github.com/Arachnid/deterministic-deployment-proxy. Difference with Zoltu's factory is that salt must be prepended to bytecode of contract being deployed.
if (await ethers.provider.getCode(addressOfFactory) === `0x`) {
// Fund account of signer of transaction that deploys Arachnid's factory.
const addressOfSignerToDeployArachnidsFactory = `0x3fab184622dc19b6109349b94811493bf2a45362`
let txResponse = await walletToUse.sendTransaction({ to: addressOfSignerToDeployArachnidsFactory, value: ethers.parseUnits(`0.1`, `ether`) })
await txResponse.wait()
const txSserialized = `0xf8a58085174876e800830186a08080b853604580600e600039806000f350fe7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffe03601600081602082378035828234f58015156039578182fd5b8082525050506014600cf31ba02222222222222222222222222222222222222222222222222222222222222222a02222222222222222222222222222222222222222222222222222222222222222`
txResponse = await ethers.provider.broadcastTransaction(txSserialized)
const txReceipt = await txResponse.wait()
}
console.log(`Address of Arachnid's factory deployed keylessly: ${addressOfFactory}`)
// Deploy Contract via Arachnid's factory
// get contract bytecode
const contractName = `TESTERC20`
const constructorArgs = [
`Token 4630`,
`TOKEN4630`,
100,
[ wallet1.address, wallet2.address ], // test array constructor argument
{ x: 10, y: 5 }, // test struct constructor argument
`0xabcdef`, // test byte constructor argument. bytes have to be 0x-prefixed
]
const cfToken = await ethers.getContractFactory(contractName)
const bytecodeOfContractWithArgs = (await cfToken.getDeployTransaction(...constructorArgs)).data
const salt = ethers.encodeBytes32String(`XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX`)
const txData = {
from: walletToUse.address,
to: addressOfFactory,
data: bytecodeOfContractWithArgs.replace(`0x`, salt),
}
const expectedAddressUsingCall = await walletToUse.call(txData)
console.log(`expected address using await walletToUse.call(txData): ${expectedAddressUsingCall}`)
const expectedAddressCalculated = ethers.getCreate2Address(addressOfFactory, salt, ethers.keccak256(bytecodeOfContractWithArgs))
console.log(`expected address using ethers.getCreate2Address: ${expectedAddressCalculated}`)
console.log(`Now deploying ${contractName} using Arachnid's factory...`)
const txReceipt = await walletToUse.sendTransaction(txData)
await txReceipt.wait()
console.log(`${contractName} was deployed by ${walletToUse.address}`)
console.log(`Testing deployed contract by getting contract at address ${expectedAddressUsingCall}:`)
const contract = await ethers.getContractAt(contractName, expectedAddressUsingCall, walletToUse)
console.log(`${walletToUse.address} calling contract.point(): ${await contract.point()}`) // Test deployed Contract contract at expected address
console.log(`${walletToUse.address} calling contract.privilegedFunction(): ${await contract.privilegedFunction()}`) // Test function that only a privileged account can call
}
main().catch(error => {
console.error(error)
process.exitCode = 1
})
Using network: hardhat (31337), account: 0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266 having 10000.0 of native currency, RPC url: undefined
Address of Arachnid's factory deployed keylessly: 0x4e59b44847b379578588920ca78fbf26c0b4956c
expected address using await walletToUse.call(txData): 0xd8d463e3a19f1ea97e8a62670054515f3f38b740
expected address using ethers.getCreate2Address: 0xd8D463e3a19F1eA97E8A62670054515f3f38B740
Now deploying TESTERC20 using Arachnid's factory...
TESTERC20 was deployed by 0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266
Testing deployed contract by getting contract at address 0xd8D463e3a19F1eA97E8A62670054515f3f38B740:
0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266 calling contract.point(): 10,5
0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266 calling contract.privilegedFunction(): 1
Using network: hardhat (31337), account: 0x70997970C51812dc3A010C7d01b50e0d17dc79C8 having 10000.0 of native currency, RPC url: undefined
Address of Arachnid's factory deployed keylessly: 0x4e59b44847b379578588920ca78fbf26c0b4956c
expected address using await walletToUse.call(txData): 0xd8d463e3a19f1ea97e8a62670054515f3f38b740
expected address using ethers.getCreate2Address: 0xd8D463e3a19F1eA97E8A62670054515f3f38B740
Now deploying TESTERC20 using Arachnid's factory...
TESTERC20 was deployed by 0x70997970C51812dc3A010C7d01b50e0d17dc79C8
Testing deployed contract by getting contract at address 0xd8D463e3a19F1eA97E8A62670054515f3f38B740:
0x70997970C51812dc3A010C7d01b50e0d17dc79C8 calling contract.point(): 10,5
0x70997970C51812dc3A010C7d01b50e0d17dc79C8 calling contract.privilegedFunction(): 1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment