Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Lazy setting of bridged tokens metadata #43

Merged
merged 3 commits into from
Jun 30, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion contracts/tokens/ERC1155BridgeToken.sol
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,18 @@ contract ERC1155BridgeToken is ERC1155, IBurnableMintableERC1155Token {
_burnBatch(msg.sender, _tokenIds, _values);
}

/**
* @dev Updated bridged token name/symbol parameters.
* Only bridge owner or bridge itself can call this method.
* @param _name new name parameter, will be saved as is, without additional suffixes like " from Mainnet".
* @param _symbol new symbol parameter.
*/
function setMetadata(string calldata _name, string calldata _symbol) external onlyOwner {
require(bytes(_name).length > 0 && bytes(_symbol).length > 0);
name = _name;
symbol = _symbol;
}

/**
* @dev Updates the bridge contract address.
* Can be called by bridge owner after token contract was instantiated.
Expand Down Expand Up @@ -170,6 +182,6 @@ contract ERC1155BridgeToken is ERC1155, IBurnableMintableERC1155Token {
uint64 patch
)
{
return (1, 1, 0);
return (1, 2, 0);
}
}
25 changes: 24 additions & 1 deletion contracts/tokens/ERC721BridgeToken.sol
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,29 @@ contract ERC721BridgeToken is ERC721, IBurnableMintableERC721Token {
_burn(_tokenId);
}

// hack to access private fields in ERC721 contract
struct MetadataStorage {
string name;
string symbol;
}

/**
* @dev Updated bridged token name/symbol parameters.
* Only bridge owner or bridge itself can call this method.
* @param _name new name parameter, will be saved as is, without additional suffixes like " from Mainnet".
* @param _symbol new symbol parameter.
*/
function setMetadata(string calldata _name, string calldata _symbol) external onlyOwner {
require(bytes(_name).length > 0 && bytes(_symbol).length > 0);

MetadataStorage storage metadata;
assembly {
metadata.slot := 6
akolotov marked this conversation as resolved.
Show resolved Hide resolved
}
metadata.name = _name;
metadata.symbol = _symbol;
}

/**
* @dev Sets the base URI for all tokens.
* Can be called by bridge owner after token contract was instantiated.
Expand Down Expand Up @@ -118,6 +141,6 @@ contract ERC721BridgeToken is ERC721, IBurnableMintableERC721Token {
uint64 patch
)
{
return (1, 0, 1);
return (1, 1, 0);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,10 @@ abstract contract BasicNFTOmnibridge is
) external onlyMediator {
address bridgedToken = bridgedTokenAddress(_token);
if (bridgedToken == address(0)) {
if (bytes(_name).length == 0) {
require(bytes(_symbol).length > 0);
if (bytes(_name).length == 0 && bytes(_symbol).length == 0) {
_name = "Bridged NFT";
akolotov marked this conversation as resolved.
Show resolved Hide resolved
_symbol = "NFT";
} else if (bytes(_name).length == 0) {
_name = _symbol;
} else if (bytes(_symbol).length == 0) {
_symbol = _name;
Expand Down Expand Up @@ -296,8 +298,6 @@ abstract contract BasicNFTOmnibridge is
string memory name = _readName(_token);
string memory symbol = _readSymbol(_token);

require(bytes(name).length > 0 || bytes(symbol).length > 0);

return
abi.encodeWithSelector(
this.deployAndHandleBridgedNFT.selector,
Expand Down
32 changes: 32 additions & 0 deletions test/omnibridge_nft/common.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1135,6 +1135,22 @@ function runTests(accounts, isHome) {
expect(await deployedToken.symbol()).to.be.equal('Test')
})

it('should use default name, which can be reset later', async () => {
const data = deployAndHandleBridgedERC721({ tokenId: 1, name: '', symbol: '' })

expect(await executeMessageCall(exampleMessageId, data)).to.be.equal(true)

const deployedToken = await ERC721BridgeToken.at(await contract.bridgedTokenAddress(otherSideToken1))
expect(await deployedToken.name()).to.be.equal(modifyName('Bridged NFT'))
expect(await deployedToken.symbol()).to.be.equal('NFT')

await deployedToken.setMetadata('newName', 'newSymbol', { from: user }).should.be.rejected
await deployedToken.setMetadata('newName', 'newSymbol', { from: owner }).should.be.fulfilled

expect(await deployedToken.name()).to.be.equal('newName')
expect(await deployedToken.symbol()).to.be.equal('newSymbol')
})

it('should not allow to operate when execution is disabled globally', async () => {
const data = deployAndHandleBridgedERC721({ tokenId: 1 })

Expand Down Expand Up @@ -1692,6 +1708,22 @@ function runTests(accounts, isHome) {
const event = await getEvents(contract, { event: 'TokensBridged' })
expect(event.length).to.be.equal(2)
})

it('should use default name, which can be reset later', async () => {
const data = deployAndHandleBridgedERC1155({ tokenIds: [1, 2], values: [1, 3], name: '', symbol: '' })
akolotov marked this conversation as resolved.
Show resolved Hide resolved

expect(await executeMessageCall(exampleMessageId, data)).to.be.equal(true)

const deployedToken = await ERC1155BridgeToken.at(await contract.bridgedTokenAddress(otherSideToken1))
expect(await deployedToken.name()).to.be.equal(modifyName('Bridged NFT'))
expect(await deployedToken.symbol()).to.be.equal('NFT')

await deployedToken.setMetadata('newName', 'newSymbol', { from: user }).should.be.rejected
await deployedToken.setMetadata('newName', 'newSymbol', { from: owner }).should.be.fulfilled

expect(await deployedToken.name()).to.be.equal('newName')
expect(await deployedToken.symbol()).to.be.equal('newSymbol')
})
})

describe('handleBridgedNFT', () => {
Expand Down