UNPKG

@lucoadam/zebec-wormhole-sdk

Version:

This sdk can be use to transfer assets across chains and to interact with the Zebec's xchain bridge smart contracts for passing message from EVM chain to solana specially to utilize the features of Zebec Streaming and Zebec Multisig Streaming protocol.

259 lines (240 loc) 10.2 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.ZebecInstructions = void 0; var _anchor = require("@project-serum/anchor"); var _splToken = require("@solana/spl-token"); var _web = require("@solana/web3.js"); var _utils = require("../../../utils"); var _zebec__factory = require("./zebec__factory"); /** * Factory class for zebec instructions */ class ZebecInstructions { PREFIX = "withdraw_sol"; PREFIX_TOKEN = "withdraw_token"; OPERATE = "NewVaultOption"; OPERATEDATA = "NewVaultOptionData"; constructor(provider) { this._zebecProgram = _zebec__factory.Zebec__factory.getProgram(new _web.PublicKey(_utils.ZEBEC_ADDRESS), provider); } getZebecVaultAddressSync(address) { return _web.PublicKey.findProgramAddressSync([address.toBuffer()], this._zebecProgram.programId)[0]; } getWithdrawDataAddressSync(address, mint) { return mint ? _web.PublicKey.findProgramAddressSync([_anchor.utils.bytes.utf8.encode(this.PREFIX_TOKEN), address.toBuffer(), mint.toBytes()], this._zebecProgram.programId)[0] : _web.PublicKey.findProgramAddressSync([_anchor.utils.bytes.utf8.encode(this.PREFIX), address.toBuffer()], this._zebecProgram.programId)[0]; } getFeeVaultAddressSync(address) { return _web.PublicKey.findProgramAddressSync([address.toBuffer(), _anchor.utils.bytes.utf8.encode(this.OPERATE)], this._zebecProgram.programId)[0]; } getFeeVaultDataAddressSync(address, addressVault) { return _web.PublicKey.findProgramAddressSync([address.toBuffer(), _anchor.utils.bytes.utf8.encode(this.OPERATEDATA), addressVault.toBuffer()], this._zebecProgram.programId)[0]; } async getDepositTokenIxn(sourceAccount, mint, amount) { const sourceAccountTokenAccount = (0, _splToken.getAssociatedTokenAddressSync)(mint, sourceAccount, true); const zebecVault = this.getZebecVaultAddressSync(sourceAccount); const pdaAccountTokenAccount = (0, _splToken.getAssociatedTokenAddressSync)(mint, zebecVault, true); const instruction = await this._zebecProgram.methods.depositToken(amount).accounts({ associatedTokenProgram: _splToken.ASSOCIATED_TOKEN_PROGRAM_ID, mint, pdaAccountTokenAccount, rent: _web.SYSVAR_RENT_PUBKEY, sourceAccount, sourceAccountTokenAccount, systemProgram: _web.SystemProgram.programId, tokenProgram: _splToken.TOKEN_PROGRAM_ID, zebecVault }).instruction(); // logInstruction(instruction); return { instruction }; } async getWithdrawTokenIxn(sourceAccount, mint, amount) { const sourceAccountTokenAccount = (0, _splToken.getAssociatedTokenAddressSync)(mint, sourceAccount, true); const zebecVault = this.getZebecVaultAddressSync(sourceAccount); const pdaAccountTokenAccount = (0, _splToken.getAssociatedTokenAddressSync)(mint, zebecVault, true); const withdrawData = this.getWithdrawDataAddressSync(sourceAccount, mint); const instruction = await this._zebecProgram.methods.tokenWithdrawal(amount).accounts({ associatedTokenProgram: _splToken.ASSOCIATED_TOKEN_PROGRAM_ID, mint, pdaAccountTokenAccount, rent: _web.SYSVAR_RENT_PUBKEY, sourceAccount, sourceAccountTokenAccount, systemProgram: _web.SystemProgram.programId, tokenProgram: _splToken.TOKEN_PROGRAM_ID, withdrawData, zebecVault }).instruction(); // logInstruction(instruction); return { instruction }; } async getInitializeStreamIxn(sourceAccount, destAccount, mint, feeOwner, startTime, endTime, amount, canCancel, canUpdate) { const feeVault = this.getFeeVaultAddressSync(feeOwner); const feeVaultData = this.getFeeVaultDataAddressSync(feeOwner, feeVault); const withdrawData = this.getWithdrawDataAddressSync(sourceAccount, mint); const dataAccountKeypair = _web.Keypair.generate(); console.log("DATA ACCOUNT: ", dataAccountKeypair.publicKey.toBase58()); const instruction = await this._zebecProgram.methods.tokenStream(startTime, endTime, amount, canCancel, canUpdate).accounts({ dataAccount: dataAccountKeypair.publicKey, destAccount, feeOwner, feeVault, feeVaultData, mint, rent: _web.SYSVAR_RENT_PUBKEY, sourceAccount, systemProgram: _web.SystemProgram.programId, tokenProgram: _splToken.TOKEN_PROGRAM_ID, withdrawData }).instruction(); const preInstruction = await this._zebecProgram.account.streamToken.createInstruction(dataAccountKeypair, this._zebecProgram.account.streamToken.size); const signers = [dataAccountKeypair]; // logInstruction(instruction); return { instruction, preInstruction, signers }; } async getWithdrawTokenStreamIxn(sourceAccount, destAccount, dataAccount, mint, feeOwner) { const zebecVault = this.getZebecVaultAddressSync(sourceAccount); const pdaAccountTokenAccount = (0, _splToken.getAssociatedTokenAddressSync)(mint, zebecVault, true); const withdrawData = this.getWithdrawDataAddressSync(sourceAccount, mint); const destTokenAccount = (0, _splToken.getAssociatedTokenAddressSync)(mint, destAccount, true); const feeVault = this.getFeeVaultAddressSync(feeOwner); const feeReceiverTokenAccount = (0, _splToken.getAssociatedTokenAddressSync)(mint, feeVault, true); const feeVaultData = this.getFeeVaultDataAddressSync(feeOwner, feeVault); const instruction = await this._zebecProgram.methods.withdrawTokenStream().accounts({ associatedTokenProgram: _splToken.ASSOCIATED_TOKEN_PROGRAM_ID, dataAccount, destAccount, destTokenAccount, feeOwner, feeReceiverTokenAccount, feeVault, feeVaultData, mint, pdaAccountTokenAccount, rent: _web.SYSVAR_RENT_PUBKEY, sourceAccount, systemProgram: _web.SystemProgram.programId, tokenProgram: _splToken.TOKEN_PROGRAM_ID, withdrawData, zebecVault }).instruction(); // logInstruction(instruction); return { instruction }; } async getPauseResumeTokenStreamIxn(sender, receiver, dataAccount, mint) { const withdrawData = this.getWithdrawDataAddressSync(sender, mint); let instruction = await this._zebecProgram.methods.pauseResumeTokenStream().accounts({ dataAccount, receiver, sender, mint, withdrawData }).instruction(); // logInstruction(instruction); return { instruction }; } async getCancelTokenStreamIxn(sourceAccount, destAccount, dataAccount, mint, feeOwner) { const destTokenAccount = (0, _splToken.getAssociatedTokenAddressSync)(mint, destAccount, true); const feeVault = this.getFeeVaultAddressSync(feeOwner); const feeReceiverTokenAccount = (0, _splToken.getAssociatedTokenAddressSync)(mint, feeVault, true); const feeVaultData = this.getFeeVaultDataAddressSync(feeOwner, feeVault); const zebecVault = this.getZebecVaultAddressSync(sourceAccount); const pdaAccountTokenAccount = (0, _splToken.getAssociatedTokenAddressSync)(mint, zebecVault, true); const withdrawData = this.getWithdrawDataAddressSync(sourceAccount, mint); const instruction = await this._zebecProgram.methods.cancelTokenStream().accounts({ associatedTokenProgram: _splToken.ASSOCIATED_TOKEN_PROGRAM_ID, dataAccount, destAccount, destTokenAccount, feeOwner, feeReceiverTokenAccount, feeVault, feeVaultData, mint, pdaAccountTokenAccount, rent: _web.SYSVAR_RENT_PUBKEY, sourceAccount, systemProgram: _web.SystemProgram.programId, tokenProgram: _splToken.TOKEN_PROGRAM_ID, withdrawData, zebecVault }).instruction(); // logInstruction(instruction); return { instruction }; } async getTokenStreamUpdateIxn(sourceAccount, destAccount, dataAccount, mint, startTime, endTime, amount) { const withdrawData = this.getWithdrawDataAddressSync(sourceAccount, mint); const instruction = await this._zebecProgram.methods.tokenStreamUpdate(startTime, endTime, amount).accounts({ dataAccount, destAccount, mint, sourceAccount, withdrawData }).instruction(); // logInstruction(instruction); return { instruction }; } async getTokenTransferIxn(sourceAccount, destAccount, mint, amount) { const destTokenAccount = (0, _splToken.getAssociatedTokenAddressSync)(mint, destAccount, true); const zebecVault = this.getZebecVaultAddressSync(sourceAccount); const pdaAccountTokenAccount = (0, _splToken.getAssociatedTokenAddressSync)(mint, zebecVault, true); const withdrawData = this.getWithdrawDataAddressSync(sourceAccount, mint); const instruction = await this._zebecProgram.methods.instantTokenTransfer(amount).accounts({ associatedTokenProgram: _splToken.ASSOCIATED_TOKEN_PROGRAM_ID, destAccount, destTokenAccount, mint, pdaAccountTokenAccount, rent: _web.SYSVAR_RENT_PUBKEY, sourceAccount, systemProgram: _web.SystemProgram.programId, tokenProgram: _splToken.TOKEN_PROGRAM_ID, withdrawData, zebecVault }).instruction(); // logInstruction(instruction); return { instruction }; } async getCreateFeeAccountIxn(feeOwner, feePercent) { const feeVault = this.getFeeVaultAddressSync(feeOwner); const feeVaultData = this.getFeeVaultDataAddressSync(feeOwner, feeVault); const instruction = await this._zebecProgram.methods.createFeeAccount(feePercent).accounts({ feeVault, feeVaultData, feeOwner, systemProgram: _anchor.web3.SystemProgram.programId, rent: _anchor.web3.SYSVAR_RENT_PUBKEY }).instruction(); // logInstruction(instruction); return instruction; } async getDecodedDataAccount(address, commitment) { const info = await this._zebecProgram.account.streamToken.getAccountInfo(address, commitment); if (!info || !info.data) { throw new Error("data account is undefined"); } const data = this._zebecProgram.coder.accounts.decode("StreamToken", info?.data); return data; } } exports.ZebecInstructions = ZebecInstructions; //# sourceMappingURL=instructions.js.map