DevelopersSKALE Developers

Transferring ETH

You can use the Interchain Messaging Agent for managing ETH between Ethereum and SKALE. The following three steps guide you through a complete transfer from Ethereum to SKALE and back. Unlike ERC20, ERC721, and ERC1155, ETH is natively supported so there is no need for you to set up and map ETH on your SKALE Chain.

Also, note that the following steps apply to transferring Ethereum ETH to SKALE Chains, rather than describing SKALE Chain ETH (skETH), which has no monetary value and is used exclusively on SKALE Chains.

Live ETH IMA Demo

1. Deposit ETH on Ethereum

To send ETH from a user’s wallet to the IMA Deposit Box on Ethereum, you will need to use the deposit function within the DepositBox IMA Contract on Ethereum.

This method is called from Ethereum to lock the funds.

The DepositBox contract is on Rinkeby testnet. To get the ABIs to interact with IMA on Rinkeby, check out the current release page.

Be sure you are using web3.js version 1.3.5. Don’t use web3.js 2.x.x.

Web3 Javascript
const Web3 = require('web3');
const Tx = require('ethereumjs-tx');

let rinkebyABIs = require("[YOUR_SKALE_ABIs_ON_RINKEBY]");
let privateKey = new Buffer('[YOUR_PRIVATE_KEY]', 'hex')
let account = "[YOUR_ACCOUNT_ADDRESS]";
let rinkeby = "[RINKEBY_ENDPOINT]";
let schainName = "[YOUR_SKALE_CHAIN_NAME]";
let chainID = "[ETHEREUM_CHAIN_ID]";

const depositBoxAddress = rinkebyABIs.deposit_box_address;
const depositBoxABI = rinkebyABIs.deposit_box_abi;

const web3 = new Web3(rinkeby);

let contract = new web3.eth.Contract(depositBoxABI, depositBoxAddress);

/*
 * prepare the smart contract function
 * deposit(string schainID, address to)
 */
let deposit = contract.methods
  .deposit(
    schainName,
    account,
    )
  .encodeABI();

//get nonce
web3.eth.getTransactionCount(account).then(nonce => {

  //create raw transaction to send 1 ETH
  const rawTx = {
    chainId: chainId,
    from: account,
    nonce: "0x" + nonce.toString(16),
    data: deposit,
    to: depositBoxAddress,
    gas: 6500000,
    gasPrice: 100000000000,
    value: web3.utils.toHex(web3.utils.toWei("1", "ether"))
  };

  //sign transaction
  const tx = new Tx(rawTx);
  tx.sign(privateKey);

  //serialize transaction
  const serializedTx = tx.serialize();

  //send signed transaction
  web3.eth
    .sendSignedTransaction("0x" + serializedTx.toString("hex"))
    .on("receipt", receipt => {
      //record receipt to console
      console.log(receipt);
    })
    .catch(console.error);
});

2. Exit from SKALE Chain

To send ETH back to Ethereum, you will need to use the exitToMain function within the TokenManager contract on the SKALE Chain.

This method is called from the SKALE Chain to send funds and move the token back to Ethereum.

Note that the SKALE Chain user must have:

  • skETH to conduct the exitToMain transaction on the SKALE Chain TokenManager contract.

  • a sufficient balance of ETH in the Community Pool to initiate the exit to Ethereum *See Funding Exits.

The TokenManager contract is on your SKALE Chain. Check out the current release page for ABIs.

Javascript
const Web3 = require('web3');
const Tx = require('ethereumjs-tx').Transaction;

let schainABIs = require("[YOUR_SKALE_CHAIN_ABIs]");
let privateKey = new Buffer('[YOUR_PRIVATE_KEY]', 'hex')
let account = "[YOUR_ACCOUNT_ADDRESS]";
let schainEndpoint = "[YOUR_SKALE_CHAIN_ENDPOINT]";

const tokenManagerAddress = schainABIs.token_manager_eth_address;
const tokenManagerABI = schainABIs.token_manager_eth_abi;

const web3 = new Web3(new Web3.providers.HttpProvider(schainEndpoint));

let contract = new web3.eth.Contract(
  tokenManagerABI,
  tokenManagerAddress
);

/*
 * prepare the smart contract function
 * exitToMain(address to)
 */
let exitToMain = contract.methods.exitToMain(
    account,
    web3.utils.toWei('1', 'ether')
  )
  .encodeABI();

//get nonce
web3.eth.getTransactionCount(account).then((nonce) => {
  //create raw transaction
  const rawTx = {
    chainId: chainId,
    nonce: "0x" + nonce.toString(16),
    from: account,
    nonce: "0x" + nonce.toString(16),
    data : exitToMain,
    to: tokenManagerAddress,
    gasPrice: 100000000000,
    gas: 8000000
  }

  //sign transaction
  const tx = new Tx(rawTx);
  tx.sign(privateKey);

  //serialize transaction
  const serializedTx = tx.serialize();

  //send signed transaction
  web3.eth.sendSignedTransaction('0x' + serializedTx.toString('hex')).
    on('receipt', receipt => {
      //record receipt to console
      console.log(receipt);
   }).
    catch(console.error);
});

3. getMyEth from DepositBox

To retrieve ETH to its final destination, you will need to use getMyEth function within the DepositBox contract on Ethereum.

The DepositBox contract is on your SKALE Chain. Check out the current release page for ABIs.

Javascript
const Web3 = require("web3");
const Tx = require("ethereumjs-tx").Transaction;

export function getMyEth() {
  let rinkebyABIs = require("./contracts/rinkeby_ABIs.json");
  let privateKey = new Buffer(
    process.env.REACT_APP_INSECURE_PRIVATE_KEY,
    "hex"
  );
  let accountForMainnet = process.env.REACT_APP_INSECURE_ACCOUNT;
  let accountForSchain = process.env.REACT_APP_INSECURE_ACCOUNT;
  let rinkeby = process.env.REACT_APP_INSECURE_RINKEBY;
  let chainId = process.env.REACT_APP_INSECURE_RINKEBY_CHAIN_ID;

  const depositBoxAddress = rinkebyABIs.deposit_box_eth_address;

  const depositBoxABI = rinkebyABIs.deposit_box_eth_abi;

  const web3 = new Web3(rinkeby);

  let DepositBox = new web3.eth.Contract(depositBoxABI, depositBoxAddress);

  let getMyEth = DepositBox.methods.getMyEth().encodeABI();

  //get nonce
  web3.eth.getTransactionCount(accountForSchain).then((nonce) => {
    //create raw transaction
    const rawTxGetMyEth = {
      chainId: chainId,
      from: accountForMainnet,
      nonce: "0x" + nonce.toString(16),
      data: getMyEth,
      to: depositBoxAddress,
      gas: 6500000,
      gasPrice: 100000000000,
    };

    //sign transaction
    const txGetMyEth = new Tx(rawTxGetMyEth, {
      chain: "rinkeby",
      hardfork: "petersburg"
    });
    txGetMyEth.sign(privateKey);

    //serialize transaction
    const serializedTxGetMyEth = txGetMyEth.serialize();

    //send signed transaction
    web3.eth
      .sendSignedTransaction("0x" + serializedTxGetMyEth.toString("hex"))
      .on("receipt", (receipt) => {
        //record receipt to console
        console.log(receipt);
      })
      .catch(console.error);
  });
}
Developers
  • Support & Learning
  • Languages &
Tooling
  • More
GET DEV UPDATES IN SKALE NEWSLETTER
JOIN THE CONVERSATION
© 2021 SKALE Labs. All right reserved.
skale.networkCookiesPrivacy Policy