UNPKG

@secux/app-sol

Version:
18 lines (15 loc) 3.81 kB
"use strict"; /*! Copyright 2022 SecuX Technology Inc Copyright Chen Wei-En Copyright Wu Tsung-Yu Licensed under the Apache License, Version 2.0 (the License); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an AS IS BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */Object.defineProperty(exports,"__esModule",{value:!0}),exports.Action=void 0;const bs58_1=require("@secux/utility/lib/bs58"),app_sol_1=require("./app-sol"),instruction_1=require("./instruction"),interface_1=require("./interface"),utils_1=require("./utils"),ow_1=require("ow"),utility_1=require("@secux/utility"),logger=null===utility_1.Logger||void 0===utility_1.Logger?void 0:utility_1.Logger.child({id:"action"});function isAccount(str){try{return(0,ow_1.default)(str,interface_1.ow_address),!0}catch(error){}return!1}exports.Action=class{static transferToken(params){var _a,_b;(0,ow_1.default)(params,ow_1.default.object.partialShape({to:interface_1.ow_address,owner:interface_1.ow_address,mint:interface_1.ow_address,createAccount:ow_1.default.optional.boolean}));const publickey=bs58_1.Base58.decode(params.owner),_program=null!==(_a=params.program)&&void 0!==_a?_a:instruction_1.TokenInstruction.TOKEN_PROGRAM_ID,from=null!==(_b=params.from)&&void 0!==_b?_b:app_sol_1.SecuxSOL.addressConvert(publickey,{mintAccount:params.mint,program:_program});let ata=params.to;(0,utils_1.isOnCurve)(Buffer.from((0,utils_1.toPublickey)(params.to),"hex"))&&(ata=app_sol_1.SecuxSOL.addressConvert(bs58_1.Base58.decode(params.to),{mintAccount:params.mint,program:_program}));const instructions=[{type:interface_1.InstructionType.TransferTokenChecked,params:{from,to:ata,owner:params.owner,amount:params.amount,mint:params.mint,decimal:params.decimal,program:_program}}];return params.createAccount&&instructions.unshift({type:interface_1.InstructionType.CreateAssociatedAccount,params:{payer:params.owner,owner:params.to,mint:params.mint,program:_program}}),instructions}static stake(params){(0,ow_1.default)(params,ow_1.default.object.partialShape({owner:interface_1.ow_address,stake:ow_1.default.string.nonEmpty}));const instructions=[];let stake=params.stake;if(!isAccount(stake)){null==logger||logger.warn(`treat value of stake "${stake}" as seed`);const seed=params.stake,programId=bs58_1.Base58.encode(Buffer.from(instruction_1.StakeInstruction.programId,"hex"));stake=app_sol_1.SecuxSOL.addressConvert(bs58_1.Base58.decode(params.owner),{seed,programId}),instructions.push({type:interface_1.InstructionType.CreateAccountWithSeed,params:{payer:params.owner,seed,programId,lamports:params.lamports,space:params.space}},{type:interface_1.InstructionType.InitializeStake,params:{owner:params.owner,stake}})}return instructions.push({type:interface_1.InstructionType.Delegate,params:{owner:params.owner,stake,vote:params.vote}}),instructions}static unstake(params){(0,ow_1.default)(params,ow_1.default.object.partialShape({owner:interface_1.ow_address,stake:ow_1.default.string.nonEmpty}));let stake=params.stake;if(!isAccount(stake)){null==logger||logger.warn(`treat value of stake "${stake}" as seed`);const programId=bs58_1.Base58.encode(Buffer.from(instruction_1.StakeInstruction.programId,"hex"));stake=app_sol_1.SecuxSOL.addressConvert(bs58_1.Base58.decode(params.owner),{seed:stake,programId})}return[{type:interface_1.InstructionType.Deactivate,params:{owner:params.owner,stake}},{type:interface_1.InstructionType.Withdraw,params:{owner:params.owner,stake,lamports:params.lamports}}]}};