Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
bring back generic attach and connect methods
- Loading branch information
Showing
16 changed files
with
602 additions
and
666 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
231 changes: 223 additions & 8 deletions
231
src/contracts/modules/sbt/ASBT/asbt-contract-wrapper.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
}; | ||
} |
Oops, something went wrong.