alfacoder
Member
- Сообщения
- 58
- Реакции
- 11
Gram Transfer в TON на языке Solidity
Данный пример демонстрирует, как работает передача Grams. Вызывается через метод Call MyContract.method(address anotherContract, uint amount). Данная операция требует некоторое количество Grams <amount> из контракта, развернутого по указанному адресу.
Удаленный контракт выполняет transfer <amount> количества Grams вызывающей стороне через msg.sender.transfer(value). Каждый смарт-контракт имеет внутренний счетчик транзакций. Значение счетчика увеличивается после каждой транзакции и сохраняется в постоянной памяти.
Данный пример демонстрирует, как работает передача Grams. Вызывается через метод Call MyContract.method(address anotherContract, uint amount). Данная операция требует некоторое количество Grams <amount> из контракта, развернутого по указанному адресу.
Код:
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;
}
}
Код:
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;
}
}