Skip to content

Commit

Permalink
bring back generic attach and connect methods
Browse files Browse the repository at this point in the history
  • Loading branch information
H34D committed Jun 15, 2023
1 parent dd2e51f commit ca48bb9
Show file tree
Hide file tree
Showing 16 changed files with 602 additions and 666 deletions.
6 changes: 2 additions & 4 deletions src/base/masa-module-base.ts
Expand Up @@ -268,7 +268,7 @@ export class MasaModuleBase extends MasaBase {
*/
protected getMintPrice = async (
paymentMethod: PaymentMethod,
contract: MasaSBTSelfSovereign | MasaSBTAuthority | MasaSBT,
contract: MasaSBT,
// slippage in bps where 10000 is 100%. 250 would be 2,5%
slippage: number | undefined = 250
): Promise<{
Expand Down Expand Up @@ -337,9 +337,7 @@ export class MasaModuleBase extends MasaBase {
* @param address
* @param factory
*/
protected static loadSBTContract = async <
Contract extends MasaSBTSelfSovereign | MasaSBTAuthority | MasaSBT
>(
protected static loadSBTContract = async <Contract extends MasaSBT>(
masaConfig: MasaConfig,
address: string,
factory: ContractFactory
Expand Down
11 changes: 3 additions & 8 deletions src/contracts/masa-contracts.ts
@@ -1,10 +1,5 @@
import type { LogDescription } from "@ethersproject/abi";
import type { Log } from "@ethersproject/abstract-provider";
import type {
MasaSBT,
ReferenceSBTAuthority,
ReferenceSBTSelfSovereign,
} from "@masa-finance/masa-contracts-identity";
import type { BaseContract } from "ethers";

import { MasaBase } from "../base";
Expand All @@ -29,9 +24,9 @@ export class MasaContracts extends MasaBase {
/**
* SBTs
*/
public sbt: SBTContract<MasaSBT>;
public sssbt: SSSBTContract<ReferenceSBTSelfSovereign>;
public asbt: ASBTContract<ReferenceSBTAuthority>;
public sbt: SBTContract;
public sssbt: SSSBTContract;
public asbt: ASBTContract;
/**
* Soul Linker
*/
Expand Down
231 changes: 223 additions & 8 deletions src/contracts/modules/sbt/ASBT/asbt-contract-wrapper.ts
@@ -1,24 +1,239 @@
import type { ReferenceSBTAuthority } from "@masa-finance/masa-contracts-identity";
import { LogDescription } from "@ethersproject/abi";
import { BigNumber } from "@ethersproject/bignumber";
import { ReferenceSBTAuthority } from "@masa-finance/masa-contracts-identity";
import { PayableOverrides } from "ethers";

import { Messages } from "../../../../collections";
import type { PaymentMethod } from "../../../../interface";
import type { SBTContractWrapper } from "../SBT";
import { isNativeCurrency } from "../../../../utils";
import { SBTContractWrapper } from "../SBT";

export interface ASBTContractWrapper<Contract extends ReferenceSBTAuthority>
extends SBTContractWrapper<Contract> {
export class ASBTContractWrapper<
Contract extends ReferenceSBTAuthority
> extends SBTContractWrapper<Contract> {
/**
*
* @param paymentMethod
* @param receiver
*/
mint: (paymentMethod: PaymentMethod, receiver: string) => Promise<boolean>;
mint = async (
paymentMethod: PaymentMethod,
receiver: string
): Promise<boolean> => {
// current limit for ASBT is 1 on the default installation
let limit: number = 1;

try {
limit = (await this.sbtContract.maxSBTToMint()).toNumber();
} catch {
if (this.masa.config.verbose) {
console.info("Loading limit failed, falling back to 1!");
}
}

try {
const balance: BigNumber = await this.sbtContract.balanceOf(receiver);

if (limit > 0 && balance.gte(limit)) {
console.error(
`Minting of ASBT failed: '${receiver}' exceeded the limit of '${limit}'!`
);
return false;
}
} catch (error: unknown) {
if (error instanceof Error) {
console.warn(error.message);
}
}

const { price, paymentAddress } = await this.getPrice(paymentMethod);

const mintASBTArguments: [
string, // paymentAddress string
string // receiver string
] = [paymentAddress, receiver];

const feeData = await this.getNetworkFeeInformation();

const mintASBTOverrides: PayableOverrides = {
value: isNativeCurrency(paymentMethod) ? price : undefined,
...(feeData && feeData.maxPriorityFeePerGas
? {
maxPriorityFeePerGas: BigNumber.from(feeData.maxPriorityFeePerGas),
}
: undefined),
...(feeData && feeData.maxFeePerGas
? {
maxFeePerGas: BigNumber.from(feeData.maxFeePerGas),
}
: undefined),
};

if (this.masa.config.verbose) {
console.info(mintASBTArguments, mintASBTOverrides);
}

const {
"mint(address,address)": mint,
estimateGas: { "mint(address,address)": estimateGas },
} = this.sbtContract;

let gasLimit: BigNumber = await estimateGas(
...mintASBTArguments,
mintASBTOverrides
);

if (this.masa.config.network?.gasSlippagePercentage) {
gasLimit = ASBTContractWrapper.addSlippage(
gasLimit,
this.masa.config.network.gasSlippagePercentage
);
}

const { wait, hash } = await mint(...mintASBTArguments, {
...mintASBTOverrides,
gasLimit,
});

console.log(
Messages.WaitingToFinalize(
hash,
this.masa.config.network?.blockExplorerUrls?.[0]
)
);

const { logs } = await wait();

const parsedLogs = this.masa.contracts.parseLogs(logs, [this.sbtContract]);

const mintEvent = parsedLogs.find(
(log: LogDescription) => log.name === "Mint"
);

if (mintEvent) {
const { args } = mintEvent;
console.log(
`Minted to token with ID: ${args._tokenId} receiver '${args._owner}'`
);

return true;
}

return false;
};

/**
*
* @param paymentMethod
* @param receiver
* @param receivers
*/
bulkMint: (
bulkMint = async (
paymentMethod: PaymentMethod,
receivers: string[]
) => Promise<boolean[]>;
): Promise<boolean[]> => {
const result = [];

// current limit for ASBT is 1 on the default installation
let limit: number = 1;

try {
limit = (await this.sbtContract.maxSBTToMint()).toNumber();
} catch {
if (this.masa.config.verbose) {
console.info("Loading limit failed, falling back to 1!");
}
}

for (const receiver of receivers) {
try {
const balance: BigNumber = await this.sbtContract.balanceOf(receiver);

if (limit > 0 && balance.gte(limit)) {
console.error(
`Minting of ASBT failed: '${receiver}' exceeded the limit of '${limit}'!`
);
result.push(false);
}
} catch (error: unknown) {
if (error instanceof Error) {
console.warn(error.message);
}
}
}

const { price, paymentAddress } = await this.getPrice(paymentMethod);

const mintASBTArguments: [
string, // paymentAddress string
string[] // receivers string[]
] = [paymentAddress, receivers];

const feeData = await this.getNetworkFeeInformation();

const mintASBTOverrides: PayableOverrides = {
value: isNativeCurrency(paymentMethod) ? price : undefined,
...(feeData && feeData.maxPriorityFeePerGas
? {
maxPriorityFeePerGas: BigNumber.from(feeData.maxPriorityFeePerGas),
}
: undefined),
...(feeData && feeData.maxFeePerGas
? {
maxFeePerGas: BigNumber.from(feeData.maxFeePerGas),
}
: undefined),
};

if (this.masa.config.verbose) {
console.info(mintASBTArguments, mintASBTOverrides);
}

const {
"mint(address,address[])": mint,
estimateGas: { "mint(address,address[])": estimateGas },
} = this.sbtContract;

let gasLimit: BigNumber = await estimateGas(
...mintASBTArguments,
mintASBTOverrides
);

if (this.masa.config.network?.gasSlippagePercentage) {
gasLimit = ASBTContractWrapper.addSlippage(
gasLimit,
this.masa.config.network.gasSlippagePercentage
);
}

const { wait, hash } = await mint(...mintASBTArguments, {
...mintASBTOverrides,
gasLimit,
});

console.log(
Messages.WaitingToFinalize(
hash,
this.masa.config.network?.blockExplorerUrls?.[0]
)
);

const { logs } = await wait();

const parsedLogs = this.masa.contracts.parseLogs(logs, [this.sbtContract]);

const mintEvent = parsedLogs.find(
(log: LogDescription) => log.name === "Mint"
);

if (mintEvent) {
const { args } = mintEvent;
console.log(
`Minted to token with ID: ${args._tokenId} receiver '${args._owner}'`
);

result.push(true);
}

return result;
};
}

0 comments on commit ca48bb9

Please sign in to comment.