From 483c2476e4e363965ded5d05f1d0971ff9c30d82 Mon Sep 17 00:00:00 2001 From: Joseph Ferano Date: Mon, 4 Jul 2022 21:01:03 +0700 Subject: [PATCH] TransferNFT script --- Bot/Bot.fsproj | 1 + Bot/Scripts/TransferNFT.fsx | 97 +++++++++++++++++++++++++++++++++++++ 2 files changed, 98 insertions(+) create mode 100644 Bot/Scripts/TransferNFT.fsx diff --git a/Bot/Bot.fsproj b/Bot/Bot.fsproj index 0669f35..8bbe60b 100644 --- a/Bot/Bot.fsproj +++ b/Bot/Bot.fsproj @@ -30,6 +30,7 @@ + diff --git a/Bot/Scripts/TransferNFT.fsx b/Bot/Scripts/TransferNFT.fsx new file mode 100644 index 0000000..fe74fa3 --- /dev/null +++ b/Bot/Scripts/TransferNFT.fsx @@ -0,0 +1,97 @@ +#load "/home/joe/Development/DegenzGame/.paket/load/net6.0/main.group.fsx";; + +open Npgsql.FSharp +open dotenv.net +open System.IO +open System.Diagnostics +open System.Threading.Tasks + +let prodEnv = DotEnv.Read(DotEnvOptions(envFilePaths = [ "./.prod.env" ])) +let ( _ , prodConnStr )= prodEnv.TryGetValue("DATABASE_URL") + +type NftTransfer = { + Id : uint64 + Name : string + NftName : string + NftAddress : string + UserWallet : string +} + + +printfn "Getting Wallet Addresses:" +let pending = + prodConnStr + |> Sql.connect + |> Sql.query """ + SELECT u.id, u.display_name, token.name, token.address, mpa.tx_wallet FROM gen_one_token token + JOIN mint_pass_assignment mpa on token.id = mpa.token_id + JOIN "user" u on mpa.recipient_id = u.id + WHERE mpa.fulfilled = false AND token.locked = false AND u.id <> '815567646606229514' + ORDER BY mpa.created_at; + """ + |> Sql.execute (fun reader -> + { Id = reader.string "id" |> uint64 + Name = reader.string "display_name" + NftName = reader.string "name" + NftAddress = reader.string "address" + UserWallet = reader.string "tx_wallet" }) + + +let testSample = [ pending |> List.last ] + +type CommandResult = { + ExitCode: int; + StandardOutput: string; + StandardError: string +} + +let executeCommand executable args = + async { + let startInfo = ProcessStartInfo() + startInfo.FileName <- executable + + for a in args do + startInfo.ArgumentList.Add(a) + + startInfo.RedirectStandardOutput <- true + startInfo.RedirectStandardError <- true + startInfo.UseShellExecute <- false + startInfo.CreateNoWindow <- true + use p = new Process() + p.StartInfo <- startInfo + p.Start() |> ignore + + let outTask = + Task.WhenAll( + [| p.StandardOutput.ReadToEndAsync() + p.StandardError.ReadToEndAsync() |] + ) + + do! p.WaitForExitAsync() |> Async.AwaitTask + let! out = outTask |> Async.AwaitTask + + return + { ExitCode = p.ExitCode + StandardOutput = out.[0] + StandardError = out.[1] } + } +let executeShellCommand command = + executeCommand "/usr/bin/env" [ "-S"; "bash"; "-c"; command ] + + +let transfer (nft : NftTransfer) = + printfn $"Transferring {nft.NftName} to {nft.Name} " + let result = + $"spl-token transfer --allow-unfunded-recipient --fund-recipient {nft.NftAddress} 1 {nft.UserWallet}" + |> executeShellCommand + |> Async.RunSynchronously + if result.ExitCode = 0 then + let user = $"{nft.Id} - {nft.Name} - {nft.NftName}" + let tx = result.StandardOutput + File.AppendAllLines("/home/joe/Downloads/transactions", [ user ; tx ]) + else + printfn $"{result.StandardError}" + + +for nft in testSample do + transfer nft \ No newline at end of file