This repository has been archived by the owner on Apr 23, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 18
/
asset_converter.go
184 lines (155 loc) · 5.12 KB
/
asset_converter.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
package backend
import (
"fmt"
"math/big"
"net/http"
"strings"
"github.com/stellar/go/amount"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/math"
"github.com/stellar/go/support/render/problem"
"github.com/stellar/go/xdr"
)
var (
WithdrawalAssetInvalid = problem.P{
Type: "withdrawal_asset_invalid",
Title: "Withdrawal Asset Invalid",
Status: http.StatusBadRequest,
Detail: "Withdrawing the requested asset is not supported by the bridge." +
"Refund the deposit once the withdrawal period has expired.",
}
WithdrawalAmountInvalid = problem.P{
Type: "withdrawal_amount_invalid",
Title: "Withdrawal Amount Invalid",
Status: http.StatusBadRequest,
Detail: "Withdrawing the requested amount is not supported by the bridge." +
"Refund the deposit once the withdrawal period has expired.",
}
)
// AssetMappingConfigEntry is the toml representation of
// a mapping between a Stellar asset and an Ethereum token
type AssetMappingConfigEntry struct {
StellarAsset string `toml:"stellar_asset" valid:"-"`
EthereumToken string `toml:"ethereum_token" valid:"-"`
StellarToEthereum string `toml:"stellar_to_ethereum" valid:"-"`
}
type stellarRate struct {
asset string
rate *big.Rat
}
type ethereumRate struct {
token common.Address
rate *big.Rat
}
// AssetConverter maps assets from Stellar to their
// equivalent tokens on Ethereum and vice versa.
type AssetConverter struct {
ethereumToStellar map[common.Address]stellarRate
stellarToEthereum map[string]ethereumRate
}
func isAsset(assetString string) bool {
var asset xdr.Asset
if strings.ToLower(assetString) == "native" {
return true
} else {
parts := strings.Split(assetString, ":")
if len(parts) != 2 {
return false
}
code := parts[0]
if !xdr.ValidAssetCode.MatchString(code) {
return false
}
issuer, err := xdr.AddressToAccountId(parts[1])
if err != nil {
return false
}
if err := asset.SetCredit(code, issuer); err != nil {
return false
}
}
return true
}
// NewAssetConverter constructs a new instance of AssetConverter
func NewAssetConverter(configEntries []AssetMappingConfigEntry) (AssetConverter, error) {
converter := AssetConverter{
ethereumToStellar: map[common.Address]stellarRate{},
stellarToEthereum: map[string]ethereumRate{},
}
if len(configEntries) == 0 {
return converter, fmt.Errorf("config entries are empty")
}
for _, entry := range configEntries {
if !isAsset(entry.StellarAsset) {
return converter, fmt.Errorf("%s is not a valid stellar asset", entry.StellarAsset)
}
if !common.IsHexAddress(entry.EthereumToken) {
return converter, fmt.Errorf("%s is not a valid ethereum address", entry.EthereumToken)
}
multiplier, ok := new(big.Int).SetString(entry.StellarToEthereum, 10)
if !ok {
return converter, fmt.Errorf("%s is not a valid multiplier", entry.StellarToEthereum)
}
token := common.HexToAddress(entry.EthereumToken)
_, exists := converter.stellarToEthereum[entry.StellarAsset]
if exists {
return converter, fmt.Errorf("asset %v is repeated in the asset mapping ", entry.StellarAsset)
}
_, exists = converter.ethereumToStellar[token]
if exists {
return converter, fmt.Errorf("token %v is repeated in the asset mapping ", entry.EthereumToken)
}
converter.stellarToEthereum[entry.StellarAsset] = ethereumRate{
token: token,
rate: new(big.Rat).SetFrac(multiplier, big.NewInt(1)),
}
converter.ethereumToStellar[token] = stellarRate{
asset: entry.StellarAsset,
rate: new(big.Rat).SetFrac(big.NewInt(1), multiplier),
}
}
return converter, nil
}
// ToStellar returns the Stellar asset and amount for the given Ethereum token
func (c AssetConverter) ToStellar(token string, tokenAmount string) (string, int64, error) {
if !common.IsHexAddress(token) {
return "", 0, WithdrawalAssetInvalid
}
parsedAmount := &big.Int{}
_, ok := parsedAmount.SetString(tokenAmount, 10)
if !ok || parsedAmount.Cmp(big.NewInt(0)) <= 0 {
return "", 0, WithdrawalAmountInvalid
}
entry, ok := c.ethereumToStellar[common.HexToAddress(token)]
if !ok {
return "", 0, WithdrawalAssetInvalid
}
product := new(big.Rat).Mul(new(big.Rat).SetFrac(parsedAmount, big.NewInt(1)), entry.rate)
if product.IsInt() {
val := product.Num().Int64()
if product.Num().IsInt64() && val > 0 {
return entry.asset, val, nil
}
}
return entry.asset, 0, WithdrawalAmountInvalid
}
// ToEthereum returns the Ethereum token and amount for the given Stellar asset
func (c AssetConverter) ToEthereum(asset string, assetAmount string) (common.Address, *big.Int, error) {
entry, ok := c.stellarToEthereum[asset]
if !ok {
return common.Address{}, nil, WithdrawalAssetInvalid
}
parsedAmount, err := amount.ParseInt64(assetAmount)
if err != nil {
return common.Address{}, nil, WithdrawalAssetInvalid
}
product := new(big.Rat).Mul(new(big.Rat).SetFrac(big.NewInt(parsedAmount), big.NewInt(1)), entry.rate)
if product.IsInt() {
val := product.Num()
if val.Cmp(big.NewInt(0)) <= 0 || val.Cmp(math.MaxBig256) > 0 {
return entry.token, nil, WithdrawalAmountInvalid
}
return entry.token, val, nil
}
return entry.token, nil, WithdrawalAmountInvalid
}