-
Notifications
You must be signed in to change notification settings - Fork 4
/
mint.go
153 lines (133 loc) · 5.12 KB
/
mint.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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
package imx
import (
"context"
"encoding/json"
"fmt"
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/ethereum/go-ethereum/crypto"
"github.com/immutable/imx-core-sdk-golang/imx/api"
)
// Note: These structs need to be in the order defined as below and should match the
// implementation in imx-engine to pass the validation step while minting tokens.
type MintFee struct {
Recipient string `json:"recipient"`
Percentage float32 `json:"percentage" validate:"max=100,gt=0"`
}
type MintableTokenData struct {
ID string `json:"id"`
Blueprint string `json:"blueprint" validate:"max=15000"`
TokenAddress string `json:"token_address,omitempty"`
Royalties []MintFee `json:"royalties,omitempty" validate:"max=50"` // token-level overridable fees (optional)
}
type User struct {
User string `json:"ether_key" validate:"required,eth_addr"`
Tokens []MintableTokenData `json:"tokens" validate:"required,dive,min=1"`
}
type UnsignedMintRequest struct {
ContractAddress string `json:"contract_address" validate:"required,eth_addr"`
Royalties []MintFee `json:"royalties,omitempty" validate:"max=50,dive"` // contract-level (optional)
Users []User `json:"users" validate:"required,dive,min=1"`
AuthSignature string `json:"auth_signature" validate:"required"`
}
/*
Mint assists in minting tokens to the given imx user.
@param ctx context.Context - for cancellation, deadlines, tracing, etc or context.Background().
@param l1Signer Ethereum signer to sign message.
@param unsignedMintRequest An array to UnsignedMintRequests to mint.
@return MintTokensResponse
*/
func (c *Client) Mint(
ctx context.Context,
l1signer L1Signer,
unsignedMintRequest []UnsignedMintRequest,
) (*api.MintTokensResponse, error) {
mintRequest := make([]api.MintRequest, len(unsignedMintRequest))
for requestIndex, eachMintRequest := range unsignedMintRequest {
mintRequestInBytes, err := json.Marshal(eachMintRequest)
if err != nil {
return nil, fmt.Errorf("error in json marshaling eachMintRequest: %v", err)
}
requestHash := crypto.Keccak256Hash(mintRequestInBytes)
authSignatureInBytes, err := l1signer.SignMessage(requestHash.String())
if err != nil {
return nil, fmt.Errorf("error in signing eachMintRequest message: %v", err)
}
mintFees := make([]api.MintFee, len(eachMintRequest.Royalties))
for index, eachMintFee := range eachMintRequest.Royalties {
mintFees[index] = api.MintFee{
Percentage: eachMintFee.Percentage,
Recipient: eachMintFee.Recipient,
}
}
mintToUsers := make([]api.MintUser, len(eachMintRequest.Users))
for userIndex, eachMintUser := range eachMintRequest.Users {
mintTokens := make([]api.MintTokenDataV2, len(eachMintUser.Tokens))
for tokenIndex, eachMintToken := range eachMintUser.Tokens {
mintFeesPerToken := make([]api.MintFee, len(eachMintToken.Royalties))
for royaltyIndex, eachMintFeePerToken := range eachMintToken.Royalties {
mintFeesPerToken[royaltyIndex] = api.MintFee{
Percentage: eachMintFeePerToken.Percentage,
Recipient: eachMintFeePerToken.Recipient,
}
}
mintTokens[tokenIndex] = api.MintTokenDataV2{
Blueprint: eachMintToken.Blueprint,
Id: eachMintToken.ID,
Royalties: mintFeesPerToken,
}
}
mintToUsers[userIndex] = api.MintUser{
Tokens: mintTokens,
User: eachMintUser.User,
}
}
authSignatureEncodedInHex := hexutil.Encode(authSignatureInBytes)
mintRequest[requestIndex] = api.MintRequest{
AuthSignature: authSignatureEncodedInHex,
ContractAddress: eachMintRequest.ContractAddress,
Royalties: mintFees,
Users: mintToUsers,
}
}
mintTokensResponse, httpResponse, err := c.MintsAPI.MintTokens(ctx).MintTokensRequestV2(mintRequest).Execute()
defer httpResponse.Body.Close()
if err != nil {
return nil, NewIMXError(httpResponse, err)
}
return mintTokensResponse, nil
}
/*
GetMint Get details of a mint with the given ID
@param ctx context.Context - for cancellation, deadlines, tracing, etc or context.Background().
@param id Mint ID. This is the transaction_id returned from listMints
@return ApiGetMintRequest
*/
func (c *Client) GetMint(ctx context.Context, id string) ([]api.Mint, error) {
response, httpResponse, err := c.MintsAPI.GetMint(ctx, id).Execute()
defer httpResponse.Body.Close()
if err != nil {
return nil, NewIMXError(httpResponse, err)
}
return response, nil
}
/*
NewListMintsRequest Creates a new ApiListMintsRequest object with required params.
@param ctx context.Context - for cancellation, deadlines, tracing, etc or context.Background().
@return ApiListMintsRequest
*/
func (c *Client) NewListMintsRequest(ctx context.Context) api.ApiListMintsRequest {
return c.MintsAPI.ListMints(ctx)
}
/*
ListMints Gets a list of mints
@param req the api request struct with all params populated to make the request
@return ListMintsResponse
*/
func (c *Client) ListMints(req *api.ApiListMintsRequest) (*api.ListMintsResponse, error) {
response, httpResponse, err := c.MintsAPI.ListMintsExecute(*req)
defer httpResponse.Body.Close()
if err != nil {
return nil, NewIMXError(httpResponse, err)
}
return response, nil
}