wrap_unwrap.gno
2.28 Kb ยท 84 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
13// wrapWithTransfer wraps GNOT into WUGNOT and transfers it to the specified address.
14func wrapWithTransfer(toAddress address, amount int64) error {
15 if amount <= 0 {
16 return nil
17 }
18
19 if amount < UGNOT_MIN_DEPOSIT_TO_WRAP {
20 return makeErrorWithDetails(
21 errWugnotMinimum,
22 ufmt.Sprintf("amount(%d) < minimum(%d)", amount, UGNOT_MIN_DEPOSIT_TO_WRAP),
23 )
24 }
25
26 // transfer ugnot from fromAddress to current realm
27 currentRealmAddr := runtime.CurrentRealm().Address()
28
29 sentCoins := banker.OriginSend()
30 ugnotSent := sentCoins.AmountOf(GNOT_DENOM)
31 if ugnotSent != amount {
32 return makeErrorWithDetails(
33 errInvalidInput,
34 ufmt.Sprintf("user(%s) sent ugnot(%d) amount not equal to rewardAmount(%d)", toAddress.String(), ugnotSent, amount),
35 )
36 }
37
38 // wrap ugnot to wugnot
39 wugnotAddr := chain.PackageAddress(WUGNOT_PATH)
40 banker_ := banker.NewBanker(banker.BankerTypeRealmSend)
41 banker_.SendCoins(currentRealmAddr, wugnotAddr, sentCoins)
42 wugnot.Deposit(cross)
43
44 // if to address is not current realm, transfer wugnot to to address
45 if toAddress != currentRealmAddr {
46 wugnot.Transfer(cross, toAddress, amount)
47 }
48
49 return nil
50}
51
52// unwrapWithTransfer unwraps WUGNOT to GNOT and sends it to the specified address.
53func unwrapWithTransfer(toAddress address, wugnotAmount int64) error {
54 if wugnotAmount == 0 {
55 return nil
56 }
57
58 wugnot.Withdraw(cross, wugnotAmount)
59
60 currentRealmAddr := runtime.CurrentRealm().Address()
61 banker_ := banker.NewBanker(banker.BankerTypeRealmSend)
62 banker_.SendCoins(currentRealmAddr, toAddress, chain.Coins{{GNOT_DENOM, int64(wugnotAmount)}})
63
64 return nil
65}
66
67// unwrapWithTransferFrom transfers WUGNOT from a source address, unwraps it to GNOT, and sends it to the target.
68func unwrapWithTransferFrom(fromAddress, toAddress address, wugnotAmount int64) error {
69 if wugnotAmount == 0 {
70 return nil
71 }
72
73 currentRealmAddr := runtime.CurrentRealm().Address()
74 if fromAddress != currentRealmAddr {
75 wugnot.TransferFrom(cross, fromAddress, currentRealmAddr, wugnotAmount)
76 }
77
78 wugnot.Withdraw(cross, wugnotAmount)
79
80 banker_ := banker.NewBanker(banker.BankerTypeRealmSend)
81 banker_.SendCoins(currentRealmAddr, toAddress, chain.Coins{{GNOT_DENOM, int64(wugnotAmount)}})
82
83 return nil
84}