/
nft.go
58 lines (49 loc) · 1.92 KB
/
nft.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
package types
import (
"encoding/base64"
"encoding/json"
"fmt"
"strconv"
"strings"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/nft"
)
// VeNftClass represents the NFT class of ve
var VeNftClass = nft.Class{
Id: "veNFT",
Name: "veNFT",
Symbol: "veNFT",
Description: "Warmage locks, can be used to boost gauge yields, vote on token emission, and receive bribes",
}
// VeIDFromUint64 converts ve id from integer to string
func VeIDFromUint64(idNumber uint64) string {
return fmt.Sprintf("ve-%d", idNumber)
}
// Uint64FromVeID converts ve id from string to integer
func Uint64FromVeID(veID string) uint64 {
splits := strings.Split(veID, "-")
if len(splits) != 2 || splits[0] != "ve" {
return EmptyVeID
}
id, err := strconv.ParseUint(splits[1], 10, 64)
if err != nil || id == EmptyVeID {
return EmptyVeID
}
return id
}
func VeNftUri(nftID string, balance sdk.Int, lockedEnd uint64, value sdk.Int) string {
output := fmt.Sprintf(`<svg xmlns="http://www.w3.org/2000/svg" preserveAspectRatio="xMinYMin meet" viewBox="0 0 350 350"><style>.base { fill: white; font-family: serif; font-size: 14px; }</style><rect width="100%%" height="100%%" fill="black" /><text x="10" y="20" class="base">token %s</text><text x="10" y="40" class="base">balanceOf %s</text><text x="10" y="60" class="base">locked_end %d</text><text x="10" y="80" class="base">value %s</text></svg>`, nftID, balance, lockedEnd, value)
var uri struct {
Name string `json:"name"`
Description string `json:"description"`
Image string `json:"image"`
}
uri.Name = fmt.Sprintf("lock #%s", nftID)
uri.Description = VeNftClass.Description
uri.Image = fmt.Sprintf("data:image/svg+xml;base64,%s", base64.URLEncoding.EncodeToString([]byte(output)))
uriStr, err := json.Marshal(&uri)
if err != nil {
panic(err)
}
return fmt.Sprintf("data:application/json;base64,%s", base64.URLEncoding.EncodeToString(uriStr))
}