diff --git a/config.yml b/config.yml new file mode 100644 index 0000000..a7b84e3 --- /dev/null +++ b/config.yml @@ -0,0 +1,8 @@ +name: crypto_ex +build: + include: + - . + - !exclude: + - vendor/** +permissions: + - methods: '*' diff --git a/contract.go b/contract.go index b9694bc..0ef2e5f 100644 --- a/contract.go +++ b/contract.go @@ -1,28 +1,99 @@ //go:build !exclude_crypto_ecdsa -package crypto_ex +package main import ( - "encoding/json" - "fmt" - "github.com/go-resty/resty/v2" "github.com/nspcc-dev/neo-go/pkg/interop" "github.com/nspcc-dev/neo-go/pkg/interop/native/gas" + "github.com/nspcc-dev/neo-go/pkg/interop/native/std" "github.com/nspcc-dev/neo-go/pkg/interop/runtime" "github.com/nspcc-dev/neo-go/pkg/interop/storage" - "strconv" ) type Wallet struct { Tokens map[string]int } -type TickerPrice struct { - Symbol string `json:"symbol"` - Price string `json:"price"` +func (w Wallet) getTokenBalance(token string) int { + value := getValueFromStringIntMap(w.Tokens, token) + if value == nil { + return 0 + } + return value.(int) } -func GetWallet(address interop.Hash160) Wallet { +var prices map[string]int + +// All prices are multiplied by 1e10 and rounded to be represented as fixed point int +func init() { + prices = map[string]int{ + "XMR": 1521000000000, + "XRP": 6035000000, + "ADA": 5793000000, + "AVAX": 396900000000, + "BNB": 3083000000000, + "BTC": 466489200000000, + "ETH": 26230000000000, + "SOL": 1011800000000, + } +} + +func TopUp(address interop.Hash160, token string, amount int) bool { + if !isKnownToken(token) { + runtime.Log("Token " + token + " is unkown") + return false + } + + wallet := getWallet(address) + balance := wallet.getTokenBalance(token) + wallet.Tokens[token] = balance + amount + saveWallet(address, wallet) + + return true +} + +func GetTokenBalance(address interop.Hash160, token string) int { + return getWallet(address).getTokenBalance(token) +} + +func Exchange(address interop.Hash160, amount int, fromToken string, toToken string) bool { + if amount <= 0 { + runtime.Log("Invalid amount") + return false + } + + wallet := getWallet(address) + fromBalance := wallet.getTokenBalance(fromToken) + if fromBalance < amount { + runtime.Log("Not enough funds to exchange") + return false + } + + fromTokenPrice := getPrice(fromToken) + toTokenPrice := getPrice(toToken) + convertedAmount := amount * fromTokenPrice / toTokenPrice + + wallet.Tokens[fromToken] -= amount + wallet.Tokens[toToken] = wallet.getTokenBalance(toToken) + convertedAmount + + saveWallet(address, wallet) + + runtime.Log("Exchange successful: " + std.Itoa10(amount) + " " + fromToken + + " exchanged for " + std.Itoa10(convertedAmount) + " " + toToken) + + return true +} + +func PrintWallet(address interop.Hash160) { + wallet := getWallet(address) + + runtime.Log("Wallet " + ":") + for key, value := range wallet.Tokens { + runtime.Log(key + ":" + std.Itoa10(value)) + } +} + +func getWallet(address interop.Hash160) Wallet { ctx := storage.GetContext() walletBytes := storage.Get(ctx, address) if walletBytes == nil { @@ -31,107 +102,36 @@ func GetWallet(address interop.Hash160) Wallet { } } - var wallet Wallet - walletBytesAsBytes, _ := walletBytes.([]byte) - err := json.Unmarshal(walletBytesAsBytes, &wallet) - if err != nil { + walletBytesAsBytes := walletBytes.([]byte) + deserialized := std.Deserialize(walletBytesAsBytes) + if deserialized == nil { return Wallet{} } - return wallet + return deserialized.(Wallet) } -func UpdateWallet(address interop.Hash160, wallet Wallet) { +func saveWallet(address interop.Hash160, wallet Wallet) { ctx := storage.GetContext() - serialized, _ := json.Marshal(wallet) + serialized := std.Serialize(wallet) storage.Put(ctx, address, serialized) } -func GetBalance(address interop.Hash160, token string) int { - wallet := GetWallet(address) - return wallet.Tokens[token] +func getPrice(token string) int { + if !isKnownToken(token) { + panic("trying to get price for unknown token") + } + return prices[token] } -func RequestExchangeRate(fromToken string, toToken string) float64 { - originalString := "https://api.binance.com/api/v3/ticker/price?symbol=%sUSDT" +func getValueFromStringIntMap(mp map[string]int, key string) any { + defer func() { + recover() + }() - urlFrom := fmt.Sprintf(originalString, fromToken) - urlTo := fmt.Sprintf(originalString, toToken) - - client := resty.New() - - responseFrom, errFrom := client.R().Get(urlFrom) - responseTo, errTo := client.R().Get(urlTo) - if errFrom != nil || errTo != nil { - fmt.Println("Error during doing response") - return 0 - } - - var tickerPriceFrom TickerPrice - var tickerPriceTo TickerPrice - errFrom = json.Unmarshal(responseFrom.Body(), &tickerPriceFrom) - errTo = json.Unmarshal(responseTo.Body(), &tickerPriceTo) - if errFrom != nil || errTo != nil { - fmt.Println("Error during unpacking JSON") - return 0 - } - - priceFrom, _ := strconv.ParseFloat(tickerPriceFrom.Price, 64) - priceTo, _ := strconv.ParseFloat(tickerPriceTo.Price, 64) - result := priceFrom / priceTo - return result + return mp[key] } -func Transfer(address interop.Hash160, amount int, fromToken string, toToken string) bool { - if amount <= 0 { - runtime.Log("Invalid amount") - return false - } - - balance := gas.BalanceOf(address) - if balance < amount { - runtime.Log("Low gas balance for transaction") - return false - } - - contractHash := runtime.GetExecutingScriptHash() - result := gas.Transfer(address, contractHash, amount, nil) - if !result { - runtime.Log("Transfer operation failed") - return false - } - - wallet := GetWallet(address) - if GetBalance(address, fromToken) < amount { - runtime.Log("Not enough funds to exchange") - return false - } - - exchangeRate := RequestExchangeRate(fromToken, toToken) - if exchangeRate == 0 { - return false - } - - convertedAmount := float64(amount) * exchangeRate - - wallet.Tokens[fromToken] -= amount - wallet.Tokens[toToken] += int(convertedAmount) - - UpdateWallet(address, wallet) - - runtime.Log("Exchange successful: " + strconv.Itoa(amount) + " " + fromToken + - " exchanged for " + strconv.Itoa(int(convertedAmount)) + " " + toToken) - - return true -} - -func printBalance(address interop.Hash160) bool { - wallet := GetWallet(address) - fmt.Println("Balance: ") - - for key, value := range wallet.Tokens { - fmt.Println(key + ":" + strconv.Itoa(value)) - } - - return true +func isKnownToken(token string) bool { + return getValueFromStringIntMap(prices, token) != nil } diff --git a/contract.nef b/contract.nef new file mode 100644 index 0000000..373177f Binary files /dev/null and b/contract.nef differ diff --git a/go.mod b/go.mod index 3f2e8c8..382cbcc 100644 --- a/go.mod +++ b/go.mod @@ -1,10 +1,5 @@ module contracts -go 1.21.5 +go 1.21 -require ( - github.com/go-resty/resty/v2 v2.11.0 - github.com/nspcc-dev/neo-go/pkg/interop v0.0.0-20231228104959-3176f72878ba -) - -require golang.org/x/net v0.19.0 // indirect +require github.com/nspcc-dev/neo-go/pkg/interop v0.0.0-20231228104959-3176f72878ba diff --git a/go.sum b/go.sum index 3f23531..78461bd 100644 --- a/go.sum +++ b/go.sum @@ -1,43 +1,2 @@ -github.com/go-resty/resty/v2 v2.11.0/go.mod h1:iiP/OpA0CkcL3IGt1O0+/SIItFUbkkyw5BGXiVdTu+A= +github.com/nspcc-dev/neo-go/pkg/interop v0.0.0-20231228104959-3176f72878ba h1:eDgYwTfBkupMnrVxs2Rmk21Cf/BG0jocYc3EvlH9np4= github.com/nspcc-dev/neo-go/pkg/interop v0.0.0-20231228104959-3176f72878ba/go.mod h1:J/Mk6+nKeKSW4wygkZQFLQ6SkLOSGX5Ga0RuuuktEag= -github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY= -golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= -golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc= -golang.org/x/crypto v0.14.0/go.mod h1:MVFd36DqK4CsrnJYDkBA3VC4m2GkXAM0PvzMCn4JQf4= -golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4= -golang.org/x/mod v0.8.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs= -golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= -golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg= -golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c= -golang.org/x/net v0.6.0/go.mod h1:2Tu9+aMcznHK/AK1HMvgo6xiTLG5rD5rZLDS+rp2Bjs= -golang.org/x/net v0.10.0/go.mod h1:0qNGK6F8kojg2nk9dLZ2mShWaEBan6FAoqfSigmmuDg= -golang.org/x/net v0.17.0/go.mod h1:NxSsAGuq816PNPmqtQdLE42eU2Fs7NoRIZrHJAlaCOE= -golang.org/x/net v0.19.0/go.mod h1:CfAk/cbD4CthTvqiEl8NpboMuiuOYsAr/7NOjZJtv1U= -golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= -golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= -golang.org/x/sync v0.1.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= -golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= -golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= -golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/sys v0.8.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/sys v0.13.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= -golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8= -golang.org/x/term v0.5.0/go.mod h1:jMB1sMXY+tzblOD4FWmEbocvup2/aLOaQEp7JmGp78k= -golang.org/x/term v0.8.0/go.mod h1:xPskH00ivmX89bAKVGSKKtLOWNx2+17Eiy94tnKShWo= -golang.org/x/term v0.13.0/go.mod h1:LTmsnFJwVN6bCy1rVCoS+qHT1HhALEFxKncY3WNNh4U= -golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= -golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= -golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ= -golang.org/x/text v0.7.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8= -golang.org/x/text v0.9.0/go.mod h1:e1OnstbJyHTd6l/uOt8jFFHp6TRDWZR/bV3emEE/zU8= -golang.org/x/text v0.13.0/go.mod h1:TvPlkZtksWOMsz7fbANvkp4WM8x/WCo/om8BMLbz+aE= -golang.org/x/time v0.3.0/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ= -golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= -golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= -golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc= -golang.org/x/tools v0.6.0/go.mod h1:Xwgl3UAJ/d3gWutnCtw505GrjyAbvKui8lOU390QaIU= -golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=