Created
September 14, 2022 03:56
-
-
Save 0xlxy/9dfc6532cf335ce84f82c34a1a89ad9a to your computer and use it in GitHub Desktop.
Script for making an offer on looksrare
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
const https = require('https') | |
const { Wallet } = require('ethers') | |
const { | |
addressesByNetwork, | |
SupportedChainId, | |
generateMakerOrderTypedData, | |
} = require('@looksrare/sdk') | |
require('dotenv').config() | |
const WALLET_PRIVATE_KEY = process.env.WALLET_PRIVATE_KEY | |
const LOOKSRARE_API_KEY = process.env.LOOKSRARE_API_KEY | |
const signerAddress = "0x5E113EDC0eaf00699889FC510DB121308bBA1261" | |
const contractAddress = "0x3A112D86899578E63b373B6D09322E18Aed924Cb" | |
const price = "1000000000000000" | |
const tokenId = "4946" | |
const validity = 30 | |
async function makeOffer(opt) { | |
const data = JSON.stringify(opt) | |
const options = { | |
hostname: 'api.looksrare.org', | |
port: 443, | |
path: '/api/v1/orders', | |
method: 'POST', | |
headers: { | |
'Content-Type': 'application/json', | |
'Content-Length': data.length, | |
"X-API-KEY": LOOKSRARE_API_KEY | |
}, | |
} | |
return new Promise((resolve, reject) => { | |
const req = https.request(options, (res) => { | |
res.on('data', (data) => { | |
data = JSON.parse(data) | |
resolve(data) | |
}) | |
}) | |
req.on('error', (error) => { | |
reject(JSON.parse(error)) | |
}) | |
req.write(data) | |
req.end() | |
}) | |
} | |
async function getUserNonce(signerAddress) { | |
const options = { | |
hostname: 'api.looksrare.org', | |
port: 443, | |
path: `/api/v1/orders/nonce?address=${signerAddress}`, | |
} | |
return new Promise((resolve, reject) => { | |
const req = https.get(options, (res) => { | |
res.on('data', (data) => { | |
data = JSON.parse(data) | |
resolve(data.data) | |
}) | |
}) | |
req.on('error', (error) => { | |
reject(JSON.parse(error)) | |
}) | |
req.end() | |
}) | |
} | |
async function main( | |
signerAddress, | |
contractAddress, | |
price, | |
tokenId, | |
validity, | |
WALLET_PRIVATE_KEY | |
) { | |
const chainId = SupportedChainId.MAINNET | |
const addresses = addressesByNetwork[chainId] | |
const nonce = await getUserNonce(signerAddress) // Fetch from the api | |
const now = Math.floor(Date.now() / 1000) | |
const paramsValue = [] | |
// This variable is used to enforce a max slippage of 25% on all orders, if a collection change the fees to be >25%, the order will become invalid | |
const minNetPriceRatio = 7500 | |
const makerOrder = { | |
isOrderAsk: false, | |
signer: signerAddress, | |
collection: contractAddress, | |
price, | |
tokenId, | |
amount: '1', | |
strategy: addresses.STRATEGY_STANDARD_SALE, | |
currency: addresses.WETH, | |
nonce: nonce, | |
startTime: now, | |
endTime: now + 86400 * validity, | |
minPercentageToAsk: minNetPriceRatio, | |
params: paramsValue, | |
} | |
const signer = new Wallet(WALLET_PRIVATE_KEY) | |
const { domain, value, type } = generateMakerOrderTypedData( | |
signerAddress, | |
chainId, | |
makerOrder | |
) | |
const signature = await signer._signTypedData(domain, type, value) | |
const res = await makeOffer({ ...makerOrder, signature }) | |
console.log(res) | |
} | |
main( | |
signerAddress, | |
contractAddress, | |
price, | |
tokenId, | |
validity, | |
WALLET_PRIVATE_KEY | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment