Skip to content

Instantly share code, notes, and snippets.

@TheBojda
Created December 23, 2021 12:51
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 TheBojda/a001fedc87f9978e4ef79b684f5e1194 to your computer and use it in GitHub Desktop.
Save TheBojda/a001fedc87f9978e4ef79b684f5e1194 to your computer and use it in GitHub Desktop.
Ethereum crowdfunding widget
const web3 = new Web3(config.PROVIDER_URL)
const balance = parseInt(await web3.eth.getBalance(config.ETH_ADDRESS))
const canvas = createCanvas(200, 270)
const ctx = canvas.getContext('2d')
ctx.fillStyle = "#aaaaaa"
ctx.fillRect(0, 0, canvas.width, canvas.height)
let qrcode = new Image()
qrcode.src = await QRCode.toDataURL(config.ETH_ADDRESS)
ctx.drawImage(qrcode, 10, 10, 180, 180)
ctx.fillStyle = '#000000'
ctx.font = '12px Impact'
ctx.fillText(config.TEXT, 10, 210)
ctx.fillText(`${(balance / 10 ** 18).toFixed(2).toString()} ${config.SYMBOL} / ${config.TARGET_VALUE.toFixed(2).toString()} ${config.SYMBOL}`, 10, 230)
ctx.fillStyle = '#ffffff'
ctx.fillRect(10, 240, 180, 10)
let percent = balance / (config.TARGET_VALUE * 10 ** 18);
if (percent > 1)
percent = 1;
ctx.fillStyle = '#037362'
ctx.fillRect(10, 240, Math.round(180 * percent), 10)
ctx.strokeStyle = '#000000'
ctx.lineWidth = 1
ctx.strokeRect(10, 240, 180, 10)
@TheBojda
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment