Skip to content

Commit

Permalink
createNFT full flow work successfully
Browse files Browse the repository at this point in the history
  • Loading branch information
JackBekket committed Nov 25, 2022
1 parent 5133e3d commit a38ae6c
Show file tree
Hide file tree
Showing 3 changed files with 116 additions and 23 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ go 1.17

require (
github.com/MoonSHRD/IKY-telegram-bot v0.2.1
github.com/MoonSHRD/TelegramNFT-Wizard-Contracts v0.1.0
github.com/StarkBotsIndustries/telegraph/v2 v2.0.0
github.com/ethereum/go-ethereum v1.10.26
github.com/go-telegram-bot-api/telegram-bot-api/v5 v5.5.1
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ github.com/BurntSushi/xgb v0.0.0-20160522181843-27f122750802/go.mod h1:IVnqGOEym
github.com/DATA-DOG/go-sqlmock v1.3.3/go.mod h1:f/Ixk793poVmq4qj/V1dPUg2JEAKC73Q5eFN3EC/SaM=
github.com/MoonSHRD/IKY-telegram-bot v0.2.1 h1:QJjJwMtzpuSZEUh1d8LRjpt9I7zm3hN0PtZjvRkJ0BM=
github.com/MoonSHRD/IKY-telegram-bot v0.2.1/go.mod h1:oR7+q9qz0IivIxTHN58xQRIA+dg+1dLXwrNH8rnnTFc=
github.com/MoonSHRD/TelegramNFT-Wizard-Contracts v0.1.0 h1:2OBpvT6CQ3FjM1QwhaA2X5npIL6cngyLiMz9fuuqsEA=
github.com/MoonSHRD/TelegramNFT-Wizard-Contracts v0.1.0/go.mod h1:FINo9dc++NH/xdhoy8YJM9nZiEIlIs38lX0lgCZeBwY=
github.com/OneOfOne/xxhash v1.2.2/go.mod h1:HSdplMjZKSmBqAxg5vPj2TmRDmfkzw+cTzAElWljhcU=
github.com/StackExchange/wmi v0.0.0-20180116203802-5d049714c4a6 h1:fLjPD/aNc3UIOA6tDi6QXUemppXK3P9BI7mr2hd6gx8=
github.com/StackExchange/wmi v0.0.0-20180116203802-5d049714c4a6/go.mod h1:3eOhrUMpNV+6aFIbp5/iudMxNCF27Vw2OZgy4xEx0Fg=
Expand Down
136 changes: 113 additions & 23 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,13 @@ import (
passport "github.com/MoonSHRD/IKY-telegram-bot/artifacts/TGPassport"
//passport "IKY-telegram-bot/artifacts/TGPassport"

//SingletonNFT "github.com/MoonSHRD/TelegramNFT-Wizard-Contracts/go/SingletonNFT"
SingletonNFT "github.com/MoonSHRD/TelegramNFT-Wizard-Contracts/go/SingletonNFT"

"github.com/ethereum/go-ethereum/accounts/abi/bind"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/ethclient"
"github.com/ethereum/go-ethereum/event"

"github.com/StarkBotsIndustries/telegraph/v2"
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5"
Expand Down Expand Up @@ -53,7 +54,9 @@ type user struct {
dialog_status int64
}


// channel to get this event from blockchain
var ch = make(chan *SingletonNFT.SingletonNFTItemCreated)
//var ch_index = make(chan *passport.PassportPassportAppliedIndexed)



Expand Down Expand Up @@ -81,7 +84,7 @@ func main() {

msgTemplates["hello"] = "Hey, this bot is allowing you to create NFT"
msgTemplates["case0"] = "Open following link in metamask broswer"
msgTemplates["await"] = "Awaiting for verification"
msgTemplates["await"] = "Awaiting for NFT mint"
msgTemplates["case1"] = "Send me a _single_ file which u want to transform into NFT"
msgTemplates["who_is"] = "Input wallet address to know it's associated telegram nickname"
msgTemplates["not_registred"] = "You are not registred yet, first attach your wallet to your tg account via this bot https://t.me/E_Passport_bot"
Expand All @@ -91,7 +94,7 @@ func main() {
//var baseURL = "http://localhost:3000/"
//var baseURL = "https://ikytest-gw0gy01is-s0lidarnost.vercel.app/"
//var baseURL = myenv["BASEURL"];

var baseURL = "https://telegram-nft-wizard.vercel.app/"


bot, err = tgbotapi.NewBotAPI(string(tgApiKey))
Expand Down Expand Up @@ -128,6 +131,11 @@ func main() {
log.Fatalf("Failed to instantiate a TGPassport contract: %v", err)
}

singletonCollection, err := SingletonNFT.NewSingletonNFT(common.HexToAddress(myenv["SINGLETON_ADDRESS"]), client)
if err != nil {
log.Fatalf("Failed to instantiate a SingletonNFT contract: %v", err)
}

// Wrap the Passport contract instance into a session
session := &passport.PassportSession{
Contract: passportCenter,
Expand All @@ -147,6 +155,26 @@ func main() {

log.Printf("session with passport center initialized")


//Wrap SingletonNFT contract instance into a session
session_single_nft := &SingletonNFT.SingletonNFTSession{
Contract: singletonCollection,
CallOpts: bind.CallOpts{
Pending: true,
From: auth.From,
Context: context.Background(),
},
TransactOpts: bind.TransactOpts{
From: auth.From,
Signer: auth.Signer,
GasLimit: 0,
GasFeeCap: nil,
GasTipCap: nil,
Context: context.Background(),
},
}
log.Printf("session with singleton NFT initialized")

log.Printf("Authorized on account %s", bot.Self.UserName)

u := tgbotapi.NewUpdate(0)
Expand Down Expand Up @@ -184,6 +212,9 @@ func main() {
updateDb.dialog_status = 0
userDatabase[update.Message.From.ID] = updateDb
} else {
msg := tgbotapi.NewMessage(userDatabase[update.Message.From.ID].tgid, msgTemplates["hello"])
msg.ReplyMarkup = mainKeyboard
bot.Send(msg)
updateDb.dialog_status = 1
userDatabase[update.Message.From.ID] = updateDb
}
Expand Down Expand Up @@ -211,6 +242,7 @@ func main() {

}
// fallthrough // МЫ ЛЕД ПОД НОГАМИ МАЙОРА!
// choose option
case 1:
if updateDb, ok := userDatabase[update.Message.From.ID]; ok {
msg := tgbotapi.NewMessage(userDatabase[update.Message.From.ID].tgid, msgTemplates["case1"])
Expand All @@ -220,7 +252,7 @@ func main() {
userDatabase[update.Message.From.ID] = updateDb

}

// download file from user and upload to telegraph
case 2:
if updateDb, ok := userDatabase[update.Message.From.ID]; ok {
if update.Message.Document != nil {
Expand Down Expand Up @@ -255,31 +287,30 @@ func main() {
GetFile(file,httpClient(),direct_url,file_name)
telegraph_link := UploadFileToTelegraph(file_name)

/*
file_config := tgbotapi.FileConfig{
FileID: file_id,
}
file,err := bot.GetFile(file_config)
if err != nil {
log.Println(err)
}
o_file_id := file.FileID
log.Println(o_file_id)
*/

//bot.UploadFiles("https://telegra.ph/upload",tgbotapi.Params{},[]tgbotapi.RequestFile{})


fmt.Println(direct_url)
log.Println(direct_url)



//TODO: make a POST query to telegraph!
//bot.UploadFiles("https://telegra.ph/upload",tgbotapi.Params{},[]tgbotapi.RequestFile{})
msg = tgbotapi.NewMessage(userDatabase[update.Message.From.ID].tgid, "telegraph URL is:" + telegrap_base_url + telegraph_link)
bot.Send(msg)

// remove file locally after upload
deleteFile(file_name)


// create link to mint NFT
createLink(userDatabase[update.Message.From.ID].tgid,file_name,baseURL)

subscription, err := SubscribeForCreateItem(session_single_nft, ch) // this is subscription to UNINDEXED event.
if err != nil {
log.Println(err)
}

go AsyncCreateItemListener(ctx,subscription,userDatabase[update.Message.From.ID].tgid,auth,userDatabase)

updateDb.dialog_status = 3
userDatabase[update.Message.From.ID] = updateDb
Expand All @@ -291,18 +322,27 @@ func main() {
}
}

// whois
// await for mint
case 3:
if updateDb, ok := userDatabase[update.Message.From.ID]; ok {


msg := tgbotapi.NewMessage(userDatabase[update.Message.From.ID].tgid, msgTemplates["await"])
//msg.ReplyMarkup = optionKeyboard
bot.Send(msg)

updateDb.dialog_status = 3
userDatabase[update.Message.From.ID] = updateDb

}

//
// mint successfull(?)
case 4:
if updateDb, ok := userDatabase[update.Message.From.ID]; ok {
updateDb.dialog_status = 3
msg := tgbotapi.NewMessage(userDatabase[update.Message.From.ID].tgid, "Your NFT is succesfully minted, you will redirect to main menu")
//msg.ReplyMarkup = optionKeyboard
bot.Send(msg)
updateDb.dialog_status = 1
userDatabase[update.Message.From.ID] = updateDb
}

Expand All @@ -322,7 +362,14 @@ func loadEnv() {
}
}


func createLink(tgid int64,file_name string, base_url string) {

msg := tgbotapi.NewMessage(tgid, "Open following link in metamask browser and click CreateNFT!")
bot.Send(msg)
link := base_url + "?file_id=" + file_name
msg = tgbotapi.NewMessage(userDatabase[tgid].tgid, link)
bot.Send(msg)
}


// download file
Expand Down Expand Up @@ -401,6 +448,49 @@ func checkError(err error) {
}
}

// subscribing for CreateItem events. We use watchers without fast-forwarding past events
func SubscribeForCreateItem(session *SingletonNFT.SingletonNFTSession, listenChannel chan<- *SingletonNFT.SingletonNFTItemCreated) (event.Subscription, error) {
subscription, err := session.Contract.WatchItemCreated(&bind.WatchOpts{
Start: nil, //last block
Context: nil, // nil = no timeout
}, listenChannel,
// applierTGID,
)
if err != nil {
return nil, err
}
return subscription, err
}

func AsyncCreateItemListener(ctx context.Context,subscription event.Subscription, tgid int64, auth *bind.TransactOpts, userDatabase map[int64]user) {
EventLoop:
for {
select {
case <-ctx.Done():
{
subscription.Unsubscribe()
break EventLoop
}
case eventResult := <-ch:
{
fmt.Println("NFT token ID:", eventResult.TokenId)
fmt.Println("NFT collection address:", eventResult.Raw.Address)
fmt.Println("File_ID string: ", eventResult.FileId)

msg := tgbotapi.NewMessage(userDatabase[tgid].tgid, " your NFT token has been created, token ID is: " + eventResult.TokenId.String())
bot.Send(msg)
msg = tgbotapi.NewMessage(tgid,"address of NFT collection (add it to metamask): " + eventResult.Raw.Address.Hex())
bot.Send(msg)
subscription.Unsubscribe()
break EventLoop
}
}
}
updateDb := userDatabase[tgid]
updateDb.dialog_status = 4
userDatabase[tgid] = updateDb
}


// allow bot to get tg nickname associated with this eth wallet
func WhoIsAddress(session *passport.PassportSession,address_to_check common.Address) (string,error){
Expand Down

0 comments on commit a38ae6c

Please sign in to comment.