Gram Transfer smart contract

alfacoder

Member
Сообщения
58
Реакции
11
This sample demonstrates how Gram transfer works. Call "MyContract.method(address anotherContract, uint amount)". This requests <amount> of Grams from the contract deployed at the specified address.

Код:
pragma solidity ^0.5.0;

// the interface of a remote contract
contract AnotherContract {
    function remoteMethod(uint value) public;
}

// the contract calling the remote contract function with parameter
contract MyContract {

    // runtime function that allows contract to process external messages, which bring
    // no value with themselves.
    function tvm_accept() private pure {}

    // modifier that allows public function to accept all calls before parameters decoding.
    modifier alwaysAccept {
        tvm_accept(); _;
    }

    // state variable storing the number of times 'method' function was called
    uint m_callCounter;

    function method(AnotherContract anotherContract, uint amount) public alwaysAccept {
        // call function of remote contract with a parameter
        anotherContract.remoteMethod(amount);
        // incrementing the counter
        m_callCounter++;
        return;
    }

}
The remote contract transfers <amount> of Grams to the caller via msg.sender.transfer(value). Each contract has an internal transaction counter. The counter value is increased after each transaction and stored in the persistent memory.

Код:
pragma solidity ^0.5.0;

// the interface of a remote contract
contract AnotherContract {
    function remoteMethod(uint value) public;
}


contract MyContract is AnotherContract {

    // runtime function that allows contract to process external messages, which bring
    // no value with themselves.
    function tvm_accept() private pure {}

    // modifier that allows public function to accept all calls before parameters decoding.
    modifier alwaysAccept {
        tvm_accept(); _;
    }

    // state variable storing the number of times 'remoteMethod' function was called
    uint m_callCounter;

    // A function to be called from another contract
    // This function receives parameter 'value' from another contract and
    // transfers value to the caller.
    function remoteMethod(uint value) public alwaysAccept {
        msg.sender.transfer(value);
        m_callCounter++;
        return;
    }
    
}
 
Сверху