Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: post-mint-lock decimals handling v2 #454

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍


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
Loading