-
Notifications
You must be signed in to change notification settings - Fork 4
/
encode.go
78 lines (69 loc) · 2.58 KB
/
encode.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
package imx
import (
"context"
"fmt"
"math/big"
"github.com/immutable/imx-core-sdk-golang/imx/api"
)
const (
defaultAssetType = "asset"
mintableAssetType = "mintable-asset"
)
// ETHAsset returns encoded AssetType for ETH.
func (c *Client) encodeETHAsset(ctx context.Context) (*big.Int, error) {
encodeAssetRequestToken := api.NewEncodeAssetRequestToken()
encodeAssetRequestToken.SetType(string(ETHTokenType))
return c.encodedAssetType(ctx, encodeAssetRequestToken, defaultAssetType)
}
// ERC20Asset returns encoded AssetType for ERC20 token.
func (c *Client) encodeERC20Asset(ctx context.Context, tokenAddress string) (*big.Int, error) {
encodeAssetRequestToken := &api.EncodeAssetRequestToken{
Data: &api.EncodeAssetTokenData{
TokenAddress: &tokenAddress,
},
Type: s(ERC20TokenType),
}
return c.encodedAssetType(ctx, encodeAssetRequestToken, defaultAssetType)
}
// ERC721Asset returns encoded AssetType for ERC721 token.
func (c *Client) encodeERC721Asset(ctx context.Context, tokenID, tokenAddress string) (*big.Int, error) {
encodeAssetRequestToken := &api.EncodeAssetRequestToken{
Data: &api.EncodeAssetTokenData{
TokenAddress: &tokenAddress,
TokenId: &tokenID,
},
Type: s(ERC721TokenType),
}
return c.encodedAssetType(ctx, encodeAssetRequestToken, defaultAssetType)
}
// MintableERC721Asset returns encoded MintableAssetType for ERC721 token.
func (c *Client) encodeMintableERC721Asset(ctx context.Context, tokenID, tokenAddress, blueprint string) (*big.Int, error) {
encodeAssetRequestToken := &api.EncodeAssetRequestToken{
Data: &api.EncodeAssetTokenData{
Id: &tokenID,
TokenAddress: &tokenAddress,
Blueprint: &blueprint,
},
Type: s(ERC721TokenType),
}
return c.encodedAssetType(ctx, encodeAssetRequestToken, mintableAssetType)
}
// encodedAssetType performs encoding on asset details to get an AssetType (required for stark contract request)
func (c *Client) encodedAssetType(
ctx context.Context,
encodeAssetRequestToken *api.EncodeAssetRequestToken,
assetType string) (*big.Int, error) {
encodeAssetRequest := api.EncodeAssetRequest{
Token: *encodeAssetRequestToken,
}
encodedAssetResponse, httpResponse, err := c.EncodingAPI.EncodeAsset(ctx, assetType).EncodeAssetRequest(encodeAssetRequest).Execute()
defer httpResponse.Body.Close()
if err != nil {
return nil, NewIMXError(httpResponse, err)
}
encodedAssetType, ok := new(big.Int).SetString(encodedAssetResponse.AssetType, 10)
if !ok {
return nil, fmt.Errorf("error converting encoded asset type to bigint: %v", encodedAssetResponse.AssetType)
}
return encodedAssetType, nil
}