SPB Git

spb/os-vault Public

Self-custody, multi-chain crypto wallet for macOS. One recovery phrase, six chain families, zero API keys — nothing leaves your Mac.

Swift 96% Shell 3.4% Makefile 0.6%
7.5 KB · 175 lines swift
Raw Blame History
1import Foundation23// TODO: - In construction45public enum TokenSwapProgram {6    // MARK: - Nested type78    private enum Index: UInt8, BytesEncodable {9        case initialize = 010        case swap = 111        case deposit = 212        case withdraw = 313    }1415    // MARK: - Instruction builder1617    public static func swapInstruction(18        tokenSwap: PublicKey,19        authority: PublicKey,20        userTransferAuthority: PublicKey,21        userSource: PublicKey,22        poolSource: PublicKey,23        poolDestination: PublicKey,24        userDestination: PublicKey,25        poolMint: PublicKey,26        feeAccount: PublicKey,27        hostFeeAccount: PublicKey?,28        swapProgramId: PublicKey,29        tokenProgramId: PublicKey,30        amountIn: UInt64,31        minimumAmountOut: UInt6432    ) -> TransactionInstruction {33        var keys = [34            AccountMeta(publicKey: tokenSwap, isSigner: false, isWritable: false),35            AccountMeta(publicKey: authority, isSigner: false, isWritable: false),36            AccountMeta(publicKey: userTransferAuthority, isSigner: true, isWritable: false),37            AccountMeta(publicKey: userSource, isSigner: false, isWritable: true),38            AccountMeta(publicKey: poolSource, isSigner: false, isWritable: true),39            AccountMeta(publicKey: poolDestination, isSigner: false, isWritable: true),40            AccountMeta(publicKey: userDestination, isSigner: false, isWritable: true),41            AccountMeta(publicKey: poolMint, isSigner: false, isWritable: true),42            AccountMeta(publicKey: feeAccount, isSigner: false, isWritable: true),43            AccountMeta(publicKey: tokenProgramId, isSigner: false, isWritable: false),44        ]4546        if let hostFeeAccount = hostFeeAccount {47            keys.append(AccountMeta(publicKey: hostFeeAccount, isSigner: false, isWritable: true))48        }4950        return TransactionInstruction(51            keys: keys,52            programId: swapProgramId,53            data: [Index.swap, amountIn, minimumAmountOut]54        )55    }5657    // MARK: - Deposit5859    public static func depositInstruction(60        tokenSwap: PublicKey,61        authority: PublicKey,62        sourceA: PublicKey,63        sourceB: PublicKey,64        intoA: PublicKey,65        intoB: PublicKey,66        poolToken: PublicKey,67        poolAccount: PublicKey,68        tokenProgramId: PublicKey,69        swapProgramId: PublicKey,70        poolTokenAmount: UInt64,71        maximumTokenA: UInt64,72        maximumTokenB: UInt6473    ) -> TransactionInstruction {74        TransactionInstruction(75            keys: [76                AccountMeta(publicKey: tokenSwap, isSigner: false, isWritable: false),77                AccountMeta(publicKey: authority, isSigner: false, isWritable: false),78                AccountMeta(publicKey: sourceA, isSigner: false, isWritable: true),79                AccountMeta(publicKey: sourceB, isSigner: false, isWritable: true),80                AccountMeta(publicKey: intoA, isSigner: false, isWritable: true),81                AccountMeta(publicKey: intoB, isSigner: false, isWritable: true),82                AccountMeta(publicKey: poolToken, isSigner: false, isWritable: true),83                AccountMeta(publicKey: poolAccount, isSigner: false, isWritable: true),84                AccountMeta(publicKey: tokenProgramId, isSigner: false, isWritable: true),85            ],86            programId: swapProgramId,87            data: [Index.deposit, poolTokenAmount, maximumTokenA, maximumTokenB]88        )89    }9091    // MARK: - Withdraw9293    public static func withdrawInstruction(94        tokenSwap: PublicKey,95        authority: PublicKey,96        poolMint: PublicKey,97        feeAccount: PublicKey,98        sourcePoolAccount: PublicKey,99        fromA: PublicKey,100        fromB: PublicKey,101        userAccountA: PublicKey,102        userAccountB: PublicKey,103        swapProgramId: PublicKey,104        tokenProgramId: PublicKey,105        poolTokenAmount: UInt64,106        minimumTokenA: UInt64,107        minimumTokenB: UInt64108    ) -> TransactionInstruction {109        TransactionInstruction(110            keys: [111                AccountMeta(publicKey: tokenSwap, isSigner: false, isWritable: false),112                AccountMeta(publicKey: authority, isSigner: false, isWritable: false),113                AccountMeta(publicKey: poolMint, isSigner: false, isWritable: true),114                AccountMeta(publicKey: sourcePoolAccount, isSigner: false, isWritable: true),115                AccountMeta(publicKey: fromA, isSigner: false, isWritable: true),116                AccountMeta(publicKey: fromB, isSigner: false, isWritable: true),117                AccountMeta(publicKey: userAccountA, isSigner: false, isWritable: true),118                AccountMeta(publicKey: userAccountB, isSigner: false, isWritable: true),119                AccountMeta(publicKey: feeAccount, isSigner: false, isWritable: false),120                AccountMeta(publicKey: tokenProgramId, isSigner: false, isWritable: false),121            ],122            programId: swapProgramId,123            data: [Index.withdraw, poolTokenAmount, minimumTokenA, minimumTokenB]124        )125    }126127    //        public func initialize(128    //            tokenSwapAccount: PublicKey,129    //            authority: PublicKey,130    //            tokenAccountA: PublicKey,131    //            tokenAccountB: PublicKey,132    //            tokenPool: PublicKey,133    //            feeAccount: PublicKey,134    //            tokenAccountPool: PublicKey,135    //            tokenProgramId: PublicKey,136    //            swapProgramId: PublicKey,137    //            nonce: UInt8,138    //            curveType: UInt8,139    //            tradeFeeNumerator: UInt64,140    //            tradeFeeDenominator: UInt64,141    //            ownerTradeFeeNumerator: UInt64,142    //            ownerTradeFeeDenominator: UInt64,143    //            ownerWithdrawFeeNumerator: UInt64,144    //            ownerWithdrawFeeDenominator: UInt64,145    //            hostFeeNumerator: UInt64,146    //            hostFeeDenominator: UInt64147    //        ) -> TransactionInstruction {148    //            let keys = [149    //                AccountMeta(publicKey: tokenSwapAccount, isSigner: false, isWritable: true),150    //                AccountMeta(publicKey: authority, isSigner: false, isWritable: false),151    //                AccountMeta(publicKey: authority, isSigner: false, isWritable: false),152    //                AccountMeta(publicKey: tokenAccountA, isSigner: false, isWritable: false),153    //                AccountMeta(publicKey: tokenAccountB, isSigner: false, isWritable: false),154    //                AccountMeta(publicKey: tokenPool, isSigner: false, isWritable: true),155    //                AccountMeta(publicKey: feeAccount, isSigner: false, isWritable: false),156    //                AccountMeta(publicKey: tokenAccountPool, isSigner: false, isWritable: true),157    //                AccountMeta(publicKey: tokenProgramId, isSigner: false, isWritable: false)158    //            ]159    //            var data = TransactionType.initialize.encode([160    //                nonce,161    //                tradeFeeNumerator,162    //                tradeFeeDenominator,163    //                ownerTradeFeeNumerator,164    //                ownerTradeFeeDenominator,165    //                ownerWithdrawFeeNumerator,166    //                ownerWithdrawFeeDenominator,167    //                hostFeeNumerator,168    //                hostFeeDenominator,169    //                curveType,170    //                Data(capacity: 32)171    //            ])172    //            return TransactionInstruction(keys: keys, programId: , data: data.bytes)173    //        }174}175