(LATEST VERSION/ MUST RUN WITH THE JAVASCRIPT SDK VERSION WITH TRUSTLINES https://github.com/quantnetwork/overledger-sdk-javascript/tree/ethereum-smart-contract-after-demo-and-trustline-2)
This is the Minimal Viable Prototype to demonstrate asset migration functionality. Specifically we are looking at one of the more complicated use-cases where a digital asset that cannot be recreated on a ledger is temporarily migrated from one ledger to another and back again. To help allow for this migration to occur we introduce a 3rd party Oracle who is in charge of locking the asset on one ledger and generating it’s digital twin on another ledger (or vis versa). This Oracle is a trusted entity to keep the asset locked on one chain when it is being traded on the other chain.
We do not need an Oracle for:
- permanent asset migration (as we can permanently destroy the asset on the first ledger)
- temporary asset migration for an asset that can be re-created on the first ledger (as we can destroy the asset on the migrating ledger and re-create it when needed)
Future work: If we do not want one Oracle to be in control of this migration process, we can use multiple Oracles (known as decentralised Oracles), the most popular implementation being from Chainlink. Further more, we can introduce inclusion proofs to a single or decentralised oracle design to make it more difficult for an Oracle to fabricate information. Inclusion proofs require Oracles to post block header information of one ledger into another ledger and then provide a Merkle proof that a transaction was linked to in the block header.
Returning to our demo. We have the following setup:
-
Three actors are involved in the asset migration demo (An Oracle, Party A and Party B)
-
ERC20 tokens on Ethereum will be migrated to the XRP ledger and will be migrated back to Ethereum on demand
-
We use an Oracle as we are migrating ERC20 tokens that have a finite-fixed supply, i.e. all possible ERC-20 tokens of this type have already been created.
-
The Oracle is the issuer of ERC20 tokens on the XRP Ledger. This currency is called an issued currency on the XRP ledger.
-
The Oracle is the owner of the asset migration smart contract on the Ethereum ledger. Sending ERC20 tokens to the address of the asset migration smart contract locks the tokens in escrow, where only the Oracle can move them out of this asset migration smart contract.
-
The rules of our Asset Migration Treaty Contract says that the Oracle must have ERC20 tokens in its possession (here from Party A, but can be from multiple customers on the Ethereum DLT) to be able to issue ERC20 tokens on the XRP ledger. Note that as all actors hold their private keys we cannot enforce the actions of any individual and so we cannot enforce that the Oracle abides by the logic of the Treaty Contract. But we can embed functions into the Treaty Contract that can notify Party A or Party B if the Oracle does not conform to the logic embedded within the Treaty Contract.
-
In our demo flow, the ERC20 tokens are sent by Party A to the Oracle, by sending the tokens to the asset migration contract. At the same time, Party A creates a trustline between Party A and the Oracle on the XRP ledger for this ERC20 token.
-
As Party A provides ERC20 tokens to the Oracle, it can claim them back on the XRP ledger through a payment of issued ERC20 tokens, from the Oracle to Party A, over a trustline between the two parties. The Oracle issues this token with an XRP ledger payment transaction.
-
Now the demo proceeds by Party A sending ERC20 tokens (issued on the XRP ledger) to Party B, trading over a Trustline between the two parties.
-
Party B has now received ERC20 tokens on the XRP ledger and wants to migrate them back to the Ethereum ledger.
-
Over a trustline created by the Oracle between itself and the Party B, Party B sends an amount of issued ERC20 tokens to the Oracle on the XRP Ledger.
-
Then the Oracle unlocks the same ERC20 tokens amount on the Ethereum ledger from the smart contract and sends them to Ethereum's Party B address.
-
DLT
s:Ethereum
andXRP
Ledger. -
Asset Migration Smart Contract (AMC)
: Smart Contract in the Ethereum Ledger. It locks the assets to be migrated and only the Oracle can unlock them. It is written in Solidity. -
Asset Migration Treaty Contract (AMTC)
: A Treaty Contract details the rules of interaction between the multiple DLTs, i.e. it contains the multi-distributed ledger logic. It can be written in any language, instantiated and shared between multiple participants and be run in many locations at the same time. It can call directly the DLT node (Ethereum or XRP) or theAMC
. It is written in Javascript/Node.js. -
ERC20 Token
: assets to be locked up in the migration and unlocked while getting them back on the Ethereum Ledger (OMG, LINK, BNB,...). -
Trustline
: object allowing to perform payment transactions between accounts in a non-XRP currency in the XRP Ledger.
-
Party A
: it runs anATMC
. It migrates ERC20 Tokens from Ethereum to XRP. ERC20 Tokens are then locked up in theAMC
and the Oracle pays it back the equivalent amount in the same currency on the XRP Ledger. -
Party B
: it runs anATMC
. It withdraws ERC20 Tokens from XRP to Ethereum. It pays ERC20 Tokens on XRP to the Oracle and gets them back on the Ethereum Ledger from the Oracle. The Oracle uses the ERC20 Tokens locked up on theAMC
. -
Oracle
: it runs anATMC
. A trusted third party that locks the asset to be migrated to a ledger to another and back again.
- Initialization of the Asset Migration Smart Contract (AMC)
-
Party A
runs a local instance of theAsset Migration Treaty Contract (AMTC)
. -
It deploys the
Asset Migration Smart Contract (AMC)
on Ethereum and initialises it using the constructor or finds an already deployedAMC
(using theinitialiseAssetsMigrationContract
function of theAMTC
). -
When the
AMC
is created, theOracle
can be run.
- Waiting for the Oracle to be initialized on the
AMC
- It waits in a loop until the
Oracle
is set in theAMC
for the XRP and Ethereum Ledgers (using thegetOracleOnOtherLedger
function of theAMTC
which is reading thegetOracleAccountOnOtherLedger
function of theAMC
).
- Creation of a Trustline object to the
Oracle
- When the
Oracle
is set on theAMC
and then the Oracle's XRP address can be read from theAMC
,Party A
creates a Trustline object to theOracle
on the XRP Ledger (using thecreateTrustlineTransaction
function of theAMTC
). The limit of the trustline must support the payment's amount X theOracle
will have to sent toParty A
on the XRP Ledger when the tokens fromParty A
will be transferred to theAMC
and locked up there.
- ERC20 Token asset migration request
- It performs a migration request to the
AMC
(migrateERC20AssetsToLock
on theAMTC
which is calling themigrateAssetsRequest
function of theAMC
). At this step, an amount X of an ERC20 token is transferred fromParty A
's Ethereum address to theAMC
address ( using the core ERC20 solidity function interfacetransferFrom
). These tokens are then locked by theAMC
and can be unlocked only by theOracle
. In our scenario, they will be spent when a withdrawal request will be made byParty B
.
- Reading a new withdrawal request
- It waits for a new withdrawal request, here from
Party B
. It is calling thereadData
(in theAMTC
) to read the data from theAMC
. When thetotalWithdrawalRequests
variable is incremented in theAMC
, this means a new withdrawal request has been sent to theAMC
from a party running an instance of theAMTC
in our demo, or more generally, calling the withdrawal request function of theAMC
to initialise a new withdrawal request on theAMC
.
- Payment to the withdrawal requester
Party B
- It performs then a payment to
Party B
(makeIssuedCurrencyPayment
) on the XRP Ledger using the Trustline thatParty B
has created toParty A
. The amount Y of the payment is read from theAMC
as it is the amount set byParty B
when it has performed the withdrawal request to theAMC
. IN DEPLOYMENT WE WOULD HAVE TO THINK OF A DIFFERENT WAY FOR PARTY B AND PARTY A TO TRANSFER ERC-20 ON XRP_LEDGER AS PARTY B MAY NOT WANT TO WITHDRAW ERC-20 INTO ETHEREUM AND MAY WANT TO CONTINUE TRADING IT ON THE XRP_LEDGER
- Update data control on the
AMC
Party A
set the transaction hash of the last payment toParty B
on theAMC
. This transaction hash will be read, along with the transaction hash of the Trustline from theOracle
toParty B
, byParty B
to let it perform the payment to theOracle
on the XRP Ledger.
Oracle
initialization on theAMC
-
The Oracle runs a local instance of the
Asset Migration Treaty Contract AMTC
, whenParty A
has theAMC
address. -
It gets the latest
AMC
. The called function isgetLatestContract
on theAMTC
, given the sha256 hash of the smart contract byte code throughcontractIdentifier
variable set on theAMC
when deploying it. -
It reads the total number of migrations and withdrawals request (
getRequestId
function of theAMTC
to get the values oftotalMigrationRequests
andtotalWithdrawalRequests
AMC
variables). -
It initializes itself as an Oracle on XRP and Ethereum Ledgers (
initialiseOracleOnOtherLedger
on theAMTC
callingaddCreationAccountOnOtherLedger
function of theAMC
), if it has not already (getOracleOnOtherLedger
on theAMTC
callingoracleAccountOnOtherLedger
map on theAMC
).
- Reading a new migration request
-
It waits for a new migration request, here from
Party A
. It is calling thereadData
AMTC
function to read data from theAMC
. When thetotalMigrationRequests
on theAMC
is incremented, this means a new migration request has been sent to theAMC
from a party running an instance of theAMTC
. -
It gets the XRP address of the user requesting for the asset migration and the amount which is due to be sent to it on the XRP ledger (
readMigrationWithdraw
on theAMTC
) after this same user has already sent an amount of ERC20 tokens to theAMC
on the Ethereum Ledger. This user isParty A
in our scenario.
- Payment to the migration requester
- It performs then a payment to
Party A
(makeIssuedCurrencyPayment
on theAMTC
) on the XRP Ledger using the Trustline thatParty A
has created to theOracle
. TheOracle
is playing the role of a non-XRP currency issuer, having the source of issuance from theAMC
where parties requesting for migration put there ERC20 tokens in to be paid on the XRP Ledger in exchange. The payment's amount will be done in the ERC20 token symbol. In our scenario, the amount of this payment can be less or equal than the amount of the ERC20 tokens sent byParty A
to theAMC
.
- Finishing the migration request on the
AMC
- It completes the migration request (using the
completeMigration
function of theAMTC
calling thecompletedMigrationRequest
on theAMC
) by sending data to terminate the migration by updating data on theAMC
for the already processed migration identifier. It increments the total number of the completed migrations and set the current one to a completed state.
- Reading a new withdrawal request
- It waits for a new withdrawal request, here from
Party B
. It is calling thereadData
to read the data from theAMC
. When thetotalWithdrawalRequests
on theAMC
is incremented, this means a new withdrawal request has been sent to theAMC
from a party running an instance of theAMTC
or calling thewithdrawAssetsRequest
function of theAMC
.
- Reading the withdrawal amount
- It reads from the
AMC
the withdrawal amount Y requested and the XRP address of the user which had performed this withdrawal request. In our scenario, this user isParty B
.
- Creation of a Trustline object to
Party B
- The
Oracle
creates a Trustline object toParty B
on the XRP Ledger (createTrustlineTransaction
on theAMTC
). The limit of the trustline must support the payment's amount YParty B
will have to do to theOracle
on the XRP Ledger, before theOracle
performs a transaction on the Ethereum Ledger from theAMC
toParty B
's Ethereum address of an equal amount Y of ERC20 tokens.
- Update data control on the
AMC
- The
Oracle
adds the transaction hash of the trustline toParty B
on theAMC
. This transaction hash will be read, along with the transaction hash of the payment fromParty A
toParty B
, byParty B
to let it perform the payment to theOracle
on the XRP Ledger.
- Waiting for a payment from
Party B
- It reads the payment transaction hash performed by
Party B
to theOracle
on the XRP Ledger. It is calling the endpointreadData
on thegetWithdrawalOtherLedgerTxID
AMC
's function to check if the transaction has been set on theAMC
.
- Complete the withdrawal request
- When the payment from
Party B
is done, it completes the withdrawal request (completeWithdrawal
) by updating datas (the state of the current withdrawal set to completed and incrementes the total number of the completed withdrawal requests) on theAMC
to terminate the processed withdrawal request. At that step, having the signature, the public key and the message, theOracle
verifies that the signature is valid i.e, the hashed message has been signed by the key pair private key of the stored public key. TheOracle
performs then a request to theAMTC
(completeWithdrawal
which is calling the functioncompleteWithdrawalRequest
on theAMC
) that unlocks the ERC20 tokens amount Y from theAMC
toParty B
's address on the Ethereum Ledger. Unlocking the tokens by theAMC
is done by implementing thetransfer
ERC20 solidity method in the complete withdrawal call.
- Waiting for a migration request to be completed
- It waits for a migration request to be finished. It is calling the
readData
to read the data from theAMC
. When thecompletedMigrations
variable on theAMC
is incremented, this means a migration has been completed by theOracle
.
- Creation of a Trustline object to the
Oracle
Party B
creates a Trustline object to theParty A
on the XRP Ledger (createTrustlineTransaction
on theAMTC
). The limit of the trustline must support the payment's amount YParty B
will have to do to theOracle
on the XRP Ledger, before theOracle
performs a transaction on the Ethereum Ledger from theAMC
toParty B
's Ethereum address of an equal amount Y of ERC20 tokens.
- ERC20 Token asset withdrawal request
- It performs a withdrawal request calling the
AMC
(withdrawERC20AssetsToUnlock
on theAMTC
calling thewithdrawAssetsRequest
on theAMC
). In addition to the usual datas necessary to perform the withdrawal (ledger, address, assets amount...) to be send to control the withdrawal flows of transactions, the XRP public key ofParty B
is sent along with a signed hashed message byParty B
's private key. This signature will be verified later on by the Oracle when it transfers the ERC20 tokens from theAMC
toParty B
's Ethereum address. This verification is done to be certain that the user requesting for a withdrawal is the owner of the XRP account (account means the address on the XRP Ledger).
- Reading data control updates on the
AMC
- It waits for the creation of a Trustline object from the
Oracle
and for the payment fromParty A
. It uses thereadData
endpoint ongetWithdrawalOracleTrustlineTxID
andgetWithdrawalOtherLedgerIncomeTxID
AMC
's functions to read respectively from theAMC
the Trustline transaction hash of theOracle
and the payment transaction hash fromParty A
.
- Payment to the
Oracle
- When both transaction data hashes (
Oracle
's trustline andParty A
's payment) are set on theAMC
,Party B
performs then a payment to theOracle
(makeIssuedCurrencyPayment
on theAMTC
) on the XRP Ledger using the Trustline that theOracle
has created toParty B
.Party B
will get in return the same amount of ERC20 tokens from theAMC
. That transaction on the Ethereum Ledger will be performed by theOracle
after receivingParty B
's payment on the XRP Ledger.
- Update data control on the
AMC
Party B
sets the transaction hash of the payment done to theOracle
on theAMC
. This transaction hash will be read by theOracle
to let it perform the ERC20 tokens transfer from theAMC
toParty B
's Ethereum address on the Ethereum Ledger.