Solidity

如何從另一個合約中銷毀代幣或代幣列表?

  • October 19, 2021

$$ Edit 14-03-2018 $$ 我想創建一個可以生成隨機數代幣 A 的合約 A 和另一個接收並銷毀該代幣中的一個或多個以生成新代幣 B 的合約 B。 這就像將一隻 CryptosKitty 發送到另一個合約 B,燒掉小貓,並用小貓的基因生成一隻狗。

謝謝您的幫助。

如果合約A允許代幣持有者燒掉他們的代幣,你只需A要從合約中呼叫相應的合約方法,B如下所示:

contract B is ERC721 {
 uint nextDogID = 0;
 A a = ...;

 function catToDog (uint _catID) public (returns _dogID) {
   _dogID = nextDogID++;
   _mint (msg.sender, _dogID);
   copyGene (_catID, _dogID);
   require (a.transferFrom (msg.sender, address (this), _catID));
   require (a.burn (_catID)); // Here we burn the cat token!
 }
}

如果A不允許銷毀,只需將“已銷毀”的令牌發送到某個死地址:

contract B is ERC721 {
 uint nextDogID = 0;
 IERC721 a = ...;

 function catToDog (uint _catID) public (returns _dogID) {
   _dogID = nextDogID++;
   _mint (msg.sender, _dogID);
   copyGene (_catID, _dogID);

   // Here we send the cat token to dead address effectively burning it
   require (a.transferFrom (
     msg.sender,
     0x000000000000000000000000000000000000dead,
     _catID));
 }
}

你實際上不需要“接收”任何東西。合約 B 只需要能夠銷毀合約 A 中列出的一些代幣。

這是一些範常式式碼:

contract A {
   uint tokens = 10;
   // which address is allowed to burn tokens
   address allowedBurnerAddress = 0x1234;

   // allowed contract (B) can call this to burn tokens
   function burn(uint amount) public {
       require(msg.sender == allowedBurnerAddress);
       tokens -= amount;
   }
}

contract B {
   // address for contract A
   address burnContract = 0x0987;

   // call this to burn tokens which exist in contract A
   function PerformBurn() public {
       // add some requirements for who can call this function

       A original = A(burnContract);
       original.burn(5);
   }
}

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