Skip to content

Commit

Permalink
introduce SBT baseclass
Browse files Browse the repository at this point in the history
  • Loading branch information
H34D committed Jun 16, 2023
1 parent 1378843 commit 2b376bb
Show file tree
Hide file tree
Showing 15 changed files with 172 additions and 159 deletions.
1 change: 1 addition & 0 deletions src/base/index.ts
@@ -1,3 +1,4 @@
export * from "./masa-base";
export * from "./masa-linkable";
export * from "./masa-module-base";
export * from "./masa-sbt-module-base";
109 changes: 0 additions & 109 deletions src/base/masa-module-base.ts
Expand Up @@ -12,12 +12,10 @@ import { constants, utils } from "ethers";
import { verifyTypedData } from "ethers/lib/utils";

import { Messages } from "../collections";
import type { ContractFactory } from "../contracts";
import type {
IIdentityContracts,
MasaInterface,
PaymentMethod,
PriceInformation,
} from "../interface";
import type { ERC20 } from "../stubs";
import { ERC20__factory } from "../stubs";
Expand Down Expand Up @@ -257,111 +255,4 @@ export class MasaModuleBase extends MasaBase {
}
}
};

/**
*
* @param paymentMethod
* @param contract
* @param slippage
*/
protected getMintPrice = async (
paymentMethod: PaymentMethod,
contract: MasaSBT,
// slippage in bps where 10000 is 100%. 250 would be 2,5%
slippage: number | undefined = 250
): Promise<PriceInformation> => {
const paymentAddress = this.getPaymentAddress(paymentMethod);

let mintFee: BigNumber | undefined,
protocolFee: BigNumber = BigNumber.from(0);

try {
// load protocol and mint fee
const fees = await contract.getMintPriceWithProtocolFee(paymentAddress);
mintFee = fees.price;
protocolFee = fees.protocolFee;
} catch {
// ignore this is a soul store 2.0 function and does not work on older contracts
}

if (!mintFee) {
// fallback to classical price calculation
mintFee = await contract.getMintPrice(paymentAddress);
}

// calculate total price
let price = mintFee.add(protocolFee);

if (slippage) {
if (isNativeCurrency(paymentMethod)) {
price = MasaModuleBase.addSlippage(price, slippage);
}
}

// total price
const formattedPrice = await this.formatPrice(paymentAddress, price);

// mint fee
const formattedMintFee = await this.formatPrice(paymentAddress, mintFee);

// protocol fee
const formattedProtocolFee = await this.formatPrice(
paymentAddress,
protocolFee
);

return {
paymentAddress,
price,
formattedPrice,
mintFee,
formattedMintFee,
protocolFee,
formattedProtocolFee,
};
};

/**
*
* @param address
* @param factory
*/
protected loadSBTContract = async <Contract extends MasaSBT>(
address: string,
factory: ContractFactory
): Promise<Contract> => {
let error = `Smart contract '${address}' does not exist on network '${this.masa.config.networkName}'!`;

// address invalid, unable to load
if (!utils.isAddress(address)) {
error = `Address '${address}' is not valid!`;
console.error(error);
throw new Error(error);
}
// fetch code to see if the contract exists
const code: string | undefined =
await this.masa.config.signer.provider?.getCode(address);

const contractExists: boolean = !!code && code !== "0x";

// no code exists, unable to load
if (!contractExists) {
throw new Error(error);
}

const contract = (factory as typeof ContractFactory).connect<Contract>(
address,
this.masa.config.signer
);

// failed to load, unable to load
if (!contract) {
console.error(error);
throw new Error(error);
} else if (this.masa.config.verbose) {
console.info(`Loaded contract with name: ${await contract.name()}`);
}

return contract;
};
}
117 changes: 117 additions & 0 deletions src/base/masa-sbt-module-base.ts
@@ -0,0 +1,117 @@
import { BigNumber } from "@ethersproject/bignumber";
import type { MasaSBT } from "@masa-finance/masa-contracts-identity";
import { utils } from "ethers";

import { ContractFactory } from "../contracts";
import type { PaymentMethod, PriceInformation } from "../interface";
import { isNativeCurrency } from "../utils";
import { MasaModuleBase } from "./masa-module-base";

export class MasaSBTModuleBase extends MasaModuleBase {
/**
*
* @param paymentMethod
* @param contract
* @param slippage
*/
protected getMintPrice = async (
paymentMethod: PaymentMethod,
contract: MasaSBT,
// slippage in bps where 10000 is 100%. 250 would be 2,5%
slippage: number | undefined = 250
): Promise<PriceInformation> => {
const paymentAddress = this.getPaymentAddress(paymentMethod);

let mintFee: BigNumber | undefined,
protocolFee: BigNumber = BigNumber.from(0);

try {
// load protocol and mint fee
const fees = await contract.getMintPriceWithProtocolFee(paymentAddress);
mintFee = fees.price;
protocolFee = fees.protocolFee;
} catch {
// ignore this is a soul store 2.0 function and does not work on older contracts
}

if (!mintFee) {
// fallback to classical price calculation
mintFee = await contract.getMintPrice(paymentAddress);
}

// calculate total price
let price = mintFee.add(protocolFee);

if (slippage) {
if (isNativeCurrency(paymentMethod)) {
price = MasaModuleBase.addSlippage(price, slippage);
}
}

// total price
const formattedPrice = await this.formatPrice(paymentAddress, price);

// mint fee
const formattedMintFee = await this.formatPrice(paymentAddress, mintFee);

// protocol fee
const formattedProtocolFee = await this.formatPrice(
paymentAddress,
protocolFee
);

return {
paymentAddress,
price,
formattedPrice,
mintFee,
formattedMintFee,
protocolFee,
formattedProtocolFee,
};
};

/**
*
* @param address
* @param factory
*/
protected loadSBTContract = async <Contract extends MasaSBT>(
address: string,
factory: ContractFactory
): Promise<Contract> => {
let error = `Smart contract '${address}' does not exist on network '${this.masa.config.networkName}'!`;

// address invalid, unable to load
if (!utils.isAddress(address)) {
error = `Address '${address}' is not valid!`;
console.error(error);
throw new Error(error);
}
// fetch code to see if the contract exists
const code: string | undefined =
await this.masa.config.signer.provider?.getCode(address);

const contractExists: boolean = !!code && code !== "0x";

// no code exists, unable to load
if (!contractExists) {
throw new Error(error);
}

const contract = (factory as typeof ContractFactory).connect<Contract>(
address,
this.masa.config.signer
);

// failed to load, unable to load
if (!contract) {
console.error(error);
throw new Error(error);
} else if (this.masa.config.verbose) {
console.info(`Loaded contract with name: ${await contract.name()}`);
}

return contract;
};
}
14 changes: 7 additions & 7 deletions src/contracts/modules/credit-score.ts
Expand Up @@ -5,7 +5,7 @@ import type {
TypedDataDomain,
} from "ethers";

import { MasaModuleBase } from "../../base";
import { MasaSBTModuleBase } from "../../base";
import { Messages } from "../../collections";
import type { PaymentMethod, PriceInformation } from "../../interface";
import {
Expand All @@ -14,11 +14,11 @@ import {
signTypedData,
} from "../../utils";

export class CreditScore extends MasaModuleBase {
export class CreditScore extends MasaSBTModuleBase {
/**
*
*/
types = {
public readonly types = {
MintCreditScore: [
{ name: "identityId", type: "uint256" },
{ name: "authorityAddress", type: "address" },
Expand All @@ -31,7 +31,7 @@ export class CreditScore extends MasaModuleBase {
* @param paymentMethod
* @param slippage
*/
getPrice = async (
public getPrice = async (
paymentMethod: PaymentMethod,
// slippage in bps where 10000 is 100%. 250 would be 2,5%
slippage: number | undefined = 250
Expand All @@ -52,7 +52,7 @@ export class CreditScore extends MasaModuleBase {
* @param signature
* @param slippage
*/
mint = async (
public mint = async (
paymentMethod: PaymentMethod,
identityId: BigNumber,
authorityAddress: string,
Expand Down Expand Up @@ -173,7 +173,7 @@ export class CreditScore extends MasaModuleBase {
* Signs a credit score
* @param identityId
*/
sign = async (
public sign = async (
identityId: BigNumber
): Promise<
| {
Expand Down Expand Up @@ -221,7 +221,7 @@ export class CreditScore extends MasaModuleBase {
*
* @param creditScoreId
*/
burn = async (creditScoreId: BigNumber): Promise<boolean> => {
public burn = async (creditScoreId: BigNumber): Promise<boolean> => {
console.log(`Burning Credit Score with ID '${creditScoreId}'!`);

try {
Expand Down
14 changes: 7 additions & 7 deletions src/contracts/modules/green.ts
Expand Up @@ -5,7 +5,7 @@ import type {
TypedDataDomain,
} from "ethers";

import { MasaModuleBase } from "../../base";
import { MasaSBTModuleBase } from "../../base";
import { Messages } from "../../collections";
import type { PaymentMethod, PriceInformation } from "../../interface";
import {
Expand All @@ -14,11 +14,11 @@ import {
signTypedData,
} from "../../utils";

export class Green extends MasaModuleBase {
export class Green extends MasaSBTModuleBase {
/**
*
*/
types = {
public readonly types = {
MintGreen: [
{ name: "to", type: "address" },
{ name: "authorityAddress", type: "address" },
Expand All @@ -31,7 +31,7 @@ export class Green extends MasaModuleBase {
* @param paymentMethod
* @param slippage
*/
getPrice = async (
public getPrice = async (
paymentMethod: PaymentMethod,
slippage: number | undefined = 250
): Promise<
Expand Down Expand Up @@ -75,7 +75,7 @@ export class Green extends MasaModuleBase {
* @param signature
* @param slippage
*/
mint = async (
public mint = async (
paymentMethod: PaymentMethod,
receiver: string,
authorityAddress: string,
Expand Down Expand Up @@ -187,7 +187,7 @@ export class Green extends MasaModuleBase {
* Signs a masa green
* @param receiver
*/
sign = async (
public sign = async (
receiver: string
): Promise<
| {
Expand Down Expand Up @@ -235,7 +235,7 @@ export class Green extends MasaModuleBase {
*
* @param greenId
*/
burn = async (greenId: BigNumber): Promise<boolean> => {
public burn = async (greenId: BigNumber): Promise<boolean> => {
try {
console.log(`Burning Green with ID '${greenId}'!`);

Expand Down

0 comments on commit 2b376bb

Please sign in to comment.