|
| 1 | +package lcsdex |
| 2 | + |
| 3 | +import ( |
| 4 | + "std" |
| 5 | + "strconv" |
| 6 | + |
| 7 | + "gno.land/p/demo/ufmt" |
| 8 | + "gno.land/p/demo/users" |
| 9 | + "gno.land/r/demo/foo20" |
| 10 | + "gno.land/r/demo/wugnot" |
| 11 | +) |
| 12 | + |
| 13 | +// Possible improvements to this code |
| 14 | +// Handle multiple liquidity pools to allow for any grc20 swap |
| 15 | +// Implement rewards for liquidity providers |
| 16 | +// Improve rendering & error messages |
| 17 | +// Add tests |
| 18 | + |
| 19 | +type LastTrade struct { |
| 20 | + addr std.Address |
| 21 | + amtWugnot uint64 |
| 22 | + amtFoo20 uint64 |
| 23 | + typ string // "wff" || "ffw" |
| 24 | +} |
| 25 | + |
| 26 | +var ( |
| 27 | + wugnotReserve uint64 |
| 28 | + foo20Reserve uint64 |
| 29 | + dexAddr = std.CurrentRealm().Addr() // Realm{pkgPath, address} // std.Address |
| 30 | + lt *LastTrade |
| 31 | +) |
| 32 | + |
| 33 | +// constant product amm -> k = x * y |
| 34 | + |
| 35 | +// amm -> LP (reserves of tokens) > who provides reseves |
| 36 | +// Please, use grc20.Approve before calling this function |
| 37 | +func AddLiquidity(fooAmt, wugnotAmt uint64) string { |
| 38 | + // add both wugnot & foo20 reserves |
| 39 | + // provide equal amounts |
| 40 | + // user provides amounts |
| 41 | + // approves beforehand |
| 42 | + if fooAmt == 0 || wugnotAmt == 0 { |
| 43 | + panic("you need to provide both tokens to the liquidity pool") |
| 44 | + } |
| 45 | + |
| 46 | + if fooAmt != wugnotAmt { |
| 47 | + panic("provided amounts need to be the same") |
| 48 | + } |
| 49 | + |
| 50 | + caller := std.PrevRealm().Addr() // std.Address // type Address string |
| 51 | + // todo: implement fee system |
| 52 | + |
| 53 | + foo20.TransferFrom(users.AddressOrName(caller.String()), users.AddressOrName(dexAddr.String()), fooAmt) |
| 54 | + wugnot.TransferFrom(users.AddressOrName(caller.String()), users.AddressOrName(dexAddr.String()), wugnotAmt) |
| 55 | + |
| 56 | + wugnotReserve += wugnotAmt |
| 57 | + foo20Reserve += fooAmt |
| 58 | + |
| 59 | + return ufmt.Sprintf("successfully deposited %d wugnot & %d foo20", wugnotAmt, fooAmt) |
| 60 | +} |
| 61 | + |
| 62 | +func SwapWugnotForFoo20(amtInWugnot, amtOutFoo20 uint64) string { |
| 63 | + if amtInWugnot == 0 || amtOutFoo20 == 0 { |
| 64 | + panic("you need to provide both in and out amounts") |
| 65 | + } |
| 66 | + |
| 67 | + // k = x * y |
| 68 | + k := wugnotReserve * foo20Reserve |
| 69 | + |
| 70 | + newWugnotReserve := wugnotReserve + amtInWugnot |
| 71 | + newFoo20Reserve := k / newWugnotReserve |
| 72 | + |
| 73 | + amtOut := foo20Reserve - newFoo20Reserve |
| 74 | + |
| 75 | + if amtOut < amtOutFoo20 { |
| 76 | + msg := ufmt.Sprintf("not enough liquidity. you can only get %d foo20 for %d wugnot", amtOut, amtInWugnot) |
| 77 | + panic(msg) |
| 78 | + } |
| 79 | + |
| 80 | + caller := std.PrevRealm().Addr() |
| 81 | + wugnot.TransferFrom(users.AddressOrName(caller.String()), users.AddressOrName(dexAddr.String()), amtInWugnot) |
| 82 | + foo20.Transfer(users.AddressOrName(caller.String()), amtOutFoo20) |
| 83 | + |
| 84 | + wugnotReserve = newWugnotReserve |
| 85 | + foo20Reserve = newFoo20Reserve |
| 86 | + |
| 87 | + lt = &LastTrade{ |
| 88 | + addr: caller, |
| 89 | + amtWugnot: amtInWugnot, |
| 90 | + amtFoo20: amtOutFoo20, |
| 91 | + typ: "wff", // wugnot for foo20 |
| 92 | + } |
| 93 | + |
| 94 | + return ufmt.Sprintf("successfully swapped %d wugnot for %d foo20", amtInWugnot, amtOutFoo20) |
| 95 | +} |
| 96 | + |
| 97 | +func SwapFoo20ForWugnot(amtInFoo20, amountOutWugnot uint64) string { |
| 98 | + if amtInFoo20 == 0 || amountOutWugnot == 0 { |
| 99 | + panic("you need to provide both in and out amounts") |
| 100 | + } |
| 101 | + |
| 102 | + // k = x * y |
| 103 | + k := wugnotReserve * foo20Reserve |
| 104 | + |
| 105 | + newFoo20Reserve := foo20Reserve + amtInFoo20 |
| 106 | + newWugnotReserve := k / newFoo20Reserve |
| 107 | + |
| 108 | + amtOut := wugnotReserve - newWugnotReserve |
| 109 | + |
| 110 | + if amtOut < amountOutWugnot { |
| 111 | + msg := ufmt.Sprintf("not enough liquidity. you can only get %d wugnot for %d foo20", amtOut, amtInFoo20) |
| 112 | + panic(msg) |
| 113 | + } |
| 114 | + |
| 115 | + caller := std.PrevRealm().Addr() |
| 116 | + foo20.TransferFrom(users.AddressOrName(caller.String()), users.AddressOrName(dexAddr.String()), amtInFoo20) |
| 117 | + wugnot.Transfer(users.AddressOrName(caller.String()), amountOutWugnot) |
| 118 | + |
| 119 | + wugnotReserve = newWugnotReserve |
| 120 | + foo20Reserve = newFoo20Reserve |
| 121 | + |
| 122 | + lt = &LastTrade{ |
| 123 | + addr: caller, |
| 124 | + amtWugnot: amountOutWugnot, |
| 125 | + amtFoo20: amtInFoo20, |
| 126 | + typ: "ffw", // foo20 for wugnot |
| 127 | + } |
| 128 | + |
| 129 | + return ufmt.Sprintf("successfully swapped %d foo20 for %d wugnot", amtInFoo20, amountOutWugnot) |
| 130 | +} |
| 131 | + |
| 132 | +func Render(_ string) string { |
| 133 | + output := "# GRC20 Simple DEX\n\n" |
| 134 | + |
| 135 | + output += ufmt.Sprintf("DEX Address: %s\n\n", dexAddr.String()) |
| 136 | + output += ufmt.Sprintf("Liquidity: %d wugnot, %d foo20\n\n", wugnotReserve, foo20Reserve) |
| 137 | + output += ufmt.Sprintf("1 wugnot = %s foo20\n\n", manualFloat(wugnotReserve, foo20Reserve)) |
| 138 | + output += ufmt.Sprintf("1 foo20 = %s wugnot\n\n", manualFloat(foo20Reserve, wugnotReserve)) |
| 139 | + |
| 140 | + if lt != nil { |
| 141 | + output += "### Last Swap\n\n" |
| 142 | + |
| 143 | + if lt.typ == "wff" { |
| 144 | + output += ufmt.Sprintf("%s traded %d wugnot for %d foo20", lt.addr.String(), lt.amtWugnot, lt.amtFoo20) |
| 145 | + } else { |
| 146 | + output += ufmt.Sprintf("%s traded %d foo20 for %d wugnot", lt.addr.String(), lt.amtFoo20, lt.amtWugnot) |
| 147 | + } |
| 148 | + } |
| 149 | + |
| 150 | + return output |
| 151 | +} |
| 152 | + |
| 153 | +func manualFloat(a, b uint64) string { |
| 154 | + quotient := a / b |
| 155 | + remainder := a % b |
| 156 | + |
| 157 | + quotientStr := strconv.FormatUint(quotient, 10) |
| 158 | + scaledRemainder := (remainder * 1000000) / b |
| 159 | + remainderStr := strconv.FormatUint(scaledRemainder, 10) |
| 160 | + |
| 161 | + return quotientStr + "." + remainderStr |
| 162 | +} |
0 commit comments