generated from defi-wonderland/solidity-hardhat-boilerplate
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: new contract with only base url (#2)
* feat: new contract with only base url * refactor: some suggestions
- Loading branch information
Showing
4 changed files
with
79 additions
and
5 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
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,27 @@ | ||
// SPDX-License-Identifier: GPL-2.0-or-later | ||
pragma solidity >=0.8.16; | ||
import '@openzeppelin/contracts/access/Ownable2Step.sol'; | ||
import '../interfaces/IDCAHubPositionDescriptor.sol'; | ||
import '../libraries/DescriptorUtils.sol'; | ||
|
||
/// @title Describes NFT token positions | ||
/// @notice Produces a string containing the URL for the NFT | ||
contract DCAHubPositionDescriptorFixed is IDCAHubPositionDescriptor, Ownable2Step { | ||
using Strings for uint256; | ||
|
||
string public baseURL; | ||
|
||
constructor(string memory _baseURL, address _firstOwner) { | ||
baseURL = _baseURL; | ||
super._transferOwnership(_firstOwner); | ||
} | ||
|
||
/// @inheritdoc IDCAHubPositionDescriptor | ||
function tokenURI(address _hub, uint256 _tokenId) external view returns (string memory) { | ||
return string(abi.encodePacked(baseURL, block.chainid.toString(), '-', DescriptorUtils.addressToString(_hub), '-', _tokenId.toString())); | ||
} | ||
|
||
function setBaseURL(string memory _newBaseURL) external onlyOwner { | ||
baseURL = _newBaseURL; | ||
} | ||
} |
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,47 @@ | ||
import { ethers } from 'hardhat'; | ||
import { DCAHubPositionDescriptorFixed, DCAHubPositionDescriptorFixed__factory } from '@typechained'; | ||
import { expect } from 'chai'; | ||
import { SignerWithAddress } from '@nomiclabs/hardhat-ethers/signers'; | ||
|
||
describe('DCAHubPositionDescriptorFixed', () => { | ||
let hub: SignerWithAddress; | ||
let tokenId: number; | ||
let DCAHubPositionDescriptorFixed: DCAHubPositionDescriptorFixed; | ||
let chainId: number; | ||
|
||
before('Setup accounts and contracts', async () => { | ||
tokenId = Math.floor(Math.random() * (10000 - 1)); | ||
const factory = (await ethers.getContractFactory('DCAHubPositionDescriptorFixed')) as DCAHubPositionDescriptorFixed__factory; | ||
const [signer, _hub] = await ethers.getSigners(); | ||
hub = _hub; | ||
DCAHubPositionDescriptorFixed = await factory.deploy('baseurl/', signer.address); | ||
chainId = (await ethers.provider.getNetwork()).chainId; | ||
}); | ||
|
||
describe('tokenURI', () => { | ||
it('returns the correct url', async () => { | ||
const baseURL = await DCAHubPositionDescriptorFixed.baseURL(); | ||
const url = await getPositionURI(); | ||
const expectedUrl = baseURL + chainId + '-' + hub.address + '-' + tokenId; | ||
expect(url.toLowerCase()).to.be.equal(expectedUrl.toLowerCase()); | ||
}); | ||
}); | ||
describe('setBaseURL', () => { | ||
it('prevents to modify base url', async () => { | ||
const newBaseURL = 'newurl/'; | ||
const impersonate = await ethers.getSigner('0x8894E0a0c962CB723c1976a4421c95949bE2D4E3'); | ||
await expect(DCAHubPositionDescriptorFixed.connect(impersonate.address).setBaseURL(newBaseURL)).to.be.reverted; | ||
}); | ||
|
||
it('modifies base url', async () => { | ||
const newBaseURL = 'newurl/'; | ||
await DCAHubPositionDescriptorFixed.setBaseURL(newBaseURL); | ||
expect(await DCAHubPositionDescriptorFixed.baseURL()).to.be.equal(newBaseURL); | ||
}); | ||
}); | ||
|
||
async function getPositionURI() { | ||
const result = await DCAHubPositionDescriptorFixed.tokenURI(hub.address, tokenId); | ||
return result; | ||
} | ||
}); |
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