Skip to content

Commit

Permalink
Merge pull request #804 from nounsDAO/soli-optimize-trait-count-getters
Browse files Browse the repository at this point in the history
Optimize Art Contract Trait Count Getters
  • Loading branch information
solimander authored Oct 16, 2023
2 parents 2b6d0f2 + 4744284 commit 30bcff8
Show file tree
Hide file tree
Showing 8 changed files with 1,595 additions and 36 deletions.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

30 changes: 29 additions & 1 deletion packages/nouns-contracts/contracts/NounsArt.sol
Original file line number Diff line number Diff line change
Expand Up @@ -330,10 +330,38 @@ contract NounsArt is INounsArt {
/**
* @notice Get the number of available Noun `backgrounds`.
*/
function backgroundsCount() public view override returns (uint256) {
function backgroundCount() external view returns (uint256) {
return backgrounds.length;
}

/**
* @notice Get the number of available Noun `bodies`.
*/
function bodyCount() external view returns (uint256) {
return bodiesTrait.storedImagesCount;
}

/**
* @notice Get the number of available Noun `accessories`.
*/
function accessoryCount() external view returns (uint256) {
return accessoriesTrait.storedImagesCount;
}

/**
* @notice Get the number of available Noun `heads`.
*/
function headCount() external view returns (uint256) {
return headsTrait.storedImagesCount;
}

/**
* @notice Get the number of available Noun `glasses`.
*/
function glassesCount() external view returns (uint256) {
return glassesTrait.storedImagesCount;
}

/**
* @notice Get a head image bytes (RLE-encoded).
*/
Expand Down
10 changes: 5 additions & 5 deletions packages/nouns-contracts/contracts/NounsDescriptorV2.sol
Original file line number Diff line number Diff line change
Expand Up @@ -103,35 +103,35 @@ contract NounsDescriptorV2 is INounsDescriptorV2, Ownable {
* @notice Get the number of available Noun `backgrounds`.
*/
function backgroundCount() external view override returns (uint256) {
return art.backgroundsCount();
return art.backgroundCount();
}

/**
* @notice Get the number of available Noun `bodies`.
*/
function bodyCount() external view override returns (uint256) {
return art.getBodiesTrait().storedImagesCount;
return art.bodyCount();
}

/**
* @notice Get the number of available Noun `accessories`.
*/
function accessoryCount() external view override returns (uint256) {
return art.getAccessoriesTrait().storedImagesCount;
return art.accessoryCount();
}

/**
* @notice Get the number of available Noun `heads`.
*/
function headCount() external view override returns (uint256) {
return art.getHeadsTrait().storedImagesCount;
return art.headCount();
}

/**
* @notice Get the number of available Noun `glasses`.
*/
function glassesCount() external view override returns (uint256) {
return art.getGlassesTrait().storedImagesCount;
return art.glassesCount();
}

/**
Expand Down
11 changes: 9 additions & 2 deletions packages/nouns-contracts/contracts/interfaces/INounsArt.sol
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@

pragma solidity ^0.8.6;

import { Inflate } from '../libs/Inflate.sol';
import { IInflator } from './IInflator.sol';

interface INounsArt {
Expand Down Expand Up @@ -130,7 +129,15 @@ interface INounsArt {
uint16 imageCount
) external;

function backgroundsCount() external view returns (uint256);
function backgroundCount() external view returns (uint256);

function bodyCount() external view returns (uint256);

function accessoryCount() external view returns (uint256);

function headCount() external view returns (uint256);

function glassesCount() external view returns (uint256);

function backgrounds(uint256 index) external view returns (string memory);

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
// SPDX-License-Identifier: GPL-3.0
pragma solidity ^0.8.15;

import 'forge-std/Script.sol';
import { NounsDescriptorV2 } from '../contracts/NounsDescriptorV2.sol';
import { ISVGRenderer } from '../contracts/interfaces/ISVGRenderer.sol';
import { IInflator } from '../contracts/interfaces/IInflator.sol';
import { INounsArt } from '../contracts/interfaces/INounsArt.sol';
import { NounsArt } from '../contracts/NounsArt.sol';

contract UpgradeDescriptorV2PopulateArtFromExisting is Script {
NounsDescriptorV2 public constant EXISTING_DESCRIPTOR = NounsDescriptorV2(0x6229c811D04501523C6058bfAAc29c91bb586268);
ISVGRenderer public constant EXISTING_RENDERER = ISVGRenderer(0x81d94554A4b072BFcd850205f0c79e97c92aab56);
IInflator public constant EXISTING_INFLATOR = IInflator(0xa2acee85Cd81c42BcAa1FeFA8eD2516b68872Dbe);
NounsArt public constant EXISTING_ART = NounsArt(0x48A7C62e2560d1336869D6550841222942768C49);

function run() external {
uint256 upgraderKey = vm.envUint('UPGRADER_KEY');
address upgrader = vm.addr(upgraderKey);

address palettePointer = EXISTING_ART.palettesPointers(0);

uint256 backgroundCount = EXISTING_DESCRIPTOR.backgroundCount();
string[] memory backgrounds = new string[](backgroundCount);
for (uint256 i = 0; i < backgroundCount; i++) {
backgrounds[i] = EXISTING_ART.backgrounds(i);
}

INounsArt.Trait memory bodies = EXISTING_ART.getBodiesTrait();
INounsArt.Trait memory accessories = EXISTING_ART.getAccessoriesTrait();
INounsArt.Trait memory heads = EXISTING_ART.getHeadsTrait();
INounsArt.Trait memory glasses = EXISTING_ART.getGlassesTrait();

INounsArt predictedArt = INounsArt(computeCreateAddress(upgrader, vm.getNonce(upgrader) + 1));

vm.startBroadcast(upgraderKey);

// Deploy new contracts
NounsDescriptorV2 descriptor = new NounsDescriptorV2(predictedArt, EXISTING_RENDERER);
new NounsArt(address(descriptor), EXISTING_INFLATOR);

// Populate new art contract using existing pointers
descriptor.setPalettePointer(0, palettePointer);
descriptor.addManyBackgrounds(backgrounds);

for (uint256 i = 0; i < bodies.storagePages.length; i++) {
descriptor.addBodiesFromPointer(
bodies.storagePages[i].pointer,
bodies.storagePages[i].decompressedLength,
bodies.storagePages[i].imageCount
);
}
for (uint256 i = 0; i < accessories.storagePages.length; i++) {
descriptor.addAccessoriesFromPointer(
accessories.storagePages[i].pointer,
accessories.storagePages[i].decompressedLength,
accessories.storagePages[i].imageCount
);
}
for (uint256 i = 0; i < heads.storagePages.length; i++) {
descriptor.addHeadsFromPointer(
heads.storagePages[i].pointer,
heads.storagePages[i].decompressedLength,
heads.storagePages[i].imageCount
);
}
for (uint256 i = 0; i < glasses.storagePages.length; i++) {
descriptor.addGlassesFromPointer(
glasses.storagePages[i].pointer,
glasses.storagePages[i].decompressedLength,
glasses.storagePages[i].imageCount
);
}
vm.stopBroadcast();
}
}
4 changes: 2 additions & 2 deletions packages/nouns-contracts/test/foundry/NounsArt.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ contract NounsArtTest is Test, DescriptorHelpers {

art.addBackground('ffffff');

assertEq(art.backgroundsCount(), 1);
assertEq(art.backgroundCount(), 1);
assertEq(art.backgrounds(0), 'ffffff');
}

Expand All @@ -122,7 +122,7 @@ contract NounsArtTest is Test, DescriptorHelpers {
vm.prank(descriptor);
art.addManyBackgrounds(bgs);

assertEq(art.backgroundsCount(), 2);
assertEq(art.backgroundCount(), 2);
assertEq(art.backgrounds(0), 'ffffff');
assertEq(art.backgrounds(1), '000000');
}
Expand Down
36 changes: 10 additions & 26 deletions packages/nouns-contracts/test/foundry/NounsDescriptorV2.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -133,49 +133,33 @@ contract NounsDescriptorV2Test is Test {
}

function testBackgroundCountUsesArt() public {
vm.mockCall(address(art), abi.encodeWithSelector(NounsArt.backgroundsCount.selector), abi.encode(42));
vm.mockCall(address(art), abi.encodeWithSelector(NounsArt.backgroundCount.selector), abi.encode(42));
assertEq(descriptor.backgroundCount(), 42);
vm.clearMockedCalls();
}

function testBodyCountUsesArt() public {
vm.mockCall(
address(art),
abi.encodeWithSelector(NounsArt.getBodiesTrait.selector),
abi.encode(INounsArt.Trait({ storedImagesCount: 42, storagePages: new INounsArt.NounArtStoragePage[](0) }))
);
vm.prank(address(descriptor));
art.addBodiesFromPointer(address(0), 1, 42);
assertEq(descriptor.bodyCount(), 42);
vm.clearMockedCalls();
}

function testAccessoryCountUsesArt() public {
vm.mockCall(
address(art),
abi.encodeWithSelector(NounsArt.getAccessoriesTrait.selector),
abi.encode(INounsArt.Trait({ storedImagesCount: 42, storagePages: new INounsArt.NounArtStoragePage[](0) }))
);
vm.prank(address(descriptor));
art.addAccessoriesFromPointer(address(0), 1, 42);
assertEq(descriptor.accessoryCount(), 42);
vm.clearMockedCalls();
}

function testHeadCountUsesArt() public {
vm.mockCall(
address(art),
abi.encodeWithSelector(NounsArt.getHeadsTrait.selector),
abi.encode(INounsArt.Trait({ storedImagesCount: 42, storagePages: new INounsArt.NounArtStoragePage[](0) }))
);
vm.prank(address(descriptor));
art.addHeadsFromPointer(address(0), 1, 42);
assertEq(descriptor.headCount(), 42);
vm.clearMockedCalls();
}

function testGlassesCountUsesArt() public {
vm.mockCall(
address(art),
abi.encodeWithSelector(NounsArt.getGlassesTrait.selector),
abi.encode(INounsArt.Trait({ storedImagesCount: 42, storagePages: new INounsArt.NounArtStoragePage[](0) }))
);
vm.prank(address(descriptor));
art.addGlassesFromPointer(address(0), 1, 42);
assertEq(descriptor.glassesCount(), 42);
vm.clearMockedCalls();
}

function testAddManyBackgroundsUsesArt() public {
Expand Down Expand Up @@ -477,7 +461,7 @@ contract NounsDescriptorV2Test is Test {
}

function _makeArtGettersNotRevert() internal {
vm.mockCall(address(art), abi.encodeWithSelector(NounsArt.backgroundsCount.selector), abi.encode(123));
vm.mockCall(address(art), abi.encodeWithSelector(INounsArt.backgroundCount.selector), abi.encode(123));
vm.mockCall(address(art), abi.encodeWithSelector(INounsArt.backgrounds.selector), abi.encode('return value'));
vm.mockCall(address(art), abi.encodeWithSelector(INounsArt.bodies.selector), abi.encode('return value'));
vm.mockCall(address(art), abi.encodeWithSelector(INounsArt.accessories.selector), abi.encode('return value'));
Expand Down

0 comments on commit 30bcff8

Please sign in to comment.