Skip to content

Instantly share code, notes, and snippets.

@graemecode
Last active August 31, 2018 08:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save graemecode/c77dae93323b1d9a794bf5ec0fb9a440 to your computer and use it in GitHub Desktop.
Save graemecode/c77dae93323b1d9a794bf5ec0fb9a440 to your computer and use it in GitHub Desktop.
Part 1/4 in a series on collateralizing ERC721s for loans on Dharma Protocol
/********************
* Setup Dharma.js *
*******************/
// Instantiate Dharma
const dharma = new Dharma();
/********************
* Mint an ERC721 *
*******************/
/**
* Define an address for a debtor who will own the collateral on the blockchain.
* Replace this with an address that you own, such as your MetaMask account address.
*/
const DEBTOR_ADDRESS = "0x601e6e7711b9e3b1b20e1e8016038a32dfc86ddd";
// Define some amount of max gas to use in the transactions.
const MAX_GAS = 4712388;
// We use the two above variables to define default transaction arguments.
const TX_DEFAULTS = {
from: DEBTOR_ADDRESS,
gas: MAX_GAS,
};
// Load a deployed ERC721 contract that has the ability to mint tokens.
console.log("Loading the ERC721 contract...");
dharma.contracts.loadMintableERC721ContractAsync(TX_DEFAULTS).then(async (erc721) => {
// Find out the number of tokens that have been minted thus far.
const numTokens = await erc721.totalSupply.callAsync();
console.log(
`The ERC721 contract has loaded. There have been ${numTokens} tokens minted so far.`
);
// We can use the number of tokens as the token's ID, which will be unique as
// long as we increment it by one each time we mint a token. It's also okay to use
// a randomly generated token ID.
const tokenId = numTokens;
console.log(
`Attempting to create token with ID: ${tokenId}. Please approve the transaction.`
);
// Mint a new token and transfer it to the debtor's address defined above.
let txHash = await erc721.mint.sendTransactionAsync(DEBTOR_ADDRESS, tokenId);
console.log("Waiting for the transaction to be mined (may take a while)e...");
// Wait for this transaction to be mined before proceeding to the next step.
await dharma.blockchain.awaitTransactionMinedAsync(txHash);
// Let's confirm that it was created.
const tokenExists = await erc721.exists.callAsync(tokenId);
if (!tokenExists) {
throw new Error("Something went wrong and the token doesn't exist!");
}
/*
* Once this has completed, the debtor will have an ERC721 token that they can use
* for collateral for a loan on Dharma Protocol.
*
* Log the ID of the token, and save it so we can use it when creating the debt order.
*/
console.log(`New token minted with ID: ${tokenId.toString()}`);
/********************************
* Collateralization Approval *
********************************/
// Load the contract that will hold this ERC721 token as collateral when order is filled.
console.log("Loading collateralizer contract...");
const collateralizerContract = await dharma.contracts.loadERC721CollateralizerAsync();
const collateralizerAddress = collateralizerContract.address;
// We need to grant the collateralizer contract the ability to lock our ERC721
// as collateral when the debt order is eventually filled.
console.log(
"Please approve the transaction to give the " +
"collateralizer contract access to the collateral."
);
txHash = await erc721.approve.sendTransactionAsync(
collateralizerAddress,
tokenId,
);
// Wait for this transaction to be mined before checking the approvals are correct.
console.log("Waiting for the transaction to be mined (may take a while)...");
await dharma.blockchain.awaitTransactionMinedAsync(txHash);
// Let's confirm that we have the correct approval.
const approvedAddress = await erc721.getApproved.callAsync(tokenId);
const collateralizerApproved = (approvedAddress === collateralizerAddress);
if (!collateralizerApproved) {
throw new Error(
"Something went wrong and the collateralizer doesn't have approval yet!"
);
}
console.log("The collateralizer has approval for collateralization!");
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment