Delegate my votes

Make sure the example app is being served with an HTTP server.
Please install MetaMask: https://metamask.io/
 
 
 

Our code

We use web3.js + our delegation function


  <script type="text/javascript" src="/templates/static/libs/delegate/constellation-abi.js"></script>
  <script type="text/javascript" src="https://cdn.jsdelivr.net/gh/ethereum/web3.js@1.2.6/dist/web3.min.js"></script>
<script type="text/javascript">
  window.addEventListener('load', (event) => {
    let web3;
    const web3Warning = document.getElementById('web3-warning');
    const myAddress = document.getElementById('my-address');
    const delegate = document.getElementById('current-delegate');
    const newDelegate = document.getElementById('new-delegate');
    const submit = document.getElementById('submit');
    const loader = document.getElementById('loader');
    let myAccount, constellationAddress, constellationAbi, constellation;

    if (typeof window.ethereum === 'undefined') {
      console.error('Client does not have an active Web3 provider or the example app is not being run from an HTTP server.');
      console.log('Go here to install MetaMask: https://metamask.io/');
      alert(
        'You need a Web3 provider to run this page. ' + 
        'Go here to install MetaMask:\n\n' +
        'https://metamask.io/'
      );
      web3Warning.classList.remove('hidden');
    } else {
      main();
    }

    async function main() {
      web3 = new Web3(window.ethereum);
      let net = web3.currentProvider.networkVersion;

      // This app only works with Ropsten or Main
      if (net !== '1' && net !== '3') {
        alert('Please select the Main or Ropsten network.');
        return;
      }

      const accounts = await window.ethereum.enable();
      myAccount = accounts[0];

      myAddress.innerText = myAccount;

      // Mainnet Constellation Contract
      constellationAddress = '0x28eE8BA54aFE95A6AcA17cF4913F929FBEAFD402';
      constellationAbi = window.constellationAbi;
      constellation = new web3.eth.Contract(constellationAbi, constellationAddress);

      let currentDelegate;
      try {
        currentDelegate = await constellation.methods.delegates(myAccount).call();
      } catch (e) {
        currentDelegate = 0;
      }

      delegate.innerText = currentDelegate == 0 ? 'None' : currentDelegate;

      submit.onclick = async () => {
        const delegateTo = newDelegate.value;

        if (!delegateTo) {
          alert('Invalid address to delegate your votes.');
          return;
        }

        loader.classList.remove('hidden');
        try {
          const tx = await constellation.methods.delegate(delegateTo).send({ from: myAccount });
          console.log(tx);
          alert(`Successfully Delagated to ${delegateTo}`);
          window.location.reload();
        } catch(e) {
          console.error(e);
          alert(e.message);
        }
        loader.classList.add('hidden');
      };
    }
  });
</script>
  

Join us

Discord
Twitter
constellation

Newsletter

Keep in touch ! Medium Newsletter