Venom Docs
  • General
    • Welcome to Venom
    • What's New
    • Ecosystem
    • Create a new Wallet Account
    • Balance Transfers
    • Community
    • Presskit
  • Learn
    • Glossary
    • Architecture
    • Tokens and Assets
    • Messages and Transactions
    • Accounts
  • Build
    • Development Guides
      • Comparing of Ethereum vs Venom architectures
      • Setting Up The Venom Smart Contract Development Environment
      • How to create your own fungible TIP-3 token
        • Fungible tokens in Venom network
        • Quick start developing with TIP-3
        • Venom In Action. Simple Tokensale.
        • Venom In Action. Extend our Tokensale with frontend
        • Venom In Action. Going Global
        • Venom In Action. Ways of code enhancing
      • Developing of simple voting system.
        • Voting system basics
        • Venom In Action. Voting system contracts.
        • Venom In Action. Ways of code enhancing
      • How to create your own non-fungible TIP-4 token
        • Non-Fungible tokens in Venom network
        • Quick start developing with TIP-4
        • Venom In Action. Simple NFT auction
        • Venom In Action. Extend our auction with frontend
    • Integration Guides
      • How to connect Dapp UI to Venom
    • Tools & Resources
  • Maintain
    • Network Maintainers
Powered by GitBook
On this page
  1. Build
  2. Development Guides
  3. Developing of simple voting system.

Venom In Action. Ways of code enhancing

Let's dive into some best practices and good tone coding.

First of all, if your contract is deploy some another contracts, it's advisable to have a view method for returning new contract address:

function getBallotAddress(address owner) 
    external
    view
    responsible
    returns (address)
{
    TvmCell ballotStateInit = tvm.buildStateInit({
        contr: Ballot,
        varInit: {
            _vote: address(this),
            _managerPublicKey: _managerPublicKey,
            _owner: owner
        },
        code: _ballotCode
    });
    return{value: 0, bounce: false, flag: 64} address(tvm.hash(ballotStateInit));
}

As you can see we used some keywords and syntax, that haven't been discussed before. If function marked with responsible keyword, this function will generate an outbound message for caller with value, bounce and flag you set. It's preferable to use exactly {value: 0, bounce: false, flag: 64} for this function. Do you remember TIP-3 wallet deploying from our TIP-3 guide? Function deployEmptyWallet is responsible too. That's why we can set a callback parameter there.

The next important point is a success/unsuccess callbacks or events. Enhancing your contract with event emitting wouldn't be amiss. Especially when you deploys something or ends some case. For example we can add NewBallot event (when new ballot deployed) and VoteAccepted event (after onBallotUsed callback)

Vote.sol
...
contract Vote {
    event NewBallot(address owner);
    event VoteAccepted(address ballot, bool accept);
    ...
    function deployBallot(address owner, address sendRemainingGasTo) external view {
        ...
        emit NewBallot(owner);
        sendRemainingGasTo.transfer({value: 0, flag: 128, bounce: false});
        ...
    }
    ...
    function onBallotUsed(address owner, address sendRemainingGasTo, bool accept) external {
        ...
        emit VoteAccepted(expectedAddress, accept);
        sendRemainingGasTo.transfer({value: 0, flag: 128, bounce: false});
        ...
    }
...
}

There is another small hack for helping frontend developer. You can transfer small amount of nanotons (1,2,3..,etc) to owner address. For example:

owner.transfer({value: 1, flag: 1, bounce: false))

Frontend developer can subscribe on incoming transaction to user's wallet and use this small values for detect contract behavior. For example we can send 1 nanovenom, if vote has beet accepted

Vote.sol
...
contract Vote {

    ...
    function onBallotUsed(address owner, address sendRemainingGasTo, bool accept) external {
        ...
        owner.transfer({value: 1, flag: 1, bounce: false})
        emit VoteAccepted(expectedAddress, accept);
        sendRemainingGasTo.transfer({value: 0, flag: 128, bounce: false});
    }
...
}l

Pay attention, that events and small value callbacks should be instantiate before any transfers with 128 flag.

PreviousVenom In Action. Voting system contracts.NextHow to create your own non-fungible TIP-4 token

Last updated 2 years ago