Skip to content

Commit

Permalink
fix: post-mint-lock decimals handling v2 (GalaChain#454)
Browse files Browse the repository at this point in the history
* fix: post-mint-lock decimals handling v2

CHAIN-819

* fix: lint

CHAIN-819

* fix exitGateImplementations UserRef owner type

* fix: clarify types for string / UserAlias
  • Loading branch information
sentientforest authored Dec 6, 2024
1 parent 1a144df commit 9c34ade
Showing 1 changed file with 21 additions and 15 deletions.
36 changes: 21 additions & 15 deletions chaincode/src/fees/exitGateImplementations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,31 +13,35 @@
* limitations under the License.
*/
import {
BatchMintTokenDto,
ChainError,
ErrorCode,
FeeGateCodes,
GalaChainResponse,
MintTokenDto,
MintTokenWithAllowanceDto,
PostMintLockConfiguration,
TokenClass,
TokenClassKey,
TokenInstance,
TokenInstanceKey,
TokenMintConfiguration,
UserAlias,
UserRef,
createValidDTO
} from "@gala-chain/api";
import BigNumber from "bignumber.js";

import { lockTokens } from "../locks";
import { MintTokenParams, MintTokenWithAllowanceParams } from "../mint";
import { resolveUserAlias } from "../services";
import { GalaChainContext } from "../types";
import { getObjectByKey } from "../utils";
import { burnToMintProcessing } from "./extendedFeeGateProcessing";

export interface IMintPostProcessing {
tokenClass: TokenClassKey;
tokens: TokenInstanceKey[];
owner: UserAlias;
owner: UserRef;
quantity: BigNumber;
feeCode?: FeeGateCodes | undefined;
}
Expand Down Expand Up @@ -68,16 +72,20 @@ export async function mintPostProcessing(ctx: GalaChainContext, data: IMintPostP
return;
}

const ownerAlias = await resolveUserAlias(ctx, owner);

if (mintConfiguration.postMintBurn !== undefined) {
await burnToMintProcessing(ctx, {
...data,
owner: ownerAlias,
burnConfiguration: mintConfiguration.postMintBurn
});
}

if (mintConfiguration.postMintLock !== undefined) {
await lockOnMintProcessing(ctx, {
...data,
owner: ownerAlias,
lockConfiguration: mintConfiguration.postMintLock
});
}
Expand All @@ -103,7 +111,9 @@ export async function lockOnMintProcessing(ctx: GalaChainContext, data: ILockOnM

const { lockName, lockAuthority, expirationModifier, lockPercentage } = lockConfiguration;

const mintQuantityToLock = quantity.times(lockPercentage).integerValue(BigNumber.ROUND_DOWN);
const mintQuantityToLock = quantity
.times(lockPercentage)
.decimalPlaces(tokenClassEntry.decimals, BigNumber.ROUND_DOWN);

const verifyAuthorizedOnBehalf = async (c: TokenClassKey) => undefined;

Expand Down Expand Up @@ -131,7 +141,7 @@ export async function lockOnMintProcessing(ctx: GalaChainContext, data: ILockOnM
});

await lockTokens(ctx, {
tokenInstances: [{ tokenInstanceKey: token, quantity: mintQuantityToLock, owner }],
tokenInstances: [{ tokenInstanceKey: token, quantity: mintQuantityToLock, owner: owner }],
allowancesToUse: [],
name: `${lockName}_${ctx.stub.getTxID()}`,
lockAuthority: lockAuthority,
Expand All @@ -143,12 +153,11 @@ export async function lockOnMintProcessing(ctx: GalaChainContext, data: ILockOnM

export async function mintTokenExitGate(
ctx: GalaChainContext,
dto: MintTokenParams,
dto: MintTokenDto,
response: GalaChainResponse<TokenInstanceKey[]>
): Promise<void> {
const { tokenClass, quantity } = dto;
const owner = dto.owner ?? ctx.callingUser;
const tokenClass = dto.tokenClassKey;
const quantity = dto.quantity;
const tokens: TokenInstanceKey[] | undefined = response.Data;

if (tokens === undefined || tokens.length < 1) {
Expand All @@ -160,12 +169,11 @@ export async function mintTokenExitGate(

export async function mintTokenWithAllowanceExitGate(
ctx: GalaChainContext,
dto: MintTokenWithAllowanceParams,
dto: MintTokenWithAllowanceDto,
response: GalaChainResponse<TokenInstanceKey[]>
): Promise<void> {
const { tokenClass, quantity } = dto;
const owner = dto.owner ?? ctx.callingUser;
const tokenClass = dto.tokenClassKey;
const quantity = dto.quantity;
const tokens: TokenInstanceKey[] | undefined = response.Data;

if (tokens === undefined || tokens.length < 1) {
Expand All @@ -177,14 +185,12 @@ export async function mintTokenWithAllowanceExitGate(

export async function batchMintTokenExitGate(
ctx: GalaChainContext,
paramsArr: MintTokenParams[],
dto: BatchMintTokenDto,
response: GalaChainResponse<TokenInstanceKey[]>
): Promise<void> {
for (const mintDto of paramsArr) {
for (const mintDto of dto.mintDtos) {
const { tokenClass, quantity } = mintDto;
const owner = mintDto.owner ?? ctx.callingUser;
const tokenClass = mintDto.tokenClassKey;
const quantity = mintDto.quantity;

// todo: batchMintToken currently returns a singular array of TokenInstanceKeys,
// and they are not ordered in a way that corresponds to the incoming mintDto.
// passing in the specific NFT instances minted per mintDto would require
Expand Down

0 comments on commit 9c34ade

Please sign in to comment.