Skip to content

Commit

Permalink
Native currency label (Uniswap#222)
Browse files Browse the repository at this point in the history
* allow the native currency label to be specified in the nft descriptor library

* tests

* fix the comment
  • Loading branch information
moodysalem authored Nov 4, 2021
1 parent ee79829 commit de4e437
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 13 deletions.
24 changes: 21 additions & 3 deletions contracts/NonfungibleTokenPositionDescriptor.sol
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,25 @@ contract NonfungibleTokenPositionDescriptor is INonfungibleTokenPositionDescript
address private constant WBTC = 0x2260FAC5E5542a773Aa44fBCfeDf7C193bc2C599;

address public immutable WETH9;
/// @dev A null-terminated string
bytes32 public immutable nativeCurrencyLabelBytes;

constructor(address _WETH9) {
constructor(address _WETH9, bytes32 _nativeCurrencyLabelBytes) {
WETH9 = _WETH9;
nativeCurrencyLabelBytes = _nativeCurrencyLabelBytes;
}

/// @notice Returns the native currency label as a string
function nativeCurrencyLabel() public view returns (string memory) {
uint256 len = 0;
while (len < 32 && nativeCurrencyLabelBytes[len] != 0) {
len++;
}
bytes memory b = new bytes(len);
for (uint256 i = 0; i < len; i++) {
b[i] = nativeCurrencyLabelBytes[i];
}
return string(b);
}

/// @inheritdoc INonfungibleTokenPositionDescriptor
Expand Down Expand Up @@ -58,9 +74,11 @@ contract NonfungibleTokenPositionDescriptor is INonfungibleTokenPositionDescript
quoteTokenAddress: quoteTokenAddress,
baseTokenAddress: baseTokenAddress,
quoteTokenSymbol: quoteTokenAddress == WETH9
? 'ETH'
? nativeCurrencyLabel()
: SafeERC20Namer.tokenSymbol(quoteTokenAddress),
baseTokenSymbol: baseTokenAddress == WETH9 ? 'ETH' : SafeERC20Namer.tokenSymbol(baseTokenAddress),
baseTokenSymbol: baseTokenAddress == WETH9
? nativeCurrencyLabel()
: SafeERC20Namer.tokenSymbol(baseTokenAddress),
quoteTokenDecimals: IERC20Metadata(quoteTokenAddress).decimals(),
baseTokenDecimals: IERC20Metadata(baseTokenAddress).decimals(),
flipRatio: _flipRatio,
Expand Down
43 changes: 43 additions & 0 deletions test/NonfungibleTokenPositionDescriptor.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -170,5 +170,48 @@ describe('NonfungibleTokenPositionDescriptor', () => {
expect(metadata.name).to.match(/TEST\/TEST/)
expect(metadata.description).to.match(/TEST-TEST/)
})

it('can render a different label for native currencies', async () => {
const [token0, token1] = sortedTokens(weth9, tokens[1])
await nft.createAndInitializePoolIfNecessary(
token0.address,
token1.address,
FeeAmount.MEDIUM,
encodePriceSqrt(1, 1)
)
await weth9.approve(nft.address, 100)
await tokens[1].approve(nft.address, 100)
await nft.mint({
token0: token0.address,
token1: token1.address,
fee: FeeAmount.MEDIUM,
tickLower: getMinTick(TICK_SPACINGS[FeeAmount.MEDIUM]),
tickUpper: getMaxTick(TICK_SPACINGS[FeeAmount.MEDIUM]),
recipient: wallets[0].address,
amount0Desired: 100,
amount1Desired: 100,
amount0Min: 0,
amount1Min: 0,
deadline: 1,
})

const nftDescriptorLibraryFactory = await ethers.getContractFactory('NFTDescriptor')
const nftDescriptorLibrary = await nftDescriptorLibraryFactory.deploy()
const positionDescriptorFactory = await ethers.getContractFactory('NonfungibleTokenPositionDescriptor', {
libraries: {
NFTDescriptor: nftDescriptorLibrary.address,
},
})
const nftDescriptor = (await positionDescriptorFactory.deploy(
weth9.address,
// 'FUNNYMONEY' as a bytes32 string
'0x46554e4e594d4f4e455900000000000000000000000000000000000000000000'
)) as NonfungibleTokenPositionDescriptor

const metadata = extractJSONFromURI(await nftDescriptor.tokenURI(nft.address, 1))
expect(metadata.name).to.match(/(\sFUNNYMONEY\/TEST|TEST\/FUNNYMONEY)/)
expect(metadata.description).to.match(/(TEST-FUNNYMONEY|\sFUNNYMONEY-TEST)/)
expect(metadata.description).to.match(/(\nFUNNYMONEY\sAddress)/)
})
})
})
16 changes: 8 additions & 8 deletions test/__snapshots__/SwapRouter.gas.spec.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -6,26 +6,26 @@ exports[`SwapRouter gas tests #exactInput 0 -> 1 1`] = `107759`;

exports[`SwapRouter gas tests #exactInput 0 -> 1 minimal 1`] = `98059`;

exports[`SwapRouter gas tests #exactInput 0 -> WETH9 1`] = `127590`;
exports[`SwapRouter gas tests #exactInput 0 -> WETH9 1`] = `127578`;

exports[`SwapRouter gas tests #exactInput WETH9 -> 0 1`] = `106083`;
exports[`SwapRouter gas tests #exactInput WETH9 -> 0 1`] = `106071`;

exports[`SwapRouter gas tests #exactInputSingle 0 -> 1 1`] = `107221`;

exports[`SwapRouter gas tests #exactInputSingle 0 -> WETH9 1`] = `127046`;
exports[`SwapRouter gas tests #exactInputSingle 0 -> WETH9 1`] = `127034`;

exports[`SwapRouter gas tests #exactInputSingle WETH9 -> 0 1`] = `105727`;
exports[`SwapRouter gas tests #exactInputSingle WETH9 -> 0 1`] = `105715`;

exports[`SwapRouter gas tests #exactOutput 0 -> 1 -> 2 1`] = `169275`;

exports[`SwapRouter gas tests #exactOutput 0 -> 1 1`] = `111757`;

exports[`SwapRouter gas tests #exactOutput 0 -> WETH9 1`] = `128821`;
exports[`SwapRouter gas tests #exactOutput 0 -> WETH9 1`] = `128809`;

exports[`SwapRouter gas tests #exactOutput WETH9 -> 0 1`] = `114156`;
exports[`SwapRouter gas tests #exactOutput WETH9 -> 0 1`] = `114144`;

exports[`SwapRouter gas tests #exactOutputSingle 0 -> 1 1`] = `111986`;

exports[`SwapRouter gas tests #exactOutputSingle 0 -> WETH9 1`] = `129050`;
exports[`SwapRouter gas tests #exactOutputSingle 0 -> WETH9 1`] = `129038`;

exports[`SwapRouter gas tests #exactOutputSingle WETH9 -> 0 1`] = `114567`;
exports[`SwapRouter gas tests #exactOutputSingle WETH9 -> 0 1`] = `114555`;
2 changes: 1 addition & 1 deletion test/__snapshots__/V3Migrator.spec.ts.snap
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`V3Migrator #migrate gas 1`] = `710237`;
exports[`V3Migrator #migrate gas 1`] = `710213`;
4 changes: 3 additions & 1 deletion test/shared/completeFixture.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,9 @@ const completeFixture: Fixture<{
},
})
const nftDescriptor = (await positionDescriptorFactory.deploy(
tokens[0].address
tokens[0].address,
// 'ETH' as a bytes32 string
'0x4554480000000000000000000000000000000000000000000000000000000000'
)) as NonfungibleTokenPositionDescriptor

const positionManagerFactory = await ethers.getContractFactory('MockTimeNonfungiblePositionManager')
Expand Down

0 comments on commit de4e437

Please sign in to comment.