Skip to content

Commit

Permalink
properly type create workflows
Browse files Browse the repository at this point in the history
  • Loading branch information
H34D committed Nov 16, 2022
1 parent a93e8ea commit d153cb8
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 19 deletions.
1 change: 1 addition & 0 deletions src/2fa/create.ts
Expand Up @@ -9,6 +9,7 @@ export const create2FA = async (
): Promise<Create2FAResult> => {
const result: Create2FAResult = {
success: false,
message: "Unknown Error",
};

if (await masa.session.checkLogin()) {
Expand Down
8 changes: 3 additions & 5 deletions src/interface/2FA.ts
@@ -1,18 +1,16 @@
import { BigNumber } from "ethers";
import { BaseResult } from "./masa";

export interface I2FA {
name: "Masa Soulbound 2FA v1.0.0";
description: "A decentralized 2FA";
image: "https://metadata.masa.finance/v1.0/2fa/2fa.png";
properties: {
tokenId: string;
identityId?: string;
account?: string;
};
}

export interface Create2FAResult {
success: boolean;
export interface Create2FAResult extends BaseResult {
status?: string;
tokenId?: string | BigNumber;
message?: string;
}
9 changes: 3 additions & 6 deletions src/interface/credit-score.ts
@@ -1,11 +1,12 @@
import { BigNumber } from "ethers";
import { BaseResult } from "./masa";

export interface ICreditScore {
name: "Masa Soulbound Credit Score v1.0.0";
description: "A decentralized credit score";
image: "https://metadata.masa.finance/v1.0/credit-score/credit-score.png";
properties: {
tokenId: string;
identityId?: string;
account?: string;
lastUpdated?: string;
model_version?: string;
Expand All @@ -15,8 +16,4 @@ export interface ICreditScore {
};
}

export interface CreateCreditScoreResult {
tokenId?: string | BigNumber;
success: boolean;
message: string;
}
export type CreateCreditScoreResult = BaseResult;
8 changes: 7 additions & 1 deletion src/interface/masa.ts
@@ -1,4 +1,4 @@
import { ethers } from "ethers";
import { BigNumber, ethers } from "ethers";

export interface MasaArgs {
cookie?: string;
Expand All @@ -20,3 +20,9 @@ export interface MasaConfig {
network: string;
wallet: ethers.Signer | ethers.Wallet;
}

export interface BaseResult {
success: boolean;
message: string;
tokenId?: string | BigNumber;
}
6 changes: 6 additions & 0 deletions src/interface/soul-name.ts
@@ -1,3 +1,5 @@
import { BaseResult } from "./masa";

export interface Attribute {
display_type?: string;
trait_type: string;
Expand All @@ -11,3 +13,7 @@ export interface ISoulName {
name: string;
attributes: Attribute[];
}

export interface CreateSoulNameResult extends BaseResult {
soulName?: string;
}
33 changes: 26 additions & 7 deletions src/soul-name/create.ts
@@ -1,6 +1,7 @@
import Masa from "../masa";
import { PaymentMethod } from "../contracts";
import { ethers } from "ethers";
import { CreateSoulNameResult } from "../interface";

export const getRegistrationPrice = async (
masa: Masa,
Expand Down Expand Up @@ -37,7 +38,7 @@ const purchaseSoulName = async (
soulName: string,
duration: number,
paymentMethod: PaymentMethod
) => {
): Promise<{ tokenId: string; soulName: string } | undefined> => {
if (await masa.contracts.isAvailable(soulName)) {
console.log("Writing metadata");
const storeMetadataData = await masa.metadata.store(soulName);
Expand Down Expand Up @@ -65,9 +66,12 @@ const purchaseSoulName = async (
if (purchasedEvent && purchasedEvent.decode) {
const decodedEvent = purchasedEvent.decode(purchasedEvent.data);
tokenId = decodedEvent.tokenId.toNumber();
console.log(`Token with ID: ${tokenId} created.`);
return {
tokenId,
soulName,
};
}

console.log(`Token with ID: ${tokenId} created.`);
}
} else {
console.error(`Soulname ${soulName}.soul already taken.`);
Expand All @@ -79,7 +83,12 @@ export const createSoulName = async (
soulName: string,
duration: number,
paymentMethod: PaymentMethod
) => {
): Promise<CreateSoulNameResult> => {
const result: CreateSoulNameResult = {
success: false,
message: "Unknown Error",
};

if (await masa.session.checkLogin()) {
if (soulName.endsWith(".soul")) {
soulName = soulName.replace(".soul", "");
Expand All @@ -88,10 +97,20 @@ export const createSoulName = async (
const address = await masa.config.wallet.getAddress();

const identityId = await masa.identity.load(address);
if (!identityId) return;
if (!identityId) return result;

await purchaseSoulName(masa, soulName, duration, paymentMethod);
const aa = await purchaseSoulName(masa, soulName, duration, paymentMethod);

if (aa) {
result.success = true;
result.message = "Soulname created!";
result.tokenId = aa.tokenId;
result.soulName = soulName;
}
} else {
console.log("Not logged in please login first");
result.message = "Not logged in please login first";
console.log(result.message);
}

return result;
};

0 comments on commit d153cb8

Please sign in to comment.