Possibility of creating two interacting tokens

Consider two tokens A and B. A has fixed supply, say, 1000 units, while B initially has zero supply. B is created, along with a brand new public-private key-pair, when A is burned, where A = B. So, there will be 1000 tokens in total be it A or B. B again can be created by burning A.

Some guideline to create such a contract, if possible at all, on testnet would be much appreciated.

• > This cycle will allow circulation of 1000 units of A and 0 units of B, or 2000 units of B and 0 units of A... I don't think that this is correct: if B is exchanged for 0.5A and there are 1000 A then there can only be a maximum of 500 B. – SteveJaxon Nov 1 '17 at 11:31
• I will work on exact conversion ratio. Could you provide some guideline given A and B are equal? @SteveJaxon – moazzem Nov 1 '17 at 12:41
• I'm primarily interested in whether the interaction between two tokens is possible. Basically, each token has its own purpose and conversion between them should be possible. Any method to tag each should work as well. – moazzem Nov 1 '17 at 17:40

Here's an example of one way to approach this - and a demonstration of how to make two Token Smart Contracts interact with each other (and how you would go about maintaining the ledgers of each):

pragma solidity ^0.4.18;

// Minimal token interface
contract ERC20Basic {
uint256 public totalSupply;
function balanceOf(address who) public constant returns (uint256);
function transfer(address to, uint256 value) public returns (bool);
event Transfer(address indexed from, address indexed to, uint256 value);
}

contract Interacting is ERC20Basic {
uint256 public conversionRate;
Interacting public interactWith;

function convert(uint256 amount) internal returns (uint256);

function burn(uint256 amount) public {
// Implement business logic / validation etc.
totalSupply = totalSupply - amount;
interactWith.mint(convert(amount));
}

function mint(uint256 amount) external {
// Implement business logic / validation etc.
totalSupply = totalSupply + amount;
}

// Define ERC20Basic methons
// ...
}

contract InteractingTokenA is Interacting {
function InteractingTokenA(uint256 supply, uint256 rate) public {
totalSupply = supply;
conversionRate = rate;
}

// A little bit clumsy, but I'm not sure of a better solution for linking the two contract instances
function setInteractingWith(Interacting contractAddy) public {
}

function convert(uint256 amount) internal returns (uint256) {
// Obviously checks have been made and necessary rounding would be done here
return amount / conversionRate;
}
}

contract InteractingTokenB is Interacting {
function InteractingTokenB(Interacting contractAddy, uint256 rate) public {
totalSupply = 0;
conversionRate = rate;
}

function convert(uint256 amount) internal returns (uint256) {
// Obviously checks have been made and necessary rounding would be done here
return amount * conversionRate;
}

// Define ERC20Basic methods
// ...
}

There are quite a lot of assumptions here. For example, "Consider two tokens A and B" - I assume that you mean ERC20 token and this opens up a question of whether the owner burns their tokens on A to get B or whether the owner of token A is burning their supply etc.

There is also questions about how you deal with 'loss of precision' when converting back and forwards between tokens if there is a remainder after the conversion rate is applied.