-
Notifications
You must be signed in to change notification settings - Fork 17
/
buyback_test.go
108 lines (83 loc) · 3.2 KB
/
buyback_test.go
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
//go:build bdd
package emoney_test
import (
"encoding/json"
"time"
"github.com/tidwall/sjson"
sdk "github.com/cosmos/cosmos-sdk/types"
nt "github.com/e-money/em-ledger/networktest"
tmrand "github.com/tendermint/tendermint/libs/rand"
"github.com/tidwall/gjson"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
)
var _ = Describe("Buyback", func() {
var (
emcli = testnet.NewEmcli()
key1 = testnet.Keystore.Key1
key2 = testnet.Keystore.Key2
)
queryBuybackBalance := func() (balance sdk.Coins) {
bz, err := emcli.QueryBuybackBalance()
Expect(err).ToNot(HaveOccurred())
js := gjson.GetBytes(bz, "balance")
json.Unmarshal([]byte(js.Raw), &balance)
return
}
It("starts a new testnet", func() {
awaitReady, err := testnet.RestartWithModifications(
func(bz []byte) []byte {
// Allow for stablecoin inflation to create a buyback balance
genesisTime := time.Now().Add(-365 * 24 * time.Hour).UTC()
bz = setGenesisTime(bz, genesisTime)
// Disable inflation for NGM token to better detect burn events.
bz = setInflation(bz, "ungm", sdk.ZeroDec())
// Disable ejpy inflation to be able to accurately detect fee distributions to the buyback module
bz = setInflation(bz, "ejpy", sdk.ZeroDec())
// Set buyback module to act on every block
bz, err := sjson.SetBytes(bz, "app_state.buyback.interval", time.Millisecond.String())
if err != nil {
panic(err)
}
return bz
})
Expect(err).ShouldNot(HaveOccurred())
Expect(awaitReady()).To(BeTrue())
})
It("Executes a buyback and checks supply", func() {
var buybackBalance sdk.Coins
var bz []byte
for i := 0; i < 20; i++ { // await
_, _ = nt.IncChain(1)
buybackBalance = queryBuybackBalance()
if len(buybackBalance) > 0 {
break
}
}
// eeur and echf are inflated
Expect(buybackBalance).To(HaveLen(2), "Buyback module does not appear to have a balance %v", string(bz))
supplyBefore, err := emcli.QueryTotalSupply()
Expect(err).ToNot(HaveOccurred())
// Sell some NGM tokens to the buyback module and verify that they are burned.
_, success, err := emcli.MarketAddLimitOrder(key1, "4000ungm", "1000eeur", tmrand.Str(10))
Expect(err).ToNot(HaveOccurred())
Expect(success).To(BeTrue())
_, err = nt.IncChain(1)
Expect(err).ToNot(HaveOccurred())
supplyAfter, err := emcli.QueryTotalSupply()
Expect(err).ToNot(HaveOccurred())
ngmSupplyBefore, _ := sdk.NewIntFromString(gjson.GetBytes(supplyBefore, "supply.#(denom==\"ungm\").amount").Str)
ngmSupplyAfter, _ := sdk.NewIntFromString(gjson.GetBytes(supplyAfter, "supply.#(denom==\"ungm\").amount").Str)
Expect(ngmSupplyBefore.Sub(ngmSupplyAfter).String()).To(Equal(sdk.NewInt(4000).String()))
})
It("pays a fee using ejpy", func() {
// Check that the buyback module doesn't have an ejpy balance prior to the test
balance := queryBuybackBalance()
Expect(balance.AmountOf("ejpy")).To(Equal(sdk.ZeroInt()))
_, err := emcli.CustomCommand("tx", "bank", "send", key1.GetName(), key2.GetAddress(), "5000eeur", "--fees", "1000ejpy")
Expect(err).ToNot(HaveOccurred())
// Verify that ejpy fee was sent to buyback module
balance = queryBuybackBalance()
Expect(balance.AmountOf("ejpy")).To(Equal(sdk.NewInt(1000)))
})
})