Skip to content

Commit

Permalink
Merge pull request #1 from rosen-bridge/kii/uiInitiation
Browse files Browse the repository at this point in the history
User Interface
  • Loading branch information
kii-dot authored Jun 12, 2024
2 parents 70c0a7e + 8401037 commit febf0eb
Show file tree
Hide file tree
Showing 36 changed files with 12,595 additions and 92 deletions.
21 changes: 21 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*
pnpm-debug.log*
lerna-debug.log*

node_modules
dist
*.local

.vscode/*
!.vscode/extensions.json
.idea
.DS_Store

coverage
yarn.lock
*.sqlite
*.tsbuildinfo
92 changes: 0 additions & 92 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,92 +0,0 @@
# Rosen Sdk



## Getting started

To make it easy for you to get started with GitLab, here's a list of recommended next steps.

Already a pro? Just edit this README.md and make it your own. Want to make it easy? [Use the template at the bottom](#editing-this-readme)!

## Add your files

- [ ] [Create](https://docs.gitlab.com/ee/user/project/repository/web_editor.html#create-a-file) or [upload](https://docs.gitlab.com/ee/user/project/repository/web_editor.html#upload-a-file) files
- [ ] [Add files using the command line](https://docs.gitlab.com/ee/gitlab-basics/add-file.html#add-a-file-using-the-command-line) or push an existing Git repository with the following command:

```
cd existing_repo
git remote add origin https://git.ergopool.io/ergo/rosen-bridge/rosen-sdk.git
git branch -M master
git push -uf origin master
```

## Integrate with your tools

- [ ] [Set up project integrations](https://git.ergopool.io/ergo/rosen-bridge/rosen-sdk/-/settings/integrations)

## Collaborate with your team

- [ ] [Invite team members and collaborators](https://docs.gitlab.com/ee/user/project/members/)
- [ ] [Create a new merge request](https://docs.gitlab.com/ee/user/project/merge_requests/creating_merge_requests.html)
- [ ] [Automatically close issues from merge requests](https://docs.gitlab.com/ee/user/project/issues/managing_issues.html#closing-issues-automatically)
- [ ] [Enable merge request approvals](https://docs.gitlab.com/ee/user/project/merge_requests/approvals/)
- [ ] [Automatically merge when pipeline succeeds](https://docs.gitlab.com/ee/user/project/merge_requests/merge_when_pipeline_succeeds.html)

## Test and Deploy

Use the built-in continuous integration in GitLab.

- [ ] [Get started with GitLab CI/CD](https://docs.gitlab.com/ee/ci/quick_start/index.html)
- [ ] [Analyze your code for known vulnerabilities with Static Application Security Testing(SAST)](https://docs.gitlab.com/ee/user/application_security/sast/)
- [ ] [Deploy to Kubernetes, Amazon EC2, or Amazon ECS using Auto Deploy](https://docs.gitlab.com/ee/topics/autodevops/requirements.html)
- [ ] [Use pull-based deployments for improved Kubernetes management](https://docs.gitlab.com/ee/user/clusters/agent/)
- [ ] [Set up protected environments](https://docs.gitlab.com/ee/ci/environments/protected_environments.html)

***

# Editing this README

When you're ready to make this README your own, just edit this file and use the handy template below (or feel free to structure it however you want - this is just a starting point!). Thank you to [makeareadme.com](https://www.makeareadme.com/) for this template.

## Suggestions for a good README
Every project is different, so consider which of these sections apply to yours. The sections used in the template are suggestions for most open source projects. Also keep in mind that while a README can be too long and detailed, too long is better than too short. If you think your README is too long, consider utilizing another form of documentation rather than cutting out information.

## Name
Choose a self-explaining name for your project.

## Description
Let people know what your project can do specifically. Provide context and add a link to any reference visitors might be unfamiliar with. A list of Features or a Background subsection can also be added here. If there are alternatives to your project, this is a good place to list differentiating factors.

## Badges
On some READMEs, you may see small images that convey metadata, such as whether or not all the tests are passing for the project. You can use Shields to add some to your README. Many services also have instructions for adding a badge.

## Visuals
Depending on what you are making, it can be a good idea to include screenshots or even a video (you'll frequently see GIFs rather than actual videos). Tools like ttygif can help, but check out Asciinema for a more sophisticated method.

## Installation
Within a particular ecosystem, there may be a common way of installing things, such as using Yarn, NuGet, or Homebrew. However, consider the possibility that whoever is reading your README is a novice and would like more guidance. Listing specific steps helps remove ambiguity and gets people to using your project as quickly as possible. If it only runs in a specific context like a particular programming language version or operating system or has dependencies that have to be installed manually, also add a Requirements subsection.

## Usage
Use examples liberally, and show the expected output if you can. It's helpful to have inline the smallest example of usage that you can demonstrate, while providing links to more sophisticated examples if they are too long to reasonably include in the README.

## Support
Tell people where they can go to for help. It can be any combination of an issue tracker, a chat room, an email address, etc.

## Roadmap
If you have ideas for releases in the future, it is a good idea to list them in the README.

## Contributing
State if you are open to contributions and what your requirements are for accepting them.

For people who want to make changes to your project, it's helpful to have some documentation on how to get started. Perhaps there is a script that they should run or some environment variables that they need to set. Make these steps explicit. These instructions could also be useful to your future self.

You can also document commands to lint the code or run tests. These steps help to ensure high code quality and reduce the likelihood that the changes inadvertently break something. Having instructions for running tests is especially helpful if it requires external setup, such as starting a Selenium server for testing in a browser.

## Authors and acknowledgment
Show your appreciation to those who have contributed to the project.

## License
For open source projects, say how it is licensed.

## Project status
If you have run out of energy or time for your project, put a note at the top of the README saying that development has slowed down or stopped completely. Someone may choose to fork your project or volunteer to step in as a maintainer or owner, allowing your project to keep going. You can also make an explicit request for maintainers.
152 changes: 152 additions & 0 deletions docs/chains/cardano-sdk.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
# Rosen SDK: Cardano

This document states the chain-specific functions of Rosen SDK on the Cardano chain.

## Contents

- [Transaction Structure](#transaction-structure)
- [Implmentation Details](#implmentation-details)
- [getBaseNetworkFee](#getbasenetworkfee)
- [generateLockAuxiliaryData](#generatelockauxiliarydata)
- [generateLockTransaction](#generatelocktransaction)

## Transaction Structure

The lock transaction on Cardano should contain two main components:

1. Metadata: The Rosen data for transfer is written on the transaction metadata field. The fields are:

- `to`: _string_, the target blockchain
- `bridgeFee`: _string_, the amount of bridge fee for this transfer
- `networkFee`: _string_, the amount of network fee for this transfer
- `toAddress`: _string_, the destination address in the target blockchain
- `fromAddress`: _array of strings_, the address that is requesting the transfer

The data is formed as a JSON and is written under the `0` key of the metadata JSON. An example ([transaction](https://cardanoscan.io/transaction/1945e7c0b005698b7fa6d0e5578f14193879b9d1962075857befd4a838dcd840?tab=metadata)):

```json
{
"0": {
"to": "ergo",
"bridgeFee": "80325000",
"networkFee": "19213",
"toAddress": "9hBEAVZ9MHLf7mwVrvP3nqptdqYVdYGu1byPH8XFzC7KDuzrb8W",
"fromAddress": [
"addr1q85fh9t2cn7j8g62te545uyayjtlc7cwl9df50dugpe78r5d9z85ctlucfs",
"nmefqrehxuvjw6ws6pzc5rvwyjdl2g8us6pr89f"
]
}
}
```

2. Locked Assets: The amount of transfer should be sent to the lock address in a single UTxO. If the UTxO contains a supported token, the transaction is recognized as a token transfer, otherwise it is recognized as an ADA transfer.

Note that if metadata is malformed or the transferring asset is not supported on the target chain, the lock transaction is not valid and is considered a donation.

## Implmentation Details

Alongside two chain-specific functions of Rosen SDK, another function is also suggested for Cardano. The implementation details of each one are described here.

### `getBaseNetworkFee`

The network fee on Cardano is fixed and 3.4 ADA. Therefore this function returns 3400000 (Lovelace unit).

```ts
/**
* calculates the network fee on Cardano in lovelace unit
* @returns the base network fee
*/
export const getBaseNetworkFee = (): bigint => 3400000n;
```

### `generateLockAuxiliaryData`

As mentioned before, the Rosen data is written in the transaction metadata field. This function generates the metadata in the `AuxiliaryData` format of the `@emurgo/cardano-serialization-lib` and returns the hex representation of it. Although this function is not necessary, it facilitates the `generateLockTransaction` function and makes it more modular.

A version of this function is available at [`@rosen-bridge/ui` GitHub](https://github.com/rosen-bridge/ui/blob/1c0b08f5407e929f5680aa01a316e2dc88ef1408/apps/rosen/app/_networks/cardano/transaction/utils.ts#L81).

```ts
/**
* generates metadata for lock transaction
* @param toChain
* @param toAddress
* @param fromAddress
* @param networkFee
* @param bridgeFee
* @returns hex representation of the auxiliary data
*/
export const generateLockAuxiliaryData: (
toChain: string,
toAddress: string,
fromAddress: string,
networkFee: string,
bridgeFee: string
) => Promise<string>;
```

### `generateLockTransaction`

This function generates an unsigned lock transaction.

The `protocolParams` is required to pass to the transaction builder. The interface is:

```ts
export interface CardanoProtocolParams {
min_fee_a: number;
min_fee_b: number;
pool_deposit: string;
key_deposit: string;
max_value_size: number;
max_tx_size: number;
coins_per_utxo_size: string;
}
```

The function should use only a portion of UTxOs that covers the required assets. It may need to fetch UTxOs page by page. To this purpose, an Iterator object of the UTxOs is passed to the function. UTxOs are in `CardanoUtxo` format, which is:

```ts
export interface CardanoAsset {
policyId: string;
assetName: string;
quantity: bigint;
}

export interface CardanoUtxo {
txId: string;
index: number;
address: string;
value: bigint;
assets: Array<CardanoAsset>;
}
```

The `auxiliaryDataHex` contains Rosen data and is generated by the `generateLockAuxiliaryData` function.

A simple version of this function is available at [`@rosen-bridge/ui` GitHub](https://github.com/rosen-bridge/ui/blob/1c0b08f5407e929f5680aa01a316e2dc88ef1408/apps/rosen/app/_networks/cardano/transaction/generateTx.ts#L31).

```ts
/**
* generates a lock transaction on Cardano
* @param protocolParams The Cardano protocol params
* @param utxoIterator an Iterator object that is used to fetch input utxos
* @param lockAddress
* @param changeAddress
* @param policyIdHex transferring asset policy ID (empty string in case of ADA transfer)
* @param assetNameHex transferring asset name in hex
* @param amount
* @param auxiliaryDataHex
* @returns hex representation of the unsigned tx
*/
export const generateLockTransaction = async (
changeAddress: string,
lockAddress: string,
protocolParams: CardanoProtocolParams,
utxoIterator:
| AsyncIterator<CardanoUtxo, undefined>
| Iterator<CardanoUtxo, undefined>,
policyIdHex: string,
assetNameHex: string,
amountString: string,
auxiliaryDataHex: string,
): Promise<string>
```
126 changes: 126 additions & 0 deletions docs/chains/ergo-sdk.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
# Rosen SDK: Ergo

This document states the chain-specific functions of Rosen SDK on the Ergo chain.

## Contents

- [Transaction Structure](#transaction-structure)
- [Implmentation Details](#implmentation-details)
- [getBaseNetworkFee](#getbasenetworkfee)
- [generateLockAuxiliaryData](#generatelockauxiliarydata)
- [generateLockTransaction](#generatelocktransaction)

## Transaction Structure

The lock transaction on Ergo contains a UTxO to the lock address (aka the lock box) with a valid R4 in the `coll coll byte` format. The values by order are:

- `toChain`: _string_, the target blockchain
- `toAddress`: _string_, the destination address in the target blockchain
- `networkFee`: _string_, the amount of network fee for this transfer
- `bridgeFee`: _string_, the amount of bridge fee for this transfer
- `fromAddress`: _string_, the address that is requesting the transfer

For example, in ([this transaction](https://explorer.ergoplatform.com/en/transactions/193e1dc2d7340d991ade1af51467a1caac097ad8e940edf66105f28421af0817)), the first output box is the lock address. It contains R4 with serialized value:

> 1a050763617264616e6f67616464723171797a653772637332396164326332796d79346d773530673875766e67643038387673786530766b386e76717739337a6d613576386c6671796a67376833363872707177356a6b386e336e636a356832743467306e6e6c6167676e7164307335736c053234313530083530303030303030333965596837455079445468524a46714d5975525436727a7a643468377074445a53335a4a684b4e4732726d4b72656556624733
which renders to:

- `63617264616e6f`
- `616464723171797a653772637332396164326332796d79346d773530673875766e67643038387673786530766b386e76717739337a6d613576386c6671796a67376833363872707177356a6b386e336e636a356832743467306e6e6c6167676e7164307335736c`
- `3234313530`
- `3530303030303030`
- `3965596837455079445468524a46714d5975525436727a7a643468377074445a53335a4a684b4e4732726d4b72656556624733`

which represents:

- `toChain`: cardano
- `toAddress`: addr1qyze7rcs29ad2c2ymy4mw50g8uvngd088vsxe0vk8nvqw93zma5v8lfqyjg7h368rpqw5jk8n3ncj5h2t4g0nnlaggnqd0s5sl
- `networkFee`: 24150
- `bridgeFee`: 50000000
- `fromAddress`: 9eYh7EPyDThRJFqMYuRT6rzzd4h7ptDZS3ZJhKNG2rmKreeVbG3

If the lock box contains a token that is supported in the target chain, the transaction is recognized as a token transfer, otherwise it is recognized as an ERG transfer. Note that if extracting data from R4 fails or the transferring asset is not supported on the target chain, the lock transaction is not valid and is considered a donation.

## Implmentation Details

Alongside two chain-specific functions of Rosen SDK, another function is also suggested for Ergo. The implementation details of each one are described here.

### `getBaseNetworkFee`

The network fee on Ergo is fixed and 0.0013 ERG. Therefore this function returns 1300000 (nano-Erg unit).

```ts
/**
* calculates the network fee on Ergo in nano-Erg unit
* @returns the base network fee
*/
export const getBaseNetworkFee = (): bigint => 1300000n;
```

### `createLockBox`

This function creates the lock box with the format specified in the [Transaction Structure section](#transaction-structure). A version of this function is available at [`@rosen-bridge/ui` GitHub](https://github.com/rosen-bridge/ui/blob/1c0b08f5407e929f5680aa01a316e2dc88ef1408/apps/rosen/app/_networks/ergo/transaction/utils.ts#L32).

```ts
/**
* creates lock box candidate
* @param lockAddress
* @param height
* @param tokenId
* @param amount
* @param toChain
* @param toAddress
* @param fromAddress
* @param bridgeFee
* @param networkFee
*/
export const createLockBox: (
lockAddress: string,
height: number,
tokenId: string,
amount: bigint,
toChain: string,
toAddress: string,
fromAddress: string,
bridgeFee: bigint,
networkFee: bigint
) => ErgoBoxCandidate;
```

### `generateLockTransaction`

This function generates an unsigned lock transaction.

The function should use only a portion of UTxOs that covers the required assets. It may need to fetch UTxOs page by page. To this purpose, an Iterator object of the UTxOs is passed to the function. UTxOs are in `ErgoBoxProxy` format, which is based on EIP-12. It also returns the unsigned transaction in EIP-12 format (which is available via the `to_js_eip12` function of `ergo-lib-wasm` with some minor modifications).

A simple version of this function is available at [`@rosen-bridge/ui` GitHub](https://github.com/rosen-bridge/ui/blob/1c0b08f5407e929f5680aa01a316e2dc88ef1408/apps/rosen/app/_networks/ergo/transaction/generateTx.ts#L32).

```ts
/**
* generates an unsigned lock transaction on Ergo
* @param changeAddress
* @param utxoIterator
* @param lockAddress
* @param toChain
* @param toAddress
* @param tokenId
* @param amount
* @param bridgeFee
* @param networkFee
* @returns
*/
export const generateUnsignedTx = async (
changeAddress: string,
lockAddress: string,
utxoIterator:
| AsyncIterator<ErgoBoxProxy, undefined>
| Iterator<ErgoBoxProxy, undefined>,
toChain: string,
toAddress: string,
tokenId: string,
amount: bigint,
bridgeFee: bigint,
networkFee: bigint,
): Promise<UnsignedErgoTxProxy>
```
Loading

0 comments on commit febf0eb

Please sign in to comment.