Skip to content Search Apps Documentation Source Content Run Reset Eval Playground File Folder Download Copy Actions State String Boolean Number Struct Map Slice Pointer Function Closure Reference Nil Package Type Interface Unknown

Community realm. Read the code first. Community realm, deployed by its author. Read the code before you interact or send coins.

claims.gno

3.80 Kb · 134 lines
  1package pool
  2
  3import (
  4	mimir "gno.land/p/g12dcsmwccgdxasqde2eumua88tlrkw83kqf6tcz/mimir"
  5)
  6
  7// owed is what `user` is owed from a resolved market: gross = stake back +
  8// share of the losing side. Full refund when either side is empty or on DRAW /
  9// UNRESOLVABLE. feeBase is the profit copy fees may be charged on: the user's
 10// NET profit (the share less their own stake on the losing side, so a hedger
 11// never pays a copy fee on their own money), counted only on the copied part
 12// of the winning stake. Deposits at or after the settlement cutoff are late:
 13// their net principal comes back and they share in nothing.
 14func owed(m *Market, p *Position) (gross, feeBase int64) {
 15	if p == nil {
 16		return 0, 0
 17	}
 18	raw := p.A + p.B
 19	if m.Outcome >= Draw {
 20		return raw, 0
 21	}
 22	totals := m.history.Before(m.SettlementCutoff)
 23	mine := p.history.Before(m.SettlementCutoff)
 24	if totals.A == 0 || totals.B == 0 {
 25		return raw, 0
 26	}
 27	late := raw - mine.A - mine.B
 28	win, lose, copied, opposite, winners := mine.A, mine.B, mine.CopiedA, totals.B, totals.A
 29	if m.Outcome == SideB {
 30		win, lose, copied, opposite, winners = mine.B, mine.A, mine.CopiedB, totals.A, totals.B
 31	}
 32	if win == 0 {
 33		return late, 0
 34	}
 35	profit := mimir.MulDiv(win, opposite, winners)
 36	gross = late + win + profit
 37	if profit > lose && copied != 0 {
 38		feeBase = mimir.MulDiv(profit-lose, copied, win)
 39	}
 40	return gross, feeBase
 41}
 42
 43func winnerPayout(m *Market, p *Position) bool {
 44	if m.Outcome >= Draw || p == nil {
 45		return false
 46	}
 47	totals := m.history.Before(m.SettlementCutoff)
 48	if totals.A == 0 || totals.B == 0 {
 49		return false
 50	}
 51	mine := p.history.Before(m.SettlementCutoff)
 52	if m.Outcome == SideA {
 53		return mine.A != 0
 54	}
 55	return mine.B != 0
 56}
 57
 58// copyFees are the copy-trade fees on the copied net profit: (to the
 59// referrer, to the platform). Nobody pays themselves: a referrer or fee
 60// recipient who is the user waives that leg.
 61func copyFees(user address, p *Position, feeBase int64) (toRef, toPlatform int64) {
 62	if feeBase == 0 || p.Referrer == "" || p.Referrer == user {
 63		return 0, 0
 64	}
 65	toRef = mimir.MulDiv(feeBase, ReferrerFeeBps, 10_000)
 66	if roles.FeeRecipient != user {
 67		toPlatform = mimir.MulDiv(feeBase, CopyFeeBps, 10_000)
 68	}
 69	return toRef, toPlatform
 70}
 71
 72// Claim collects the caller's own payout.
 73func Claim(cur realm, id int64) {
 74	claimFor(cur, id, caller(cur))
 75}
 76
 77// ClaimFor lets anyone push a user's payout to them. The money only ever goes to `user`.
 78func ClaimFor(cur realm, id int64, user address) {
 79	claimFor(cur, id, user)
 80}
 81
 82func claimFor(cur realm, id int64, user address) {
 83	m := getMarket(id)
 84	if m.State != StResolved {
 85		panic("NotResolved")
 86	}
 87	p := m.position(user)
 88	// Paused: refunds still go out; a winner waits for the unpause.
 89	if roles.Paused && winnerPayout(m, p) {
 90		panic("IsPaused")
 91	}
 92	if p != nil && p.Claimed {
 93		panic("AlreadyClaimed")
 94	}
 95	gross, feeBase := owed(m, p)
 96	if gross == 0 {
 97		panic("NothingToClaim")
 98	}
 99	p.Claimed = true
100	toRef, toPlatform := copyFees(user, p, feeBase)
101	if toRef != 0 {
102		accrue(id, p.Referrer, toRef)
103	}
104	if toPlatform != 0 {
105		accrue(id, roles.FeeRecipient, toPlatform)
106	}
107	fee := toRef + toPlatform
108	emit("Claimed", "id", itoa(id), "user", user.String(), "paid", itoa(gross-fee), "fee", itoa(fee))
109	push(cur, user, gross-fee)
110}
111
112// ClaimFees pays the caller's accrued fees (entry fees, copy fees, forfeited bonds).
113func ClaimFees(cur realm) {
114	claimFeesTo(cur, caller(cur))
115}
116
117func ClaimFeesTo(cur realm, to address) {
118	if !to.IsValid() {
119		panic("ZeroAddress")
120	}
121	claimFeesTo(cur, to)
122}
123
124func claimFeesTo(cur realm, to address) {
125	c := caller(cur)
126	amount := feesOf(c)
127	if amount == 0 {
128		panic("NothingToWithdraw")
129	}
130	accruedFees.Set(c.String(), int64(0))
131	lifetimeFeesClaimed = mimir.Add(lifetimeFeesClaimed, amount)
132	emit("FeeClaimed", "recipient", c.String(), "to", to.String(), "amount", itoa(amount))
133	push(cur, to, amount)
134}