πŸ‰
Resource AMM & LP
Become a liquidity provider and trader of resources on the StarkNet native AMM
The AMM is running on StarkNet and is Alpha software. The design of it may change from this current iteration as StarkNet evolves and the needs for the protocol evolves.
The Automatic Market in the Realmverse
The primary economic trading mechanism within the Realmverse is the Resources AMM (Automatic Market Maker). This is a decentralised marketplace where players provide resources and $LORDS pairs and are rewarded for doing so. By providing assets to the market a thriving economy emerges where players can trade instantly.
This is an ERC1155 (resources token type) & ERC20 ($LORDS token type) Automatic Market Maker & the first of its kind running on StarkNet. It is based on the Solidity AMM, NiftySwap.
πŸ‘
​
This is the buy snippet from the codebase. See the full code here. Please note that this is Alpha software, and is still in active development. If you wish to contribute either fork it or please reach out to a Core Lord in the discord. We do not suggest using this with live tokens in its current iteration.
1
func buy_tokens_loop {
2
syscall_ptr : felt*,
3
pedersen_ptr : HashBuiltin*,
4
range_check_ptr,
5
}(
6
# Amount of following arg
7
token_ids_len: felt,
8
# ERC1155 token ids
9
token_ids: felt*,
10
# Amount of following arg
11
token_amounts_len: felt,
12
# Exact amount of tokens to buy
13
token_amounts: Uint256*,
14
) -> (
15
sold: Uint256
16
):
17
alloc_locals
18
​
19
# Recursive break
20
if token_ids_len == 0:
21
return (Uint256(0, 0))
22
end
23
​
24
let (caller) = get_caller_address()
25
let (contract) = get_contract_address()
26
​
27
let (token_address_) = token_address.read()
28
let (currency_address_) = currency_address.read()
29
​
30
let (royalty_fee_thousands_) = royalty_fee_thousands.read()
31
let (royalty_fee_address_) = royalty_fee_address.read()
32
​
33
# Read current reserve levels
34
let (currency_reserves_) = currency_reserves.read([token_ids])
35
let (token_reserves) = IERC1155.balanceOf(token_address_, contract, [token_ids])
36
​
37
# Calculate prices
38
let (currency_amount_sans_royal) = get_buy_price([token_amounts], currency_reserves_, Uint256(token_reserves, 0))
39
​
40
# Add royalty fee
41
let (royalty_fee) = get_royalty_for_price(currency_amount_sans_royal)
42
let (currency_amount, _) = uint256_add(currency_amount_sans_royal, royalty_fee) # Overflow will never happen here
43
​
44
# Update reserves
45
let (new_reserves, add_overflow) = uint256_add(currency_reserves_, currency_amount_sans_royal)
46
with_attr error_message("Currency value overflow"):
47
assert add_overflow = 0
48
end
49
currency_reserves.write([token_ids], new_reserves)
50
​
51
# Transfer currency and purchased tokens
52
IERC20.transferFrom(currency_address_, caller, contract, currency_amount)
53
tempvar syscall_ptr :felt* = syscall_ptr
54
IERC1155.safeTransferFrom(token_address_, contract, caller, [token_ids], [token_amounts].low)
55
IERC20.transfer(currency_address_, royalty_fee_address_, royalty_fee) # Royalty
56
​
57
# Emit event
58
tokens_purchased.emit(caller, currency_amount, [token_ids], [token_amounts])
59
​
60
# Recurse
61
let (currency_total) = buy_tokens_loop(
62
token_ids_len - 1,
63
token_ids + 1,
64
token_amounts_len - 1,
65
token_amounts + 2 # Uint
66
)
67
let (currency_sold, add_overflow) = uint256_add(currency_total, currency_amount)
68
with_attr error_message("Total currency overflow"):
69
assert add_overflow = 0
70
end
71
​
72
return (currency_sold)
73
end
Copied!

How does trading work on the AMM?

When making a swap, the exchange will calculate the price according to the x * y = k curve. Fees are collected against the currency in both buy and sell actions. Due to this, k will steadily increase as a measure to collect rewards for the liquidity providers.

Multi-resource swaps

The key difference between the resource AMM and traditional AMMs (Uniswap) is that it allows multi-swapping of tokens. Meaning, that you can achieve the following:
1
100 Wood (assumes 1:1 pair)
2
100 Stone (assumes 1:1 pair)
3
100 Dragonhide (assumes 1:1 pair)
4
​
5
exchange for:
6
​
7
300 Lords - fees
Copied!
If you need a specific amount of resources for an upgrade you could purchase all the resources in one transaction - saving time and fees!

AMM Trading Fees

Two types of trading fees exist on the AMM.
Fee
%
Destination
LP Rewards
1.5%
LP Providers
$LORDS Commission
1.5%
Nexus
LP Rewards
$LORDS Commission
If you are supplying liquidity to a resource pool you will be returned LP tokens for that pool. These tokens represent your share of the Pool. When removing your LP tokens you will have earned interest according to the fees on $LORDS generated within that pool. (This is similar to Sushiswaps LP pools and is not the same as Uniswap V3 pools)
All trades will contain $LORDS as the opposite side of the trade. There is a commission on these $LORDS which will be set originally at 1.5%. This commission will be split into the Nexus and Treasury.

Liquidity in the AMM

Providing Liquidity

When providing liquidity to AMM, you are returned LP tokens for that pool. As trading occurs, the LP position will accrue a portion of the fees, and as with any AMM, the pool may be subject to impermanent loss. We strongly recommend understanding the risks of impermanent loss before becoming an LP merchant.
Liquidity is supplied at the current price point in the x * y = k curve.
​
Staking your LP tokens for further rewards
You can then become a Liquidity Merchant and stake your LP tokens for further rewards - learn how via The Nexus.