Search Apps Documentation Source Content File Folder Download Copy Actions Download

wrap_unwrap.gno

2.64 Kb ยท 99 lines
 1package v1
 2
 3import (
 4	"chain"
 5	"chain/banker"
 6	"chain/runtime"
 7
 8	"gno.land/r/gnoland/wugnot"
 9
10	"gno.land/p/nt/ufmt"
11)
12
13const (
14	UGNOT_MIN_DEPOSIT_TO_WRAP int64 = 1000
15	WUGNOT_PATH                     = "gno.land/r/gnoland/wugnot"
16	GNOT_DENOM                      = "ugnot"
17)
18
19var (
20	errFailedToWrapZeroUgnot = "cannot wrap 0 ugnot"
21	errFailedToWrapBelowMin  = "amount(%d) < minimum(%d)"
22)
23
24// wrapWithTransfer wraps GNOT into WUGNOT and transfers it to the specified address.
25func wrapWithTransfer(toAddress address, amount int64) error {
26	if amount <= 0 {
27		return nil
28	}
29
30	if amount < UGNOT_MIN_DEPOSIT_TO_WRAP {
31		return makeErrorWithDetails(
32			errWugnotMinimum,
33			ufmt.Sprintf("amount(%d) < minimum(%d)", amount, UGNOT_MIN_DEPOSIT_TO_WRAP),
34		)
35	}
36
37	// transfer ugnot from fromAddress to current realm
38	currentRealmAddr := runtime.CurrentRealm().Address()
39
40	sentCoins := banker.OriginSend()
41	ugnotSent := sentCoins.AmountOf(GNOT_DENOM)
42	if ugnotSent != amount {
43		return makeErrorWithDetails(
44			errInvalidInput,
45			ufmt.Sprintf("user(%s) sent ugnot(%d) amount not equal to rewardAmount(%d)", toAddress.String(), ugnotSent, amount),
46		)
47	}
48
49	// wrap ugnot to wugnot
50	wugnotAddr := chain.PackageAddress(WUGNOT_PATH)
51	banker_ := banker.NewBanker(banker.BankerTypeRealmSend)
52	banker_.SendCoins(currentRealmAddr, wugnotAddr, sentCoins)
53	wugnot.Deposit(cross)
54
55	// if to address is not current realm, transfer wugnot to to address
56	if toAddress != currentRealmAddr {
57		wugnot.Transfer(cross, toAddress, amount)
58	}
59
60	return nil
61}
62
63// unwrapWithTransferFrom transfers WUGNOT from a source address, unwraps it to GNOT, and sends it to the target.
64func unwrapWithTransferFrom(fromAddress, toAddress address, wugnotAmount int64) error {
65	if wugnotAmount == 0 {
66		return nil
67	}
68
69	currentRealmAddr := runtime.CurrentRealm().Address()
70	if fromAddress != currentRealmAddr {
71		wugnot.TransferFrom(cross, fromAddress, currentRealmAddr, wugnotAmount)
72	}
73
74	wugnot.Withdraw(cross, wugnotAmount)
75
76	sendCoins := chain.Coins{{Denom: GNOT_DENOM, Amount: wugnotAmount}}
77	banker_ := banker.NewBanker(banker.BankerTypeRealmSend)
78	banker_.SendCoins(currentRealmAddr, toAddress, sendCoins)
79
80	return nil
81}
82
83// unwrapWithTransfer unwraps WUGNOT to GNOT and sends it to the specified address.
84func unwrapWithTransfer(toAddress address, amount int64) error {
85	if amount <= 0 {
86		return nil
87	}
88
89	// unwrap wugnot to ugnot
90	wugnot.Withdraw(cross, amount)
91
92	// send ugnot to user
93	sendCoins := chain.Coins{{Denom: GNOT_DENOM, Amount: amount}}
94	banker_ := banker.NewBanker(banker.BankerTypeRealmSend)
95	currentRealmAddr := runtime.CurrentRealm().Address()
96	banker_.SendCoins(currentRealmAddr, toAddress, sendCoins)
97
98	return nil
99}