Skip to content

Commit

Permalink
Merge branch 'dev'
Browse files Browse the repository at this point in the history
  • Loading branch information
xenartist committed Nov 4, 2024
2 parents 724166e + ae4e4b5 commit e88efa4
Show file tree
Hide file tree
Showing 6 changed files with 377 additions and 9 deletions.
6 changes: 6 additions & 0 deletions burn-memo.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"title": "solXEN Burn with Memo",
"image": "https://xxx.png",
"content": "solXEN To Da Moon",
"author": "solXEN"
}
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ github.com/fatih/color v1.9.0 h1:8xPHl4/q1VyqGIPif1F+1V3Y3lSmrq01EabUW3CoW5s=
github.com/fatih/color v1.9.0/go.mod h1:eQcE1qtQxscV5RaZvpXrrb8Drkc3/DdQ+uUYCNjL+zU=
github.com/gagliardetto/binary v0.8.0 h1:U9ahc45v9HW0d15LoN++vIXSJyqR/pWw8DDlhd7zvxg=
github.com/gagliardetto/binary v0.8.0/go.mod h1:2tfj51g5o9dnvsc+fL3Jxr22MuWzYXwx9wEoN0XQ7/c=
github.com/gagliardetto/gofuzz v1.2.2 h1:XL/8qDMzcgvR4+CyRQW9UGdwPRPMHVJfqQ/uMvSUuQw=
github.com/gagliardetto/gofuzz v1.2.2/go.mod h1:bkH/3hYLZrMLbfYWA0pWzXmi5TTRZnu4pMGZBkqMKvY=
github.com/gagliardetto/solana-go v1.11.0 h1:g6mR7uRNVT0Y0LVR0bvJNfKV6TyO6oUzBYu03ZmkEmY=
github.com/gagliardetto/solana-go v1.11.0/go.mod h1:afBEcIRrDLJst3lvAahTr63m6W2Ns6dajZxe2irF7Jg=
github.com/gagliardetto/treeout v0.1.4 h1:ozeYerrLCmCubo1TcIjFiOWTTGteOOHND1twdFpgwaw=
Expand Down
165 changes: 162 additions & 3 deletions ui/tokenharvest.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package ui

import (
"encoding/json"
"fmt"
"regexp"
"strconv"
Expand All @@ -11,7 +12,15 @@ import (
"github.com/rivo/tview"
)

type BurnMemo struct {
Title string `json:"title"`
Image string `json:"image"`
Content string `json:"content"`
Author string `json:"author"`
}

var tokenOptions = []string{"solXEN", "xencat", "PV", "ORE"}
var autoHarvestCounter = 0

func CreateTokenHarvestUI(app *tview.Application) ModuleUI {
var moduleUI = CreateModuleUI(TOKEN_HARVEST_STRING, app)
Expand Down Expand Up @@ -104,11 +113,24 @@ func createAutoHarvestForm(app *tview.Application, moduleUI *ModuleUI, walletInf
config.HarvestInterval = option
})

// 5. Dropdown for burn interval
burnOptions := []string{"Off", "Burn/69", "Burn/100", "Burn/420"}
burnIndex := 0
for i, option := range burnOptions {
if option == config.HarvestBurn {
burnIndex = i
break
}
}
autoHarvestForm.AddDropDown("Burn Interval", burnOptions, burnIndex, func(option string, index int) {
config.HarvestBurn = option
})

// Add a channel to trigger config reload
reloadConfigChan := make(chan struct{})

// 5. Save Config button
autoHarvestForm.AddButton("Save Config & Let's Fuckin Harvest!", func() {
// 6. Save Config button
autoHarvestForm.AddButton("Save Config & Auto Harvest", func() {
err := utils.WriteSolXENConfigFile(config)
if err != nil {
utils.LogMessage(moduleUI.LogView, "Failed to save config: "+err.Error())
Expand Down Expand Up @@ -267,6 +289,57 @@ func createAutoHarvestForm(app *tview.Application, moduleUI *ModuleUI, walletInf
} else {
utils.LogMessage(moduleUI.LogView, fmt.Sprintf("SOL: %v -> %s: %s successfully", config.SOLPerHarvest, config.TokenToHarvest, result))

// Increment counter and check for burn condition
autoHarvestCounter++

// Set auto burn interval
var autoBurnInterval int = 0

switch config.HarvestBurn {
case "Off":
autoBurnInterval = -1
case "Burn/69":
autoBurnInterval = 69
case "Burn/100":
autoBurnInterval = 100
case "Burn/420":
autoBurnInterval = 420
}

if autoBurnInterval != -1 && autoHarvestCounter >= autoBurnInterval {
// Reset counter
autoHarvestCounter = 0

// Parse the received token amount
tokenAmount := strings.Split(result, " ")[0]

// Burn the tokens after a delay to ensure the swap completed
go func(amount, token string) {
time.Sleep(5 * time.Minute)

burnMemo := BurnMemo{
Title: "solXEN Burn with Memo",
Image: "https://xxx.png",
Content: "solXEN To Da Moon",
Author: "solXEN",
}

jsonData, err := json.Marshal(burnMemo)
memoText := string(jsonData)
if err != nil {
utils.LogMessage(moduleUI.LogView, "Error marshalling burn memo: "+err.Error())
return
}

burnResult, err := utils.BurnToken(amount, token, memoText)
if err != nil {
utils.LogMessage(moduleUI.LogView, fmt.Sprintf("Error burning tokens: %v", err))
} else {
utils.LogMessage(moduleUI.LogView, burnResult)
}
}(tokenAmount, config.TokenToHarvest)
}

// Update wallet info after 60 seconds
go func() {
time.Sleep(60 * time.Second)
Expand Down Expand Up @@ -364,7 +437,7 @@ func createManualHarvestForm(app *tview.Application, moduleUI *ModuleUI, walletI
updateTokenAmount()

// 3. Swap button
manualHarvestForm.AddButton("Manual Harvest", func() {
manualHarvestForm.AddButton("Harvest (No Burn)", func() {

// Get SOL balance
solBalance, err := utils.GetSOLBalance(utils.GetGlobalPublicKey())
Expand Down Expand Up @@ -398,5 +471,91 @@ func createManualHarvestForm(app *tview.Application, moduleUI *ModuleUI, walletI

})

// Add Burn Memo input field
titleInput := tview.NewInputField().
SetLabel("Title: ").
SetText("solXEN Burn with Memo")

imageInput := tview.NewInputField().
SetLabel("Image: ").
SetText("https://xxx.png")

contentInput := tview.NewInputField().
SetLabel("Content: ").
SetText("solXEN To Da Moon")

authorInput := tview.NewInputField().
SetLabel("Author: ").
SetText("solXEN")

manualHarvestForm.AddFormItem(titleInput)
manualHarvestForm.AddFormItem(imageInput)
manualHarvestForm.AddFormItem(contentInput)
manualHarvestForm.AddFormItem(authorInput)

// Add Manual Harvest & Burn button (new)
manualHarvestForm.AddButton("Harvest (and Burn)", func() {
// Get SOL balance
solBalance, err := utils.GetSOLBalance(utils.GetGlobalPublicKey())
if err != nil {
utils.LogMessage(moduleUI.LogView, "Error getting SOL balance: "+err.Error())
return
}

// Check if the SOL balance is greater than 0.000006
if solBalance <= 0.000006 {
utils.LogMessage(moduleUI.LogView, "Insufficient SOL balance. Minimum required: 0.000006 SOL")
return
}

utils.LogMessage(moduleUI.LogView, fmt.Sprintf("SOL: %s for %s", solAmount, selectedToken))

result, err := utils.ExchangeSolForToken(solAmount, selectedToken)
if err != nil {
utils.LogMessage(moduleUI.LogView, "Error: "+err.Error())
} else {
tokenAmountText.SetText("Amount(Est.): \n" + result + " " + selectedToken)
utils.LogMessage(moduleUI.LogView, fmt.Sprintf("SOL: %s -> %s: %s successfully", solAmount, selectedToken, result))

// Extract token amount from result
tokenAmount := strings.Split(result, " ")[0]

// Burn tokens after delay
go func() {
time.Sleep(5 * time.Minute)

burnMemo := BurnMemo{
Title: titleInput.GetText(),
Image: imageInput.GetText(),
Content: contentInput.GetText(),
Author: authorInput.GetText(),
}

jsonData, err := json.Marshal(burnMemo)
if err != nil {
utils.LogMessage(moduleUI.LogView, "Error marshalling burn memo: "+err.Error())
return
}
memoText := string(jsonData)

burnResult, err := utils.BurnToken(tokenAmount, selectedToken, memoText)
if err != nil {
utils.LogMessage(moduleUI.LogView, fmt.Sprintf("Error burning tokens: %v", err))
} else {
utils.LogMessage(moduleUI.LogView, burnResult)
}

// Update wallet info
UpdateWalletInfo(app, walletInfoView)
}()

// Update wallet info after initial purchase
go func() {
time.Sleep(60 * time.Second)
UpdateWalletInfo(app, walletInfoView)
}()
}
})

return manualHarvestForm
}
21 changes: 15 additions & 6 deletions utils/jupiter.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ const (
ORE = "ORE"
JupiterQuoteURL = "https://quote-api.jup.ag/v6/quote"
JupiterSwapURL = "https://quote-api.jup.ag/v6/swap"
JupiterPriceURL = "https://price.jup.ag/v6/price"
JupiterPriceURL = "https://api.jup.ag/price/v2"
SOLMint = "So11111111111111111111111111111111111111112"
)

Expand Down Expand Up @@ -115,7 +115,7 @@ func fetchPrice(tokenName string) float64 {
return 0
}

apiURL := fmt.Sprintf("%s?ids=SOL&vsToken=%s", JupiterPriceURL, tokenAddress)
apiURL := fmt.Sprintf("%s?ids=%s&vsToken=%s", JupiterPriceURL, SOLMint, tokenAddress)

resp, err := http.Get(apiURL)
if err != nil {
Expand All @@ -132,22 +132,31 @@ func fetchPrice(tokenName string) float64 {

var priceResponse struct {
Data map[string]struct {
Price float64 `json:"price"`
ID string `json:"id"`
Type string `json:"type"`
Price string `json:"price"`
} `json:"data"`
TimeTaken float64 `json:"timeTaken"`
}
if err := json.Unmarshal(body, &priceResponse); err != nil {
LogToFile(fmt.Sprintf("Failed to parse JSON response: %v", err))
return 0
}

priceData, ok := priceResponse.Data["SOL"]
priceData, ok := priceResponse.Data[SOLMint]
if !ok {
LogToFile("Price not found in response")
return 0
}

LogToFile(fmt.Sprintf("Fetched price for %s: %f", tokenName, priceData.Price))
return priceData.Price
price, err := strconv.ParseFloat(priceData.Price, 64)
if err != nil {
LogToFile(fmt.Sprintf("Failed to parse price string: %v", err))
return 0
}

LogToFile(fmt.Sprintf("Fetched price for %s: %f", tokenName, price))
return price
}

// GetTokenExchangeAmount queries the amount of specified token that can be exchanged for a given amount of SOL
Expand Down
Loading

0 comments on commit e88efa4

Please sign in to comment.