UNPKG

mcswap-sdk

Version:

Javascript SDK for the McSwap Protocol

828 lines 255 kB
// name: mcswap-sdk // author: @SolDapper // license: MIT https://github.com/SolDapper/mcswap-sdk/blob/master/LICENSE 'use strict'; import{PublicKey,Keypair,Connection,TransactionInstruction,TransactionMessage,VersionedTransaction,ComputeBudgetProgram,SystemProgram,SYSVAR_INSTRUCTIONS_PUBKEY}from "@solana/web3.js"; import * as solanaAccountCompression from "@solana/spl-account-compression"; import BufferLayout from "buffer-layout"; import * as splToken from "@solana/spl-token"; import bs58 from 'bs58'; import BN from "bn.js"; import { createMemoInstruction } from '@solana/spl-memo'; import { getDomainKeySync, NameRegistryState } from "@bonfida/spl-name-service"; const publicKey=(property="publicKey")=>{return BufferLayout.blob(32,property);};const uint64=(property="uint64")=>{return BufferLayout.blob(8,property);} class mcswap { constructor(){ this.NAME = "McSwap Javascript SDK"; this.PRIORITY = "Low"; this.TOLERANCE = 1.1; // mcswap-spl this.SPL_TREASURY = "5zx6c1E5aaBE6BbXu1ureKoZfpGbPBk9srmKavr3Xz3k"; this.SPL_MCSWAP_PROGRAM = "BG9YVprV4XeQR15puwwaWfBBPzamTtuMRJLkAa8pG5hz"; this.SPL_STATIC_ALT = "HtWT9EbpaxrNfbTgjsMuk9dXYmkvJwyr7q7bQjCCRQHU"; this.SPL_PROGRAM_STATE = BufferLayout.struct([ BufferLayout.u8("is_initialized"), uint64("dev_lamports"), publicKey("dev_treasury"), publicKey("swap_vault_pda"), BufferLayout.u8("swap_vault_bump"), ]); this.SPL_SWAP_STATE = BufferLayout.struct([ BufferLayout.u8("is_initialized"), uint64("utime"), publicKey("initializer"), publicKey("token1_mint"), uint64("token1_amount"), publicKey("temp_token1_account"), publicKey("token2_mint"), uint64("token2_amount"), publicKey("temp_token2_account"), publicKey("taker"), publicKey("token3_mint"), uint64("token3_amount"), publicKey("token4_mint"), uint64("token4_amount"), BufferLayout.u8("physical"), ]); // mcswap-core this.CORE_TREASURY = "Fmu3MXN9oNkcJsgf9Y2X19tHyYJ4SsPxyrgVoou1iKke"; this.CORE_MCSWAP_PROGRAM = "DE6UDLhAu8U8CL6b1XPjj76merQeSKFYgPQs2R4jc7Ba"; this.CORE_PROGRAM = "CoREENxT6tW1HoK8ypY1SxRMZTcVPm7R94rH4PZNhX7d"; this.CORE_PROGRAM_STATE = BufferLayout.struct([ BufferLayout.u8("is_initialized"), uint64("fee_lamports"), publicKey("dev_treasury"), ]); this.CORE_SWAP_STATE = BufferLayout.struct([ BufferLayout.u8("is_initialized"), uint64("utime"), BufferLayout.u8("is_swap"), publicKey("initializer"), publicKey("initializer_asset"), publicKey("taker"), publicKey("swap_asset"), uint64("swap_lamports"), publicKey("swap_token_mint"), uint64("swap_tokens"), BufferLayout.u8("physical"), ]); // mcswap-nft this.NFT_TREASURY = "7y1PdbvkkCuSYAE1zKeDrfv81Wfcuhi3ukHYPZsUoTiE"; this.NFT_MCSWAP_PROGRAM = "34dUBGrhkvjGDPSuH3zgtpBdBwZ6QSag8JpvZAnXmXTR"; this.NFT_STATIC_ALT = "BT4AUPXSxvbDrzSt3LLkE3Jd5s8R3fBSxJuyicyEMYH3"; this.NFT_PROGRAM_STATE = BufferLayout.struct([ BufferLayout.u8("is_initialized"), uint64("fee_lamports"), publicKey("dev_treasury"), ]); this.NFT_SWAP_STATE = BufferLayout.struct([ BufferLayout.u8("is_initialized"), uint64("utime"), BufferLayout.u8("is_swap"), publicKey("initializer"), publicKey("initializer_mint"), publicKey("temp_mint_account"), publicKey("taker"), publicKey("swap_mint"), uint64("swap_lamports"), publicKey("swap_token_mint"), uint64("swap_tokens"), BufferLayout.u8("physical"), ]); // mcswap-pnft this.PNFT_TREASURY = "FpStfD3eZaHzdsbpyYnXPLYoCnDVbmQBDxim7kvmf5R"; this.PNFT_MCSWAP_PROGRAM = "6aGKsKBA9zRbBZ2xKof94JEFf73vQg6kTWkB6gqtgfFm"; this.PNFT_STATIC_ALT = "F33TuQuCtiSpTjsCv4h51E2q48Wt5tyr469Lxb4Mgazu"; this.PNFT_ATA_PROGRAM = "ATokenGPvbdGVxr1b2hvZbsiqW5xWH25efTNsLJA8knL"; this.PNFT_RULES_PROGRAM = "auth9SigNpDKz4sJJ1DfCTuZrZNSAgh9sFD3rboVmgg"; this.PNFT_RULES_ACCT = "eBJLFYPxJmMGKuFwpDWkzxZeUrad92kZRC5BJLpzyT9"; this.PNFT_METADATA_PROGRAM = "metaqbxxUerdq28cj1RbAWkYQm3ybzjb6a8bt518x1s"; this.PNFT_PROGRAM_STATE = BufferLayout.struct([ BufferLayout.u8("is_initialized"), uint64("fee_lamports"), publicKey("dev_treasury"), ]); this.PNFT_SWAP_STATE = BufferLayout.struct([ BufferLayout.u8("is_initialized"), uint64("utime"), BufferLayout.u8("is_swap"), publicKey("initializer"), publicKey("initializer_mint"), publicKey("taker"), publicKey("swap_mint"), uint64("swap_lamports"), publicKey("swap_token_mint"), uint64("swap_tokens"), BufferLayout.u8("physical"), ]); // mcswap-cnft this.CNFT_TREASURY = "H6WH84cmQGbXPwMi4fsv9bPbwWjWJuyy2iviEDZS9SmL"; this.CNFT_MCSWAP_PROGRAM = "GyQWcNNXnU2qhTry6f8CBv4M7vjV4Jab5nojvgAMQdjg"; this.CNFT_STATIC_ALT = "6rztYc8onxK3FUku97XJrzvdZHqWavwx5xw8fB7QufCA"; this.CNFT_BUBBLEGUM_PROGRAM = "BGUMAp9Gq7iTEuizy4pqaxsTyUCBK68MDfK752saRPUY"; this.CNFT_PROGRAM_STATE = BufferLayout.struct([ BufferLayout.u8("is_initialized"), uint64("fee_lamports"), publicKey("dev_treasury"), ]); this.CNFT_SWAP_STATE = BufferLayout.struct([ BufferLayout.u8("is_initialized"), uint64("utime"), BufferLayout.u8("is_swap"), publicKey("initializer"), publicKey("delegate"), publicKey("asset_id"), publicKey("merkle_tree"), publicKey("root"), publicKey("data_hash"), publicKey("creator_hash"), uint64("nonce"), publicKey("swap_asset_id"), publicKey("swap_merkle_tree"), publicKey("swap_root"), publicKey("swap_data_hash"), publicKey("swap_creator_hash"), uint64("swap_nonce"), publicKey("swap_leaf_owner"), publicKey("swap_delegate"), uint64("swap_lamports"), publicKey("swap_token_mint"), uint64("swap_tokens"), BufferLayout.u8("physical"), ]); } // mcswap-spl async splCreate(_data_){ try{ // *************************************************************************** if(typeof _data_.memo=="undefined"){_data_.memo=false;} if(typeof _data_.sellerEmail=="undefined"||_data_.sellerEmail==false){_data_.sellerEmail="";} if(typeof _data_.physical=="undefined"||_data_.physical==false){_data_.physical=0;}else{_data_.physical=parseInt(_data_.physical);} if(typeof _data_.builder!="undefined"&&_data_.builder==false){_data_.builder=false;}else{_data_.builder=true;} if(typeof _data_.convert!="undefined"&&_data_.convert===true){ if(typeof _data_.token1Amount!="undefined"&&_data_.token1Amount>0){ const amount_1 = await this.convert({"rpc":_data_.rpc,"amount":_data_.token1Amount,"mint":_data_.token1Mint}); _data_.token1Amount = parseInt(amount_1.data); } if(typeof _data_.token2Amount!="undefined"&&_data_.token2Amount>0){ const amount_2 = await this.convert({"rpc":_data_.rpc,"amount":_data_.token2Amount,"mint":_data_.token2Mint}); _data_.token2Amount = parseInt(amount_2.data); } if(typeof _data_.token3Amount!="undefined"&&_data_.token3Amount>0){ const amount_3 = await this.convert({"rpc":_data_.rpc,"amount":_data_.token3Amount,"mint":_data_.token3Mint}); _data_.token3Amount = parseInt(amount_3.data); } if(typeof _data_.token4Amount!="undefined"&&_data_.token4Amount>0){ const amount_4 = await this.convert({"rpc":_data_.rpc,"amount":_data_.token4Amount,"mint":_data_.token4Mint}); _data_.token4Amount = parseInt(amount_4.data); } if(typeof _data_.affiliateFee!="undefined"&&_data_.affiliateFee>0){ const aflFee = await this.convert({"rpc":_data_.rpc,"amount":_data_.affiliateFee,"mint":"So11111111111111111111111111111111111111112"}); _data_.affiliateFee = parseInt(aflFee.data); } } if(typeof _data_.priority=="undefined"||_data_.priority===false){_data_.priority=this.PRIORITY;} if(typeof _data_.signers=="undefined"||_data_.signers==false){_data_.signers=false;} let affiliateWallet = this.SPL_TREASURY; let affiliateFee = 0; if(typeof _data_.affiliateWallet != "undefined" && _data_.affiliateWallet != false && typeof _data_.affiliateFee != "undefined" && _data_.affiliateFee != false && _data_.affiliateFee > 0){ affiliateWallet = _data_.affiliateWallet; affiliateFee = _data_.affiliateFee; } if(typeof _data_.buyer=="undefined"||_data_.buyer==false){_data_.buyer="11111111111111111111111111111111";} if(_data_.seller==_data_.buyer){const _error_={};_error_.status="error";_error_.message="buyer seller wallet conflict";return _error_;} // *************************************************************************** const connection = new Connection(_data_.rpc,"confirmed"); _data_ = await this.sns(connection, _data_); // *************************************************************************** const seller = new PublicKey(_data_.seller); const buyer = new PublicKey(_data_.buyer); // *************************************************************************** let meta_data = null; let response = null; let accountInfo = null; let instructions = null; let token1Mint = "11111111111111111111111111111111"; let token2Mint = "11111111111111111111111111111111"; let token3Mint = "11111111111111111111111111111111"; let token4Mint = "11111111111111111111111111111111"; let token1Amount = 0; let token2Amount = 0; let token3Amount = 0; let token4Amount = 0; // *************************************************************************** const minimumRent = await connection.getMinimumBalanceForRentExemption(0); let rentError = false; if(_data_.token1Mint=="So11111111111111111111111111111111111111112" && _data_.token1Amount < minimumRent){rentError=true;} else if(_data_.token2Mint=="So11111111111111111111111111111111111111112" && _data_.token2Amount < minimumRent){rentError=true;} if(rentError===true){ const _error_ = {} _error_.status="error"; _error_.message="A minimum of "+(minimumRent/1000000000)+" is required."; return _error_; } // *************************************************************************** token1Mint=_data_.token1Mint; token1Amount=_data_.token1Amount; if(typeof _data_.token2Mint!="undefined" && _data_.token2Mint!=false){ token2Mint=_data_.token2Mint; token2Amount=_data_.token2Amount; } token3Mint=_data_.token3Mint; token3Amount=_data_.token3Amount; if(typeof _data_.token4Mint!="undefined" && _data_.token4Mint!=false){ token4Mint=_data_.token4Mint; token4Amount=_data_.token4Amount; } // *************************************************************************** meta_data = null; let is_22_1 = false; let SPL_PROGRAM_1 = splToken.TOKEN_PROGRAM_ID; if(token1Mint!="11111111111111111111111111111111" && token1Mint!="So11111111111111111111111111111111111111112"){ response = await fetch(_data_.rpc,{method:'POST',headers:{"Content-Type":"application/json"}, body:JSON.stringify({"jsonrpc":"2.0","id":"getAsset: "+token1Mint,"method":"getAsset","params":{"id":token1Mint}})}); meta_data = await response.json(); if(typeof meta_data.result.mint_extensions!="undefined"){ SPL_PROGRAM_1 = splToken.TOKEN_2022_PROGRAM_ID; is_22_1 = true; } } meta_data = null; let is_22_2 = false; let SPL_PROGRAM_2 = splToken.TOKEN_PROGRAM_ID; if(token2Mint!="11111111111111111111111111111111" && token2Mint!="So11111111111111111111111111111111111111112"){ response = await fetch(_data_.rpc,{method:'POST',headers:{"Content-Type":"application/json"}, body:JSON.stringify({"jsonrpc":"2.0","id":"token2Mint","method":"getAsset","params":{"id":token2Mint}})}); meta_data = await response.json(); if(typeof meta_data.result.mint_extensions!="undefined"){ SPL_PROGRAM_2 = splToken.TOKEN_2022_PROGRAM_ID; is_22_2 = true; } } meta_data = null; let is_22_3 = false; let SPL_PROGRAM_3 = splToken.TOKEN_PROGRAM_ID; if(token3Mint!="11111111111111111111111111111111" && token3Mint!="So11111111111111111111111111111111111111112"){ response = await fetch(_data_.rpc,{method:'POST',headers:{"Content-Type":"application/json"}, body:JSON.stringify({"jsonrpc":"2.0","id":"text","method":"getAsset","params":{"id":token3Mint}})}); meta_data = await response.json(); if(typeof meta_data.result.mint_extensions!="undefined"){ SPL_PROGRAM_3 = splToken.TOKEN_2022_PROGRAM_ID; is_22_3 = true; } } meta_data = null; let is_22_4 = false; let SPL_PROGRAM_4 = splToken.TOKEN_PROGRAM_ID; if(token4Mint!="11111111111111111111111111111111" && token4Mint!="So11111111111111111111111111111111111111112"){ response = await fetch(_data_.rpc,{method:'POST',headers:{"Content-Type":"application/json"}, body:JSON.stringify({"jsonrpc":"2.0","id":"text","method":"getAsset","params":{"id":token4Mint}})}); meta_data = await response.json(); if(typeof meta_data.result.mint_extensions!="undefined"){ SPL_PROGRAM_4 = splToken.TOKEN_2022_PROGRAM_ID; is_22_4 = true; } } // *************************************************************************** const programStatePDA = PublicKey.findProgramAddressSync([Buffer.from("program-state")],new PublicKey(this.SPL_MCSWAP_PROGRAM)); let programState = null; programState = await connection.getAccountInfo(programStatePDA[0]).catch(function(err){const _error_={};_error_.status="error";_error_.message=err;return _error_;}); let devTreasury = null; let swapVaultPDA = null; if(programState != null){ const encodedProgramStateData = programState.data; const decodedProgramStateData = this.SPL_PROGRAM_STATE.decode(encodedProgramStateData); devTreasury = new PublicKey(decodedProgramStateData.dev_treasury); swapVaultPDA = new PublicKey(decodedProgramStateData.swap_vault_pda); } else{const _error_={};_error_.status="error";_error_.message="Program State Not Initialized!";return _error_;} // *************************************************************************** let token1Setting = false; let extensionTypes_1 = []; let transferFeeBasisPoints_1 = null; let tempToken1 = new PublicKey("11111111111111111111111111111111"); let tempToken1Account = new PublicKey("11111111111111111111111111111111"); let providerToken1ATA = new PublicKey("11111111111111111111111111111111"); if(token1Amount > 0){ tempToken1Account = new Keypair(); tempToken1 = tempToken1Account.publicKey; token1Setting = true; if(token1Mint != "So11111111111111111111111111111111111111112"){ providerToken1ATA = await splToken.getAssociatedTokenAddress(new PublicKey(token1Mint),seller,false,SPL_PROGRAM_1,splToken.ASSOCIATED_TOKEN_PROGRAM_ID,); const mintAccountInfo_1 = await splToken.getMint(connection,new PublicKey(token1Mint),"confirmed",SPL_PROGRAM_1); if(is_22_1===true){ extensionTypes_1 = splToken.getExtensionTypes(mintAccountInfo_1.tlvData); if(extensionTypes_1.includes(1)){ const extensionTransferFeeConfig = splToken.getExtensionData(splToken.ExtensionType.TransferFeeConfig,mintAccountInfo_1.tlvData); const data_1 = splToken.TransferFeeConfigLayout.decode(extensionTransferFeeConfig); transferFeeBasisPoints_1 = data_1.newerTransferFee.transferFeeBasisPoints; } } } } const swapStatePDA = PublicKey.findProgramAddressSync([Buffer.from("swap-state"),tempToken1.toBytes()],new PublicKey(this.SPL_MCSWAP_PROGRAM)); // *************************************************************************** let token2Setting = false; let extensionTypes_2 = []; let transferFeeBasisPoints_2 = null; let tempToken2 = new PublicKey("11111111111111111111111111111111"); let tempToken2Account = new PublicKey("11111111111111111111111111111111"); let providerToken2ATA = new PublicKey("11111111111111111111111111111111"); if(token2Amount > 0){ tempToken2Account = new Keypair(); tempToken2 = tempToken2Account.publicKey; token2Setting = true; if(token2Mint != "So11111111111111111111111111111111111111112"){ providerToken2ATA = await splToken.getAssociatedTokenAddress(new PublicKey(token2Mint),seller,false,SPL_PROGRAM_2,splToken.ASSOCIATED_TOKEN_PROGRAM_ID,); const mintAccountInfo_2 = await splToken.getMint(connection,new PublicKey(token2Mint),"confirmed",SPL_PROGRAM_2); if(is_22_2===true){ extensionTypes_2 = splToken.getExtensionTypes(mintAccountInfo_2.tlvData); if(extensionTypes_2.includes(1)){ const extensionTransferFeeConfig = splToken.getExtensionData(splToken.ExtensionType.TransferFeeConfig,mintAccountInfo_2.tlvData); const data_2 = splToken.TransferFeeConfigLayout.decode(extensionTransferFeeConfig); transferFeeBasisPoints_2 = data_2.newerTransferFee.transferFeeBasisPoints; } } } } // *************************************************************************** accountInfo = null; let createToken3ATA = false; let createToken3ATAIx = null; let token3ATA = null; if(token3Mint != "So11111111111111111111111111111111111111112" && token3Mint != "11111111111111111111111111111111"){ token3ATA = await splToken.getAssociatedTokenAddress(new PublicKey(token3Mint),seller,false,SPL_PROGRAM_3,splToken.ASSOCIATED_TOKEN_PROGRAM_ID,); accountInfo = await connection.getAccountInfo(token3ATA); if (accountInfo == null) { createToken3ATA = true; createToken3ATAIx = splToken.createAssociatedTokenAccountInstruction(seller,token3ATA,seller,new PublicKey(token3Mint),SPL_PROGRAM_3,splToken.ASSOCIATED_TOKEN_PROGRAM_ID); } else { createToken3ATA = false; } } accountInfo = null; let createToken4ATA = false; let createToken4ATAIx = null; let token4ATA = null; if(token4Mint != "So11111111111111111111111111111111111111112" && token4Mint != "11111111111111111111111111111111"){ token4ATA = await splToken.getAssociatedTokenAddress(new PublicKey(token4Mint),seller,false,SPL_PROGRAM_4,splToken.ASSOCIATED_TOKEN_PROGRAM_ID); accountInfo = await connection.getAccountInfo(token4ATA); if (accountInfo == null) { createToken4ATA = true; createToken4ATAIx = splToken.createAssociatedTokenAccountInstruction(seller,token4ATA,seller,new PublicKey(token4Mint),SPL_PROGRAM_4,splToken.ASSOCIATED_TOKEN_PROGRAM_ID); } else { createToken4ATA = false; } } // *************************************************************************** const totalSize = 1 + 32 + 8 + 8 + 32 + 8 + 32 + 8 + 8 + 1; let uarray = new Uint8Array(totalSize); let counter = 0; uarray[counter++] = 0; // 0 = token_swap InitializeSwap instruction let arr; let takerb58 = bs58.decode(_data_.buyer); arr = Array.prototype.slice.call(Buffer.from(takerb58), 0); for (let i = 0; i < arr.length; i++) { uarray[counter++] = arr[i]; } let byteArray; let byte; if(extensionTypes_1.includes(1)){ let token1LessFee = token1Amount - (token1Amount * (transferFeeBasisPoints_1 / 100 / 100)); byteArray = [0, 0, 0, 0, 0, 0, 0, 0]; for (let index = 0; index < byteArray.length; index ++ ) { byte = token1LessFee & 0xff; byteArray [ index ] = byte; token1LessFee = (token1LessFee - byte) / 256 ; } } else{ byteArray = [0, 0, 0, 0, 0, 0, 0, 0]; for (let index = 0; index < byteArray.length; index ++ ) { byte = token1Amount & 0xff; byteArray [ index ] = byte; token1Amount = (token1Amount - byte) / 256 ; } } for (let i = 0; i < byteArray.length; i++) { uarray[counter++] = byteArray[i]; } if(extensionTypes_2.includes(1)){ let token2LessFee = token2Amount - (token2Amount * (transferFeeBasisPoints_2 / 100 / 100)); byteArray = [0, 0, 0, 0, 0, 0, 0, 0]; for (let index = 0; index < byteArray.length; index ++ ) { byte = token2LessFee & 0xff; byteArray [ index ] = byte; token2LessFee = (token2LessFee - byte) / 256 ; } } else{ byteArray = [0, 0, 0, 0, 0, 0, 0, 0]; for (let index = 0; index < byteArray.length; index ++ ) { byte = token2Amount & 0xff; byteArray [ index ] = byte; token2Amount = (token2Amount - byte) / 256 ; } } for (let i = 0; i < byteArray.length; i++) { uarray[counter++] = byteArray[i]; } const token3Mintb58 = bs58.decode(token3Mint); arr = Array.prototype.slice.call(Buffer.from(token3Mintb58), 0); for (let i = 0; i < arr.length; i++) { uarray[counter++] = arr[i]; } byteArray = [0, 0, 0, 0, 0, 0, 0, 0]; for (let index = 0; index < byteArray.length; index ++ ) { byte = token3Amount & 0xff; byteArray [ index ] = byte; token3Amount = (token3Amount - byte) / 256 ; } for (let i = 0; i < byteArray.length; i++) { uarray[counter++] = byteArray[i]; } const token4Mintb58 = bs58.decode(token4Mint); arr = Array.prototype.slice.call(Buffer.from(token4Mintb58), 0); for (let i = 0; i < arr.length; i++) { uarray[counter++] = arr[i]; } byteArray = [0, 0, 0, 0, 0, 0, 0, 0]; for (let index = 0; index < byteArray.length; index ++ ) { byte = token4Amount & 0xff; byteArray [ index ] = byte; token4Amount = (token4Amount - byte) / 256 ; } for (let i = 0; i < byteArray.length; i++) { uarray[counter++] = byteArray[i]; } byteArray = [0, 0, 0, 0, 0, 0, 0, 0]; for (let index = 0; index < byteArray.length; index ++ ) { byte = affiliateFee & 0xff; byteArray [ index ] = byte; affiliateFee = (affiliateFee - byte) / 256 ; } for (let i = 0; i < byteArray.length; i++) { uarray[counter++] = byteArray[i]; } uarray[counter++] = _data_.physical; // *************************************************************************** const keys = [ { pubkey: seller, isSigner: true, isWritable: true }, // 0 { pubkey: programStatePDA[0], isSigner: false, isWritable: false }, // 1 { pubkey: swapVaultPDA, isSigner: false, isWritable: false }, // 2 { pubkey: swapStatePDA[0], isSigner: false, isWritable: true }, // 3 { pubkey: new PublicKey(token1Mint), isSigner: false, isWritable: true }, // 4 { pubkey: tempToken1, isSigner: true, isWritable: true }, // 5 { pubkey: providerToken1ATA, isSigner: false, isWritable: true }, // 6 { pubkey: new PublicKey(token2Mint), isSigner: false, isWritable: true }, // 7 { pubkey: tempToken2, isSigner: token2Setting, isWritable: token2Setting }, // 8 { pubkey: providerToken2ATA, isSigner: false, isWritable: true }, // 9 { pubkey: SystemProgram.programId, isSigner: false, isWritable: false }, // 10 { pubkey: splToken.TOKEN_PROGRAM_ID, isSigner: false, isWritable: false }, // 11 { pubkey: splToken.TOKEN_2022_PROGRAM_ID, isSigner: false, isWritable: false }, // 12 { pubkey: devTreasury, isSigner: false, isWritable: true }, // 13 { pubkey: new PublicKey(affiliateWallet), isSigner: false, isWritable: true }, // 14 ]; const initializeSwapIx = new TransactionInstruction({programId:new PublicKey(this.SPL_MCSWAP_PROGRAM),data:Buffer.from(uarray),keys:keys}); // *************************************************************************** const lookupTable = new PublicKey(this.SPL_STATIC_ALT); const lookupTableAccount = await connection.getAddressLookupTable(lookupTable).then((res)=>res.value); if(!lookupTableAccount){ const _error_ = {} _error_.status="error"; _error_.message="Could not fetch ALT!"; return _error_; } // *************************************************************************** if(createToken3ATA==true && createToken4ATA==true){instructions=[createToken3ATAIx,createToken4ATAIx,initializeSwapIx];} else if(createToken3ATA==true){instructions=[createToken3ATAIx,initializeSwapIx];} else if(createToken4ATA==true){instructions=[createToken4ATAIx,initializeSwapIx];} else{instructions=[initializeSwapIx];} // *************************************************************************** const _tx_ = {}; if(typeof _data_.tolerance!="undefined"){ _tx_.tolerance = _data_.tolerance; } if(typeof _data_.blink!="undefined"&&_data_.blink===true){ _tx_.serialize = true; _tx_.encode = true; _tx_.compute = false; _tx_.fees = false; } else{ _tx_.serialize = false; _tx_.encode = false; _tx_.compute = true; _tx_.fees = true; } let signers; if(token2Setting===true){ signers = [tempToken1Account,tempToken2Account]; } else{ signers = [tempToken1Account]; } if(_data_.memo!=false){ _tx_.memo = _data_.memo; } _tx_.rpc = _data_.rpc; _tx_.account = _data_.seller; _tx_.instructions = instructions; _tx_.signers = signers; _tx_.table = lookupTableAccount; _tx_.priority = _data_.priority; if(_data_.builder==false){ return {status:"ok",message:"builder disabled",ix:instructions,table:lookupTableAccount,signers:signers,escrow:tempToken1}; } else{ const transaction = await this.tx(_tx_); if(transaction.status){ return transaction; } else{ return {status:"ok",escrow:swapStatePDA[0].toString(),tx:transaction}; } } // *************************************************************************** } catch(err){ const _error_ = {} _error_.status="error"; _error_.message=err; return _error_; } } async splCancel(_data_){ try{ // *************************************************************************** if(typeof _data_.builder!="undefined"&&_data_.builder==false){_data_.builder=false;}else{_data_.builder=true;} if(typeof _data_.priority=="undefined"||_data_.priority===false){_data_.priority=this.PRIORITY;} if(typeof _data_.signers=="undefined"||_data_.signers==false){_data_.signers=false;} if(typeof _data_.escrow=="undefined"||_data_.escrow==false){const _error_={};_error_.status="error";_error_.message="no escrow id provided";return _error_;} const connection = new Connection(_data_.rpc, "confirmed"); const programStatePDA = PublicKey.findProgramAddressSync([Buffer.from("program-state")],new PublicKey(this.SPL_MCSWAP_PROGRAM)); const swapVaultPDA = PublicKey.findProgramAddressSync([Buffer.from("swap-vault")],new PublicKey(this.SPL_MCSWAP_PROGRAM)); // *************************************************************************** const swapStatePDA = new PublicKey(_data_.escrow); const swapState = await connection.getAccountInfo(swapStatePDA).catch(function(){const _error_={};_error_.status="error";_error_.message="Contract Not Found!";return _error_;}); const encodedSwapStateData = swapState.data; const decodedSwapStateData = this.SPL_SWAP_STATE.decode(encodedSwapStateData); const seller = new PublicKey(decodedSwapStateData.initializer); const token1Mint = new PublicKey(decodedSwapStateData.token1_mint); const token1Amount = new BN(decodedSwapStateData.token1_amount, 10, "le").toString(); const tempToken1Account = new PublicKey(decodedSwapStateData.temp_token1_account); const token2Mint = new PublicKey(decodedSwapStateData.token2_mint); const token2Amount = new BN(decodedSwapStateData.token2_amount, 10, "le").toString(); const tempToken2Account = new PublicKey(decodedSwapStateData.temp_token2_account); // *************************************************************************** let response = null; let meta_data = null; let token1ATA = new PublicKey("11111111111111111111111111111111"); let SPL_PROGRAM_1 = splToken.TOKEN_PROGRAM_ID; if(token1Amount>0 && token1Mint.toString()!="So11111111111111111111111111111111111111112"){ response = await fetch(_data_.rpc,{method:'POST',headers:{"Content-Type":"application/json"}, body:JSON.stringify({"jsonrpc":"2.0","id":"text","method":"getAsset","params":{"id":token1Mint.toString()}})}); meta_data = await response.json(); if(typeof meta_data.result.mint_extensions!="undefined"){SPL_PROGRAM_1=splToken.TOKEN_2022_PROGRAM_ID;} token1ATA = await splToken.getAssociatedTokenAddress(token1Mint,seller,false,SPL_PROGRAM_1,splToken.ASSOCIATED_TOKEN_PROGRAM_ID); } let token2ATA = new PublicKey("11111111111111111111111111111111"); let SPL_PROGRAM_2 = splToken.TOKEN_PROGRAM_ID; if(token2Amount>0 && token2Mint.toString()!="So11111111111111111111111111111111111111112"){ response = await fetch(_data_.rpc,{method:'POST',headers:{"Content-Type":"application/json"}, body:JSON.stringify({"jsonrpc":"2.0","id":"text","method":"getAsset","params":{"id":token2Mint.toString()}})}); meta_data = await response.json(); if(typeof meta_data.result.mint_extensions!="undefined"){SPL_PROGRAM_2=splToken.TOKEN_2022_PROGRAM_ID;} token2ATA = await splToken.getAssociatedTokenAddress(token2Mint,seller,false,SPL_PROGRAM_2,splToken.ASSOCIATED_TOKEN_PROGRAM_ID); } // *************************************************************************** const totalSize = 1 + 32; let uarray=new Uint8Array(totalSize); let counter=0; uarray[counter++]=2; const keys = [ { pubkey: seller, isSigner: true, isWritable: true }, // 0 { pubkey: programStatePDA[0], isSigner: false, isWritable: false }, // 1 { pubkey: swapVaultPDA[0], isSigner: false, isWritable: false }, // 2 { pubkey: swapStatePDA, isSigner: false, isWritable: true }, // 3 { pubkey: token1Mint, isSigner: false, isWritable: true }, // 4 { pubkey: tempToken1Account, isSigner: false, isWritable: true }, // 5 { pubkey: token2Mint, isSigner: false, isWritable: true }, // 6 { pubkey: tempToken2Account, isSigner: false, isWritable: true }, // 7 { pubkey: token1ATA, isSigner: false, isWritable: true }, // 8 { pubkey: token2ATA, isSigner: false, isWritable: true }, // 9 { pubkey: splToken.TOKEN_PROGRAM_ID, isSigner: false, isWritable: false }, // 10 { pubkey: splToken.TOKEN_2022_PROGRAM_ID, isSigner: false, isWritable: false }, // 11 { pubkey: SystemProgram.programId, isSigner: false, isWritable: false }, // 12 ]; const reverseSwapIx = new TransactionInstruction({programId:new PublicKey(this.SPL_MCSWAP_PROGRAM),data:Buffer.from(uarray),keys:keys}); const instructions = [reverseSwapIx]; const lookupTable = new PublicKey(this.SPL_STATIC_ALT); const lookupTableAccount = await connection.getAddressLookupTable(lookupTable).then((res)=>res.value); if(!lookupTableAccount){const _error_={};_error_.status="error";_error_.message="Could not fetch ALT!";return _error_;} // *************************************************************************** const _tx_ = {}; if(typeof _data_.tolerance!="undefined"){ _tx_.tolerance = _data_.tolerance; } if(typeof _data_.blink!="undefined"&&_data_.blink===true){ _tx_.serialize = true; _tx_.encode = true; _tx_.compute = false; _tx_.fees = false; } else{ _tx_.serialize = false; _tx_.encode = false; _tx_.compute = true; _tx_.fees = true; } _tx_.rpc = _data_.rpc; _tx_.account = _data_.seller; _tx_.instructions = instructions; _tx_.signers = _data_.signers; _tx_.table = lookupTableAccount; _tx_.priority = _data_.priority; if(_data_.builder==false){ return {"status":"ok","message":"builder disabled","ix":instructions,"table":lookupTableAccount}; } else{ return await this.tx(_tx_); } // *************************************************************************** } catch(err){ const _error_ = {} _error_.status="error"; _error_.message=err; return _error_; } } async splExecute(_data_){ try{ // *************************************************************************** if(typeof _data_.memo=="undefined"){_data_.memo=false;} if(typeof _data_.builder!="undefined"&&_data_.builder==false){_data_.builder=false;}else{_data_.builder=true;} if(typeof _data_.convert!="undefined"&&_data_.convert===true){ if(typeof _data_.affiliateFee!="undefined"&&_data_.affiliateFee>0){ const aflFee = await this.convert({"rpc":_data_.rpc,"amount":_data_.affiliateFee,"mint":"So11111111111111111111111111111111111111112"}); _data_.affiliateFee = parseInt(aflFee.data); } } if(typeof _data_.priority=="undefined"||_data_.priority===false){_data_.priority=this.PRIORITY;} if(typeof _data_.signers=="undefined"||_data_.signers==false){_data_.signers=false;} let affiliateWallet = this.SPL_TREASURY; let affiliateFee = 0; if(typeof _data_.affiliateWallet != "undefined" && _data_.affiliateWallet != false && typeof _data_.affiliateFee != "undefined" && _data_.affiliateFee != false && _data_.affiliateFee > 0){ affiliateWallet = _data_.affiliateWallet; affiliateFee = _data_.affiliateFee; } if(typeof _data_.buyer=="undefined"||_data_.buyer==false){const _error_={};_error_.status="error";_error_.message="no buyer wallet provided";return _error_;} if(typeof _data_.escrow=="undefined"||_data_.escrow==false){const _error_={};_error_.status="error";_error_.message="no escrow id provided";return _error_;} // *************************************************************************** const connection = new Connection(_data_.rpc, "confirmed"); // *************************************************************************** const buyer = new PublicKey(_data_.buyer); let fee = null; let devTreasury = null; let programState = null; let swapVaultPDA = null; const programStatePDA = PublicKey.findProgramAddressSync([Buffer.from("program-state")],new PublicKey(this.SPL_MCSWAP_PROGRAM)); programState = await connection.getAccountInfo(programStatePDA[0]); if(programState!=null){ const encodedProgramStateData = programState.data; const decodedProgramStateData = this.SPL_PROGRAM_STATE.decode(encodedProgramStateData); fee = new BN(decodedProgramStateData.dev_lamports, 10, "le"); devTreasury = new PublicKey(decodedProgramStateData.dev_treasury); swapVaultPDA = new PublicKey(decodedProgramStateData.swap_vault_pda); } else{ const _error_ = {}; _error_.status="error"; _error_.message="Program State Not Initialized"; return _error_; } const swapStatePDA = new PublicKey(_data_.escrow); let swapState = null; swapState = await connection.getAccountInfo(swapStatePDA); // // *************************************************************************** let token1Mint = null; let token1Amount = null; let tempToken1Account = null; let token2Mint = null; let token2Amount = null; let tempToken2Account = null; let token3Mint = null; let token3Amount = null; let token4Mint = null; let token4Amount = null; let response = null; let meta_data = null; let spl_symbol = null; let spl_decimals = null; let spl_amount = null; let seller = null; if(swapState!=null){ const encodedSwapStateData = swapState.data; const decodedSwapStateData = this.SPL_SWAP_STATE.decode(encodedSwapStateData); seller = new PublicKey(decodedSwapStateData.initializer); token1Mint = new PublicKey(decodedSwapStateData.token1_mint); token1Amount = new BN(decodedSwapStateData.token1_amount, 10, "le"); tempToken1Account = new PublicKey(decodedSwapStateData.temp_token1_account); token2Mint = new PublicKey(decodedSwapStateData.token2_mint); token2Amount = new BN(decodedSwapStateData.token2_amount, 10, "le"); tempToken2Account = new PublicKey(decodedSwapStateData.temp_token2_account); token3Mint = new PublicKey(decodedSwapStateData.token3_mint); token3Amount = new BN(decodedSwapStateData.token3_amount, 10, "le"); token4Mint = new PublicKey(decodedSwapStateData.token4_mint); token4Amount = new BN(decodedSwapStateData.token4_amount, 10, "le"); } else{const _error_={};_error_.status="error";_error_.message="No Contract Found";return _error_;} // *************************************************************************** let token1ATA = new PublicKey("11111111111111111111111111111111"); let SPL_PROGRAM_1 = splToken.TOKEN_PROGRAM_ID; let account_1 = null; let createToken1ATA = null; let createToken1ATAIx = null; if(token1Amount>0 && token1Mint.toString()!="So11111111111111111111111111111111111111112"){ response = await fetch(_data_.rpc,{method:'POST',headers:{"Content-Type":"application/json"},body:JSON.stringify({"jsonrpc": "2.0","id": "text","method": "getAsset","params":{"id":token1Mint.toString()}})}); meta_data = await response.json(); spl_symbol = meta_data.result.token_info.symbol; spl_decimals = parseInt(meta_data.result.token_info.decimals); spl_amount = parseInt(token1Amount); if(typeof meta_data.result.mint_extensions!="undefined"){SPL_PROGRAM_1=splToken.TOKEN_2022_PROGRAM_ID;} token1ATA = await splToken.getAssociatedTokenAddress(token1Mint,buyer,false,SPL_PROGRAM_1,splToken.ASSOCIATED_TOKEN_PROGRAM_ID,); account_1 = await connection.getAccountInfo(token1ATA).catch(function(error){}); if(account_1==null){ createToken1ATA = true; createToken1ATAIx = splToken.createAssociatedTokenAccountInstruction(buyer,token1ATA,buyer,token1Mint,SPL_PROGRAM_1,splToken.ASSOCIATED_TOKEN_PROGRAM_ID,); }else{createToken1ATA=false;} }else{createToken1ATA=false;} // *************************************************************************** let token2ATA = new PublicKey("11111111111111111111111111111111"); let SPL_PROGRAM_2 = splToken.TOKEN_PROGRAM_ID; let account_2 = null; let createToken2ATA = null; let createToken2ATAIx = null; if(token2Amount>0 && token2Mint.toString()!="So11111111111111111111111111111111111111112"){ response = await fetch(_data_.rpc,{method:'POST',headers:{"Content-Type":"application/json"},body:JSON.stringify({"jsonrpc": "2.0","id": "text","method": "getAsset","params":{"id":token2Mint.toString()}})}); meta_data = await response.json(); spl_symbol = meta_data.result.token_info.symbol; spl_decimals = parseInt(meta_data.result.token_info.decimals); spl_amount = parseInt(token2Amount); if(typeof meta_data.result.mint_extensions!="undefined"){SPL_PROGRAM_2=splToken.TOKEN_2022_PROGRAM_ID;} token2ATA = await splToken.getAssociatedTokenAddress(token2Mint,buyer,false,SPL_PROGRAM_2,splToken.ASSOCIATED_TOKEN_PROGRAM_ID,); account_2 = await connection.getAccountInfo(token2ATA).catch(function(error){}); if(account_2==null){ createToken2ATA = true; createToken2ATAIx = splToken.createAssociatedTokenAccountInstruction(buyer,token2ATA,buyer,token2Mint,SPL_PROGRAM_2,splToken.ASSOCIATED_TOKEN_PROGRAM_ID,); }else{createToken2ATA=false;} }else{createToken2ATA=false;} // *************************************************************************** let buyerToken3ATA = new PublicKey("11111111111111111111111111111111"); let SPL_PROGRAM_3 = splToken.TOKEN_PROGRAM_ID; if(token3Amount>0){ if(token3Mint.toString()!="So11111111111111111111111111111111111111112"){ response = await fetch(_data_.rpc,{method:'POST',headers:{"Content-Type":"application/json"},body:JSON.stringify({"jsonrpc":"2.0","id":"text","method":"getAsset","params":{"id":token3Mint.toString()}})}); meta_data = await response.json(); spl_symbol = meta_data.result.token_info.symbol; spl_decimals = parseInt(meta_data.result.token_info.decimals); spl_amount = parseInt(token3Amount); if(typeof meta_data.result.mint_extensions!="undefined"){SPL_PROGRAM_3=splToken.TOKEN_2022_PROGRAM_ID;} buyerToken3ATA = await splToken.getAssociatedTokenAddress(token3Mint,buyer,false,SPL_PROGRAM_3,splToken.ASSOCIATED_TOKEN_PROGRAM_ID,); } } let token3ATA = new PublicKey("11111111111111111111111111111111"); if(token3Amount>0 && token3Mint.toString()!="So11111111111111111111111111111111111111112"){ token3ATA = await splToken.getAssociatedTokenAddress(token3Mint,seller,false,SPL_PROGRAM_3,splToken.ASSOCIATED_TOKEN_PROGRAM_ID,); } let buyerToken4ATA = new PublicKey("11111111111111111111111111111111"); let SPL_PROGRAM_4 = splToken.TOKEN_PROGRAM_ID; if(token4Amount>0){ if(token4Mint.toString()!="So11111111111111111111111111111111111111112"){ response = await fetch(_data_.rpc,{method:'POST',headers:{"Content-Type":"application/json"},body:JSON.stringify({"jsonrpc":"2.0","id":"text","method":"getAsset","params":{"id":token4Mint.toString()}})}); meta_data = await response.json(); spl_symbol = meta_data.result.token_info.symbol; spl_decimals = parseInt(meta_data.result.token_info.decimals); spl_amount = parseInt(token4Amount); if(typeof meta_data.result.mint_extensions!="undefined"){SPL_PROGRAM_4=splToken.TOKEN_2022_PROGRAM_ID;} buyerToken4ATA = await splToken.getAssociatedTokenAddress(token4Mint,buyer,false,SPL_PROGRAM_4,splToken.ASSOCIATED_TOKEN_PROGRAM_ID,); } } let token4ATA = new PublicKey("11111111111111111111111111111111"); if(token4Amount>0 && token4Mint.toString()!="So11111111111111111111111111111111111111112"){ token4ATA = await splToken.getAssociatedTokenAddress(token4Mint,seller,false,SPL_PROGRAM_4,splToken.ASSOCIATED_TOKEN_PROGRAM_ID,); } // *************************************************************************** const totalSize = 1 + 8; let uarray = new Uint8Array(totalSize); let counter = 0; uarray[counter++] = 1; // 1 = token_swap SwapNFTs instruction var byteArray = [0, 0, 0, 0, 0, 0, 0, 0]; for (let index = 0; index < byteArray.length; index ++ ) { let byte = affiliateFee & 0xff; byteArray [ index ] = byte; affiliateFee = (affiliateFee - byte) / 256 ; } for (let i = 0; i < byteArray.lengt