Skip to content

Commit

Permalink
added deployment for reference ASBT and SSSBT
Browse files Browse the repository at this point in the history
  • Loading branch information
H34D committed May 12, 2023
1 parent d99148b commit 51b5c4c
Show file tree
Hide file tree
Showing 12 changed files with 514 additions and 200 deletions.
4 changes: 3 additions & 1 deletion src/contracts/load-sbt-contract.ts
Expand Up @@ -44,7 +44,9 @@ export const loadSBTContract = async <

if (selfSovereignSBT) {
if (masaConfig.verbose) {
console.info(await selfSovereignSBT.name());
console.info(
`Loaded contract with name: ${await selfSovereignSBT.name()}`
);
}
} else {
console.error(
Expand Down
4 changes: 2 additions & 2 deletions src/contracts/modules/identity.ts
Expand Up @@ -14,8 +14,8 @@ export class Identity extends MasaModuleBase {
*/
purchase = async (): Promise<ContractTransaction> => {
const {
estimateGas: { purchaseIdentity: estimateGas },
purchaseIdentity,
estimateGas: { "purchaseIdentity()": estimateGas },
"purchaseIdentity()": purchaseIdentity,
} = this.instances.SoulStoreContract.connect(this.masa.config.wallet);

// estimate gas
Expand Down
2 changes: 1 addition & 1 deletion src/contracts/modules/masa-module-base.ts
Expand Up @@ -90,7 +90,7 @@ export class MasaModuleBase extends MasaBase {
* @param paymentMethod
* @private
*/
protected getPaymentAddress = (paymentMethod: PaymentMethod): string => {
public getPaymentAddress = (paymentMethod: PaymentMethod): string => {
let paymentAddress: string | undefined = isNativeCurrency(paymentMethod)
? constants.AddressZero
: this.masa.config.network?.addresses?.tokens?.[paymentMethod];
Expand Down
315 changes: 194 additions & 121 deletions src/contracts/modules/sbt.ts
Expand Up @@ -16,6 +16,190 @@ export const isMasaSBTSelfSovereign = (
!!(contract as MasaSBTSelfSovereign).getMintPrice;

export class SBT extends MasaModuleBase {
protected wrapper = <
Contract extends MasaSBTSelfSovereign | MasaSBTAuthority
>(
sbtContract?: Contract
): {
sbtContract?: Contract;
sign: (
name: string,
types: Record<string, Array<TypedDataField>>,
value: Record<string, string | BigNumber | number>
) => Promise<
| {
signature: string;
authorityAddress: string;
}
| undefined
>;
getPrice: (
paymentMethod: PaymentMethod,
slippage?: number
) => Promise<
| {
price: BigNumber;
paymentAddress: string;
formattedPrice: string;
}
| undefined
>;
prepareMint: (
paymentMethod: PaymentMethod,
name: string,
types: Record<string, Array<TypedDataField>>,
value: Record<string, string | BigNumber | number>,
signature: string,
authorityAddress: string,
slippage?: number
) => Promise<
| {
price: BigNumber;
paymentAddress: string;
}
| undefined
>;
} => ({
/**
* instance of the SBT that this factory instance uses
*/
sbtContract,

/**
* Signs an SBT based on its address
* @param name
* @param types
* @param value
*/
sign: async (
name: string,
types: Record<string, Array<TypedDataField>>,
value: Record<string, string | BigNumber | number>
): Promise<
| {
signature: string;
authorityAddress: string;
}
| undefined
> => {
if (!sbtContract) return;

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

const { signature, domain } = await signTypedData(
sbtContract,
this.masa.config.wallet as Wallet,
name,
types,
value
);

await this.verify(
"Signing SBT failed!",
sbtContract,
domain,
types,
value,
signature,
authorityAddress
);

return { signature, authorityAddress };
},

getPrice: async (
paymentMethod: PaymentMethod,
slippage: number | undefined = 250
): Promise<
| {
price: BigNumber;
paymentAddress: string;
formattedPrice: string;
}
| undefined
> => {
if (!sbtContract || !isMasaSBTSelfSovereign(sbtContract)) return;

const paymentAddress = this.getPaymentAddress(paymentMethod);

let price = await sbtContract.getMintPrice(paymentAddress);

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

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

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

prepareMint: async (
paymentMethod: PaymentMethod,
name: string,
types: Record<string, Array<TypedDataField>>,
value: Record<string, string | BigNumber | number>,
signature: string,
authorityAddress: string,
slippage: number | undefined = 250
): Promise<
| {
price: BigNumber;
paymentAddress: string;
}
| undefined
> => {
if (!sbtContract) return;

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

await this.verify(
"Verifying SBT failed!",
sbtContract,
domain,
types,
value,
signature,
authorityAddress
);

const { getPrice } = await this.attach(sbtContract);
const priceObject = await getPrice(paymentMethod, slippage);

if (!priceObject) return;

if (this.masa.config.verbose) {
console.log({
price: priceObject.price.toString(),
paymentAddress: priceObject.paymentAddress,
formattedPrice: priceObject.formattedPrice,
});
}

return {
price: priceObject.price,
paymentAddress: priceObject.paymentAddress,
};
},
});

/**
* loads an sbt instance and connects the contract functions to it
* @param address
* @param factory
*/
connect = async <Contract extends MasaSBTSelfSovereign | MasaSBTAuthority>(
address: string,
factory: ContractFactory = MasaSBTSelfSovereign__factory
Expand All @@ -26,127 +210,16 @@ export class SBT extends MasaModuleBase {
factory
);

return {
/**
* instance of the SBT that this factory instance uses
*/
sbtContract,

/**
* Signs an SBT based on its address
* @param name
* @param types
* @param value
*/
sign: async (
name: string,
types: Record<string, Array<TypedDataField>>,
value: Record<string, string | BigNumber | number>
): Promise<
| {
signature: string;
authorityAddress: string;
}
| undefined
> => {
if (!sbtContract) return;

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

const { signature, domain } = await signTypedData(
sbtContract,
this.masa.config.wallet as Wallet,
name,
types,
value
);

await this.verify(
"Signing SBT failed!",
sbtContract,
domain,
types,
value,
signature,
authorityAddress
);

return { signature, authorityAddress };
},

getPrice: async (
paymentMethod: PaymentMethod,
slippage: number | undefined = 250
) => {
if (!sbtContract || !isMasaSBTSelfSovereign(sbtContract)) return;

const paymentAddress = this.getPaymentAddress(paymentMethod);

let price = await sbtContract.getMintPrice(paymentAddress);

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

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

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

prepareMint: async (
paymentMethod: PaymentMethod,
name: string,
types: Record<string, Array<TypedDataField>>,
value: Record<string, string | BigNumber | number>,
signature: string,
authorityAddress: string,
slippage: number | undefined = 250
) => {
if (!sbtContract) return;

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

await this.verify(
"Verifying SBT failed!",
sbtContract,
domain,
types,
value,
signature,
authorityAddress
);

const { getPrice } = await this.connect<Contract>(address, factory);
const priceObject = await getPrice(paymentMethod, slippage);

if (!priceObject) return;

if (this.masa.config.verbose) {
console.log({
price: priceObject.price.toString(),
paymentAddress: priceObject.paymentAddress,
formattedPrice: priceObject.formattedPrice,
});
}
return this.wrapper<Contract>(sbtContract);
};

return {
price: priceObject.price,
paymentAddress: priceObject.paymentAddress,
};
},
};
/**
* attaches the contract function to an existing instances
* @param contract
*/
attach = async <Contract extends MasaSBTSelfSovereign | MasaSBTAuthority>(
contract: Contract
) => {
return this.wrapper<Contract>(contract);
};
}

0 comments on commit 51b5c4c

Please sign in to comment.