UNPKG

@sega-so/sega-sdk

Version:

An SDK for building applications on top of SEGA.

2 lines 11.6 kB
var me=Object.defineProperty,pe=Object.defineProperties;var de=Object.getOwnPropertyDescriptors;var q=Object.getOwnPropertySymbols;var fe=Object.prototype.hasOwnProperty,ge=Object.prototype.propertyIsEnumerable;var G=(n,e,t)=>e in n?me(n,e,{enumerable:!0,configurable:!0,writable:!0,value:t}):n[e]=t,m=(n,e)=>{for(var t in e||(e={}))fe.call(e,t)&&G(n,t,e[t]);if(q)for(var t of q(e))ge.call(e,t)&&G(n,t,e[t]);return n},f=(n,e)=>pe(n,de(e));import{Keypair as je,PublicKey as ce}from"@solana/web3.js";import He from"bn.js";import{PublicKey as rt}from"@solana/web3.js";import{get as j,set as he}from"lodash";var I=class{constructor(e){this.logLevel=e.logLevel!==void 0?e.logLevel:0,this.name=e.name}set level(e){this.logLevel=e}get time(){return Date.now().toString()}get moduleName(){return this.name}isLogLevel(e){return e<=this.logLevel}error(...e){return this.isLogLevel(0)?(console.error(this.time,this.name,"sdk logger error",...e),this):this}logWithError(...e){let t=e.map(r=>typeof r=="object"?JSON.stringify(r):r).join(", ");throw new Error(t)}warning(...e){return this.isLogLevel(1)?(console.warn(this.time,this.name,"sdk logger warning",...e),this):this}info(...e){return this.isLogLevel(2)?(console.info(this.time,this.name,"sdk logger info",...e),this):this}debug(...e){return this.isLogLevel(3)?(console.debug(this.time,this.name,"sdk logger debug",...e),this):this}},H={},ye={};function i(n){let e=j(H,n);if(!e){let t=j(ye,n);e=new I({name:n,logLevel:t}),he(H,n,e)}return e}import{MINT_SIZE as at,TOKEN_PROGRAM_ID as ut,getTransferFeeConfig as ct,unpackMint as lt}from"@solana/spl-token";var mt=i("Sega_accountInfo_util");import c from"bn.js";import ur from"decimal.js";import ke from"big.js";import Rt from"bn.js";import be from"toformat";var Te=be,h=Te;import B from"big.js";import Pe from"bn.js";import we from"decimal.js-light";import y from"bn.js";var z=9007199254740991;function a(n){let e=i("Sega_parseBigNumberish");if(n instanceof y)return n;if(typeof n=="string"){if(n.match(/^-?[0-9]+$/))return new y(n);e.logWithError(`invalid BigNumberish string: ${n}`)}return typeof n=="number"?(n%1&&e.logWithError(`BigNumberish number underflow: ${n}`),(n>=z||n<=-z)&&e.logWithError(`BigNumberish number overflow: ${n}`),new y(String(n))):typeof n=="bigint"?new y(n.toString()):(e.error(`invalid BigNumberish value: ${n}`),new y(0))}var S=i("module/fraction"),R=h(B),b=h(we),Se={0:b.ROUND_DOWN,1:b.ROUND_HALF_UP,2:b.ROUND_UP},Be={0:B.roundDown,1:B.roundHalfUp,2:B.roundUp},u=class n{constructor(e,t=new Pe(1)){this.numerator=a(e),this.denominator=a(t)}get quotient(){return this.numerator.div(this.denominator)}invert(){return new n(this.denominator,this.numerator)}add(e){let t=e instanceof n?e:new n(a(e));return this.denominator.eq(t.denominator)?new n(this.numerator.add(t.numerator),this.denominator):new n(this.numerator.mul(t.denominator).add(t.numerator.mul(this.denominator)),this.denominator.mul(t.denominator))}sub(e){let t=e instanceof n?e:new n(a(e));return this.denominator.eq(t.denominator)?new n(this.numerator.sub(t.numerator),this.denominator):new n(this.numerator.mul(t.denominator).sub(t.numerator.mul(this.denominator)),this.denominator.mul(t.denominator))}mul(e){let t=e instanceof n?e:new n(a(e));return new n(this.numerator.mul(t.numerator),this.denominator.mul(t.denominator))}div(e){let t=e instanceof n?e:new n(a(e));return new n(this.numerator.mul(t.denominator),this.denominator.mul(t.numerator))}toSignificant(e,t={groupSeparator:""},r=1){Number.isInteger(e)||S.logWithError(`${e} is not an integer.`),e<=0&&S.logWithError(`${e} is not positive.`),b.set({precision:e+1,rounding:Se[r]});let s=new b(this.numerator.toString()).div(this.denominator.toString()).toSignificantDigits(e);return s.toFormat(s.decimalPlaces(),t)}toFixed(e,t={groupSeparator:""},r=1){return Number.isInteger(e)||S.logWithError(`${e} is not an integer.`),e<0&&S.logWithError(`${e} is negative.`),R.DP=e,R.RM=Be[r]||1,new R(this.numerator.toString()).div(this.denominator.toString()).toFormat(e,t)}isZero(){return this.numerator.isZero()}};var Wt=i("Sega_amount"),vt=h(ke);import{PublicKey as Le}from"@solana/web3.js";import{TOKEN_PROGRAM_ID as Y}from"@solana/spl-token";var J={chainId:101,address:Le.default.toBase58(),programId:Y.toBase58(),decimals:9,symbol:"SOL",name:"solana",logoURI:"https://img-v1.raydium.io/icon/So11111111111111111111111111111111111111112.png",tags:[],priority:2,type:"sega",extensions:{coingeckoId:"solana"}},p={chainId:101,address:"So11111111111111111111111111111111111111112",programId:Y.toBase58(),decimals:9,symbol:"WSOL",name:"Wrapped SOL",logoURI:"https://img-v1.raydium.io/icon/So11111111111111111111111111111111111111112.png",tags:[],priority:2,type:"sega",extensions:{coingeckoId:"solana"}};import{PublicKey as M}from"@solana/web3.js";import{PublicKey as o,SystemProgram as Z,SYSVAR_RENT_PUBKEY as Ce}from"@solana/web3.js";import{TOKEN_PROGRAM_ID as Ie}from"@solana/spl-token";function D({pubkey:n,isSigner:e=!1,isWritable:t=!0}){return{pubkey:n,isWritable:t,isSigner:e}}var Jt=[D({pubkey:Ie,isWritable:!1}),D({pubkey:Z.programId,isWritable:!1}),D({pubkey:Ce,isWritable:!1})];function X({publicKey:n,transformSol:e}){let t=$(n.toString());if(t instanceof o)return e&&t.equals(T)?N:t;if(e&&t.toString()===T.toBase58())return N;if(typeof t=="string"){if(t===o.default.toBase58())return o.default;try{return new o(t)}catch(r){throw new Error("invalid public key")}}throw new Error("invalid public key")}function $(n){try{return new o(n)}catch(e){return n}}var Zt=new o("MemoSq4gqABAXKb96qnH8TysNcWxMyWCqXgDLGmfcHr"),Xt=new o("MemoSq4gqABAXKb96qnH8TysNcWxMyWCqXgDLGmfcHr"),$t=new o("SysvarRent111111111111111111111111111111111"),Qt=new o("SysvarC1ock11111111111111111111111111111111"),Ne=new o("metaqbxxUerdq28cj1RbAWkYQm3ybzjb6a8bt518x1s"),en=new o("Sysvar1nstructions1111111111111111111111111"),tn=Z.programId,nn=new o("4k3Dyjzvzp8eMZWUXbBCjEvwSkkk59S5iCNLY3QrkX6R"),rn=new o("Ea5SjE2Y6yvCeW5dYTn7PYMuW5ikXkvbGdcmSnXeaLjS"),on=new o("SRMuApVNdxXokk5GT7XD5cUUgXMBCoAz2LHeuAoKWRt"),sn=new o("EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v"),an=new o("Es9vMFrzaCERmJfrF4H2FYD4KCoNkY11McCe8BenwNYB"),un=new o("mSoLzYCxHdYgdzU16g5QSh3i5K3z3KZK7ytfqcJm7So"),cn=new o("7dHbWXmci3dT8UFYWYZweBLXgycu7Y3iL6trKn1Y7ARj"),ln=new o("USDH1SM1ojwWUga67PGrgFWUHibbjqMvuMaDkRJTgkX"),mn=new o("NRVwhjBQiUPYtfDT5zRBVJajzFQHaBUNtC7SNVvqRFa"),pn=new o("ANAxByE6G2WjFp7A4NqtWYXb3mgruyzZYg3spfxe6Lbo"),dn=new o("7vfCXTUXx5WJV5JADk17DUJ4ksgau7utNKj4b963voxs"),N=new o("So11111111111111111111111111111111111111112"),T=o.default;var k=class k{constructor({mint:e,decimals:t,symbol:r,name:s,skipMint:g=!1,isToken2022:d=!1}){if(e===T.toBase58()||e instanceof M&&T.equals(e)){this.decimals=p.decimals,this.symbol=p.symbol,this.name=p.name,this.mint=new M(p.address),this.isToken2022=!1;return}this.decimals=t,this.symbol=r||e.toString().substring(0,6),this.name=s||e.toString().substring(0,6),this.mint=g?M.default:X({publicKey:e}),this.isToken2022=d}equals(e){return this===e?!0:this.mint.equals(e.mint)}};k.WSOL=new k(f(m({},p),{mint:p.address}));var x=k;var A=class A{constructor({decimals:e,symbol:t="UNKNOWN",name:r="UNKNOWN"}){this.decimals=e,this.symbol=t,this.name=r}equals(e){return this===e}};A.SOL=new A(J);var E=A;import Re from"bn.js";var An=new u(new Re(100));var Dn=i("Sega_price");import{PublicKey as Jn}from"@solana/web3.js";import Xn from"bn.js";var De=new c(0),br=new c(1),Tr=new c(2),xr=new c(3),Pr=new c(5),Ae=new c(10),wr=new c(100),Sr=new c(1e3),Br=new c(1e4);import{PublicKey as _e}from"@solana/web3.js";import{TOKEN_PROGRAM_ID as Fe}from"@solana/spl-token";import{ComputeBudgetProgram as Mr,Keypair as Fr,PublicKey as Ee,Transaction as Kr,TransactionMessage as Wr,VersionedTransaction as vr}from"@solana/web3.js";import{TOKEN_PROGRAM_ID as jr}from"@solana/spl-token";var Hr=i("Sega_txUtil");function ne(n,e){let[t,r]=Ee.findProgramAddressSync(n,e);return{publicKey:t,nonce:r}}function re(n,e,t){return ne([n.toBuffer(),(t!=null?t:Fe).toBuffer(),e.toBuffer()],new _e("ATokenGPvbdGVxr1b2hvZbsiqW5xWH25efTNsLJA8knL"))}import{PublicKey as l}from"@solana/web3.js";var to=new l("SegazTQwbYWknDZkJ6j2Kgvm5gw3MrHGKtWstZdoNKZ"),no=new l("Ajk8d9bERSaFdeoT1d8JUVfaDayrYLnAdgeB47TFMUaG"),ro=new l("2HbjxVVKJ7Ct72Rcd8WK4VTqmwTXL5aAggkH1CHGFGmh"),Oe=new l("auetiVUVJkRxvNPLYEvVnz8UJSqQ1VgoA83PcicHVD1"),Ke=new l("GzqQwxPSmUZnTyCfX9YWqi32MJg43ujP786ZoacL559Y"),Ve=new l("GzqQwxPSmUZnTyCfX9YWqi32MJg43ujP786ZoacL559Y");var oo={SERUM_MARKET:l.default,UTIL1216:l.default,CREATE_CPMM_POOL_PROGRAM:Oe,CREATE_CPMM_POOL_AUTH:Ke,CREATE_CPMM_POOL_FEE_ACC:Ve};import ao from"bn.js";import{PublicKey as fo,AddressLookupTableAccount as go}from"@solana/web3.js";import{PublicKey as Do,sendAndConfirmTransaction as Mo,SystemProgram as Fo,Transaction as Oo,TransactionMessage as Vo,VersionedTransaction as Wo}from"@solana/web3.js";import Uo from"axios";import{Connection as Zo}from"@solana/web3.js";import{TOKEN_PROGRAM_ID as ze}from"@solana/spl-token";import{sha256 as Ye}from"@noble/hashes/sha256";import{PublicKey as Ge}from"@solana/web3.js";import ie,{isBN as ys}from"bn.js";import{bits as bi,BitStructure as Ti,blob as We,Blob as xi,cstr as Pi,f32 as wi,f32be as Si,f64 as Bi,f64be as ki,greedy as Ai,Layout as ve,ns64 as Li,ns64be as Ci,nu64 as Ii,nu64be as Ni,offset as Ri,s16 as Di,s16be as Mi,s24 as Ei,s24be as _i,s32 as Fi,s32be as Oi,s40 as Ki,s40be as Vi,s48 as Wi,s48be as vi,s8 as Ui,seq as qi,struct as Gi,Structure as Ue,u16 as ji,u16be as Hi,u24 as zi,u24be as Yi,u32 as Ji,u32be as Zi,u40 as Xi,u40be as $i,u48 as Qi,u48be as es,u8 as ts,UInt as qe,union as ns,Union as rs,unionLayoutDiscriminator as os,utf8 as is}from"@solana/buffer-layout";var _=ve,oe=Ue;var F=qe;var O=We;var K=class extends _{constructor(e,t,r){super(e,r),this.blob=O(e),this.signed=t}decode(e,t=0){let r=new ie(this.blob.decode(e,t),10,"le");return this.signed?r.fromTwos(this.span*8).clone():r}encode(e,t,r=0){return typeof e=="number"&&(e=new ie(e)),this.signed&&(e=e.toTwos(this.span*8)),this.blob.encode(e.toArrayLike(Buffer,"le",this.span),t,r)}};function se(n){return new F(1,n)}function L(n){return new F(4,n)}function C(n){return new K(8,!1,n)}var V=class extends _{constructor(e,t,r,s){super(e.span,s),this.layout=e,this.decoder=t,this.encoder=r}decode(e,t){return this.decoder(this.layout.decode(e,t))}encode(e,t,r){return this.layout.encode(this.encoder(e),t,r)}getSpan(e,t){return this.layout.getSpan(e,t)}};function P(n){return new V(O(32),e=>new Ge(e),e=>e.toBuffer(),n)}var W=class extends oe{decode(e,t){return super.decode(e,t)}};function ae(n,e,t){return new W(n,e,t)}var ue=ae([P("mint"),P("owner"),C("amount"),L("delegateOption"),P("delegate"),se("state"),L("isNativeOption"),C("isNative"),C("delegatedAmount"),L("closeAuthorityOption"),P("closeAuthority")]);var Ms=i("Sega_Util");function Es({owner:n,solAccountResp:e,tokenAccountResp:t}){let r=[],s=[];for(let{pubkey:g,account:d}of t.value){let v=ue.decode(d.data),{mint:U,amount:le}=v;r.push({publicKey:g,mint:U,amount:le,isAssociated:re(n,U,d.owner).publicKey.equals(g),isNative:!1,programId:d.owner}),s.push({pubkey:g,accountInfo:v,programId:d.owner})}return e&&r.push({mint:ce.default,amount:new He(String(e.lamports)),isNative:!0,programId:e.owner}),{tokenAccounts:r,tokenAccountRawInfos:s}}function _s({fromPublicKey:n,programId:e=ze,assignSeed:t}){let r=t?btoa(t).slice(0,32):je.generate().publicKey.toBase58().slice(0,32);return{publicKey:Je(n,r,e),seed:r}}function Je(n,e,t){let r=Buffer.concat([n.toBuffer(),Buffer.from(e),t.toBuffer()]),s=Ye(r);return new ce(s)}export{_s as generatePubKey,Es as parseTokenAccountResp}; //# sourceMappingURL=util.mjs.map