Skip to content

Instantly share code, notes, and snippets.

@devonwesley
Last active November 21, 2017 07:34
Show Gist options
  • Save devonwesley/948dd48f13b105141ed632e966a45dec to your computer and use it in GitHub Desktop.
Save devonwesley/948dd48f13b105141ed632e966a45dec to your computer and use it in GitHub Desktop.
Code that is being used for a blog
function renderContractList() {
const contractListContainer = document.getElementById('contract-list')
const { contracts } = compiledContract
Object.keys(contracts).forEach((contract, index) => {
const label = `contract-id-${index}`
const gas = contracts[contract].gasEstimates.creation
createContractInfo(gas, contract, label, function(el){
contractListContainer.appendChild(el)
const btnContainer = document.getElementById(label)
btnContainer.appendChild(
buttonFactory('primary', contract, contracts[contract], 'details')
)
})
})
}
function createContractInfo(gas, contractName, label, callback) {
const el = document.createElement('DIV')
el.innerHTML = `
<div class="mui-panel">
<div id="${label}" class="mui-row">
<div class="mui-col-md-3">
Contract Name: <strong>${contractName.substring(1, contractName.length)}</strong>
</div>
<div class="mui-col-md-3">
Gas Estimate: <strong style="color: green;">
${sumArrayOfInts(gas)}
</strong>
</div>
</div>
</div>
`
callback(el)
}
function sumArrayOfInts(array) {
return array.reduce((acc, el) => (acc += el), 0)
}
function buttonFactory(color, contractName, contract, type) {
const btn = document.createElement('BUTTON')
const btnContainer = document.createElement('DIV')
btn.className = `mui-btn mui-btn--small mui-btn--${color} mui-btn--raised"`
btn.innerText = type
btn.addEventListener('click', () => type === 'details'
? 'DETAILS BUTTON'
: 'DEPLOY BUTTON'
)
btnContainer.className = 'mui-col-md-3'
btnContainer.appendChild(btn)
return btnContainer
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment