/* * Copyright (c) Gala Games Inc. All rights reserved. * 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. */ import { AllowanceType, FetchAllowancesResponse, TokenAllowance } from "@gala-chain/api"; import { GalaChainContext, GrantAllowanceParams, deleteAllowances, fetchAllowancesWithPagination, grantAllowance } from "@gala-chain/chaincode"; import { DeleteSwapAllowancesDto, FetchSwapAllowancesDto, GrantBulkSwapAllowanceDto, GrantSwapAllowanceDto } from "../../api/types/SwapAllowanceDtos"; /** * Grants swap allowances for the specified token instance * @param ctx - GalaChain context * @param dto - Grant swap allowance DTO * @returns Array of created TokenAllowance objects */ export async function grantSwapAllowance( ctx: GalaChainContext, dto: GrantSwapAllowanceDto ): Promise { const params: GrantAllowanceParams = { tokenInstance: dto.tokenInstance, allowanceType: AllowanceType.Transfer, quantities: dto.quantities, uses: dto.uses, expires: dto.expires ?? 0 }; return grantAllowance(ctx, params); } /** * Grants swap allowances for multiple token instances in a single operation * @param ctx - GalaChain context * @param dto - Grant bulk swap allowance DTO * @returns Array of created TokenAllowance objects */ export async function grantBulkSwapAllowance( ctx: GalaChainContext, dto: GrantBulkSwapAllowanceDto ): Promise { const allAllowances: TokenAllowance[] = []; // For each token quantity, create allowance for the single user for (const tokenQuantity of dto.tokenQuantities) { const params: GrantAllowanceParams = { tokenInstance: tokenQuantity.tokenInstanceKey, allowanceType: AllowanceType.Transfer, quantities: [{ user: dto.grantedTo, quantity: tokenQuantity.quantity }], uses: dto.uses, expires: dto.expires ?? 0 }; const allowances = await grantAllowance(ctx, params); allAllowances.push(...allowances); } return allAllowances; } /** * Fetches swap allowances with pagination * @param ctx - GalaChain context * @param dto - Fetch swap allowances DTO * @returns Paginated response with allowances */ export async function fetchSwapAllowances( ctx: GalaChainContext, dto: FetchSwapAllowancesDto ): Promise { return fetchAllowancesWithPagination(ctx, { ...dto, allowanceType: AllowanceType.Transfer, limit: dto.limit ?? FetchSwapAllowancesDto.DEFAULT_LIMIT }); } /** * Deletes swap allowances matching the specified criteria * @param ctx - GalaChain context * @param dto - Delete swap allowances DTO * @returns Number of allowances deleted */ export async function deleteSwapAllowances( ctx: GalaChainContext, dto: DeleteSwapAllowancesDto ): Promise { return deleteAllowances(ctx, { ...dto, allowanceType: AllowanceType.Transfer }); }