---
# System prepended metadata

title: StarkEx

---

# StarkEx

## 数据结构
Account模型
- stark_key 签名交易 
- amount 
- token_id 
- vault_id ： is the account number in StarkEx that you are depositing to — this account must be an account that is associated with the specified stark_key
- type： describes the transaction type submitted, in this case, a DepositRequest.

```
{
        	"vault_id": 1654615998,
        	"token_id": "0x2dd48fd7a024204f7c1bd874da5e709d4713d60c8a70639eb1167b367a9c378",
        	"stark_key": "0x7c65c1e82e2e662f728b4fa42485e3a0a5d2f346baa9455e3e70682c2094cac",
        	"amount": "4029557120079369747",
        	"type": "DepositRequest"
    	}
```
### balancesTree
叶子结点是 Vaults

### orderTree
订单数是一个Patricia tree,比默克尔树更容易更新和搜索。
防止交易重放。

订单树中的一个叶子代表一个订单。每个叶子都有一个键和一个值,键是订单hash，值是value，0或1。订单树是用来跟踪以前铸造的资产的。
支持部分成交功能。

StarkEx智能合约存储了Balances树的根和Orders树的根，
layer2链下状态是一组根。当合约收到一个新的证明，即存在一组有效的交易，执行完成，可以将当前状态改变为新的状态。




#### Perpetual trading


- starkKey 
- collateralAmount -vault的抵押物
- A list of synthetic assetId. 

1. assetId
2. amount 
3. cachedFundingIndex

**其他元素** 

Global funding indices
Current system time
Current prices


限价单：
限价单包含用户交易资产（合成资产和抵押品）的 , 和 , 以及positionId订单assetId的

https://docs.starkware.co/starkex/in-perpetual.html#limit-order 


####  order contract
https://github.com/starkware-libs/starkex-contracts/blob/master/scalable-dex/contracts/src/starkex/components/OrderRegistry.sol

#### state update
https://github.com/starkware-libs/starkex-contracts/blob/master/scalable-dex/contracts/src/starkex/interactions/UpdateState.sol


## 交易流程分析及starkex合约分析

starkex合约：https://github.com/starkware-libs/starkex-contracts

1. deposit 

CurrencyConvertor: https://etherscan.io/address/0x8e8bd01b5a9eb272cc3892a2e40e64a716aa2a40#code 

https://ethtx.info/mainnet/0x6f7baaa37932a0d868a14a1940ab91d01b665528468299054760bd624604044e/


主要调用了 PerpetualTokenAndRamping 合约的 registerUser 和 deposit 方法. 
![](https://i.imgur.com/ia0o6Rp.png)


PerpetualTokenAndRamping
https://etherscan.io/address/0x3fed7bf5bf3e738bc30fbe61b048fdcb82368545#code

```
 pendingDeposits[starkKey][assetId][vaultId] += quantizedAmount;
```
