From 7d669aa047143a5363dee33f57675232220f073e Mon Sep 17 00:00:00 2001 From: unknown Date: Wed, 6 Nov 2024 14:38:30 +0100 Subject: [PATCH 1/2] Add NFT-to-token and token-to-NFT swap flow --- mpl_create_nft.rs | 35 +++++++++++++++++++++++++++++++++++ mpl_nft_to_token_swap.rs | 34 ++++++++++++++++++++++++++++++++++ mpl_swap_to_token.rs | 34 ++++++++++++++++++++++++++++++++++ mpl_token_to_nft_swap.rs | 34 ++++++++++++++++++++++++++++++++++ 4 files changed, 137 insertions(+) create mode 100644 mpl_create_nft.rs create mode 100644 mpl_nft_to_token_swap.rs create mode 100644 mpl_swap_to_token.rs create mode 100644 mpl_token_to_nft_swap.rs diff --git a/mpl_create_nft.rs b/mpl_create_nft.rs new file mode 100644 index 00000000..aee3a2be --- /dev/null +++ b/mpl_create_nft.rs @@ -0,0 +1,35 @@ +host = "0.0.0.0" +port = 8080 +local_storage = "_data/guest_local_storage" +cors_origins = ["*"] + +[supabase] +anon_key = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJzdXBhYmFzZSIsInJlZiI6Imh5amJvYmxramVldmt6YXFzeXhlIiwicm9sZSI6ImFub24iLCJpYXQiOjE3Mjc4ODc1MjgsImV4cCI6MjA0MzQ2MzUyOH0.J4tyAfa2_j1irMW3sZUi7ykTrubGdoqXWo9cPXqZ9iw" +endpoint = "https://hyjboblkjeevkzaqsyxe.supabase.co" + +[db] +upstream_url = "https://dev-api.spaceoperator.com" +api_keys = ["b3-EGITlELGk29ZKg42LxFFIn80BpCWcFlM-C15kE0owOA"] + + +use mpl_hybrid::instructions::create_nft_instruction; +use solana_client::rpc_client::RpcClient; +use solana_sdk::pubkey::Pubkey; + +// Function that will call the `create_nft_instruction` to create an NFT on the blockchain. +pub fn mpl_create_nft(user_address: &str, nft_metadata: Metadata) -> Result<(), SomeErrorType> { + // Step 1: Establish a connection to Solana's blockchain. + let rpc_url = "https://api.devnet.solana.com"; // You can use mainnet or testnet here. + let connection = RpcClient::new(rpc_url); + + // Step 2: Convert the user’s address into a Pubkey (Solana's key format) + let user_pubkey = Pubkey::from_str(user_address)?; + + // Step 3: Call the `create_nft_instruction` and pass the necessary data. + let instruction = create_nft_instruction(&connection, user_pubkey, nft_metadata)?; + + // Step 4: Send the transaction to the blockchain to actually create the NFT. + send_transaction(&connection, &instruction)?; + + Ok(()) +} diff --git a/mpl_nft_to_token_swap.rs b/mpl_nft_to_token_swap.rs new file mode 100644 index 00000000..bd11b824 --- /dev/null +++ b/mpl_nft_to_token_swap.rs @@ -0,0 +1,34 @@ +host = "0.0.0.0" +port = 8080 +local_storage = "_data/guest_local_storage" +cors_origins = ["*"] + +[supabase] +anon_key = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJzdXBhYmFzZSIsInJlZiI6Imh5amJvYmxramVldmt6YXFzeXhlIiwicm9sZSI6ImFub24iLCJpYXQiOjE3Mjc4ODc1MjgsImV4cCI6MjA0MzQ2MzUyOH0.J4tyAfa2_j1irMW3sZUi7ykTrubGdoqXWo9cPXqZ9iw" +endpoint = "https://hyjboblkjeevkzaqsyxe.supabase.co" + +[db] +upstream_url = "https://dev-api.spaceoperator.com" +api_keys = ["b3-EGITlELGk29ZKg42LxFFIn80BpCWcFlM-C15kE0owOA"] +use mpl_hybrid::instructions::{swap_nft_for_token_instruction}; +use solana_client::rpc_client::RpcClient; +use solana_sdk::{pubkey::Pubkey, signer::Signer}; + +pub fn mpl_nft_to_token_swap(user_address: &str, nft_address: &str, token_amount: u64) -> Result<(), Box> { + // Step 1: Establish a connection to Solana blockchain (devnet or mainnet) + let rpc_url = "https://api.devnet.solana.com"; // Update to mainnet if needed + let connection = RpcClient::new(rpc_url); + + // Step 2: Convert the user address and NFT address to Solana Pubkeys + let user_pubkey = Pubkey::from_str(user_address)?; + let nft_pubkey = Pubkey::from_str(nft_address)?; + + // Step 3: Define the token swap logic (this could be based on your specific token or exchange mechanism) + // Here we assume you have a function `swap_nft_for_token_instruction` that handles the swap logic + let instruction = swap_nft_for_token_instruction(&connection, user_pubkey, nft_pubkey, token_amount)?; + + // Step 4: Send the transaction to the blockchain + send_transaction(&connection, &instruction)?; + + Ok(()) +} diff --git a/mpl_swap_to_token.rs b/mpl_swap_to_token.rs new file mode 100644 index 00000000..b7c5b751 --- /dev/null +++ b/mpl_swap_to_token.rs @@ -0,0 +1,34 @@ +host = "0.0.0.0" +port = 8080 +local_storage = "_data/guest_local_storage" +cors_origins = ["*"] + +[supabase] +anon_key = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJzdXBhYmFzZSIsInJlZiI6Imh5amJvYmxramVldmt6YXFzeXhlIiwicm9sZSI6ImFub24iLCJpYXQiOjE3Mjc4ODc1MjgsImV4cCI6MjA0MzQ2MzUyOH0.J4tyAfa2_j1irMW3sZUi7ykTrubGdoqXWo9cPXqZ9iw" +endpoint = "https://hyjboblkjeevkzaqsyxe.supabase.co" + +[db] +upstream_url = "https://dev-api.spaceoperator.com" +api_keys = ["b3-EGITlELGk29ZKg42LxFFIn80BpCWcFlM-C15kE0owOA"] + +use mpl_hybrid::instructions::swap_to_token_instruction; +use solana_client::rpc_client::RpcClient; +use solana_sdk::pubkey::Pubkey; + +// Function that calls the `swap_to_token_instruction` to swap an NFT for a token +pub fn mpl_swap_to_token(user_address: &str, token_data: TokenData) -> Result<(), SomeErrorType> { + // Step 1: Establish a connection to the Solana blockchain. + let rpc_url = "https://api.devnet.solana.com"; // You can use mainnet or testnet here. + let connection = RpcClient::new(rpc_url); + + // Step 2: Convert user address into a Solana Pubkey. + let user_pubkey = Pubkey::from_str(user_address)?; + + // Step 3: Call the `swap_to_token_instruction` and pass necessary data. + let instruction = swap_to_token_instruction(&connection, user_pubkey, token_data)?; + + // Step 4: Send the transaction to the blockchain to complete the swap. + send_transaction(&connection, &instruction)?; + + Ok(()) +} diff --git a/mpl_token_to_nft_swap.rs b/mpl_token_to_nft_swap.rs new file mode 100644 index 00000000..19c7c880 --- /dev/null +++ b/mpl_token_to_nft_swap.rs @@ -0,0 +1,34 @@ +host = "0.0.0.0" +port = 8080 +local_storage = "_data/guest_local_storage" +cors_origins = ["*"] + +[supabase] +anon_key = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJzdXBhYmFzZSIsInJlZiI6Imh5amJvYmxramVldmt6YXFzeXhlIiwicm9sZSI6ImFub24iLCJpYXQiOjE3Mjc4ODc1MjgsImV4cCI6MjA0MzQ2MzUyOH0.J4tyAfa2_j1irMW3sZUi7ykTrubGdoqXWo9cPXqZ9iw" +endpoint = "https://hyjboblkjeevkzaqsyxe.supabase.co" + +[db] +upstream_url = "https://dev-api.spaceoperator.com" +api_keys = ["b3-EGITlELGk29ZKg42LxFFIn80BpCWcFlM-C15kE0owOA"] + +use mpl_hybrid::instructions::{swap_token_for_nft_instruction}; +use solana_client::rpc_client::RpcClient; +use solana_sdk::{pubkey::Pubkey, signer::Signer}; + +pub fn mpl_token_to_nft_swap(user_address: &str, token_address: &str, nft_metadata: Metadata) -> Result<(), Box> { + // Step 1: Establish a connection to Solana blockchain (devnet or mainnet) + let rpc_url = "https://api.devnet.solana.com"; // Update to mainnet if needed + let connection = RpcClient::new(rpc_url); + + // Step 2: Convert the user address and token address to Solana Pubkeys + let user_pubkey = Pubkey::from_str(user_address)?; + let token_pubkey = Pubkey::from_str(token_address)?; + + // Step 3: Define the logic for swapping token to NFT (specific token logic) + let instruction = swap_token_for_nft_instruction(&connection, user_pubkey, token_pubkey, nft_metadata)?; + + // Step 4: Send the transaction to the blockchain + send_transaction(&connection, &instruction)?; + + Ok(()) +} From 52ce275ebf66da825f0ae246fd85063c846afaad Mon Sep 17 00:00:00 2001 From: unknown Date: Wed, 6 Nov 2024 14:48:35 +0100 Subject: [PATCH 2/2] Add NFT-to-token and token-to-NFT swap flow --- mpl_create_nft.rs => crates/cmds-solana/src/mpl_create_nft.rs | 0 .../cmds-solana/src/mpl_nft_to_token_swap.rs | 0 .../cmds-solana/src/mpl_swap_to_token.rs | 0 .../cmds-solana/src/mpl_token_to_nft_swap.rs | 0 4 files changed, 0 insertions(+), 0 deletions(-) rename mpl_create_nft.rs => crates/cmds-solana/src/mpl_create_nft.rs (100%) rename mpl_nft_to_token_swap.rs => crates/cmds-solana/src/mpl_nft_to_token_swap.rs (100%) rename mpl_swap_to_token.rs => crates/cmds-solana/src/mpl_swap_to_token.rs (100%) rename mpl_token_to_nft_swap.rs => crates/cmds-solana/src/mpl_token_to_nft_swap.rs (100%) diff --git a/mpl_create_nft.rs b/crates/cmds-solana/src/mpl_create_nft.rs similarity index 100% rename from mpl_create_nft.rs rename to crates/cmds-solana/src/mpl_create_nft.rs diff --git a/mpl_nft_to_token_swap.rs b/crates/cmds-solana/src/mpl_nft_to_token_swap.rs similarity index 100% rename from mpl_nft_to_token_swap.rs rename to crates/cmds-solana/src/mpl_nft_to_token_swap.rs diff --git a/mpl_swap_to_token.rs b/crates/cmds-solana/src/mpl_swap_to_token.rs similarity index 100% rename from mpl_swap_to_token.rs rename to crates/cmds-solana/src/mpl_swap_to_token.rs diff --git a/mpl_token_to_nft_swap.rs b/crates/cmds-solana/src/mpl_token_to_nft_swap.rs similarity index 100% rename from mpl_token_to_nft_swap.rs rename to crates/cmds-solana/src/mpl_token_to_nft_swap.rs