Bitcoin Integration

DMEX has integrated with Provable.xyz's (the same company providing the Oracle services) new service called pTokens that offers a trustless way to move Bitcoin to the Ethereum blockchain and back.

How does it work?

Provable is running a TEE (Trusted Execution Environment), or in other words a blackbox machine that is preprogrammed to act in a certain way and has no access to it from external parties. This machine once started generates private keys for two blockchains (Bitcoin and Ethereum), no-one has access to the private keys, just the TEE.

The TEE can be contacted to request a BTC deposit address that would be linked to an Etheruem address. Every time a user sends BTC to the BTC deposit address, the TEE mints an equal amount of pBTC (a ERC777 Ethereum Token which is pegged 1:1 with Bitcoin, 1 pBTC = 1 BTC) and sends the newly minted pBTC tokens to the Ethereum address specified when requesting the BTC address.

At anytime, a user can redeem his/her pBTC into BTC by calling the redeem() method on the pBTC contract.

Why this approach is superior to other BTC pegged tokens?

pBTC is a direct representation of Bitcoin on the Ethereum blockchain. Other pegged tokens (such as WBTC) require an exchange to switch between BTC and WBTC which adds fees and spread costs. pBTC on the other hand allows switching from BTC to pBTC and the other way around without any cost and not requiring liquidity. All bitcoins sent to the TEE are converted into the exactly the same amount of pBTC minus the blockchain fees and a small fee taken by Provable for the service.

How is pBTC integrated into DMEX?

DMEX was designed from the get go with user experience in mind, therefore we were not satisfied with simply adding pBTC as a token. That would require users to also have ETH in order to be able to deposit funds into the DMEX contract. We wanted to give our users the ability to use Bitcoin on DMEX without needing to purchase any ETH (users still need an Ethereum account in order to sign orders, but no ETH is required).

In order to provide the best user experience we have adopted the following approach:

  1. When a user clicks on the "Deposit" button, while having BTC chosen as the margin currency. The website generates a deposit contract for the user (everything is done in the browser). This contract is specific to the user's wallet, all funds sent to it will be credited to the users' account on DMEX). The deposit contract code is as follows:

pragma solidity >0.4.99 <0.6.0;
// https://theethereum.wiki/w/index.php/ERC20_Token_Standard
contract ERC20Interface {
function approve(address spender, uint tokens) public returns (bool success);
function balanceOf(address tokenOwner) public view returns (uint balance);
}
contract DMEXBaseInterface {
function depositTokenForUser(address token, uint128 amount, address user) public;
}
contract DepositToDMEX {
address public owner;
address public dmex_contract;
event Log(address tokenAddress);
constructor(address owner_, address dmex_contract_, address token) public {
owner = owner_;
dmex_contract = dmex_contract_;
IERC1820Registry(0x1820a4B7618BdE71Dce8cdc73aAB6C95905faD24).setInterfaceImplementer(address(this), keccak256("ERC777TokensRecipient"), address(this));
sendTokensToDMEX(token);
}
function sendTokensToDMEX(address token) public
{
uint256 availableBalance = ERC20Interface(token).balanceOf(address(this));
uint128 shortAvailableBalance = uint128(availableBalance);
ERC20Interface(token).approve(dmex_contract, availableBalance);
DMEXBaseInterface(dmex_contract).depositTokenForUser(token, shortAvailableBalance, owner);
}
function tokensReceived(address operator, address from, address to, uint256 amount, bytes memory userData, bytes memory operatorData) public
{
emit Log(msg.sender);
sendTokensToDMEX(msg.sender);
}
}

2. The deposit contract address is used when requesting the BTC address from pTokens. The BTC address received from pTokens is displayed to the user. All Bitcoins sent to the BTC address will be forwarded in pBTC token form to the user's deposit contract.

3. When the user wants to withdraw his BTC from DMEX, the redeem function is called from the DMEX contract on the pBTC contract with the Bitcoin withdrawal address as argument. This redeem() call triggers the TEE to release the specified bitcoin amount to the BTC withdrawal address and burn the associated pBTC.

This approach allows for users not familiar with Ethereum to use their Bitcoins in a trustless way while using DMEX. All the user needs to do, is send his/her bitcoins to the BTC deposit address and the Bitcoins will appear in the user's account on DMEX.

The deposit processing time will be equal to: Bitcoin transaction confirmation (1 confirmation) + Ethereum transaction confirmation (1 confirmation)