Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/token to token fa12 fa2 #39

Merged
merged 65 commits into from
Jun 22, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
65 commits
Select commit Hold shift + click to select a range
fb877f6
divest tested
KStasi Jun 1, 2021
e39120c
update achitectrure
KStasi Jun 1, 2021
c835c9d
clarify error msg
KStasi Jun 1, 2021
a77f439
token to token one direction tested
KStasi Jun 2, 2021
0a5cb47
token to token tested
KStasi Jun 2, 2021
1e4e748
tests added
KStasi Jun 2, 2021
89c5625
remove .only
KStasi Jun 2, 2021
4757e8c
fix tests
KStasi Jun 2, 2021
3efa3b2
readme updated
KStasi Jun 2, 2021
3881463
fix tests
KStasi Jun 2, 2021
833ae19
new contracts
KStasi Jun 4, 2021
a885449
migrations updated
KStasi Jun 4, 2021
017b28a
update migrations
KStasi Jun 4, 2021
ad7d718
fix order requirement for FA2/FA12 pairs
KStasi Jun 4, 2021
49e63ea
update tests
KStasi Jun 4, 2021
65be8e4
this.skip
KStasi Jun 4, 2021
aac7719
Merge pull request #40 from madfish-solutions/test/token-to-token
KStasi Jun 4, 2021
29fe81e
update tests
KStasi Jun 4, 2021
126ca05
update CI
KStasi Jun 4, 2021
41c4685
fix test
KStasi Jun 4, 2021
acbc724
fix order for FA2FA12 pairs
KStasi Jun 4, 2021
2943c6f
fix initialize fa2/fa12 exchange test
KStasi Jun 4, 2021
4619dd6
merge token to token of diff standards into 1 dex
KStasi Jun 7, 2021
ece146a
fixes & updated tests for token to token
KStasi Jun 7, 2021
601051c
minor fixes
KStasi Jun 7, 2021
777dc93
remove only
KStasi Jun 7, 2021
2c5f76a
minor fixes
KStasi Jun 7, 2021
a21a74d
fix invest liquidity
KStasi Jun 7, 2021
bafc703
router method draft
KStasi Jun 7, 2021
4a0338b
routerr implemented
KStasi Jun 8, 2021
3ada5ee
fix wrong index
KStasi Jun 8, 2021
be7b823
fix test
KStasi Jun 8, 2021
b8131e4
router test draft
KStasi Jun 8, 2021
e17e517
fixes
KStasi Jun 8, 2021
e7b9514
router buy tested
KStasi Jun 9, 2021
64c71d8
test updated
KStasi Jun 9, 2021
321df80
fix test
KStasi Jun 9, 2021
182c377
add router tests
KStasi Jun 9, 2021
a220d7b
update test
KStasi Jun 9, 2021
ae5626b
fix loop
KStasi Jun 9, 2021
840472a
fix loop
KStasi Jun 9, 2021
7c442c5
update tests
KStasi Jun 9, 2021
dee4698
uncomment all tests
KStasi Jun 9, 2021
3c55202
fix ttContext
KStasi Jun 9, 2021
c329560
enable all tests
KStasi Jun 9, 2021
04c265e
code style improvements
KStasi Jun 15, 2021
a9b4ecd
fix
KStasi Jun 15, 2021
f612ba1
token to token refactoring
KStasi Jun 16, 2021
afbf19a
update tests
KStasi Jun 16, 2021
768e085
update tests
KStasi Jun 16, 2021
c5fa5c3
fix order
KStasi Jun 16, 2021
2fc2aee
fix test call
KStasi Jun 16, 2021
9757aeb
update test
KStasi Jun 16, 2021
af3f42e
reentracy protection draft
KStasi Jun 16, 2021
83b7d91
add reentrancy protection
KStasi Jun 16, 2021
baf6630
Terser token to token function
konchunas Jun 16, 2021
29d24ed
Merge pull request #42 from madfish-solutions/proposal/token-token-te…
KStasi Jun 17, 2021
85e1260
fix reentrancy protection
KStasi Jun 17, 2021
f1aa72c
token to token refactoring
KStasi Jun 17, 2021
858a889
refactoring
KStasi Jun 17, 2021
72b517b
refactoring
KStasi Jun 17, 2021
a108c21
improve code style
KStasi Jun 17, 2021
1027679
improve comments
KStasi Jun 17, 2021
340cd9a
fix bug
KStasi Jun 17, 2021
a508e5b
update migrations
KStasi Jun 18, 2021
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/master.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
standard: ["FA12", "FA2"]
standard: ["FA12", "FA2", "MIXED"]
steps:
- uses: actions/checkout@v2
- uses: actions/setup-node@v1
Expand Down
Binary file modified Architecture.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
292 changes: 8 additions & 284 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,14 @@ The current implementation supports [FA1.2](https://gitlab.com/tzip/tzip/-/blob/

![Architecture](Architecture.png)

The solution consists of 4 types of contracts:
The solution consists of 6 types of contracts:

1. `Factory` - singleton used to deploy new exchange pair;
1. `Factory` - singleton used to deploy new TokenX-XTZ exchange pair;
2. `Dex` - contract for TokenX-XTZ pair exchanges;
3. `Token` - FA token implementation.
4. `MetadataStorage` - contract to store and upgrade the shares token metadata.
3. `TTDex` - contract for TokenX-TokenY pair exchanges;
4. `Token` - FA token implementation.
5. `BakerRegistry` - bakery registrar.
6. `MetadataStorage` - contract to store and upgrade the shares token metadata.

# Project structure

Expand Down Expand Up @@ -61,14 +63,13 @@ To compile and deploy contracts to Delphinet
1. Chose configure the version - `FA12` or `FA2` - by setting `EXCHANGE_TOKEN_STANDARD` in `.env` and run:

```
yarn run migrate-delphinet
yarn migrate
```

For other networks:

```
yarn run migrate # development
yarn run migrate --network NAME # other networks
yarn migrate --network NAME
```

# Usage
Expand Down Expand Up @@ -102,283 +103,6 @@ yarn migrate

Addresses of deployed contracts are displayed in terminal. At this stage, new MetadataStorage, Factory are originated. Aditionaly, for testnets two new pairs are deployed.

# Entrypoints

The Ligo interfaces of the contracts can be found in `contracts/partials/I__CONTRACT_NAME__.ligo`

## Factory

Factory contains the code template for the `Dex` Token-XTZ pair contracts, deploys them and keeps the list of deployed pairs. The functions for `Dex` are stored in `Factory` contract but because of gas and operation limits their code cannot be stored in Factory contract during the origination: they are added separately one by one.

New exchange pairs are registered and deployed via `LaunchExchange`. The only difference between factory standards is the token identifier type, for F1.2 it is the token address and for FA2 it is the address the token id:

```
#if FA2_STANDARD_ENABLED
type token_identifier is (address * nat)
#else
type token_identifier is address
#endif
```

The contract has the following entrypoints:

```
type launch_exchange_params is record [
token : token_identifier;
token_amount : nat;
]

type set_token_function_params is record [
func : token_func;
index : nat;
]
type set_dex_function_params is record [
func : dex_func;
index : nat;
]

type exchange_action is
| LaunchExchange of launch_exchange_params
| SetDexFunction of set_dex_function_params
| SetTokenFunction of set_token_function_params
```

### SetDexFunction

Sets the dex specific function. Is used before the whole system is launched.

`index` : the key in functions map;

`func` : function code.

Each `index` is designed for a specific `func` which functionality is described below.

### SetTokenFunction

Sets the FA1.2 function. Is used before the whole system is launched.

`index` : the key in functions map;

`func` : function code.

Each `index` is designed for a specific `func` which functionality is described below.

### LaunchExchange

Deploys a new empty `Dex` for `token`, stores the address of the new contract, and puts initial liquidity; has to be called with XTZ amount that will be used as initial liquidity.

`token` : address(address and token id for FA2) of the paired token;

`token_amount` : amount of tokens that will be withdrawn from the user account and used as initial liquidity.

`tez_amount`(not an argument) : the XTZ for initial liquidity should be sent along with the launch transaction.

## Dex

`Dex` fully implements FA1.2/FA2 token interface. For more details check the this [spec](https://gitlab.com/tzip/tzip/-/blob/master/proposals/tzip-7/tzip-7.md) and this [spec](https://gitlab.com/tzip/tzip/-/blob/master/proposals/tzip-12/tzip-12.md). And the extends it with other exchange-related methods.

The contract has the following entrypoints common for both standards:

```

type tez_to_token_payment_params is record [
amount : nat;
receiver : address;
]

type token_to_tez_payment_params is record [
amount : nat;
min_out : nat;
receiver : address;
]

type divest_liquidity_params is record [
min_tez : nat;
min_tokens : nat;
shares : nat;
]

type vote_params is record [
candidate : key_hash;
value : nat;
voter : address;
]

type veto_params is record [
value : nat;
voter : address;
]

type dex_action is
| InitializeExchange of (nat)
| TezToTokenPayment of tez_to_token_payment_params
| TokenToTezPayment of token_to_tez_payment_params
| InvestLiquidity of (nat)
| DivestLiquidity of divest_liquidity_params
| Vote of vote_params
| Veto of veto_params
| WithdrawProfit of (address)

type default_params is unit
type use_params is dex_action
```

For FA1.2 standard compatibility the following entrypoints are implemented:

```
type transfer_params is michelson_pair(address, "from", michelson_pair(address, "to", nat, "value"), "")
type approve_params is michelson_pair(address, "spender", nat, "value")
type balance_params is michelson_pair(address, "owner", contract(nat), "")
type allowance_params is michelson_pair(michelson_pair(address, "owner", address, "spender"), "", contract(nat), "")
type total_supply_params is (unit * contract(nat))

type token_action is
| ITransfer of transfer_params
| IApprove of approve_params
| IGetBalance of balance_params
| IGetAllowance of allowance_params
| IGetTotalSupply of total_supply_params

type full_action is
| Use of use_params
| Default of default_params
| Transfer of transfer_params
| Approve of approve_params
| GetBalance of balance_params
| GetAllowance of allowance_params
| GetTotalSupply of total_supply_params
```

For FA2 standard compatibility the following entrypoints are implemented:

```
type transfer_params is list (transfer_param)
type token_metadata_registry_params is contract (address)
type update_operator_params is list (update_operator_param)

type token_action is
| ITransfer of transfer_params
| IBalance_of of balance_params
| IToken_metadata_registry of token_metadata_registry_params
| IUpdate_operators of update_operator_params

type full_action is
| Use of use_params
| Default of default_params
| Transfer of transfer_params
| Balance_of of balance_params
| Token_metadata_registry of token_metadata_registry_params
| Update_operators of update_operator_params
```

### Default (index 8)

Used to collect rewards from bakers, donations or misguided payments without specified entrypoint.

### Use

Executes the exchange-related which code is stored in map of lamdas and thus the `index` param is needed.

Actions have the following parameters (index in the list matches the index in `lambdas`):

#### InitializeExchange (index 0)

Sets initial liquidity, XTZ must be sent.

`amount` : the token amount for initial liquidity;

`tez_amount`(not an argument) : the XTZ for initial liquidity should be send along with the launch transaction.

#### TezToTokenPayment (index 1)

Exchanges XTZ to tokens and sends them to `receiver`; operation is reverted if the amount of exchanged tokens is less than `amount`.

`amount` : min amount of tokens received to accept exchange;

`receiver` : tokens received;

`tez_amount`(not an argument) : the XTZ to be exchanged.

#### TokenToTezPayment (index 2)

Exchanges `amount` tokens to XTZ and sends them to `receiver`; operation is reverted if the amount of exchanged XTZ is less than `min_out`.

`amount` : amount of tokens to be exchanged;

`min_out` : min amount of XTZ received to accept exchange;

`receiver` : tokens received;

#### WithdrawProfit (index 3)

Withdraws delegation reward of the sender to `receiver` address.

`receiver` : XTZ received;

#### InvestLiquidity (index 4)

Mints `min_shares` by investing tokens and XTZ; the corresponding amount of XTZ has to be sent via transaction and max amount of tokens to be spent should be approved for `Dex`.

`max_tokens` : the max amount of tokens to be invested;

`tez_amount`(not an argument) : the XTZ to be provided as liquidity.

#### DivestLiquidity (index 5)

Burns `shares` and sends tokens and XTZ to the owner; operation is reverted if the amount of divested tokens is smaller than `min_tokens` or the amount of divested XTZ is smaller than `min_tez`.

`shares` : amount of shares to be burnt;

`min_tez` : min amount of XTZ received to accept the divestment;

`min_tokens` : min amount of tokens received to accept the divestment;

#### Vote (index 6)

Votes for `candidate` with shares of `voter`.

`candidate` : the chosen baker;

`value` : amount of shares that are used to vote;

`voter` : the account from which the voting is done.

#### Veto (index 7)

Votes against current delegate with `value` shares of `voter`; the `value` is frozen and can't be transferred or used for another voting.

`value` : amount of shares that are used to vote against the chosen baker;

`voter` : the account from which the veto voting is done.

## Token

Implements [FA1.2](https://gitlab.com/tzip/tzip/-/blob/master/proposals/tzip-7/tzip-7.md) or [FA2](https://gitlab.com/tzip/tzip/-/blob/master/proposals/tzip-12/tzip-12.md) token interface.

## MetadataStorage

Stores the metadata for Dex as it can't be placed inside the `Dex` contract because of operation size limits under the current protocol rules.

The metadata can be updated by authorities to follow the unstable metadata standards.

```
type update_owner_type is record [
owner : address;
add : bool;
]
type metadata_type is map (string, bytes)

type storage is record [
metadata : metadata_type;
owners : set(address);
]

type storage_action is
| Update_owners of update_owner_type
| Update_storage of metadata_type
| Get_metadata of contract (metadata_type)
```

# Testing

If you'd like to run tests on the local environment, you might want to run `ganache-cli` for Tezos using the following command:
Expand Down
Loading