In this document, we'll explain the CW20 Adapter Contract that allows exchanging CW-20 tokens for Injective issued native tokens (using the TokenFactory module) and vice versa. For the CW-20 Adapter GitHub repository, see here.
Background
CW-20 is a specification for fungible tokens in CosmWasm, loosely based on ERC-20 specification. It allows creating and handling of arbitrary fungible tokens within CosmWasm, specifying methods for creating, minting and burning and transferring those tokens between accounts. The adapter contract will ensure that only authorized source CW-20 contracts can mint tokens (to avoid creating “counterfeit” tokens).
While the CW-20 standard is relatively mature and complete, the tokens exist purely within the CosmWasm context and are entirely managed by the issuing contract (including keeping track of account balances). That means that they cannot interact directly with Injective's native modules (for example, it’s not possible to trade them via the Injective exchange module, or to transfer without involving issuing contracts).
Considering the above, it’s necessary to provide a solution that would work as a bridge between CW20 and the Injective bank module.
The workflow of the contract is
Register a new CW-20 token
Exchange amount of X CW-20 tokens for Y TokenFactory tokens (original CW-20 tokens will be held by the contract)
Exchange Y TF tokens back for X CW-20 tokens (CW-20 tokens are released and TokenFactory tokens are burned)
Messages
RegisterCw20Contract { addr: Addr }
Registers a new CW-20 contract (addr) that will be handled by the adapter and creates a new TokenFactory token in format factory/{adapter_contract}/{cw20_contract}.
Redeem attached TokenFactory tokens and transfer CW-20 tokens to the recipient. If the recipient is not provided, they will be sent to the message sender.