/
withdrawal.go
415 lines (364 loc) · 13.2 KB
/
withdrawal.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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
package imx
import (
"context"
"fmt"
"math/big"
"github.com/ethereum/go-ethereum/accounts/abi/bind"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/ethereum/go-ethereum/core/types"
"github.com/immutable/imx-core-sdk-golang/imx/api"
"github.com/immutable/imx-core-sdk-golang/imx/internal/convert"
)
/*
PrepareWithdrawal submits a withdrawal request for ETH, ERC20 and ERC721 tokens to be included in the generation and submission of the next batch.
Upon batch confirmation (on-chain state update), the asset is available to be withdrawn by the initial owner/originator of the asset.
@param ctx context.Context - for cancellation, deadlines, tracing, etc or context.Background().
@param l1Signer Ethereum signer to sign message.
@param l2signer Stark signer to sign the payload hash.
@param request The request struct with all the params.
@return CreateWithdrawalResponse
*/
func (c *Client) PrepareWithdrawal(
ctx context.Context,
l1signer L1Signer,
l2signer L2Signer,
request api.GetSignableWithdrawalRequest,
) (*api.CreateWithdrawalResponse, error) {
ethAddress := l1signer.GetAddress()
request.User = ethAddress
signableResponse, httpResponse, err := c.withdrawalsAPI.GetSignableWithdrawal(ctx).GetSignableWithdrawalRequest(request).Execute()
if err != nil {
return nil, NewIMXError(httpResponse, err)
}
ethSignature, starkSignature, err := createSignatures(&signableResponse.SignableMessage, &signableResponse.PayloadHash, l1signer, l2signer)
if err != nil {
return nil, err
}
withdrawalRequest := api.CreateWithdrawalRequest{
Amount: request.Amount,
AssetId: signableResponse.AssetId,
Nonce: signableResponse.Nonce,
StarkKey: signableResponse.StarkKey,
StarkSignature: starkSignature,
VaultId: signableResponse.VaultId,
}
apiCreateWithdrawalRequest := c.withdrawalsAPI.CreateWithdrawal(ctx).XImxEthAddress(ethAddress).XImxEthSignature(ethSignature)
withdrawalResponse, httpResponse, err := apiCreateWithdrawalRequest.CreateWithdrawalRequest(withdrawalRequest).Execute()
if err != nil {
return nil, NewIMXError(httpResponse, err)
}
return withdrawalResponse, nil
}
type TokenWithdrawal interface {
CompleteWithdrawal(ctx context.Context, c *Client, l1signer L1Signer, starkPublicKey string, overrides *bind.TransactOpts) (*types.Transaction, error)
}
// ERC20Withdrawal implements TokenWithdrawal. Used for withdrawal of ETh Tokens.
type EthWithdrawal struct {
}
// ERC20Withdrawal implements TokenWithdrawal. Used for withdrawal of ERC20 Tokens.
type ERC20Withdrawal struct {
TokenAddress string
}
// ERC721Withdrawal implements TokenWithdrawal. Used for withdrawal of ERC721 Tokens.
type ERC721Withdrawal struct {
TokenID string
TokenAddress string
}
// NewEthWithdrawal instantiates a new EthWithdrawal object with the given tokenAddress.
func NewEthWithdrawal() *EthWithdrawal {
this := EthWithdrawal{}
return &this
}
// NewERC20Withdrawal instantiates a new ERC20Withdrawal object with the given tokenAddress.
func NewERC20Withdrawal(tokenAddress string) *ERC20Withdrawal {
this := ERC20Withdrawal{}
this.TokenAddress = tokenAddress
return &this
}
// NewERC721Withdrawal instantiates a new ERC721Withdrawal object with the given tokenId and tokenAddress.
func NewERC721Withdrawal(tokenID, tokenAddress string) *ERC721Withdrawal {
this := ERC721Withdrawal{}
this.TokenID = tokenID
this.TokenAddress = tokenAddress
return &this
}
/*
CompleteWithdrawal performs the complete withdrawal workflow for ETH
@param ctx context.Context - for cancellation, deadlines, tracing, etc or context.Background().
@param c Client object from interface.go used to make API calls.
@param l1Signer Ethereum signer to sign message.
@param starkKeyHex Stark key string in hex decimal format.
@param overrides Optional transaction params that overrides the default values.
@return Transaction
*/
func (w *EthWithdrawal) CompleteWithdrawal(
ctx context.Context,
c *Client,
l1signer L1Signer,
starkKeyHex string,
overrides *bind.TransactOpts,
) (*types.Transaction, error) {
assetType, err := c.encodeETHAsset(ctx)
if err != nil {
return nil, err
}
return c.completeFungiblesWithdrawal(ctx, l1signer, starkKeyHex, assetType, overrides)
}
/*
CompleteWithdrawal performs the complete withdrawal workflow for ERC20 tokens.
@param ctx context.Context - for cancellation, deadlines, tracing, etc or context.Background().
@param c Client object from interface.go used to make API calls.
@param l1Signer Ethereum signer to sign message.
@param starkKeyHex Stark key string in hex decimal format.
@param overrides Optional transaction params that overrides the default values.
@return Transaction
*/
func (w *ERC20Withdrawal) CompleteWithdrawal(
ctx context.Context,
c *Client,
l1signer L1Signer,
starkKeyHex string,
overrides *bind.TransactOpts,
) (*types.Transaction, error) {
assetType, err := c.encodeERC20Asset(ctx, w.TokenAddress)
if err != nil {
return nil, err
}
return c.completeFungiblesWithdrawal(ctx, l1signer, starkKeyHex, assetType, overrides)
}
func (c *Client) completeFungiblesWithdrawal(
ctx context.Context,
l1signer L1Signer,
starkKeyHex string,
assetType *big.Int,
overrides *bind.TransactOpts,
) (*types.Transaction, error) {
starkKey, ok := new(big.Int).SetString(starkKeyHex, 0)
if !ok {
return nil, fmt.Errorf("error converting starkKeyHex to bigint")
}
isRegistered, _ := c.RegistrationContract.IsRegistered(&bind.CallOpts{Context: ctx}, starkKey)
if isRegistered {
return c.withdrawFungibles(ctx, l1signer, starkKey, assetType, overrides)
}
return c.registerAndWithdrawFungibles(ctx, l1signer, starkKey, assetType, overrides)
}
func (c *Client) withdrawFungibles(
ctx context.Context,
l1signer L1Signer,
starkKey, assetType *big.Int,
overrides *bind.TransactOpts,
) (*types.Transaction, error) {
opts := c.buildTransactOpts(ctx, l1signer, overrides)
tnx, err := c.CoreContract.Withdraw(opts, starkKey, assetType)
if err != nil {
return nil, err
}
return tnx, nil
}
func (c *Client) registerAndWithdrawFungibles(
ctx context.Context,
l1signer L1Signer,
starkKey *big.Int,
assetType *big.Int,
overrides *bind.TransactOpts,
) (*types.Transaction, error) {
etherKey := l1signer.GetAddress()
signableRegistration, err := c.getSignableRegistrationOnchain(ctx, etherKey, hexutil.EncodeBig(starkKey))
if err != nil {
return nil, err
}
opts := c.buildTransactOpts(ctx, l1signer, overrides)
operatorSignature, err := convert.HexToByteArray(signableRegistration.OperatorSignature)
if err != nil {
return nil, err
}
transaction, err := c.RegistrationContract.RegisterAndWithdraw(opts, common.HexToAddress(etherKey), starkKey, operatorSignature, assetType)
if err != nil {
return nil, err
}
return transaction, nil
}
func (w *ERC721Withdrawal) withdrawMintedNft(
ctx context.Context,
c *Client,
l1signer L1Signer,
starkKeyHex string,
overrides *bind.TransactOpts,
) (*types.Transaction, error) {
assetType, err := c.encodeERC721Asset(ctx, w.TokenID, w.TokenAddress)
if err != nil {
return nil, err
}
starkKey, ok := new(big.Int).SetString(starkKeyHex, 0)
if !ok {
return nil, fmt.Errorf("error converting starkKeyHex to bigint")
}
isRegistered, err := c.RegistrationContract.IsRegistered(&bind.CallOpts{Context: ctx}, starkKey)
if err != nil {
return nil, fmt.Errorf("error when calling 'ethClient.RegistrationContract.IsRegistered': %v", err)
}
tokenID, ok := new(big.Int).SetString(w.TokenID, 10)
if !ok {
return nil, fmt.Errorf("error converting tokenID to bigint: %v", w.TokenID)
}
if isRegistered {
return c.withdrawMintedNft(ctx, l1signer, starkKey, assetType, tokenID, overrides)
}
return c.registerAndWithdrawMintedNft(ctx, l1signer, starkKey, assetType, tokenID, overrides)
}
func (c *Client) withdrawMintedNft(
ctx context.Context,
l1signer L1Signer,
starkKey, assetType, tokenID *big.Int,
overrides *bind.TransactOpts,
) (*types.Transaction, error) {
opts := c.buildTransactOpts(ctx, l1signer, overrides)
tnx, err := c.CoreContract.WithdrawNft(opts, starkKey, assetType, tokenID)
if err != nil {
return nil, err
}
return tnx, nil
}
func (c *Client) registerAndWithdrawMintedNft(
ctx context.Context,
l1signer L1Signer,
starkKey *big.Int,
assetType *big.Int,
tokenID *big.Int,
overrides *bind.TransactOpts,
) (*types.Transaction, error) {
etherKey := l1signer.GetAddress()
signableRegistration, err := c.getSignableRegistrationOnchain(ctx, etherKey, hexutil.EncodeBig(starkKey))
if err != nil {
return nil, err
}
opts := c.buildTransactOpts(ctx, l1signer, overrides)
operatorSignature, err := convert.HexToByteArray(signableRegistration.OperatorSignature)
if err != nil {
return nil, err
}
tnx, err := c.RegistrationContract.RegisterAndWithdrawNft(opts, common.HexToAddress(etherKey), starkKey, operatorSignature, assetType, tokenID)
if err != nil {
return nil, err
}
return tnx, nil
}
/*
CompleteWithdrawal performs the completion step of the withdrawal process for ERC721 token.
@param ctx context.Context - for cancellation, deadlines, tracing, etc or context.Background().
@param c Client object from interface.go used to make API calls.
@param l1Signer Ethereum signer to sign message.
@param starkKeyHex Stark key string in hex decimal format.
@param overrides Optional transaction params that overrides the default values.
@return Transaction
*/
func (w *ERC721Withdrawal) CompleteWithdrawal(
ctx context.Context,
c *Client,
l1signer L1Signer,
starkKeyHex string,
overrides *bind.TransactOpts,
) (*types.Transaction, error) {
mintableTokenResponse, httpResponse, err := c.mintsAPI.GetMintableTokenDetailsByClientTokenId(ctx, w.TokenAddress, w.TokenID).Execute()
if err != nil {
if httpResponse.StatusCode == 404 {
// Token is already minted on L1
return w.withdrawMintedNft(ctx, c, l1signer, starkKeyHex, overrides)
}
return nil, NewIMXError(httpResponse, err)
}
blueprint := mintableTokenResponse.Blueprint
mintingBlob := getMintingBlob(w.TokenID, blueprint)
assetType, err := c.encodeMintableERC721Asset(ctx, w.TokenID, w.TokenAddress, blueprint)
if err != nil {
return nil, err
}
starkKey, ok := new(big.Int).SetString(starkKeyHex, 0)
if !ok {
return nil, fmt.Errorf("error converting starkKeyHex to bigint")
}
isRegistered, _ := c.RegistrationContract.IsRegistered(&bind.CallOpts{Context: ctx}, starkKey)
// Note: if we reach here, it means we are registered off-chain.
// Above call will return an error user is not registered but this is for on-chain
// we should swallow this error to allow the register and withdraw flow to execute.
if isRegistered {
return c.withdrawAndMintNft(ctx, l1signer, starkKey, assetType, mintingBlob, overrides)
}
return c.registerAndWithdrawAndMintNft(ctx, l1signer, starkKey, assetType, mintingBlob, overrides)
}
func (c *Client) withdrawAndMintNft(
ctx context.Context,
l1signer L1Signer,
starkKey, assetType *big.Int,
mintingBlob []byte,
overrides *bind.TransactOpts,
) (*types.Transaction, error) {
opts := c.buildTransactOpts(ctx, l1signer, overrides)
tnx, err := c.CoreContract.WithdrawAndMint(opts, starkKey, assetType, mintingBlob)
if err != nil {
return nil, err
}
return tnx, nil
}
func (c *Client) registerAndWithdrawAndMintNft(
ctx context.Context,
l1signer L1Signer,
starkKey *big.Int,
assetType *big.Int,
mintingBlob []byte,
overrides *bind.TransactOpts,
) (*types.Transaction, error) {
etherKey := l1signer.GetAddress()
signableRegistration, err := c.getSignableRegistrationOnchain(ctx, etherKey, hexutil.EncodeBig(starkKey))
if err != nil {
return nil, err
}
opts := c.buildTransactOpts(ctx, l1signer, overrides)
operatorSignature, err := convert.HexToByteArray(signableRegistration.OperatorSignature)
if err != nil {
return nil, err
}
tnx, err := c.RegistrationContract.RegsiterAndWithdrawAndMint(opts, common.HexToAddress(etherKey), starkKey, operatorSignature, assetType, mintingBlob)
if err != nil {
return nil, err
}
return tnx, nil
}
func getMintingBlob(tokenID, blueprint string) []byte {
return []byte(fmt.Sprintf("{%s}:{%s}", tokenID, blueprint))
}
/*
GetWithdrawal Get details of a withdrawal with the given ID
@param ctx context.Context - for cancellation, deadlines, tracing, etc or context.Background().
@param id Withdrawal ID
@return Withdrawal
*/
func (c *Client) GetWithdrawal(ctx context.Context, id string) (*api.Withdrawal, error) {
response, httpResponse, err := c.withdrawalsAPI.GetWithdrawal(ctx, id).Execute()
if err != nil {
return nil, NewIMXError(httpResponse, err)
}
return response, nil
}
/*
NewListWithdrawalsRequest Creates a new ApiListWithdrawalsRequest object with required params.
@param ctx context.Context - for cancellation, deadlines, tracing, etc or context.Background().
@return ApiListWithdrawalsRequest
*/
func (c *Client) NewListWithdrawalsRequest(ctx context.Context) api.ApiListWithdrawalsRequest {
return c.withdrawalsAPI.ListWithdrawals(ctx)
}
/*
ListWithdrawals Gets a list of withdrawals
@param req the api request struct with all params populated to make the request
@return ListWithdrawalsResponse
*/
func (c *Client) ListWithdrawals(req *api.ApiListWithdrawalsRequest) (*api.ListWithdrawalsResponse, error) {
response, httpResponse, err := c.withdrawalsAPI.ListWithdrawalsExecute(*req)
if err != nil {
return nil, NewIMXError(httpResponse, err)
}
return response, nil
}