-
Notifications
You must be signed in to change notification settings - Fork 166
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
feat: add nft and token linker contracts #144
Open
deanamiel
wants to merge
12
commits into
main
Choose a base branch
from
feat/add-linker-contracts
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 6 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
3f59235
feat: add linker contracts
2bc33c5
Merge branch 'main' into feat/add-linker-contracts
deanamiel 6912e51
Merge branch 'main' into feat/add-linker-contracts
deanamiel 3cfa0e7
fix: build dependency issues
e40ddfe
feat: bump hardhat version
681db37
chore: add hardhat-ethers
npty d1ff3e4
fix: remove unit tests
35a557e
Merge branch 'main' into feat/add-linker-contracts
milapsheth 8c088cf
Merge branch 'main' into feat/add-linker-contracts
deanamiel add6ef1
fix: build issue
1ef852a
fix: remove hardhat ethers
ea8ba5d
feat: update solhint and slither config
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
// SPDX-License-Identifier: MIT | ||
|
||
pragma solidity ^0.8.0; | ||
|
||
import { IERC721 } from '@openzeppelin/contracts/token/ERC721/IERC721.sol'; | ||
|
||
interface IERC721MintableBurnable is IERC721 { | ||
function mint(address to, uint256 tokenId) external; | ||
|
||
function burn(uint256 tokenId) external; | ||
} |
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,51 @@ | ||
// SPDX-License-Identifier: MIT | ||
|
||
pragma solidity ^0.8.0; | ||
|
||
import { IAxelarGateway } from '@axelar-network/axelar-gmp-sdk-solidity/contracts/interfaces/IAxelarGateway.sol'; | ||
import { IAxelarGasService } from '@axelar-network/axelar-gmp-sdk-solidity/contracts/interfaces/IAxelarGasService.sol'; | ||
import { AxelarExecutable } from '@axelar-network/axelar-gmp-sdk-solidity/contracts/executable/AxelarExecutable.sol'; | ||
import { AddressToString, StringToAddress } from '@axelar-network/axelar-gmp-sdk-solidity/contracts/libs/AddressString.sol'; | ||
import { Upgradable } from '@axelar-network/axelar-gmp-sdk-solidity/contracts/upgradable/Upgradable.sol'; | ||
|
||
abstract contract NftLinkerBase is AxelarExecutable, Upgradable { | ||
using StringToAddress for string; | ||
using AddressToString for address; | ||
|
||
bytes32 internal constant CONTRACT_ID = keccak256('nft-linker'); | ||
IAxelarGasService public immutable gasService; | ||
|
||
constructor(address gatewayAddress, address gasServiceAddress_) AxelarExecutable(gatewayAddress) Upgradable() { | ||
gasService = IAxelarGasService(gasServiceAddress_); | ||
} | ||
|
||
function contractId() external pure override returns (bytes32) { | ||
return CONTRACT_ID; | ||
} | ||
|
||
function sendNft(string memory destinationChain, address to, uint256 tokenId, address refundAddress) external payable virtual { | ||
string memory thisAddress = address(this).toString(); | ||
_takeNft(msg.sender, tokenId); | ||
bytes memory payload = abi.encode(to, tokenId); | ||
if (msg.value > 0) { | ||
gasService.payNativeGasForContractCall{ value: msg.value }( | ||
address(this), | ||
destinationChain, | ||
thisAddress, | ||
payload, | ||
refundAddress | ||
); | ||
} | ||
gateway.callContract(destinationChain, thisAddress, payload); | ||
} | ||
|
||
function _execute(string calldata /*sourceChain*/, string calldata sourceAddress, bytes calldata payload) internal override { | ||
if (sourceAddress.toAddress() != address(this)) return; | ||
(address recipient, uint256 tokenId) = abi.decode(payload, (address, uint256)); | ||
_giveNft(recipient, tokenId); | ||
} | ||
|
||
function _giveNft(address to, uint256 tokenId) internal virtual; | ||
|
||
function _takeNft(address from, uint256 tokenId) internal virtual; | ||
} |
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,39 @@ | ||
// SPDX-License-Identifier: MIT | ||
|
||
pragma solidity ^0.8.0; | ||
|
||
import { IERC721 } from '@openzeppelin/contracts/interfaces/IERC721.sol'; | ||
import { NftLinkerBase } from './NftLinkerBase.sol'; | ||
|
||
contract NftLinkerLockUnlock is NftLinkerBase { | ||
error TransferFailed(); | ||
error TransferFromFailed(); | ||
|
||
address public immutable operatorAddress; | ||
|
||
constructor( | ||
address gatewayAddress_, | ||
address gasServiceAddress_, | ||
address operatorAddress_ | ||
) NftLinkerBase(gatewayAddress_, gasServiceAddress_) { | ||
operatorAddress = operatorAddress_; | ||
} | ||
|
||
function _giveNft(address to, uint256 tokenId) internal override { | ||
(bool success, bytes memory returnData) = operatorAddress.call( | ||
abi.encodeWithSelector(IERC721.transferFrom.selector, address(this), to, tokenId) | ||
); | ||
bool transferred = success && (returnData.length == uint256(0) || abi.decode(returnData, (bool))); | ||
|
||
if (!transferred || operatorAddress.code.length == 0) revert TransferFailed(); | ||
} | ||
|
||
function _takeNft(address from, uint256 tokenId) internal override { | ||
(bool success, bytes memory returnData) = operatorAddress.call( | ||
abi.encodeWithSelector(IERC721.transferFrom.selector, from, address(this), tokenId) | ||
); | ||
bool transferred = success && (returnData.length == uint256(0) || abi.decode(returnData, (bool))); | ||
|
||
if (!transferred || operatorAddress.code.length == 0) revert TransferFromFailed(); | ||
} | ||
} |
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,39 @@ | ||
// SPDX-License-Identifier: MIT | ||
|
||
pragma solidity ^0.8.0; | ||
|
||
import { IERC721MintableBurnable } from './IERC721MintableBurnable.sol'; | ||
import { NftLinkerBase } from './NftLinkerBase.sol'; | ||
|
||
contract NftLinkerMintBurn is NftLinkerBase { | ||
error TransferFailed(); | ||
error TransferFromFailed(); | ||
|
||
address public immutable operatorAddress; | ||
|
||
constructor( | ||
address gatewayAddress_, | ||
address gasServiceAddress_, | ||
address operatorAddress_ | ||
) NftLinkerBase(gatewayAddress_, gasServiceAddress_) { | ||
operatorAddress = operatorAddress_; | ||
} | ||
|
||
function _giveNft(address to, uint256 tokenId) internal override { | ||
(bool success, bytes memory returnData) = operatorAddress.call( | ||
abi.encodeWithSelector(IERC721MintableBurnable.mint.selector, to, tokenId) | ||
); | ||
bool transferred = success && (returnData.length == uint256(0) || abi.decode(returnData, (bool))); | ||
|
||
if (!transferred || operatorAddress.code.length == 0) revert TransferFailed(); | ||
} | ||
|
||
function _takeNft(address /*from*/, uint256 tokenId) internal override { | ||
(bool success, bytes memory returnData) = operatorAddress.call( | ||
abi.encodeWithSelector(IERC721MintableBurnable.burn.selector, tokenId) | ||
); | ||
bool transferred = success && (returnData.length == uint256(0) || abi.decode(returnData, (bool))); | ||
|
||
if (!transferred || operatorAddress.code.length == 0) revert TransferFromFailed(); | ||
} | ||
} |
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,18 @@ | ||
// SPDX-License-Identifier: MIT | ||
|
||
pragma solidity ^0.8.0; | ||
|
||
import { Proxy } from '@axelar-network/axelar-gmp-sdk-solidity/contracts/upgradable/Proxy.sol'; | ||
|
||
contract NftLinkerProxy is Proxy { | ||
bytes32 internal constant CONTRACT_ID = keccak256('nft-linker'); | ||
|
||
constructor(address implementationAddress, address owner, bytes memory setupParams) Proxy(implementationAddress, owner, setupParams) {} | ||
|
||
// slither-disable-next-line dead-code | ||
function contractId() internal pure override returns (bytes32) { | ||
return CONTRACT_ID; | ||
} | ||
|
||
receive() external payable override {} | ||
} |
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,157 @@ | ||
'use strict'; | ||
|
||
const chai = require('chai'); | ||
const { expect } = chai; | ||
const { ethers } = require('hardhat'); | ||
const { | ||
constants: { AddressZero }, | ||
} = require('ethers'); | ||
|
||
describe('Ownable', () => { | ||
let ownableTestFactory; | ||
let ownableTest; | ||
|
||
let ownerWallet; | ||
let userWallet; | ||
|
||
before(async () => { | ||
[ownerWallet, userWallet] = await ethers.getSigners(); | ||
|
||
ownableTestFactory = await ethers.getContractFactory( | ||
'TestOwnable', | ||
ownerWallet, | ||
); | ||
}); | ||
|
||
beforeEach(async () => { | ||
ownableTest = await ownableTestFactory | ||
.deploy(ownerWallet.address) | ||
.then((d) => d.deployed()); | ||
}); | ||
|
||
it('should set the initial owner and return the current owner address', async () => { | ||
const currentOwner = await ownableTest.owner(); | ||
|
||
expect(currentOwner).to.equal(ownerWallet.address); | ||
}); | ||
|
||
it('should revert when non-owner calls only owner function', async () => { | ||
const num = 5; | ||
|
||
await expect( | ||
ownableTest.connect(userWallet).setNum(num), | ||
).to.be.revertedWithCustomError(ownableTest, 'NotOwner'); | ||
}); | ||
|
||
it('should not revert when owner calls only owner function', async () => { | ||
const num = 5; | ||
|
||
await expect(ownableTest.connect(ownerWallet).setNum(num)) | ||
.to.emit(ownableTest, 'NumAdded') | ||
.withArgs(num); | ||
}); | ||
|
||
it('should revert on transfer owner if not called by the current owner', async () => { | ||
const newOwner = userWallet.address; | ||
|
||
await expect( | ||
ownableTest.connect(userWallet).transferOwnership(newOwner), | ||
).to.be.revertedWithCustomError(ownableTest, 'NotOwner'); | ||
}); | ||
|
||
it('should revert on transfer owner if new owner address is invalid', async () => { | ||
const newOwner = AddressZero; | ||
|
||
await expect( | ||
ownableTest.connect(ownerWallet).transferOwnership(newOwner), | ||
).to.be.revertedWithCustomError(ownableTest, 'InvalidOwnerAddress'); | ||
}); | ||
|
||
it('should transfer ownership in one step', async () => { | ||
const newOwner = userWallet.address; | ||
|
||
await expect(ownableTest.transferOwnership(newOwner)) | ||
.to.emit(ownableTest, 'OwnershipTransferred') | ||
.withArgs(newOwner); | ||
|
||
const currentOwner = await ownableTest.owner(); | ||
|
||
expect(currentOwner).to.equal(userWallet.address); | ||
}); | ||
|
||
it('should revert on propose owner if not called by the current owner', async () => { | ||
const newOwner = userWallet.address; | ||
|
||
await expect( | ||
ownableTest.connect(userWallet).proposeOwnership(newOwner), | ||
).to.be.revertedWithCustomError(ownableTest, 'NotOwner'); | ||
}); | ||
|
||
it('should propose new owner', async () => { | ||
const newOwner = userWallet.address; | ||
|
||
await expect(ownableTest.proposeOwnership(newOwner)) | ||
.to.emit(ownableTest, 'OwnershipTransferStarted') | ||
.withArgs(newOwner); | ||
}); | ||
|
||
it('should return pending owner', async () => { | ||
const newOwner = userWallet.address; | ||
|
||
await expect(ownableTest.proposeOwnership(newOwner)) | ||
.to.emit(ownableTest, 'OwnershipTransferStarted') | ||
.withArgs(newOwner); | ||
|
||
const pendingOwner = await ownableTest.pendingOwner(); | ||
|
||
expect(pendingOwner).to.equal(userWallet.address); | ||
}); | ||
|
||
it('should revert on accept ownership if caller is not the pending owner', async () => { | ||
const newOwner = userWallet.address; | ||
|
||
await expect(ownableTest.proposeOwnership(newOwner)) | ||
.to.emit(ownableTest, 'OwnershipTransferStarted') | ||
.withArgs(newOwner); | ||
|
||
await expect( | ||
ownableTest.connect(ownerWallet).acceptOwnership(), | ||
).to.be.revertedWithCustomError(ownableTest, 'InvalidOwner'); | ||
}); | ||
|
||
it('should accept ownership', async () => { | ||
const newOwner = userWallet.address; | ||
|
||
await expect(ownableTest.proposeOwnership(newOwner)) | ||
.to.emit(ownableTest, 'OwnershipTransferStarted') | ||
.withArgs(newOwner); | ||
|
||
await expect(ownableTest.connect(userWallet).acceptOwnership()) | ||
.to.emit(ownableTest, 'OwnershipTransferred') | ||
.withArgs(newOwner); | ||
|
||
const currentOwner = await ownableTest.owner(); | ||
|
||
expect(currentOwner).to.equal(userWallet.address); | ||
}); | ||
|
||
it('should revert on accept ownership if transfer ownership is called first', async () => { | ||
const newOwner = userWallet.address; | ||
|
||
await expect(ownableTest.proposeOwnership(newOwner)) | ||
.to.emit(ownableTest, 'OwnershipTransferStarted') | ||
.withArgs(newOwner); | ||
|
||
await expect(ownableTest.transferOwnership(newOwner)) | ||
.to.emit(ownableTest, 'OwnershipTransferred') | ||
.withArgs(newOwner); | ||
|
||
const currentOwner = await ownableTest.owner(); | ||
|
||
expect(currentOwner).to.equal(userWallet.address); | ||
|
||
await expect( | ||
ownableTest.connect(userWallet).acceptOwnership(), | ||
).to.be.revertedWithCustomError(ownableTest, 'InvalidOwner'); | ||
}); | ||
}); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
add the same slither/solhint config to this repo?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
added solhint/slither configs to ignore these warnings