Skip to content

Commit

Permalink
Foundation (Uniswap#80)
Browse files Browse the repository at this point in the history
* start

* continue

* continue

* update snapshots

* prettier

* update snapshot

* update narwhal-sdk to 1.0.0-beta.10

Co-authored-by: Alice Henshaw <[email protected]>
  • Loading branch information
azflin and hensha256 authored Oct 12, 2022
1 parent 9aa27ea commit f8675a8
Show file tree
Hide file tree
Showing 8 changed files with 1,769 additions and 6 deletions.
3 changes: 3 additions & 0 deletions contracts/base/Commands.sol
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ contract Commands is V2SwapRouter, V3SwapRouter, RouterCallbacks {
uint256 constant WRAP_ETH = 0x07;
uint256 constant UNWRAP_WETH = 0x08;
uint256 constant SWEEP = 0x09;
uint256 constant FOUNDATION = 0x0f;

address immutable PERMIT_POST;

Expand Down Expand Up @@ -96,6 +97,8 @@ contract Commands is V2SwapRouter, V3SwapRouter, RouterCallbacks {
} else if (command == UNWRAP_WETH) {
(address recipient, uint256 amountMin) = abi.decode(inputs, (address, uint256));
Payments.unwrapWETH9(recipient, amountMin);
} else if (command == FOUNDATION) {
(success, output) = callAndTransfer721(inputs, Constants.FOUNDATION);
} else {
revert InvalidCommandType(command);
}
Expand Down
3 changes: 3 additions & 0 deletions contracts/libraries/Constants.sol
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,7 @@ library Constants {

/// @dev The address of LooksRare on mainnet
address internal constant X2Y2 = 0x74312363e45DCaBA76c59ec49a7Aa8A65a67EeD3;

// @dev The address of Foundation on mainnet
address internal constant FOUNDATION = 0xcDA72070E455bb31C7690a170224Ce43623d0B6f;
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
"@typechain/ethers-v5": "^4.0.0",
"@types/chai": "^4.2.6",
"@types/mocha": "^5.2.7",
"@uniswap/narwhal-sdk": "1.0.0-beta.9",
"@uniswap/narwhal-sdk": "1.0.0-beta.10",
"@uniswap/router-sdk": "^1.3.0",
"@uniswap/sdk-core": "^3.0.1",
"@uniswap/snapshot-gas-cost": "^1.0.0",
Expand Down
88 changes: 88 additions & 0 deletions test/integration-tests/Foundation.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
import { RouterPlanner, FoundationCommand } from '@uniswap/narwhal-sdk'
import FOUNDATION_ABI from './shared/abis/Foundation.json'
import { Router, ERC721 } from '../../typechain'
import { resetFork } from './shared/mainnetForkHelpers'
import {
ALICE_ADDRESS,
DEADLINE,
V2_FACTORY_MAINNET,
V3_FACTORY_MAINNET,
V2_INIT_CODE_HASH_MAINNET,
V3_INIT_CODE_HASH_MAINNET,
} from './shared/constants'
import snapshotGasCost from '@uniswap/snapshot-gas-cost'
import { SignerWithAddress } from '@nomiclabs/hardhat-ethers/signers'
import hre from 'hardhat'
import { BigNumber } from 'ethers'
import { abi as ERC721_ABI } from '../../artifacts/solmate/src/tokens/ERC721.sol/ERC721.json'
import { expect } from 'chai'
const { ethers } = hre

const FOUNDATION_INTERFACE = new ethers.utils.Interface(FOUNDATION_ABI)
const MENTAL_WORLDS_ADDRESS = '0xEf96021Af16BD04918b0d87cE045d7984ad6c38c'
const REFERRER = '0x459e213D8B5E79d706aB22b945e3aF983d51BC4C'

describe('Foundation', () => {
let alice: SignerWithAddress
let router: Router
let planner: RouterPlanner

beforeEach(async () => {
planner = new RouterPlanner()
alice = await ethers.getSigner(ALICE_ADDRESS)
})

// In this test we will buy token id 32 of mental worlds NFT (0xEf96021Af16BD04918b0d87cE045d7984ad6c38c),
// which costs 0.01 ETH
describe('Buy a mental worlds NFT from Foundation', () => {
let mentalWorlds: ERC721

beforeEach(async () => {
await resetFork(15725945)
await hre.network.provider.request({
method: 'hardhat_impersonateAccount',
params: [ALICE_ADDRESS],
})
const routerFactory = await ethers.getContractFactory('Router')
router = (
await routerFactory.deploy(
ethers.constants.AddressZero,
V2_FACTORY_MAINNET,
V3_FACTORY_MAINNET,
V2_INIT_CODE_HASH_MAINNET,
V3_INIT_CODE_HASH_MAINNET
)
).connect(alice) as Router

mentalWorlds = new ethers.Contract(MENTAL_WORLDS_ADDRESS, ERC721_ABI) as ERC721
})

it('purchases token id 32 of mental worlds', async () => {
const value = BigNumber.from('10000000000000000')
const calldata = FOUNDATION_INTERFACE.encodeFunctionData('buyV2', [MENTAL_WORLDS_ADDRESS, 32, value, REFERRER])
planner.add(FoundationCommand(value, calldata, ALICE_ADDRESS, MENTAL_WORLDS_ADDRESS, 32))
const { commands, state } = planner.plan()

const aliceBalance = await ethers.provider.getBalance(alice.address)
const referrerBalance = await ethers.provider.getBalance(REFERRER)
const receipt = await (await router.execute(DEADLINE, commands, state, { value: value })).wait()

// Expect that alice has the NFT
await expect((await mentalWorlds.connect(alice).ownerOf(32)).toLowerCase()).to.eq(ALICE_ADDRESS)
// Expect that alice's account has 0.01 (plus gas) less ETH in it
await expect(aliceBalance.sub(await ethers.provider.getBalance(alice.address))).to.eq(
value.add(receipt.gasUsed.mul(receipt.effectiveGasPrice))
)
// Expect that referrer's account has 0.0001 more ETH in it (referrers receive 1% of NFT value)
await expect((await ethers.provider.getBalance(REFERRER)).sub(referrerBalance)).to.eq(value.div(100))
})

it('gas token id 32 of mental worlds', async () => {
const value = BigNumber.from('10000000000000000')
const calldata = FOUNDATION_INTERFACE.encodeFunctionData('buyV2', [MENTAL_WORLDS_ADDRESS, 32, value, REFERRER])
planner.add(FoundationCommand(value, calldata, ALICE_ADDRESS, MENTAL_WORLDS_ADDRESS, 32))
const { commands, state } = planner.plan()
await snapshotGasCost(router.execute(DEADLINE, commands, state, { value: value }))
})
})
})
8 changes: 8 additions & 0 deletions test/integration-tests/__snapshots__/Foundation.test.ts.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`Foundation Buy a mental worlds NFT from Foundation gas token id 32 of mental worlds 1`] = `
Object {
"calldataByteLength": 836,
"gasUsed": 187936,
}
`;
2 changes: 1 addition & 1 deletion test/integration-tests/__snapshots__/Router.test.ts.snap
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`Router bytecode size 1`] = `10058`;
exports[`Router bytecode size 1`] = `10164`;
Loading

0 comments on commit f8675a8

Please sign in to comment.