Skip to main content

Migrating Smart Contracts from Ethereum to BNB Smart Chain

As Ethereum blockchain gas prices continue to rise,  smart contract owners are seeking alternatives that offer better performance and lower costs. This is a migration case study from Ethereum to BNB Smart chain.

Smartmuv

Motivation

Many growing chains like BNB Smart Chain want to attract, onboard, or switch customers and developers to their growing ecosystem. They need tools and technologies to assist and guide interested users in moving their smart contracts to their systems with ease, reliability, and speed.

As an interesting example case, since Ethereum blockchain gas prices continue to rise,  smart contract owners are seeking alternatives that offer better performance and lower costs. This is a great opportunity for BNB to offer BNB Smart Chain (BSC) as a promising alternative and to become a popular choice and destination for to-be-migrated smart contracts.

When a smart contract is migrated, both its source code and state variables are transferred to the new chain. Bridges can help migrate token assets, but the transfer of other states and data can take several days. Non-token smart contracts, such as decentralized applications (Dapps), often do not utilize bridge or wallet services and require manual migration.

Advantages
SmartMuv offers an automatic migration solution to address the above requirements and needs  that can move smart contracts and their complete state within seconds across EVM-compatible chains. This is done in an automatic, easy, and verifiable way where the entire data is correctly extracted and deployed reliably.

Migration of Smart Contract from Ethereum to BSC Testnet

To demonstrate the feasibility and functionality of SmartMuv, we show the results of successful migrations of three smart contracts from the Ethereum Mainnet to the BNB Smart Chain Testnet. The migration process takes 4-5 minutes for state extraction and deployment to another blockchain, as the complete process is automatic. The time taken can vary as it depends upon the total number of transactions of the smart contract and the size of the extracted state.
The SmartMuv migration process consists of the following steps.
 
State Analysis

In the initial stage, we are provided with the source code, name, and address of the smart contract, which is subject to analysis using our algorithm to identify and extract all relevant data. The screenshots of the analysis are shown below.

State Analysis of a Smart Contract

State Extraction of Smart Contracts

Following the analysis's completion, the smart contract's state is extracted from the Ethereum blockchain up to the latest block (16,639,365). This extracted state is then displayed, with the appropriate association to their corresponding state variables, as depicted in the screenshot below:

State Extraction of Smart Contracts


State Packing

After the state extraction, the next step is to pack and download the complete state of the smart contract by clicking on the ‘Pack’ button.
 The complete extracted states of all three smart contracts are available here.

Deployment to BSC Testnet

We proceeded with the migration process by utilizing our migration adapter to transfer the state to the BSC Testnet. The steps of migration are as follows:
● Smart contract with additional ‘initializeContract’ method is deployed.
● After deployment we pass the migration data to ‘initializeContract’ method that writes the data of every variable on the storage.
 
The addresses of each contract in the Ethereum blockchain and on the BSC Testnet after the migration are provided in the table below.
 

Network

HarmonyOne

CrespoToken

DecalinxCoin

Ethereum Mainnet

0x799a4202c12ca952cB311598a024C80eD371a41e

0x5ecb025e51415dba9fd272c551076357cf4069f5

0xE86914E7dcdcf90B10872fDf06aC1CeA56e8d118

BSC Testnet

0xf0ceb49108FA7CC6d75FE629213F03A0Dd65826c

0x00b02691F15F9871e2E943Eb6bFB318f1AC8F528

0x2bE454Ef7858A6b71beA787c269dF485a81A0eC2

Migration state size and cost

Following the smart contract migration, we calculated the corresponding gas cost and prices in both USD dollars and BNB. These calculations were based on the migrated state size of the three smart contracts, and the results are presented below:

Contract Name

Migrated Data Size (Bytes)

Deployment Gas

Data Migration Gas

Total Gas Used

Total Cost (USD/BNB)

HarmonyOne

6,528

1,613,978

4,225,891

5,839,869

$18.10

0.05839869 BNB

CrespoToken

2,720

1,433,514

1,731,796

3,165,310

$9.81

0.03165310 BNB

DecalinxCoin

2,496

1,474,763

1,599,317

3,074,080

$9.53

0.03074080 BNB

Verification of Migrated States

It is essential to perform verification after the smart contract migration process. This verification involves comparing the state of the smart contract on both Ethereum Mainnet and BSC Testnet. We verified our migration as well as extraction of the smart contract on BSC Testnet in the following way:

Method # 1

To validate the state of the migrated smart contract on BNB Smart Chain Testnet, we used methods like ‘balanceOf’ and ‘allowance’ to check each entry of ‘balance’ and ‘allowed’ mappings, and also checked values of other state variables. This allowed us to confirm that the state of the migrated smart contract on the BNB Smart Chain Testnet is consistent with the state on the Ethereum Mainnet.
 
The details of migrated keys and balance values of all three smart contracts are available here.

Method # 2

To conduct a more thorough verification of the smart contract migration process, we carried out additional tests to ensure the migrated smart contract was functioning correctly on BNB Smart Chain Testnet.
For this, we used the ‘DecalincCoin’ smart contract and modified its recipient address for balance equivalent to ‘total_supply’ in the constructor to ‘0x61B3A112665968d2D286a473E7A783749F7aB69f’. We also changed the state using the ‘transfer’ function and performed the following transfer operations:
● transfer(0x937061586a8ed07Fd82d942fb9Ce16cdBBD1bC50, 450000)
● transfer(0x3Fe71A87336C64fa499ebF821266FD3A4E5e9150, 999999)
● transfer(0x03beb60790fd3F3A114d9Ba864689513718347Cd, 3200000)
 
Prior to these operations, the assigned balance of the address was 1000000000000000000000000000. After the operations, the balance should have been reduced to 999999999999999999995350001, and there should have been three additional balance keys other than the migrated data.
We then extracted the state of the contract and compared it to the expected results, which confirmed the effectiveness of our state extraction technique on the BNB Smart Chain Testnet
The extracted state (without migrated data) on the BSC Testnet after the above changes are given below:

Variable Name

Variable Type

Value

Size (Bytes)

owner

address

0x000000000000000000000000937061586a8ed07fd82d942fb9ce16cdbbd1bc50

20

newOwner

address

0x0000000000000000000000000000000000000000000000000000000000000000

20

symbol

string

ÐCC

32

name

string

DecalinxCoin

32

decimals

uint8

18

1

_totalSupply

uint

1000000000000000000000000000

32

contract_initialized

bool

1

1

balances:key:0x61b3a112665968d2d286a473e7a783749f7ab69f

uint

999999999999999999995350001

32

balances:key:0x937061586a8ed07Fd82d942fb9Ce16cdBBD1bC50

uint

450000

32

balances:key:0x3Fe71A87336C64fa499ebF821266FD3A4E5e9150

uint

999999

32

balances:key:0x03beb60790fd3F3A114d9Ba864689513718347Cd

uint

3200000

32