Hi @msolomon4,
Using OpenZeppelin ProxyFactory
I can create a Forwarder from the ForwarderFactory.
Have a look at the contracts and test below.
Forwarder.sol
pragma solidity ^0.5.0;
import "@openzeppelin/contracts-ethereum-package/contracts/ownership/Ownable.sol";
import "@openzeppelin/upgrades/contracts/Initializable.sol";
contract Forwarder is Initializable, Ownable {
address public admin;
uint256 public version;
event Initialized(address indexed thisAddress);
function initialize(address _recipient, address _admin) public initializer {
emit Initialized(address(this));
Ownable.initialize(_recipient);
admin = _admin;
version = 1;
}
}
ForwarderFactory.sol
pragma solidity ^0.5.0;
import "@openzeppelin/upgrades/contracts/upgradeability/ProxyFactory.sol";
import "./Forwarder.sol";
contract ForwarderFactory is ProxyFactory {
address[] public forwarders;
address[] public users;
mapping (address => address) public getForwarder; // maps user => forwarder
event ForwarderCreated(address indexed _address);
function createForwarder(address _target, address _user) external {
address _admin = msg.sender;
bytes memory _payload = abi.encodeWithSignature("initialize(address,address)", _user, _admin);
// Deploy proxy
address _forwarder = deployMinimal(_target, _payload);
emit ForwarderCreated(_forwarder);
// Update state
forwarders.push(_forwarder);
users.push(_user);
getForwarder[_user] = _forwarder;
}
}
ForwarderFactory.test.js
const { accounts, contract } = require('@openzeppelin/test-environment');
const {
BN, // Big Number support
constants, // Common constants, like the zero address and largest integers
expectEvent, // Assertions for emitted events
expectRevert, // Assertions for transactions that should fail
} = require('@openzeppelin/test-helpers');
const [ creator, user ] = accounts;
const { expect } = require('chai');
const Forwarder = contract.fromArtifact('Forwarder');
const ForwarderFactory = contract.fromArtifact('ForwarderFactory');
describe('ForwarderFactory', function () {
it('creates proxy', async function () {
const factory = await ForwarderFactory.new();
const logic = await Forwarder.new();
await factory.createForwarder(logic.address, user);
forwarderAddress = await factory.forwarders(0);
forwarder = await Forwarder.at(forwarderAddress);
expect(await forwarder.version()).to.be.bignumber.equal("1");
});
});