Giter VIP home page Giter VIP logo

transfer-gateway-example's Introduction

Deprecated Repository

This repository is deprecated and no longer maintained. To get a high-level overview of how Transfer Gateway works, head over to the Transfer Gateway Documentation Page. Next, make sure to check our Transfer Gateway Testnet Tutorial and this step-by-step video walk-through.

Transfer Gateway Example

This project is built using the loom-js Transfer Gateway API, and demonstrates how to transfer ERC721/ERC20 tokens between Ethereum and Loom DAppChains via a browser frontend built with React.

Interface in action

Quick Start

Requirements

  • node >= 8
  • wget (used to download Loom DAppChain binary)
  • nc (used to check required ports aren't in use)
  • MetaMask browser extension

Install

After cloning this repository install all the necessary dependencies use the transfer_gateway shell script to automatically download & install all the required dependencies:

./transfer_gateway setup

Running

After everything has been installed use the transfer_gateway script to spin up all the required services:

./transfer_gateway start

Stop

When you're done playing around with the example you should stop all the running services using:

./transfer_gateway stop

Status

To check which services are currently running:

./transfer_gateway status

Cleanup

If you want to start with a clean slate you can remove all the node_modules directories, and the loom binary by running:

./transfer_gateway cleanup

Web Browser

After ./transfer_gateway start and starts the necessary services you can access the web frontend at localhost:8080.

MetaMask

This example requires MetaMask to be installed, you must also set MetaMask to use the Private Network at localhost:8545.

You will also need to import the private key 0xbb63b692f9d8f21f0b978b596dc2b8611899f053d68aec6c1c20d1df4f5b6ee2 generated by Ganache (account 0x5194b63f10691e46635b27925100cfc0a5ceca62) into MetaMask.

Troubleshooting

  • If you stop the example and start it up again you may encounter a MetaMask error like this:

"rpc error with payload {…nonce. account has nonce of: 0 tx has nonce of: 5"

This happens because MetaMask keeps track of the last nonce that was used to send a transaction with a particular account, and when all the chains are reset the nonce needs to be reset back to zero. You can force MetaMask to reset the nonce by reseting the imported account.

  • Restart the entire service can help to clean cache also ./transfer_gateway restart, after restart you should clean MetaMask cache.

  • Sometimes when run ./transfer_gateway start and a message like Ganache port 8545 is already in use appears it's because the service ganache is running (obvious), however if you never ran ./transfer_gateway start then it's because another agent started ganache and this example starts it self ganache version.

  • In order to stop all services (used by cards-gateway-example) you should run ./transfer_gateway stop, however if services like ganache or webpack didn't halt then you need to stop then by the process id or pid.

Example "in a nutshell"

The cards-gateway-example directory contains five sub directories:

├── dappchain
├── truffle-ethereum
├── truffle-dappchain
├── transfer-gateway-scripts
└── webclient

In the following sections we'll explain what's in each of these directories.

NOTE: In this doc we use the term Mainnet to refer to an Ethereum-like network, as a matter of convenience. In this example the only Ethereum network we actually interact with is Ganache, but you could also hook everything up to use the Ethereum Mainnet, or the Rinkeby Testnet by changing a few configuration settings.

📁 dappchain

This directory contains the loom binary (after setup), DAppChain config files, and some key pairs.

The keys have been generated specifically for this example, don't reuse them anywhere else. If you wish to use this project as a starting point for your own please don't forget to replace all the keys with your own.

Let's take a closer look at loom.yaml, this is a configuration file that can be used to configure both the DAppChain node and the Transfer Gateway Oracle. You can find documentation on the various settings in this config file in the Loom SDK docs, but we'll highlight some of the settings here that are relevant to this example.

loom.yaml

# This is a unique identifier for the DAppChain used in this example
ChainID: "default"
# This enables the latest version of the contract registry maintained by the DAppChain, which is
# required in order for the Transfer Gateway to be able verify DAppChain contract ownership.
# This setting must not be changed after the DAppChain starts for the first time!
RegistryVersion: 2
# Transfer Gateway Settings
TransferGateway:
  # Enables the Gateway Go contract (DAppChain Gateway), must be the same on all nodes.
  ContractEnabled: true

  # Enables the in-process Gateway Oracle.
  OracleEnabled: true

  # Address of the Ganache network where the Mainnet Solidity contracts are deployed
  # (take a look at the truffle-ethereum directory).
  EthereumURI: "http://127.0.0.1:8545"

  # Address of the Mainnet Gateway contract deployed to the Ganache network above.
  MainnetContractHexAddress: "0xf5cad0db6415a71a5bc67403c87b56b629b4ddaa"

  # Private key that will be used by the Gateway Oracle to sign pending withdrawals.
  MainnetPrivateKeyPath: "oracle_eth_priv.key"

  # Private key that will be used by the Gateway Oracle to send txs to the DAppChain Gateway.
  DAppChainPrivateKeyPath: "oracle_priv.key"

  # Address of DAppChain node the Gateway Oracle should interact with.
  DAppChainReadURI: "http://localhost:46658/query"
  DAppChainWriteURI: "http://localhost:46658/rpc"

  # These control how frequently the Gateway Oracle will poll the DAppChain and Ganache.
  DAppChainPollInterval: 1 # seconds
  MainnetPollInterval: 1 # seconds

  # Number of seconds to wait before starting the Gateway Oracle, this allows the DAppChain node
  # to spin up before the Gateway Oracle tries to connect to it.
  OracleStartupDelay: 5

  # Number of seconds the Gateway Oracle should wait between reconnection attempts if it encounters
  # a network error of some kind.
  OracleReconnectInterval: 5

Now let's take a look at the genesis.json file (or genesis.example.json if you haven't setup the example yet), this file specifies which Go contracts should be deployed to the DAppChain when the DAppChain starts up for the first time. You can read more about the coin and dpos contracts in the Built-in Contracts docs, but these two contracts are not relevant to this particular example. The two contracts we're interested in are the built-in addressmapper, and gateway contracts.

genesis.json

{
    "contracts": [
        {
            "vm": "plugin",
            "format": "plugin",
            "name": "addressmapper",
            "location": "addressmapper:0.1.0",
            "init": null
        },
        {
            "vm": "plugin",
            "format": "plugin",
            "name": "gateway",
            "location": "gateway:0.1.0",
            "init": {
                "owner": {
                    "chain_id": "default",
                    "local": "c/IFoEFkm4+D3wdqLmFU9F3t3Sk="
                },
                "oracles": [
                    {
                        "chain_id": "default",
                        "local": "22nuyPPZ53/qAqFnhwD2EpNu9ss="
                    }
                ]
            }
        }
    ]
}

The Address Mapper contract is responsible for mapping owned DAppChain accounts to Mainnet accounts, and vice versa, you can read more about its purpose in the Transfer Gateway docs.

The Gateway contract is responsible for contract mapping, and token transfers to and from the DAppChain. It relies on the Gateway Oracle (though there may be more than one) to transfer relevant data between the DAppChain and Mainnet.

📁 truffle-ethereum

This directory contains the contracts that are deployed to Mainnet, primarily the Gateway contract (Mainnet Gateway), and the CryptoCards/GameToken ERC721/ERC20 token contract.

The CryptoCards/GameToken ERC721/ERC20 contract implements a convenient function for depositing tokens into the Gateway contract:

function depositToGateway(uint tokenId) public {
  safeTransferFrom(msg.sender, gateway, tokenId);
}

The Gateway contract implements the withdrawERC721 or withdrawERC20 function, which should be called by a user who wishes to withdraw a token from the Mainnet Gateway back to their own Mainnet account.

NOTE: The Mainnet Gateway will only allow a user to withdraw a token if they can provide proof that it's no longer in circulation on the DAppChain, you can read more about it in the Transfer Gateway docs.

📁 truffle-dappchain

This directory contains the CryptoCardsDAppChain/GameTokenDAppChain ERC721/ERC20 contract that's deployed to the DAppChain, this contract will keep track of any tokens sent from the CryptoCards/GameToken ERC721/ERC20 contract on Mainnet to the DAppChain via the Transfer Gateway.

When a user deposits their CryptoCards/GameToken tokens into the Mainnet Gateway contract the DAppChain Gateway contract will mint the corresponding tokens in the CryptoCardsDAppChain/GameTokenDAppChain contract.

Note that the DAppChain Gateway must be authorized to mint tokens in the CryptoCardsDAppChain/GameTokenDAppChain contract, otherwise token transfers from Mainnet to the DAppChain won't work.

function mint(uint256 _uid) public {
  require(msg.sender == gateway);
  _mint(gateway, _uid);
}

📁 transfer-gateway-scripts

This directory contains a single index.js script file which is responsible for mapping the CryptoCards/GameToken ERC721/ERC20 contract on Mainnet to the CryptoCardsDAppChain/GameTokenDAppChain ERC721/ERC20 contract on the DAppChain. Without this contract mapping the Transfer Gateway won't know what to do when it receives ERC721/ERC20 tokens from either contract. You can read more about contract mapping in the Transfer Gateway docs.

📁 webclient

This directory contain the web frontend, which requires MetaMask to be installed in a compatible browser (Chrome / Firefox). The web frontend can be reached at http://localhost:8080 after running the ./transfer_gateway start.

The frontend consists of four pages:

  • Home - where you must map your Mainnet account to a DAppChain account by signing a message via MetaMask with your Mainnet account. This must be done before attempting to transfer any cards.
  • Owned Cards - where you can see the cards currently owned by you on Mainnet (yeah we just gave 5 cards to you 😉).
  • Cards on Gateway - where you can see the cards in the Mainnet Gateway that are waiting to be withdrawn by you to your Mainnet account.
  • Cards on DAppChain - where you can see the cards that have been transfered to the DAppChain from your Mainnet account.

Loom Network

https://loomx.io

License

BSD 3-Clause License

transfer-gateway-example's People

Contributors

andreipope avatar eduardonunesp avatar enlight avatar mattkanwisher avatar spasibokojima avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

transfer-gateway-example's Issues

truffle-ethereum/contracts/ValidatorManagerControl.sol typo!!

in this contract code,

function checkValidator(address _address) public view returns (bool) {
// owner is a permanent validator
if (_address == owner) { // owner should be owner()
return true;
}
return validators[_address];
}

owner should be onwer() function.

about metamask "Signing this message can have dangerous side effects"

hello,i run transfer-gateway-example,when mapping eth account to loom account,metamask will prompt

"Signing this message can have dangerous side effects. Only sign messages from sites you fully trust with your entire account. This dangerous method will be removed in a future version. Learn more"

i think it's fatal for User experience.somebody will not trust when the dangerous' message appear.and i check the loomjs,only the methord "addIdentityMappingAsync" can map the account.

do you have some other solution to reslove this problem?

Keep getting a "Message not signed by a validator"

Hello! When I'm trying to get my eth back from gateway, I get Error: VM Exception while processing transaction: revert Message not signed by a validator. Not sure if I'm missing something, but I followed all the steps of Readme

Can't change ganache-cli with Truffle develop

I was trying a few times for the last week to replace ganache-cli with truffle develop, I changed ports everywhere from 8545 to 9545 (I suppose it's the only thing that changes) and contract address in loom.yaml, and DAppchain can't read it now with error found in oracle.log:
ts=2018-09-02T15:22:45.0487359Z module=loom level=error _msg="failed to obtain latest Ethereum block number" err="json: cannot unmarshal hex number with leading zero digits into Go struct field Header.gasUsed of type hexutil.Uint64"
Can anyone please help me with integrating example with Truffle Develop, or, it would be better, with any of the Testnets?

How to reslove: evm: max code size exceeded

i have two contracts,after i compile as binary,they above 70K+,once i deploy to loom network,it will prompt "deploying contract: evm: max code size exceeded"

i think maybe the gasLimit above 8M, are there any way to set gasLimit on loom, just like ganache?
i hope some one can reponse me.i hope loom teams can regards developers' problem,a success project cant do without the active developers.
i paid 3 days to read documents on loomx.io and ask the question on delegatecall and github, but i get nothing.

Can the validators decide to do whatever they want?

From looking at the code, it seems like the validators can decide to do what they want with the cards they receive. If a validator decides to send the token back to another user that would be possible and the user would no way to complain about it? Or am I missing something here?

If this is just due to the simple nature of the example, is there another example of Plasma cash in action that prevents dishonest validators?

Warning when signing message

When signing the message in this project, I get the warning message below.

Is this a simple problem with the example that could be fixed by asking a user to sign human readable text, or is this a deeper problem with how Loom works? What will the workaround be in the future if it is a deeper problem?

Signing this message can have dangerous side effects. Only sign messages from sites you fully trust with your entire account. This dangerous method will be removed in a future version.

screen shot

Links to here:
https://consensys.zendesk.com/hc/en-us/articles/360004427792

Web client doesn't load

I get this error after running the start command and going to http://localhost:8080/:

Uncaught (in promise) TypeError: Cannot read property 'address' of undefined

The problematic code is:

{"name":"solc","version":"0.4.24+commit.e67f0147.Emscripten.clang"},"networks":{},"schemaVersion":"2.0.1","updatedAt":"2018-08-21T23:04:48.955Z"}.networks[networkId].address

The problem is here:

cards-gateway-example/webclient/src/dc_managers/dc_card_manager.js

DC_CRYPTO_CARDS_JSON.networks is {}.

The contents of cards-gateway-example/truffle-dappchain/build/contracts/CryptoCardsDappChain.json has this however, so not sure what went wrong:

"networks": {
    "474747": {
      "events": {},
      "links": {},
      "address": "0x49f5c0fc8b951060604e145308efffdc31d9bb3f",
      "transactionHash": "0xf4e4316e23537a8dfec788eda5d2091ecfcedd70e368a2ca6cd9a1aed6abd837"
    }
  },

Transfer to DAppchain never succeeds

I'm doing everything right by instruction, ./transfer_gateway setup, ./transfer_gateway start, import 0xbb63b692f9d8f21f0b978b596dc2b8611899f053d68aec6c1c20d1df4f5b6ee2, sign in the message with this account, send transaction with token transfer, everything goes without problems but no tokens appear under DAppChain Account tab.
I tried upgrading loom from 371 to 375, tried checking if ganache and loom are working, and they are reachable, tried changing ganache to truffle develop.
Also I had problem as described here, solved by manually migrating or pasting address instead of DC_CRYPTO_CARDS_JSON.networks[networkId].address, or by ./transfer_gateway restart

Import tokens in Metamask

Hi guys. Thanks for sharing this resources.

I execute this project, how do I import these tokens in Metamask? Why are the tokens sent to dappChain?

Thanks

image

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.