______ __ __ ______ __ __ ______ ______
/\ == \ /\_\_\_\ /\ ___\ /\ \ _ \ \ /\ __ \ /\ == \
\ \ _-/ \/_/\_\/_ \ \___ \ \ \ \/ ".\ \ \ \ __ \ \ \ _-/
\ \_\ /\_\/\_\ \/\_____\ \ \__/".~\_\ \ \_\ \_\ \ \_\
\/_/ \/_/\/_/ \/_____/ \/_/ \/_/ \/_/\/_/ \/_/
Deployments
- Optimism: 0x812940EDA4274702F9F17383281b8b8FD8cB6b7e
- Polygon: 0x93859840aC947cDb9a1e14Bab3b833A6563E7481
- Kava: 0x638B06F80FB28F109E65C9d5cC585aDf9A0c3f9f
- Göerli: 0x61e105c05EcAC5adfd8959aFa46cf454A4702F09
Contents
- ERC721Holder
- HandleERC20
- HandleERC721
- IERC165
- IERC20
- IERC721
- IERC721Receiver
- IErrors
- ILendData
- Ownable
ERC721Holder
Inherits: IERC721Receiver
Implementation of the {IERC721Receiver} interface. Accepts all token transfers. Make sure the contract is able to use its token with {IERC721-safeTransferFrom}, {IERC721-approve} or {IERC721-setApprovalForAll}.
Functions
onERC721Received
See {IERC721Receiver-onERC721Received}.
Always returns IERC721Receiver.onERC721Received.selector
.
function onERC721Received(address, address, uint256, bytes memory) public virtual override returns (bytes4);
HandleERC20
Functions
transferToken
function transferToken(address wantToken, address from, address to, address protocol, uint256 amount, uint256 fee)
internal;
Errors
NotSuccessful
error NotSuccessful();
HandleERC721
Functions
transferNft
function transferNft(address[] memory nft_, address from, address to, uint256 lenNft, uint256[] memory id) internal;
transferNft_
function transferNft_(address nft_, address from, address to, uint256 id) internal;
IERC165
Interface of the ERC165 standard, as defined in the https://eips.ethereum.org/EIPS/eip-165[EIP]. Implementers can declare support of contract interfaces, which can then be queried by others ({ERC165Checker}). For an implementation, see {ERC165}.
Functions
supportsInterface
Returns true if this contract implements the interface defined by
interfaceId
. See the corresponding
https://eips.ethereum.org/EIPS/eip-165#how-interfaces-are-identified[EIP section]
to learn more about how these ids are created.
This function call must use less than 30 000 gas.
function supportsInterface(bytes4 interfaceId) external view returns (bool);
IERC20
Functions
decimals
function decimals() external view returns (uint256);
balanceOf
function balanceOf(address account) external view returns (uint256);
transfer
function transfer(address to, uint256 amount) external returns (bool);
allowance
function allowance(address owner, address spender) external view returns (uint256);
approve
function approve(address spender, uint256 amount) external returns (bool);
transferFrom
function transferFrom(address from, address to, uint256 amount) external returns (bool);
Events
Transfer
event Transfer(address indexed from, address indexed to, uint256 value);
Approval
event Approval(address indexed owner, address indexed spender, uint256 value);
IERC721
Inherits: IERC165
Required interface of an ERC721 compliant contract.
Functions
balanceOf
Returns the number of tokens in owner
's account.
function balanceOf(address owner) external view returns (uint256 balance);
ownerOf
*Returns the owner of the tokenId
token.
Requirements:
tokenId
must exist.*
function ownerOf(uint256 tokenId) external view returns (address owner);
safeTransferFrom
*Safely transfers tokenId
token from from
to to
.
Requirements:
from
cannot be the zero address.to
cannot be the zero address.tokenId
token must exist and be owned byfrom
.- If the caller is not
from
, it must be approved to move this token by either {approve} or {setApprovalForAll}. - If
to
refers to a smart contract, it must implement {IERC721Receiver-onERC721Received}, which is called upon a safe transfer. Emits a {Transfer} event.*
function safeTransferFrom(address from, address to, uint256 tokenId, bytes calldata data) external;
safeTransferFrom
*Safely transfers tokenId
token from from
to to
, checking first that contract recipients
are aware of the ERC721 protocol to prevent tokens from being forever locked.
Requirements:
from
cannot be the zero address.to
cannot be the zero address.tokenId
token must exist and be owned byfrom
.- If the caller is not
from
, it must have been allowed to move this token by either {approve} or {setApprovalForAll}. - If
to
refers to a smart contract, it must implement {IERC721Receiver-onERC721Received}, which is called upon a safe transfer. Emits a {Transfer} event.*
function safeTransferFrom(address from, address to, uint256 tokenId) external;
transferFrom
*Transfers tokenId
token from from
to to
.
WARNING: Note that the caller is responsible to confirm that the recipient is capable of receiving ERC721
or else they may be permanently lost. Usage of {safeTransferFrom} prevents loss, though the caller must
understand this adds an external call which potentially creates a reentrancy vulnerability.
Requirements:
from
cannot be the zero address.to
cannot be the zero address.tokenId
token must be owned byfrom
.- If the caller is not
from
, it must be approved to move this token by either {approve} or {setApprovalForAll}. Emits a {Transfer} event.*
function transferFrom(address from, address to, uint256 tokenId) external;
approve
*Gives permission to to
to transfer tokenId
token to another account.
The approval is cleared when the token is transferred.
Only a single account can be approved at a time, so approving the zero address clears previous approvals.
Requirements:
- The caller must own the token or be an approved operator.
tokenId
must exist. Emits an {Approval} event.*
function approve(address to, uint256 tokenId) external;
setApprovalForAll
*Approve or remove operator
as an operator for the caller.
Operators can call {transferFrom} or {safeTransferFrom} for any token owned by the caller.
Requirements:
- The
operator
cannot be the caller. Emits an {ApprovalForAll} event.*
function setApprovalForAll(address operator, bool _approved) external;
getApproved
*Returns the account approved for tokenId
token.
Requirements:
tokenId
must exist.*
function getApproved(uint256 tokenId) external view returns (address operator);
isApprovedForAll
Returns if the operator
is allowed to manage all of the assets of owner
.
See {setApprovalForAll}
function isApprovedForAll(address owner, address operator) external view returns (bool);
Events
Transfer
Emitted when tokenId
token is transferred from from
to to
.
event Transfer(address indexed from, address indexed to, uint256 indexed tokenId);
Approval
Emitted when owner
enables approved
to manage the tokenId
token.
event Approval(address indexed owner, address indexed approved, uint256 indexed tokenId);
ApprovalForAll
Emitted when owner
enables or disables (approved
) operator
to manage all of its assets.
event ApprovalForAll(address indexed owner, address indexed operator, bool approved);
IERC721Receiver
Interface for any contract that wants to support safeTransfers from ERC721 asset contracts.
Functions
onERC721Received
Whenever an {IERC721} tokenId
token is transferred to this contract via {IERC721-safeTransferFrom}
by operator
from from
, this function is called.
It must return its Solidity selector to confirm the token transfer.
If any other value is returned or the interface is not implemented by the recipient, the transfer will be reverted.
The selector can be obtained in Solidity with IERC721Receiver.onERC721Received.selector
.
function onERC721Received(address operator, address from, uint256 tokenId, bytes calldata data)
external
returns (bytes4);
IErrors
Errors
ZERO
error ZERO();
NO_NFTS
error NO_NFTS();
OWN_NFT
error OWN_NFT();
BORROWED
error BORROWED();
ONLY_ONE
error ONLY_ONE();
NOT_ZERO
error NOT_ZERO();
NOT_UINT8
error NOT_UINT8();
WRONG_FEE
error WRONG_FEE();
ZERO_PRICE
error ZERO_PRICE();
NOT_LENDER
error NOT_LENDER();
NOT_UINT16
error NOT_UINT16();
CANT_RESET
error CANT_RESET();
WRONG_TOKEN
error WRONG_TOKEN();
NOT_BORROWER
error NOT_BORROWER();
MAX_DURATION
error MAX_DURATION();
ZERO_ADDRESS
error ZERO_ADDRESS();
INVALID_SCALE
error INVALID_SCALE();
INVALID_PRICE
error INVALID_PRICE();
DIDNT_BORROWED
error DIDNT_BORROWED();
INVALID_AMOUNT
error INVALID_AMOUNT();
CRITICAL_ERROR
error CRITICAL_ERROR();
TOKEN_SENTINEL
error TOKEN_SENTINEL();
R_ZERO_PAYMENT
error R_ZERO_PAYMENT();
L_ZERO_PAYMENT
error L_ZERO_PAYMENT();
NOT_ZERO_ADDRESS
error NOT_ZERO_ADDRESS();
WAIT_RETURN_DATE
error WAIT_RETURN_DATE();
PAST_RETURN_DATE
error PAST_RETURN_DATE();
INVALID_STANDARD
error INVALID_STANDARD();
ILendData
Functions
lend
function lend(
NFTStandard[] memory nftStandard,
address[] memory nftAddress,
uint256[] memory tokenID,
uint256[] memory lendAmount,
uint8[] memory maxBorrowDuration,
bytes4[] memory dailyBorrowPrice,
uint8[] memory paymentToken,
bool[] memory willAutoRenew
) external;
stopLend
function stopLend(
NFTStandard[] memory nftStandard,
address[] memory nftAddress,
uint256[] memory tokenID,
uint256[] memory lendingID
) external;
borrow
function borrow(
NFTStandard[] memory nftStandard,
address[] memory nftAddress,
uint256[] memory tokenID,
uint256[] memory lendingID,
uint8[] memory borrowDuration,
uint256[] memory borrowAmount
) external payable;
stopBorrow
function stopBorrow(
NFTStandard[] memory nftStandard,
address[] memory nftAddress,
uint256[] memory tokenID,
uint256[] memory lendingID,
uint256[] memory borrowingID
) external;
claimBorrow
function claimBorrow(
NFTStandard[] memory nftStandard,
address[] memory nftAddress,
uint256[] memory tokenID,
uint256[] memory lendingID,
uint256[] memory borrowingID
) external;
Events
Lend
event Lend(
bool is721,
address indexed lenderAddress,
address indexed nftAddress,
uint256 indexed tokenID,
uint256 lendingID,
uint8 maxBorrowDuration,
bytes4 dailyBorrowPrice,
uint16 lendAmount,
uint8 paymentToken,
bool willAutoRenew
);
Borrow
event Borrow(
address indexed borrowerAddress,
uint256 lendingID,
uint256 borrowingID,
uint16 borrowAmount,
uint8 borrowDuration,
uint32 borrowedAt
);
StopLend
event StopLend(uint256 indexed lendingID, uint32 stoppedAt, uint16 amount);
StopBorrow
event StopBorrow(uint256 indexed borrowingID, uint32 stoppedAt);
BorrowClaimed
event BorrowClaimed(uint256 indexed borrowingID, uint32 collectedAt);
Structs
CallData
struct CallData {
uint256 left;
uint256 right;
NFTStandard[] nftStandard;
address[] nftAddress;
uint256[] tokenID;
uint256[] lendAmount;
uint8[] maxBorrowDuration;
bytes4[] dailyBorrowPrice;
uint256[] lendingID;
uint256[] borrowingID;
uint8[] borrowDuration;
uint256[] borrowAmount;
uint8[] paymentToken;
bool[] willAutoRenew;
}
Lending
struct Lending {
NFTStandard nftStandard;
address payable lenderAddress;
uint8 maxBorrowDuration;
bytes4 dailyBorrowPrice;
uint16 lendAmount;
uint16 availableAmount;
uint8 paymentToken;
bool willAutoRenew;
}
Borrowing
struct Borrowing {
address payable borrowerAddress;
uint8 borrowDuration;
uint32 borrowedAt;
uint16 borrowAmount;
}
Enums
NFTStandard
enum NFTStandard {
E721,
E1155
}
Ownable
State Variables
_owner
address private _owner;
Functions
constructor
constructor();
onlyOwner
modifier onlyOwner();
owner
function owner() public view virtual returns (address);
_checkOwner
function _checkOwner() internal view virtual;
transferOwnership
function transferOwnership(address newOwner) public virtual onlyOwner;
_transferOwnership
function _transferOwnership(address newOwner) internal virtual;
Events
OwnershipTransferred
event OwnershipTransferred(address indexed previousOwner, address indexed newOwner);
PxLend
Inherits: ILendData, Ownable, ERC721Holder, ERC1155Receiver, ERC1155Holder
Authors: pxswap (https://github.com/pxswap-xyz/pxswap/blob/main/src/Pxswap.sol), Ali Konuk - @alikonuk1
This contract is for lending and borrowing non-fungible tokens (NFTs)
Please reach out to ali@pxswap.xyz if you find any issues
State Variables
beneficiary
address payable private beneficiary;
SECONDS_IN_DAY
uint256 private constant SECONDS_IN_DAY = 86400;
lendingID
uint256 private lendingID = 1;
borrowingID
uint256 private borrowingID = 1;
borrowFee
uint256 public borrowFee = 100;
tokenMap
mapping(uint8 => address) private tokenMap;
lendings
mapping(bytes32 => Lending) private lendings;
borrowings
mapping(bytes32 => Borrowing) private borrowings;
Functions
constructor
constructor(address payable beneficiary_);
lend
function lend(
ILendData.NFTStandard[] memory nftStandard,
address[] memory nftAddress,
uint256[] memory tokenID,
uint256[] memory lendAmount,
uint8[] memory maxBorrowDuration,
bytes4[] memory dailyBorrowPrice,
uint8[] memory paymentToken,
bool[] memory willAutoRenew
) external;
stopLend
function stopLend(
ILendData.NFTStandard[] memory nftStandard,
address[] memory nftAddress,
uint256[] memory tokenID,
uint256[] memory _lendingID
) external;
borrow
function borrow(
ILendData.NFTStandard[] memory nftStandard,
address[] memory nftAddress,
uint256[] memory tokenID,
uint256[] memory _lendingID,
uint8[] memory borrowDuration,
uint256[] memory borrowAmount
) external payable;
stopBorrow
function stopBorrow(
ILendData.NFTStandard[] memory nftStandard,
address[] memory nftAddress,
uint256[] memory tokenID,
uint256[] memory _lendingID,
uint256[] memory _borrowingID
) external;
claimBorrow
function claimBorrow(
ILendData.NFTStandard[] memory nftStandard,
address[] memory nftAddress,
uint256[] memory tokenID,
uint256[] memory _lendingID,
uint256[] memory _borrowingID
) external;
handleLend
function handleLend(ILendData.CallData memory cd) internal;
handleStopLend
function handleStopLend(ILendData.CallData memory cd) internal;
handleBorrow
function handleBorrow(ILendData.CallData memory cd) internal;
handleStopBorrow
function handleStopBorrow(ILendData.CallData memory cd) private;
handleClaimBorrow
function handleClaimBorrow(CallData memory cd) private;
manageWillAutoRenew
function manageWillAutoRenew(
ILendData.Lending storage lending,
ILendData.Borrowing storage borrowing,
address nftAddress,
ILendData.NFTStandard nftStandard,
uint256 tokenID,
uint256 lendingID
) internal;
bundleCall
function bundleCall(function(ILendData.CallData memory) handler, ILendData.CallData memory cd) internal;
takeFee
function takeFee(uint256 borrowAmt, ERC20 token) internal returns (uint256 fee);
distributePayments
function distributePayments(
ILendData.Lending memory lending,
ILendData.Borrowing memory borrowing,
uint256 secondsSinceBorrowStart
) private;
distributeClaimPayment
function distributeClaimPayment(ILendData.Lending memory lending, ILendData.Borrowing memory borrowing) internal;
safeTransfer
function safeTransfer(
CallData memory cd,
address from,
address to,
uint256[] memory tokenID,
uint256[] memory lendAmount
) internal;
getLending9CFF8D4
function getLending9CFF8D4(address nftAddress, uint256 tokenID, uint256 _lendingID)
external
view
returns (uint8, address, uint8, bytes4, uint16, uint16, uint8);
getBorrowing144DC65D
function getBorrowing144DC65D(address nftAddress, uint256 tokenID, uint256 _borrowingID)
external
view
returns (address, uint16, uint8, uint32);
createLendCallData
function createLendCallData(
ILendData.NFTStandard[] memory nftStandard,
address[] memory nftAddress,
uint256[] memory tokenID,
uint256[] memory lendAmount,
uint8[] memory maxBorrowDuration,
bytes4[] memory dailyBorrowPrice,
uint8[] memory paymentToken,
bool[] memory willAutoRenew
) internal pure returns (CallData memory cd);
createBorrowCallData
function createBorrowCallData(
ILendData.NFTStandard[] memory nftStandard,
address[] memory nftAddress,
uint256[] memory tokenID,
uint256[] memory _lendingID,
uint8[] memory borrowDuration,
uint256[] memory borrowAmount
) internal pure returns (CallData memory cd);
createActionCallData
function createActionCallData(
ILendData.NFTStandard[] memory nftStandard,
address[] memory nftAddress,
uint256[] memory tokenID,
uint256[] memory _lendingID,
uint256[] memory _borrowingID
) internal pure returns (CallData memory cd);
unpackPrice
function unpackPrice(bytes4 price, uint256 scale) internal pure returns (uint256);
sliceArr
function sliceArr(uint256[] memory arr, uint256 fromIx, uint256 toIx, uint256 arrOffset)
internal
pure
returns (uint256[] memory r);
isNotZero
function isNotZero(address addr) internal pure;
isZero
function isZero(address addr) internal pure;
isReturnable
function isReturnable(Borrowing memory borrowing, address msgSender, uint256 blockTimestamp) internal pure;
isPastReturnDate
function isPastReturnDate(Borrowing memory borrowing, uint256 nowTime) internal pure returns (bool);
getPaymentToken
function getPaymentToken(uint8 paymentToken) internal view returns (address);
setPaymentToken
function setPaymentToken(uint8 paymentToken, address t) external payable onlyOwner;
setBorrowFee
function setBorrowFee(uint256 borrowFee_) external payable onlyOwner;
setBeneficiary
function setBeneficiary(address payable beneficiary_) external payable onlyOwner;
Pxswap
Inherits: SwapData, Ownable, HandleERC20, HandleERC721, ERC721Holder
Authors: pxswap (https://github.com/pxswap-xyz/pxswap/blob/main/src/Pxswap.sol), Ali Konuk - @alikonuk1
This contract is for buying, selling and swapping non-fungible tokens (NFTs)
Please reach out to ali@pxswap.xyz if you find any issues
State Variables
swaps
Swap[] public swaps;
protocol
address public protocol;
pxNft
address public pxNft;
flatFee
uint256 public flatFee;
discountedFee
uint256 public discountedFee;
fee
uint256 public fee;
mutex
bool public mutex;
Functions
putSwap
Creates a new swap by the seller with the specified NFTs and tokens offered.
function putSwap(
address[] memory nftsGiven,
uint256[] memory idsGiven,
address[] memory nftsWanted,
address buyer,
address tokenWanted,
uint256 amount,
uint256 ethAmount
) external noReentrancy;
Parameters
Name | Type | Description |
---|---|---|
nftsGiven | address[] | Array of addresses of the NFTs given by the seller. |
idsGiven | uint256[] | Array of IDs of the NFTs given by the seller. |
nftsWanted | address[] | Array of addresses of the NFTs wanted by the seller. |
buyer | address | The address of the buyer for the swap. |
tokenWanted | address | The address of the ERC20 token wanted by the seller. |
amount | uint256 | The amount of ERC20 tokens wanted by the seller. |
ethAmount | uint256 | The amount of ether wanted by the seller. Emits a {PutSwap} event indicating the creation of the swap and its ID. |
cancelSwap
Allows the seller to cancel an active swap and transfer the ERC721 tokens back to the seller.
function cancelSwap(uint256 id) external noReentrancy;
Parameters
Name | Type | Description |
---|---|---|
id | uint256 | The ID of the swap to be cancelled. |
acceptSwap
Allows the buyer to accept a swap by ID and transfer the assets to the respective parties.
function acceptSwap(uint256 id, uint256[] memory tokenIds) public payable noReentrancy;
Parameters
Name | Type | Description |
---|---|---|
id | uint256 | The ID of the swap to be accepted. |
tokenIds | uint256[] | An array of token IDs for ERC721 tokens. |
setProtocol
Function to set the protocol address.
function setProtocol(address protocol_) external onlyOwner;
Parameters
Name | Type | Description |
---|---|---|
protocol_ | address | The address of the protocol. |
setFee
Allows the contract owner to set the transaction fee.
function setFee(uint256 fee_) external onlyOwner;
Parameters
Name | Type | Description |
---|---|---|
fee_ | uint256 | The new transaction fee. |
setDiscountedFee
Allows the contract owner to set the discounted transaction fee.
function setDiscountedFee(uint256 discountedFee_) external onlyOwner;
Parameters
Name | Type | Description |
---|---|---|
discountedFee_ | uint256 | The new discounted transaction fee. |
setFlatFee
Allows the contract owner to set the flat transaction fee.
function setFlatFee(uint256 flatFee_) external onlyOwner;
Parameters
Name | Type | Description |
---|---|---|
flatFee_ | uint256 | The new flat transaction fee. |
setPxNft
Allows the contract owner to set the pxswap's nft contract address.
function setPxNft(address pxNft_) external onlyOwner;
Parameters
Name | Type | Description |
---|---|---|
pxNft_ | address | pxswap's nft contract address. |
noReentrancy
modifier noReentrancy();
_nonReentrantBefore
function _nonReentrantBefore() internal;
_nonReentrantAfter
function _nonReentrantAfter() internal;
getLength
Returns the number of swaps in the contract.
function getLength() external view returns (uint256);
Returns
Name | Type | Description |
---|---|---|
<none> | uint256 | The length of the swaps array. |
getSwap
Returns the details of a specific swap by its ID.
function getSwap(uint256 id) external view returns (Swap memory);
Parameters
Name | Type | Description |
---|---|---|
id | uint256 | The ID of the swap to be retrieved. |
Returns
Name | Type | Description |
---|---|---|
<none> | Swap | The details of the swap as a memory struct. |
Events
PutSwap
event PutSwap(uint256 indexed id);
CancelSwap
event CancelSwap(uint256 indexed id);
AcceptSwap
event AcceptSwap(uint256 indexed id);
Errors
Unauthorized
error Unauthorized();
NotActive
error NotActive();
NotEnoughEth
error NotEnoughEth();
SwapData
Structs
Swap
struct Swap {
uint256[] giveId;
uint256 amount;
uint256 ethAmount;
address seller;
address buyer;
address[] giveNft;
address[] wantNft;
address wantToken;
bool active;
}