Solidity

交易所如何呼叫我的智能合約方法?

  • June 30, 2021

我是智能合約世界的新手,還有很多事情我仍然不明白如何運作。

我使用以下方法創建了一個簡單的契約:

constructor()
{
   tokenCreator = msg.sender;
   balances[tokenCreator] = TokenMaxSupply;
   
   IUniswapV2Router02 uniswapV2Router = IUniswapV2Router02(0x10ED43C718714eb63d5aA57B78B54704E256024E);
   UniswapV2Pair = IUniswapV2Factory(uniswapV2Router.factory()).createPair(address(this), uniswapV2Router.WETH());
   UniswapV2Router = uniswapV2Router;
}

/**
* @dev Returns the total supply of this token
*/
function TotalSupply() public view returns (uint256)
{
   return TokenMaxSupply;
}

/**
* @dev Check the number of tokens owned by an address including holder reflections
*/
function CheckAddressBalance(address addressToCheck) public view returns (uint256)
{
   return balances[addressToCheck];
}

/**
* @dev Check the allowance between addresses
*/
function CheckAllowance(address from, address to) public view returns (uint256)
{
   return allowances[from][to];
}

/**
* @dev Transfers tokens from one address to another.
*/
function TransferTokens(address sendingAddress, address addressToSend, uint256 amount) public returns (bool)
{
   require(addressToSend != address(0), 'Invalid Address.');
   require(sendingAddress != address(0), 'Invalid sending Address.');
   require(balances[sendingAddress] >= amount, 'Not enough tokens to transfer.');
   
   //Decrease sender balance
   balances[sendingAddress] = balances[sendingAddress].sub(amount);
   
   //Add the new amount to receiver address
   balances[addressToSend] = balances[addressToSend].add(amount);
   
   emit Transfer(sendingAddress, addressToSend, amount);
   return true;
}

我的目標是部署合約並將其添加到 pancakeswap。

我的問題是:當有人想在 pancakeswap 中購買我的代幣時,TransferTokens應該呼叫該方法來添加/減去代幣。pancakeswap 是如何知道它需要呼叫這個函式的呢?

還是 pancakeswap 呼叫了特定的方法,我需要更改方法的名稱?

是後者。

您的契約必須絕對符合ERC20 標準。您不能使用諸如您的功能之類的TransferTokens功能。

Pancakeswap(幾乎所有其他人也一樣)將呼叫 ERC20 中定義的函式。不屬於 ERC20 的自定義函式永遠不會被呼叫。

引用自:https://ethereum.stackexchange.com/questions/102736