-
Notifications
You must be signed in to change notification settings - Fork 169
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
User can deposit with a token transfer (#42)
Implemented mint tickets via simple token transfer. Gas cost: 75000 on top of mint tickets
- Loading branch information
1 parent
0528152
commit e082ab3
Showing
6 changed files
with
150 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
pragma solidity ^0.6.4; | ||
|
||
import "../periodic-prize-pool/PeriodicPrizePoolInterface.sol"; | ||
import "@openzeppelin/contracts-ethereum-package/contracts/token/ERC20/IERC20.sol"; | ||
|
||
contract CounterfactualAction { | ||
function mintTickets(address payable user, PeriodicPrizePoolInterface prizePool, bytes calldata data) external { | ||
IERC20 token = prizePool.token(); | ||
uint256 amount = token.balanceOf(address(this)); | ||
token.approve(address(prizePool), amount); | ||
prizePool.mintTickets(user, amount, data); | ||
selfdestruct(user); | ||
} | ||
|
||
function cancel(address payable user, PeriodicPrizePoolInterface prizePool) external { | ||
IERC20 token = prizePool.token(); | ||
token.transfer(user, token.balanceOf(address(this))); | ||
selfdestruct(user); | ||
} | ||
} |
43 changes: 43 additions & 0 deletions
43
contracts/counterfactual-action/CounterfactualActionFactory.sol
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
pragma solidity ^0.6.4; | ||
|
||
import "@openzeppelin/contracts-ethereum-package/contracts/utils/Create2.sol"; | ||
import "./CounterfactualAction.sol"; | ||
import "../utils/MinimalProxyLibrary.sol"; | ||
|
||
contract CounterfactualActionFactory { | ||
|
||
CounterfactualAction depositor; | ||
PeriodicPrizePoolInterface prizePool; | ||
|
||
function initialize(PeriodicPrizePoolInterface _prizePool) external { | ||
require(address(_prizePool) != address(0), "CounterfactualActionFactory/prize-pool-not-zero"); | ||
depositor = new CounterfactualAction(); | ||
prizePool = _prizePool; | ||
} | ||
|
||
function calculateAddress(address payable user) external view returns (address) { | ||
return Create2.computeAddress(salt(user), keccak256(MinimalProxyLibrary.minimalProxy(address(depositor)))); | ||
} | ||
|
||
function mintTickets(address payable user, bytes calldata data) external { | ||
CounterfactualAction d = newAction(user); | ||
d.mintTickets(user, prizePool, data); | ||
} | ||
|
||
function cancel(address payable user) external { | ||
CounterfactualAction d = newAction(user); | ||
d.cancel(user, prizePool); | ||
} | ||
|
||
function newAction(address payable user) internal returns (CounterfactualAction) { | ||
return CounterfactualAction(Create2.deploy(0, salt(user), MinimalProxyLibrary.minimalProxy(address(depositor)))); | ||
} | ||
|
||
function salt(address payable user) internal pure returns (bytes32) { | ||
return keccak256(abi.encodePacked(user)); | ||
} | ||
|
||
function code() external view returns (bytes memory) { | ||
return MinimalProxyLibrary.minimalProxy(address(depositor)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
pragma solidity ^0.6.4; | ||
|
||
// solium-disable security/no-inline-assembly | ||
library MinimalProxyLibrary { | ||
function minimalProxy(address _logic) internal pure returns (bytes memory clone) { | ||
// Adapted from https://github.com/optionality/clone-factory/blob/32782f82dfc5a00d103a7e61a17a5dedbd1e8e9d/contracts/CloneFactory.sol | ||
bytes20 targetBytes = bytes20(_logic); | ||
assembly { | ||
let size := 0x37 | ||
// allocate output byte array - this could also be done without assembly | ||
// by using clone = new bytes(size) | ||
clone := mload(0x40) | ||
// new "memory end" including padding | ||
mstore(0x40, add(clone, and(add(add(size, 0x20), 0x1f), not(0x1f)))) | ||
// store length in memory | ||
mstore(clone, size) | ||
mstore(add(clone, 0x20), 0x3d602d80600a3d3981f3363d3d373d3d3d363d73000000000000000000000000) | ||
mstore(add(clone, 0x34), targetBytes) | ||
mstore(add(clone, 0x48), 0x5af43d82803e903d91602b57fd5bf30000000000000000000000000000000000) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
const { expect } = require('chai') | ||
const CounterfactualActionFactory = require('../build/CounterfactualActionFactory.json') | ||
const PeriodicPrizePoolInterface = require('../build/PeriodicPrizePoolInterface.json') | ||
const IERC20 = require('../build/IERC20.json') | ||
const { ethers } = require('./helpers/ethers') | ||
const buidler = require('./helpers/buidler') | ||
const { deployContract, deployMockContract } = require('ethereum-waffle') | ||
|
||
const toWei = ethers.utils.parseEther | ||
|
||
describe('CounterfactualActionFactory', () => { | ||
|
||
let wallet, wallet2 | ||
|
||
let token, prizePool | ||
|
||
let provider | ||
|
||
beforeEach(async () => { | ||
[wallet, wallet2] = await buidler.ethers.getSigners() | ||
provider = buidler.ethers.provider | ||
|
||
token = await deployMockContract(wallet, IERC20.abi) | ||
prizePool = await deployMockContract(wallet, PeriodicPrizePoolInterface.abi) | ||
await prizePool.mock.token.returns(token.address) | ||
|
||
factory = await deployContract(wallet, CounterfactualActionFactory, []) | ||
await factory.initialize(prizePool.address) | ||
}) | ||
|
||
describe('mintTickets', () => { | ||
it('should allow mintTickets from anyone', async () => { | ||
let address = await factory.calculateAddress(wallet._address) | ||
let depositAmount = toWei('100') | ||
|
||
await token.mock.balanceOf.withArgs(address).returns(depositAmount) | ||
await token.mock.approve.withArgs(prizePool.address, depositAmount).returns(true) | ||
await prizePool.mock.mintTickets.withArgs(wallet._address, depositAmount, []).returns() | ||
|
||
await factory.mintTickets(wallet._address, []) | ||
}) | ||
}) | ||
|
||
describe('cancel', () => { | ||
it('should someone to withdraw their funds', async () => { | ||
let address = await factory.calculateAddress(wallet._address) | ||
let depositAmount = toWei('100') | ||
|
||
await token.mock.balanceOf.withArgs(address).returns(depositAmount) | ||
await token.mock.transfer.withArgs(wallet._address, depositAmount).returns(true) | ||
|
||
await factory.cancel(wallet._address, []) | ||
}) | ||
}) | ||
|
||
describe('code()', () => { | ||
it("should show the same code", async () => { | ||
let code = await factory.code() | ||
|
||
expect(code.length).to.equal(112) | ||
}) | ||
}) | ||
|
||
}) |