opensea-js
Version:
TypeScript SDK for the OpenSea marketplace helps developers build new experiences using NFTs and our marketplace data
64 lines • 2.97 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.TokensManager = void 0;
const ethers_1 = require("ethers");
const types_1 = require("../types");
const chain_1 = require("../utils/chain");
/**
* Token wrapping and unwrapping operations
*/
class TokensManager {
constructor(context) {
this.context = context;
}
/**
* Wrap native asset into wrapped native asset (e.g. ETH into WETH, POL into WPOL).
* Wrapped native assets are needed for making offers.
* @param options
* @param options.amountInEth Amount of native asset to wrap
* @param options.accountAddress Address of the user's wallet containing the native asset
*/
async wrapEth({ amountInEth, accountAddress, }) {
await this.context.requireAccountIsAvailable(accountAddress);
const value = (0, ethers_1.parseEther)(amountInEth.toString());
this.context.dispatch(types_1.EventType.WrapEth, { accountAddress, amount: value });
const wethContract = new ethers_1.Contract((0, chain_1.getNativeWrapTokenAddress)(this.context.chain), ["function deposit() payable"], this.context.signerOrProvider);
try {
const transaction = await wethContract.deposit({ value });
await this.context.confirmTransaction(transaction.hash, types_1.EventType.WrapEth, "Wrapping native asset");
}
catch (error) {
console.error(error);
this.context.dispatch(types_1.EventType.TransactionDenied, {
error,
accountAddress,
});
}
}
/**
* Unwrap wrapped native asset into native asset (e.g. WETH into ETH, WPOL into POL).
* Emits the `UnwrapWeth` event when the transaction is prompted.
* @param options
* @param options.amountInEth How much wrapped native asset to unwrap
* @param options.accountAddress Address of the user's wallet containing the wrapped native asset
*/
async unwrapWeth({ amountInEth, accountAddress, }) {
await this.context.requireAccountIsAvailable(accountAddress);
const amount = (0, ethers_1.parseEther)(amountInEth.toString());
this.context.dispatch(types_1.EventType.UnwrapWeth, { accountAddress, amount });
const wethContract = new ethers_1.Contract((0, chain_1.getNativeWrapTokenAddress)(this.context.chain), ["function withdraw(uint wad) public"], this.context.signerOrProvider);
try {
const transaction = await wethContract.withdraw(amount);
await this.context.confirmTransaction(transaction.hash, types_1.EventType.UnwrapWeth, "Unwrapping wrapped native asset");
}
catch (error) {
console.error(error);
this.context.dispatch(types_1.EventType.TransactionDenied, {
error,
accountAddress,
});
}
}
}
exports.TokensManager = TokensManager;
//# sourceMappingURL=tokens.js.map