Skip to content

Commit

Permalink
added support for asbt and sssbt 2.0
Browse files Browse the repository at this point in the history
added price calculation to all the mint operations
refactored price fetching
refactored sssbt and asbt loading
  • Loading branch information
H34D committed May 22, 2023
1 parent 8c05c99 commit b82aa10
Show file tree
Hide file tree
Showing 18 changed files with 491 additions and 355 deletions.
1 change: 0 additions & 1 deletion src/contracts/index.ts
@@ -1,5 +1,4 @@
export * from "./addresses";
export * from "./load-sbt-contract";
export * from "./masa-contracts";
export * from "./load-Identity-contracts";
export * from "./stubs";
Expand Down
61 changes: 0 additions & 61 deletions src/contracts/load-sbt-contract.ts

This file was deleted.

22 changes: 4 additions & 18 deletions src/contracts/modules/credit-score.ts
Expand Up @@ -30,25 +30,11 @@ export class CreditScore extends MasaModuleBase {
paymentAddress: string;
formattedPrice: string;
}> => {
const paymentAddress = this.getPaymentAddress(paymentMethod);

let price = await this.instances.SoulboundCreditScoreContract.getMintPrice(
paymentAddress
return await this.getMintPrice(
paymentMethod,
this.instances.SoulboundCreditScoreContract,
slippage
);

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

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

return {
price,
paymentAddress,
formattedPrice,
};
};

/**
Expand Down
19 changes: 4 additions & 15 deletions src/contracts/modules/green.ts
Expand Up @@ -32,21 +32,10 @@ export class Green extends MasaModuleBase {
mintTransactionFee: BigNumber;
formattedMintTransactionFee: string;
}> => {
const paymentAddress = this.getPaymentAddress(paymentMethod);

let price = await this.instances.SoulboundGreenContract.getMintPrice(
paymentAddress
);

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

const formattedPrice: string = await this.formatPrice(
paymentAddress,
price
const { paymentAddress, price, formattedPrice } = await this.getMintPrice(
paymentMethod,
this.instances.SoulboundGreenContract,
slippage
);

const gasPrice = await this.masa.config.wallet.getGasPrice();
Expand Down
139 changes: 137 additions & 2 deletions src/contracts/modules/masa-module-base.ts
Expand Up @@ -3,27 +3,39 @@ import {
IIdentityContracts,
isERC20Currency,
isNativeCurrency,
MasaConfig,
PaymentMethod,
} from "../../interface";
import { BigNumber } from "@ethersproject/bignumber";
import {
constants,
ContractReceipt,
Signer,
TypedDataDomain,
TypedDataField,
utils,
Wallet,
} from "ethers";
import { ERC20, ERC20__factory } from "../stubs";
import { Messages } from "../../utils";
import {
MasaSBT,
MasaSBTAuthority,
MasaSBTSelfSovereign,
MasaSBTSelfSovereign__factory,
SoulLinker,
SoulStore,
} from "@masa-finance/masa-contracts-identity";
import { verifyTypedData } from "ethers/lib/utils";
import Masa from "../../masa";

export class ContractFactory {
static connect: <Contract>(
address: string,
signerOrProvider: Signer | Wallet
) => Contract;
}

export class MasaModuleBase extends MasaBase {
constructor(masa: Masa, protected instances: IIdentityContracts) {
super(masa);
Expand Down Expand Up @@ -145,7 +157,12 @@ export class MasaModuleBase extends MasaBase {
*/
protected verify = async (
errorMessage: string,
contract: MasaSBTSelfSovereign | MasaSBTAuthority | SoulStore | SoulLinker,
contract:
| MasaSBT
| MasaSBTSelfSovereign
| MasaSBTAuthority
| SoulStore
| SoulLinker,
domain: TypedDataDomain,
types: Record<string, Array<TypedDataField>>,
value: Record<string, string | BigNumber | number>,
Expand All @@ -163,7 +180,12 @@ export class MasaModuleBase extends MasaBase {
}

const hasAuthorities = (
contract: MasaSBTSelfSovereign | MasaSBTAuthority | SoulStore | SoulLinker
contract:
| MasaSBT
| MasaSBTSelfSovereign
| MasaSBTAuthority
| SoulStore
| SoulLinker
): contract is MasaSBTSelfSovereign => {
return (contract as MasaSBTSelfSovereign).authorities !== undefined;
};
Expand Down Expand Up @@ -210,4 +232,117 @@ export class MasaModuleBase extends MasaBase {
}
}
};

/**
*
* @param paymentMethod
* @param contract
* @param slippage
*/
protected getMintPrice = async (
paymentMethod: PaymentMethod,
contract: MasaSBTSelfSovereign | MasaSBTAuthority | MasaSBT,
// slippage in bps where 10000 is 100%. 250 would be 2,5%
slippage: number | undefined = 250
): Promise<{
paymentAddress: string;
price: BigNumber;
formattedPrice: string;
mintFee: BigNumber;
formattedMintFee: string;
protocolFee: BigNumber;
formattedProtocolFee: string;
}> => {
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 = this.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 masaConfig
* @param address
* @param factory
*/
protected loadSBTContract = async <
Contract extends MasaSBTSelfSovereign | MasaSBTAuthority | MasaSBT
>(
masaConfig: MasaConfig,
address: string,
factory: ContractFactory = MasaSBTSelfSovereign__factory
): Promise<Contract | undefined> => {
let sbtContract: Contract | undefined;

if (utils.isAddress(address)) {
// fetch code to see if the contract exists
const code: string | undefined =
await masaConfig.wallet.provider?.getCode(address);

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

sbtContract = contractExists
? (factory as typeof ContractFactory).connect<Contract>(
address,
masaConfig.wallet
)
: undefined;

if (sbtContract && masaConfig.verbose) {
console.info(`Loaded contract with name: ${await sbtContract.name()}`);
} else {
console.error(
`Smart contract '${address}' does not exist on network '${masaConfig.networkName}'!`
);
}
} else {
console.error(`Address '${address}' is not valid!`);
}

return sbtContract;
};
}

0 comments on commit b82aa10

Please sign in to comment.