Offer signature is a required parameter of offer/make
. To generate an offer signature, follow the sample code below.
let contactAddr = '0xFa4D5258804D7723eb6A934c11b1bd423bC31623' // X2Y2 loan contract address
let chainId = 1 // Ethereum chain mainnet
const ethers = require('ethers')
// Prepare the encode function
function encode(isCollection, {
nonce,
expiry,
lender,
amount,
repayment,
nftAddress,
tokenId,
duration,
extra,
erc20Address,
createTime,
}){
try {
const offerPack = ethers.utils.solidityPack(
['address', 'uint256', 'uint256', 'address', 'uint32', 'uint256', 'bytes'],
[erc20Address, amount, repayment, nftAddress, duration, createTime, extra])
const signaturePack = ethers.utils.solidityPack(
['address', 'uint256', 'uint256'],
[lender, nonce, expiry])
let message = null
if (isCollection) {
message = ethers.utils.solidityKeccak256(
[ 'bytes', 'bytes', 'address', 'uint256'],
[ offerPack, signaturePack, contactAddr, chainId])
} else {
message = ethers.utils.solidityKeccak256(
[ 'bytes', 'uint256', 'bytes', 'address', 'uint256'],
[ offerPack, tokenId, signaturePack, contactAddr, chainId])
}
return message
} catch(e) {
console.error('signature encde', e.message)
}
return ''
}
// Construct an offer
const offer={
nonce: , // Globally unique nonce String
expiry: , // Expiry time of the offer, Unix timestamp in seconds
createTime: , // Create time of the offer, Unix timestamp in seconds
lender:, // Lender address
amount:, // Borrow amount (in wei unit),String
repayment:, // Repayment (in wei unit),String
nftAddress:'0xbc4ca0eda7647a8ab7c2061c2e118a18a936f13d', // Contract address of the NFT
tokenId:'', // NFT id, set to '' when you are making a collection offer
duration:2592000, // Duration of the offer
extra:'0x00', //Reserved for future usage. Use 0x00 for now.
erc20Address:'0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2', // wETH contract address
}
// Sign the encoded offer with user's wallet
const wallet = new ethers.Wallet(privateKey)
const signature = await wallet.signMessage(ethers.utils.arrayify(encode(offer.tokenId === '',offer))) // Signature for the offer