Skip to content

Instantly share code, notes, and snippets.

@ajb413
Last active May 9, 2023 19:03
Show Gist options
  • Save ajb413/3124c59d104dec5564bee8f0765e7a3f to your computer and use it in GitHub Desktop.
Save ajb413/3124c59d104dec5564bee8f0765e7a3f to your computer and use it in GitHub Desktop.
Vanilla JS Example for Compound Extension for Comet
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Vanilla Compound Extension</title>
</head>
<body style="background: #FFF;">
<h1>Compound Extension Example</h1>
<p>Embedded iFrame</p>
<p id="status"></p>
</body>
<script type="text/javascript" src="https://cdn.jsdelivr.net/npm/ethers@5.7.2/dist/ethers.umd.min.js"></script>
<script type="text/javascript" src="https://cdn.jsdelivr.net/npm/@compound-finance/comet-extension@0.0.13/dist/index.browser.min.js"></script>
<script type="text/javascript">
document.addEventListener('DOMContentLoaded', async () => {
const statusElement = document.getElementById('status');
if (window.ethereum) {
// be sure to add ?embedded=true query param to the end of your extension source URL
const embedded = window.location.search.includes('embedded');
statusElement.innerText = 'Not currently connected to the Compound app.';
let provider;
if (!embedded) {
provider = new ethers.providers.Web3Provider(window.ethereum);
} else {
let rpc = new CometExtension.buildRPC();
provider = new CometExtension.RPCWeb3Provider(rpc.sendRPC);
rpc.attachHandler();
rpc.sendRPC({ type: 'getSelectedMarket' }).then(({ selectedMarket }) => {
console.log('selectedMarket', selectedMarket);
// selectedMarket {
// "chainId": 1,
// "baseAssetSymbol": "USDC",
// "marketAddress": "0xc3d688B66703497DAA19211EEdff47f25384cdc3"
// }
statusElement.innerText = 'Connected to parent app successfully.';
});
}
} else {
statusElement.innerText = 'No web3 provider.';
}
});
</script>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment