Last active
January 28, 2022 20:36
-
-
Save adamgobes/49086626457b47530d290f573d1a01a7 to your computer and use it in GitHub Desktop.
Batch create loans for NFT pawn shop
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
async function main() { | |
const [deployer] = await ethers.getSigners(); | |
console.log( | |
"Running batch create loans script with signer:", | |
await deployer.getAddress() | |
); | |
const NFTLoanFacilitatorContract = await ethers.getContractFactory("NFTLoanFacilitator"); | |
const facilitator = await NFTLoanFacilitatorContract.attach( | |
"0x2b804a97b2b3710b71ed1764eb6fa36233440738" | |
) | |
let facilitatorABI = [ | |
"function createLoan(uint256 collateralTokenId, address collateralContractAddress, uint16 maxPerSecondInterest, uint256 minLoanAmount, address loanAssetContractAddress, uint32 minDurationSeconds, address mintBorrowTicketTo)" | |
]; | |
const DAIContract = await ethers.getContractFactory("DAI"); | |
const dai = await DAIContract.attach("0x6916577695D0774171De3ED95d03A3239139Eddb") | |
const CryptoPunksContract = await ethers.getContractFactory("CryptoPunks"); | |
const punks = await CryptoPunksContract.attach( | |
"0x9EC7Ff8964AfBa6D8c43DC340A2E6c6c3156e966" | |
) | |
var interest = ethers.BigNumber.from( | |
Math.floor(5 * 10 ** 8), | |
).div(31_536_000); | |
var durationSeconds = ethers.BigNumber.from(86400); | |
var loanAmount = ethers.utils.parseUnits((1000).toString(), 18) | |
const currentNonce = 73; // current nonce of fake punks NFT contract (i.e. how many token ids have already been minted) | |
const totalMints = 5 | |
for (let i = currentNonce + 1; i <= currentNonce + totalMints; i++) { | |
const tokenId = ethers.BigNumber.from(i) | |
await punks.mint() | |
await wait(300000) // make sure mint gets recorded on-chain | |
var interest = ethers.BigNumber.from( | |
Math.floor(5 * 10 ** 8), | |
).div(31_536_000); | |
var durationSeconds = 86400; | |
var loanAmount = ethers.utils.parseUnits((1000).toString(), 18) | |
await punks.approve(facilitator.address, tokenId) | |
await wait(300000) // make sure approval gets recorded on-chain | |
let iface = new ethers.utils.Interface(facilitatorABI); | |
let data = iface.encodeFunctionData("createLoan", | |
[tokenId, punks.address, interest, loanAmount, dai.address, durationSeconds, await deployer.getAddress()] | |
) | |
data = '0x55c82235' + data.substring(10) | |
const tx = { | |
from: await deployer.getAddress(), | |
to: facilitator.address, | |
value: '0x00', | |
data, | |
gasLimit: "0x64190" | |
} | |
const result = await deployer.sendTransaction(tx) | |
console.log(result.hash) | |
} | |
} | |
function wait(ms) { | |
return new Promise(r => setTimeout(r, ms)); | |
} | |
main() | |
.then(() => process.exit(0)) | |
.catch((error) => { | |
console.error(error); | |
process.exit(1); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment