Skip to content

Commit

Permalink
added template to the sbt connect method
Browse files Browse the repository at this point in the history
added asbt support to the contracts connect method
  • Loading branch information
H34D committed Apr 27, 2023
1 parent 5382dca commit ebd6f7c
Show file tree
Hide file tree
Showing 8 changed files with 258 additions and 242 deletions.
12 changes: 6 additions & 6 deletions package.json
Expand Up @@ -36,7 +36,7 @@
"license": "MIT",
"dependencies": {
"@masa-finance/masa-contracts-identity": "^1.5.3",
"@masa-finance/masa-token": "^1.0.0",
"@masa-finance/masa-token": "^2.0.0",
"arweave": "1.11.8",
"axios": "^1.3.2",
"ethers": "~5.7.2",
Expand All @@ -46,8 +46,8 @@
"devDependencies": {
"@types/chai": "^4.3.4",
"@types/mocha": "^10.0.1",
"@typescript-eslint/eslint-plugin": "^5.54.1",
"@typescript-eslint/parser": "^5.56.0",
"@typescript-eslint/eslint-plugin": "^5.59.1",
"@typescript-eslint/parser": "^5.59.1",
"chai": "^4.3.7",
"cross-env": "^7.0.3",
"eslint": "^8.25.0",
Expand All @@ -56,9 +56,9 @@
"prettier": "^2.7.1",
"terser-webpack-plugin": "^5.3.6",
"ts-mocha": "^10.0.0",
"typedoc": "^0.24.1",
"typedoc-plugin-markdown": "^3.15.1",
"webpack": "^5.74.0",
"typedoc": "^0.24.6",
"typedoc-plugin-markdown": "^3.15.2",
"webpack": "^5.81.0",
"webpack-cli": "^5.0.1"
}
}
11 changes: 10 additions & 1 deletion src/contracts/load-sbt-contract.ts
@@ -1,5 +1,6 @@
import { Signer, utils, Wallet } from "ethers";
import {
MasaSBTAuthority,
MasaSBTSelfSovereign,
MasaSBTSelfSovereign__factory,
} from "@masa-finance/masa-contracts-identity";
Expand All @@ -12,7 +13,15 @@ export class ContractFactory {
) => Contract;
}

export const loadSBTContract = async <Contract extends MasaSBTSelfSovereign>(
/**
*
* @param masaConfig
* @param address
* @param factory
*/
export const loadSBTContract = async <
Contract extends MasaSBTSelfSovereign | MasaSBTAuthority
>(
masaConfig: MasaConfig,
address: string,
factory: ContractFactory = MasaSBTSelfSovereign__factory
Expand Down
5 changes: 3 additions & 2 deletions src/contracts/modules/masa-module-base.ts
Expand Up @@ -16,6 +16,7 @@ import {
import { ERC20, ERC20__factory } from "../stubs";
import { Messages } from "../../utils";
import {
MasaSBTAuthority,
MasaSBTSelfSovereign,
SoulLinker,
SoulStore,
Expand Down Expand Up @@ -144,7 +145,7 @@ export class MasaModuleBase extends MasaBase {
*/
protected verify = async (
errorMessage: string,
contract: MasaSBTSelfSovereign | SoulStore | SoulLinker,
contract: MasaSBTSelfSovereign | MasaSBTAuthority | SoulStore | SoulLinker,
domain: TypedDataDomain,
types: Record<string, Array<TypedDataField>>,
value: Record<string, string | BigNumber | number>,
Expand All @@ -162,7 +163,7 @@ export class MasaModuleBase extends MasaBase {
}

const hasAuthorities = (
contract: MasaSBTSelfSovereign | SoulStore | SoulLinker
contract: MasaSBTSelfSovereign | MasaSBTAuthority | SoulStore | SoulLinker
): contract is MasaSBTSelfSovereign => {
return (contract as MasaSBTSelfSovereign).authorities !== undefined;
};
Expand Down
28 changes: 17 additions & 11 deletions src/contracts/modules/sbt.ts
@@ -1,5 +1,6 @@
import { MasaModuleBase } from "./masa-module-base";
import {
MasaSBTAuthority,
MasaSBTSelfSovereign,
MasaSBTSelfSovereign__factory,
} from "@masa-finance/masa-contracts-identity";
Expand All @@ -9,12 +10,17 @@ import { BigNumber } from "@ethersproject/bignumber";
import { generateSignatureDomain, signTypedData } from "../../utils";
import { isNativeCurrency, PaymentMethod } from "../../interface";

export const isMasaSBTSelfSovereign = (
contract: unknown
): contract is MasaSBTSelfSovereign =>
!!(contract as MasaSBTSelfSovereign).getMintPrice;

export class SBT extends MasaModuleBase {
connect = async <Contract extends MasaSBTSelfSovereign>(
connect = async <Contract extends MasaSBTSelfSovereign | MasaSBTAuthority>(
address: string,
factory: ContractFactory = MasaSBTSelfSovereign__factory
) => {
const selfSovereignSBT: Contract | undefined = await loadSBTContract(
const sbtContract: Contract | undefined = await loadSBTContract(
this.masa.config,
address,
factory
Expand All @@ -24,7 +30,7 @@ export class SBT extends MasaModuleBase {
/**
* instance of the SBT that this factory instance uses
*/
selfSovereignSBT,
sbtContract,

/**
* Signs an SBT based on its address
Expand All @@ -43,12 +49,12 @@ export class SBT extends MasaModuleBase {
}
| undefined
> => {
if (!selfSovereignSBT) return;
if (!sbtContract) return;

const authorityAddress = await this.masa.config.wallet.getAddress();

const { signature, domain } = await signTypedData(
selfSovereignSBT,
sbtContract,
this.masa.config.wallet as Wallet,
name,
types,
Expand All @@ -57,7 +63,7 @@ export class SBT extends MasaModuleBase {

await this.verify(
"Signing SBT failed!",
selfSovereignSBT,
sbtContract,
domain,
types,
value,
Expand All @@ -72,11 +78,11 @@ export class SBT extends MasaModuleBase {
paymentMethod: PaymentMethod,
slippage: number | undefined = 250
) => {
if (!selfSovereignSBT) return;
if (!sbtContract || !isMasaSBTSelfSovereign(sbtContract)) return;

const paymentAddress = this.getPaymentAddress(paymentMethod);

let price = await selfSovereignSBT.getMintPrice(paymentAddress);
let price = await sbtContract.getMintPrice(paymentAddress);

if (slippage) {
if (isNativeCurrency(paymentMethod)) {
Expand Down Expand Up @@ -105,17 +111,17 @@ export class SBT extends MasaModuleBase {
authorityAddress: string,
slippage: number | undefined = 250
) => {
if (!selfSovereignSBT) return;
if (!sbtContract) return;

const domain: TypedDataDomain = await generateSignatureDomain(
this.masa.config.wallet as Wallet,
name,
selfSovereignSBT.address
sbtContract.address
);

await this.verify(
"Verifying SBT failed!",
selfSovereignSBT,
sbtContract,
domain,
types,
value,
Expand Down
7 changes: 5 additions & 2 deletions src/sbt/burn.ts
@@ -1,11 +1,14 @@
import { MasaSBTSelfSovereign } from "@masa-finance/masa-contracts-identity";
import {
MasaSBTAuthority,
MasaSBTSelfSovereign,
} from "@masa-finance/masa-contracts-identity";
import { BigNumber } from "ethers";
import Masa from "../masa";
import { Messages } from "../utils";

export const burnSBT = async (
masa: Masa,
contract: MasaSBTSelfSovereign,
contract: MasaSBTSelfSovereign | MasaSBTAuthority,
SBTId: BigNumber
): Promise<boolean> => {
try {
Expand Down
20 changes: 12 additions & 8 deletions src/sbt/sbt.ts
Expand Up @@ -5,7 +5,7 @@ import {
} from "@masa-finance/masa-contracts-identity";
import { BigNumber } from "ethers";
import { MasaBase } from "../helpers/masa-base";
import { loadSBTContract } from "../contracts";
import { ContractFactory, loadSBTContract } from "../contracts";
import { MasaSoulLinker } from "../soul-linker";
import { burnSBT, deployASBT, listSBTs, mintASBT } from "./";

Expand All @@ -27,16 +27,20 @@ export class MasaSBT extends MasaBase {
/**
*
* @param address
* @param factory
*/
connect = async (address: string) => {
const sbtContract: MasaSBTSelfSovereign | MasaSBTAuthority | undefined =
await loadSBTContract(
this.masa.config,
address,
MasaSBTSelfSovereign__factory
);
connect = async <Contract extends MasaSBTSelfSovereign | MasaSBTAuthority>(
address: string,
factory: ContractFactory = MasaSBTSelfSovereign__factory
) => {
const sbtContract: Contract | undefined = await loadSBTContract(
this.masa.config,
address,
factory
);

return {
sbtContract,
/**
*
*/
Expand Down
16 changes: 14 additions & 2 deletions src/utils/crypto.ts
Expand Up @@ -9,6 +9,10 @@ import {
Wallet,
} from "ethers";

/**
*
* @param item
*/
export const isBigNumber = (item: BigNumber | string): item is BigNumber => {
return (item as BigNumber)._isBigNumber;
};
Expand Down Expand Up @@ -64,16 +68,24 @@ export const signTypedData = async (
return { signature, domain };
};

/**
*
* @param wallet
* @param name
* @param verifyingContract
* @param version
*/
export const generateSignatureDomain = async (
wallet: Wallet,
name: string,
verifyingContract: string
verifyingContract: string,
version: string = "1.0.0"
): Promise<TypedDataDomain> => {
const chainId = (await wallet.provider.getNetwork()).chainId;

const domain: TypedDataDomain = {
name,
version: "1.0.0",
version,
chainId,
verifyingContract,
};
Expand Down

0 comments on commit ebd6f7c

Please sign in to comment.