solana-presale-sdk
Version:
SDK for Solana Presale Program
2 lines (1 loc) • 15.2 kB
JavaScript
import*as s from"@coral-xyz/anchor";import{AnchorProvider as E}from"@coral-xyz/anchor";import{PublicKey as F,SystemProgram as d,SYSVAR_RENT_PUBKEY as k}from"@solana/web3.js";import{ASSOCIATED_TOKEN_PROGRAM_ID as h,getAssociatedTokenAddressSync as p,TOKEN_PROGRAM_ID as b}from"@solana/spl-token";import l from"moment";import{PublicKey as A}from"@solana/web3.js";import{PublicKey as w}from"@solana/web3.js";var K="4NiRZQQ5Rgsoy2zZ7SWAJAvbCuimUH1nY1nwCFKnfPtz",N="Ah1hf7NZgBhgnhFsrXLoj7czMMiVwUaCHnc5bP9wB6Ge",B="94N4YzP2ihmdXNe3SgXJiBjymyBrS73VSz6QwX5QPSor",g=new w(K),W=new w(N),v=new w(B),P={lamports:9};import*as I from"@coral-xyz/anchor";var Q=r=>r.slice(0,4)+"..."+r.slice(-5),c=async()=>{let[r,e]=A.findProgramAddressSync([Buffer.from("presale"),v.toBuffer()],g);return{presaleAddress:r,bump:e}},_=async()=>{let r=Buffer.from("vault"),[e,t]=A.findProgramAddressSync([r],g);return{presaleVault:e,bump:t}},f=async r=>{let[e]=A.findProgramAddressSync([Buffer.from("user"),r.toBuffer()],g);return e};function m(r){let e=D(r,P.lamports);return new I.BN(e.toString())}function Z(r){return M(r,P.lamports)}function D(r,e){if(!/^(-?)([0-9]*)\.?([0-9]*)$/.test(r))throw new Error("Invalid decimal number");let[t,a="0"]=r.split("."),n=t.startsWith("-");return n&&(t=t.slice(1)),a=a.padEnd(e,"0"),BigInt(`${n?"-":""}${t}${a}`)}function M(r,e){let t=r.toString(),a=t.startsWith("-");a&&(t=t.slice(1)),t=t.padStart(e,"0");let n=t.slice(0,t.length-e)||"0",o=t.slice(t.length-e).replace(/(0+)$/,"");return`${a?"-":""}${n}${o?`.${o}`:""}`}var x={address:"4NiRZQQ5Rgsoy2zZ7SWAJAvbCuimUH1nY1nwCFKnfPtz",metadata:{name:"terminus_presale",version:"0.1.0",spec:"0.1.0",description:"Created with Anchor"},instructions:[{name:"buy_token",discriminator:[138,127,14,91,38,87,115,105],accounts:[{name:"presale_info",writable:!0,pda:{seeds:[{kind:"const",value:[112,114,101,115,97,108,101]},{kind:"account",path:"presale_info.authority",account:"PresaleInfo"}]}},{name:"user_info",writable:!0,pda:{seeds:[{kind:"const",value:[117,115,101,114]},{kind:"account",path:"buyer"}]}},{name:"presale_vault",writable:!0,pda:{seeds:[{kind:"const",value:[118,97,117,108,116]}]}},{name:"buyer",writable:!0,signer:!0},{name:"rent",address:"SysvarRent111111111111111111111111111111111"},{name:"system_program",address:"11111111111111111111111111111111"},{name:"token_program",address:"TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA"},{name:"associated_token_program",address:"ATokenGPvbdGVxr1b2hvZbsiqW5xWH25efTNsLJA8knL"}],args:[{name:"quote_amount",type:"u64"}]},{name:"claim_token",discriminator:[116,206,27,191,166,19,0,73],accounts:[{name:"token_mint",writable:!0},{name:"token_account",writable:!0,pda:{seeds:[{kind:"account",path:"buyer"},{kind:"const",value:[6,221,246,225,215,101,161,147,217,203,225,70,206,235,121,172,28,180,133,237,95,91,55,145,58,140,245,133,126,255,0,169]},{kind:"account",path:"token_mint"}],program:{kind:"const",value:[140,151,37,143,78,36,137,241,187,61,16,41,20,142,13,131,11,90,19,153,218,255,16,132,4,142,123,216,219,233,248,89]}}},{name:"presale_associated_token_account",writable:!0,pda:{seeds:[{kind:"account",path:"presale_info"},{kind:"const",value:[6,221,246,225,215,101,161,147,217,203,225,70,206,235,121,172,28,180,133,237,95,91,55,145,58,140,245,133,126,255,0,169]},{kind:"account",path:"token_mint"}],program:{kind:"const",value:[140,151,37,143,78,36,137,241,187,61,16,41,20,142,13,131,11,90,19,153,218,255,16,132,4,142,123,216,219,233,248,89]}}},{name:"user_info",writable:!0,pda:{seeds:[{kind:"const",value:[117,115,101,114]},{kind:"account",path:"buyer"}]}},{name:"presale_info",writable:!0,pda:{seeds:[{kind:"const",value:[112,114,101,115,97,108,101]},{kind:"account",path:"presale_info.authority",account:"PresaleInfo"}]}},{name:"buyer",writable:!0,signer:!0},{name:"rent",address:"SysvarRent111111111111111111111111111111111"},{name:"system_program",address:"11111111111111111111111111111111"},{name:"token_program",address:"TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA"},{name:"associated_token_program",address:"ATokenGPvbdGVxr1b2hvZbsiqW5xWH25efTNsLJA8knL"}],args:[{name:"bump",type:"u8"}]},{name:"create_presale",discriminator:[176,144,197,158,61,119,75,135],accounts:[{name:"presale_info",writable:!0,pda:{seeds:[{kind:"const",value:[112,114,101,115,97,108,101]},{kind:"account",path:"authority"}]}},{name:"authority",writable:!0,signer:!0},{name:"system_program",address:"11111111111111111111111111111111"}],args:[{name:"token_mint_address",type:"pubkey"},{name:"softcap_amount",type:"u64"},{name:"hardcap_amount",type:"u64"},{name:"max_token_amount_per_address",type:"u64"},{name:"price_per_token",type:"u64"},{name:"start_time",type:"u64"},{name:"end_time",type:"u64"}]},{name:"deposit_token",discriminator:[11,156,96,218,39,163,180,19],accounts:[{name:"mint_account",writable:!0},{name:"token_account",writable:!0,pda:{seeds:[{kind:"account",path:"admin"},{kind:"const",value:[6,221,246,225,215,101,161,147,217,203,225,70,206,235,121,172,28,180,133,237,95,91,55,145,58,140,245,133,126,255,0,169]},{kind:"account",path:"mint_account"}],program:{kind:"const",value:[140,151,37,143,78,36,137,241,187,61,16,41,20,142,13,131,11,90,19,153,218,255,16,132,4,142,123,216,219,233,248,89]}}},{name:"admin",writable:!0,signer:!0},{name:"to_associated_token_account",writable:!0,pda:{seeds:[{kind:"account",path:"presale_info"},{kind:"const",value:[6,221,246,225,215,101,161,147,217,203,225,70,206,235,121,172,28,180,133,237,95,91,55,145,58,140,245,133,126,255,0,169]},{kind:"account",path:"mint_account"}],program:{kind:"const",value:[140,151,37,143,78,36,137,241,187,61,16,41,20,142,13,131,11,90,19,153,218,255,16,132,4,142,123,216,219,233,248,89]}}},{name:"presale_vault",writable:!0,pda:{seeds:[{kind:"const",value:[118,97,117,108,116]}]}},{name:"presale_info",writable:!0,pda:{seeds:[{kind:"const",value:[112,114,101,115,97,108,101]},{kind:"account",path:"presale_info.authority",account:"PresaleInfo"}]}},{name:"rent",address:"SysvarRent111111111111111111111111111111111"},{name:"system_program",address:"11111111111111111111111111111111"},{name:"token_program",address:"TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA"},{name:"associated_token_program",address:"ATokenGPvbdGVxr1b2hvZbsiqW5xWH25efTNsLJA8knL"}],args:[{name:"amount",type:"u64"}]},{name:"start_presale",discriminator:[57,19,73,191,195,254,45,223],accounts:[{name:"presale_info",writable:!0,pda:{seeds:[{kind:"const",value:[112,114,101,115,97,108,101]},{kind:"account",path:"presale_info.authority",account:"PresaleInfo"}]}},{name:"admin",writable:!0,signer:!0}],args:[{name:"start_time",type:"u64"},{name:"end_time",type:"u64"}]},{name:"update_presale",discriminator:[9,223,20,184,183,199,90,226],accounts:[{name:"presale_info",writable:!0,pda:{seeds:[{kind:"const",value:[112,114,101,115,97,108,101]},{kind:"account",path:"presale_info.authority",account:"PresaleInfo"}]}},{name:"authority",writable:!0,signer:!0},{name:"system_program",address:"11111111111111111111111111111111"}],args:[{name:"max_token_amount_per_address",type:"u64"},{name:"price_per_token",type:"u64"},{name:"softcap_amount",type:"u64"},{name:"hardcap_amount",type:"u64"},{name:"start_time",type:"u64"},{name:"end_time",type:"u64"}]},{name:"withdraw_sol",discriminator:[145,131,74,136,65,137,42,38],accounts:[{name:"presale_info",pda:{seeds:[{kind:"const",value:[112,114,101,115,97,108,101]},{kind:"account",path:"presale_info.authority",account:"PresaleInfo"}]}},{name:"presale_vault",writable:!0,pda:{seeds:[{kind:"const",value:[118,97,117,108,116]}]}},{name:"admin",writable:!0,signer:!0},{name:"system_program",address:"11111111111111111111111111111111"}],args:[{name:"bump",type:"u8"}]},{name:"withdraw_token",discriminator:[136,235,181,5,101,109,57,81],accounts:[{name:"mint_account",writable:!0},{name:"admin_associated_token_account",writable:!0,pda:{seeds:[{kind:"account",path:"admin"},{kind:"const",value:[6,221,246,225,215,101,161,147,217,203,225,70,206,235,121,172,28,180,133,237,95,91,55,145,58,140,245,133,126,255,0,169]},{kind:"account",path:"presale_token_mint_account"}],program:{kind:"const",value:[140,151,37,143,78,36,137,241,187,61,16,41,20,142,13,131,11,90,19,153,218,255,16,132,4,142,123,216,219,233,248,89]}}},{name:"presale_associated_token_account",writable:!0,pda:{seeds:[{kind:"account",path:"presale_info"},{kind:"const",value:[6,221,246,225,215,101,161,147,217,203,225,70,206,235,121,172,28,180,133,237,95,91,55,145,58,140,245,133,126,255,0,169]},{kind:"account",path:"presale_token_mint_account"}],program:{kind:"const",value:[140,151,37,143,78,36,137,241,187,61,16,41,20,142,13,131,11,90,19,153,218,255,16,132,4,142,123,216,219,233,248,89]}}},{name:"presale_token_mint_account",writable:!0},{name:"presale_info",writable:!0,pda:{seeds:[{kind:"const",value:[112,114,101,115,97,108,101]},{kind:"account",path:"presale_info.authority",account:"PresaleInfo"}]}},{name:"admin",writable:!0,signer:!0},{name:"rent",address:"SysvarRent111111111111111111111111111111111"},{name:"system_program",address:"11111111111111111111111111111111"},{name:"token_program",address:"TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA"},{name:"associated_token_program",address:"ATokenGPvbdGVxr1b2hvZbsiqW5xWH25efTNsLJA8knL"}],args:[{name:"amount",type:"u64"},{name:"bump",type:"u8"}]}],accounts:[{name:"PresaleInfo",discriminator:[11,19,36,47,79,104,214,40]},{name:"UserInfo",discriminator:[83,134,200,56,144,56,10,62]}],errors:[{code:6e3,name:"Unauthorized",msg:"You are not authorized to perform this action."},{code:6001,name:"NotAllowed",msg:"Not allowed"},{code:6002,name:"MathOverflow",msg:"Math operation overflow"},{code:6003,name:"AlreadyMarked",msg:"Already marked"},{code:6004,name:"PresaleNotStarted",msg:"Presale not started yet"},{code:6005,name:"PresaleEnded",msg:"Presale already ended"},{code:6006,name:"TokenAmountMismatch",msg:"Token amount mismatch"},{code:6007,name:"InsufficientFund",msg:"Insufficient Tokens"},{code:6008,name:"ExceedMaximumBuy",msg:"Token purchase exceeds the maximum allowed per user!"},{code:6009,name:"PresaleNotEnded",msg:"Presale not ended yet"},{code:6010,name:"HardCapped",msg:"Presale already ended HardCapped"},{code:6011,name:"InvalidPrice",msg:"Price per token cannot be zero."},{code:6012,name:"InvalidSoftcap",msg:"Softcap amount must be greater than zero."},{code:6013,name:"InvalidHardcap",msg:"Hardcap must be greater than or equal to the softcap."},{code:6014,name:"InvalidTimeRange",msg:"Start time must be earlier than the end time."},{code:6015,name:"CalculationError",msg:"A calculation or division error occurred."},{code:6016,name:"EmptyVault",msg:"The vault is empty. No SOL to withdraw."}],types:[{name:"PresaleInfo",type:{kind:"struct",fields:[{name:"token_mint_address",type:"pubkey"},{name:"softcap_amount",type:"u64"},{name:"hardcap_amount",type:"u64"},{name:"deposit_token_amount",type:"u64"},{name:"sold_token_amount",type:"u64"},{name:"start_time",type:"u64"},{name:"end_time",type:"u64"},{name:"max_token_amount_per_address",type:"u64"},{name:"price_per_token",type:"u64"},{name:"is_live",type:"bool"},{name:"authority",type:"pubkey"},{name:"is_soft_capped",type:"bool"},{name:"is_hard_capped",type:"bool"}]}},{name:"UserInfo",type:{kind:"struct",fields:[{name:"buy_quote_amount",type:"u64"},{name:"buy_token_amount",type:"u64"},{name:"buy_time",type:"u64"},{name:"claim_time",type:"u64"}]}}],constants:[{name:"PRESALE_SEED",type:"bytes",value:"[80, 82, 69, 83, 65, 76, 69, 95, 83, 69, 69, 68]"}]};var S=class{constructor(e,t,a){this.connection=e,this.wallet=t;let n=new E(e,t,{commitment:"processed"});this.program=new s.Program(x,n)}async fetchPresaleInfo(){let e=await this.program.account.presaleInfo.all(),t=e[0].account,a=e[0].publicKey;return{presaleInfo:t,presaleInfoAddress:a}}async fetchUserInfo(e){let t=await f(e);return await this.program.account.userInfo.fetch(t)}async startPresale(e){let{presaleAddress:t}=await c(),a=new s.BN(l(e.startTime).unix()),n=new s.BN(l(e.endTime).unix());return await this.program.methods.startPresale(a,n).accounts({presaleInfo:t,authority:this.wallet.publicKey}).rpc()}async createPresale(e){let{presaleAddress:t}=await c(),a=new F(e.tokenMintAddress),n=m(e.softcapAmount.toString()),o=m(e.hardcapAmount.toString()),i=new s.BN(e.maxTokenAmountPerAddress),u=m(e.pricePerToken.toString()),y=new s.BN(l(e.startTime).unix()),T=new s.BN(l(e.endTime).unix());return await this.program.methods.createPresale(a,n,o,i,u,y,T).accounts({presaleInfo:t,authority:this.wallet.publicKey,systemProgram:d.programId}).rpc()}async updatePresale(e){let{presaleAddress:t}=await c(),a=m(e.softcapAmount.toString()),n=m(e.hardcapAmount.toString()),o=new s.BN(e.maxTokenAmountPerAddress),i=m(e.pricePerToken.toString()),u=new s.BN(l(e.startTime).unix()),y=new s.BN(l(e.endTime).unix());return await this.program.methods.updatePresale(o,i,a,n,u,y).accounts({presaleInfo:t,authority:this.wallet.publicKey,systemProgram:d.programId}).rpc()}async buyToken(e){let{presaleAddress:t}=await c(),a=await f(this.wallet.publicKey),{presaleVault:n}=await _(),o=m(e.amount.toString());return await this.program.methods.buyToken(o).accounts({presaleInfo:t,userInfo:a,presaleVault:n,buyer:this.wallet.publicKey,rent:k,systemProgram:d.programId,tokenProgram:b,associatedTokenProgram:h}).rpc()}async depositToken(e,t){let{presaleAddress:a}=await c(),{presaleVault:n}=await _(),o=await p(t.tokenMintAddress,this.wallet.publicKey),i=await p(t.tokenMintAddress,a,!0),u=m(e.amount.toString());return await this.program.methods.depositToken(u).accounts({mintAccount:t.tokenMintAddress,tokenAccount:o,admin:this.wallet.publicKey,toAssociatedTokenAccount:i,presaleVault:n,presaleInfo:a,rent:k,systemProgram:d.programId,tokenProgram:b,associatedTokenProgram:h}).rpc()}async withdrawTokens(e,t){let{presaleAddress:a,bump:n}=await c(),o=await p(t.tokenMintAddress,this.wallet.publicKey),i=await p(t.tokenMintAddress,a,!0),u=new s.BN(e.amount);return await this.program.methods.withdrawToken(u,new s.BN(n)).accounts({mintAccount:t.tokenMintAddress,presaleTokenMintAccount:t.tokenMintAddress,adminAssociatedTokenAccount:o,presaleAssociatedTokenAccount:i,presaleInfo:a,admin:this.wallet.publicKey,rent:k,systemProgram:d.programId,tokenProgram:b,associatedTokenProgram:h}).rpc()}async claimTokens(e){let{presaleAddress:t,bump:a}=await c(),n=await f(this.wallet.publicKey),o=await p(e.tokenMintAddress,this.wallet.publicKey),i=await p(e.tokenMintAddress,t,!0);return await this.program.methods.claimToken(a).accounts({tokenMint:e.tokenMintAddress,tokenAccount:o,presaleAssociatedTokenAccount:i,userInfo:n,presaleInfo:t,buyer:this.wallet.publicKey,rent:k,systemProgram:d.programId,tokenProgram:b,associatedTokenProgram:h}).rpc()}async withdrawSOL(){let{presaleAddress:e}=await c(),{presaleVault:t,bump:a}=await _();return await this.program.methods.withdrawSol(a).accounts({presaleInfo:e,presaleVault:t,admin:this.wallet.publicKey,rent:k,systemProgram:d.programId}).rpc()}};export{B as ADMIN_PUBLIC_KEY,v as AdminPublicKey,N as MINT_TOKEN_ADDRESS,K as PROGRAM_ID,S as PresaleClient,c as derivePresaleAddress,_ as derivePresaleVaultAddress,f as deriveUserInfoAddress,Z as formatSol,W as mintTokenPubKey,m as parseSol,g as programIdPubKey,Q as shortenAddress,P as solUnits};