-
Notifications
You must be signed in to change notification settings - Fork 22
/
amm.sol
39 lines (32 loc) · 1.3 KB
/
amm.sol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
// SPDX-License-Identifier: MIT
pragma solidity ^0.8;
contract Coins {
mapping (address => uint) CoinA;
mapping (address => uint) CoinB;
constructor() { // This contract behaves as the automated market maker
CoinA[address(this)]=100000000; // Get some CoinA
CoinB[address(this)]=100000000; // Get some CoinB
}
function ActBalance(address act) public view returns (uint ABal,uint BBal){
return ( CoinA[act],CoinB[act] );
}
function AMMBalance() public view returns (uint ABal,uint BBal,uint AMMBal){
address act=address(this);
return ( CoinA[act],CoinB[act], CoinA[act]*CoinB[act] );
}
function MyBalance() public view returns (uint ABal,uint BBal){
return ActBalance(msg.sender);
}
function GiveMeA(uint amountA) public {
CoinA[msg.sender] += amountA;
}
function ExchangeAforB(uint amountA) public returns (uint BAmount) {
uint mult=CoinA[address(this)] * CoinB[address(this)];
CoinA[address(this)] += amountA;
CoinA[msg.sender] -= amountA;
uint amountB = CoinB[address(this)] - (mult / CoinA[address(this)]); // keep #A * #B constant
CoinB[address(this)] -= amountB;
CoinB[msg.sender] += amountB;
return amountB;
}
}