UNPKG

@f5i23q999d/cow-sdk

Version:

<p align="center"> <img width="400" src="https://github.com/cowprotocol/cow-sdk/raw/main/docs/images/CoW.png" /> </p>

86 lines 4.09 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.CowShedSdk = void 0; const CoWShedHooks_1 = require("./contracts/CoWShedHooks.js"); const contracts_1 = require("@cowprotocol/contracts"); const wallet_1 = require("../common/utils/wallet.js"); const constants_1 = require("@ethersproject/constants"); // FIXME: I will refactor into a new PR (log needs to be moved to the common package) const serialize_1 = require("../common/utils/serialize.js"); const utils_1 = require("ethers/lib/utils"); const log_1 = require("../common/utils/log.js"); const NON_EXPIRING_DEADLINE = constants_1.MaxUint256.toBigInt(); class CowShedSdk { options; hooksCache = new Map(); constructor(options = {}) { this.options = options; } getCowShedAccount(chainId, ownerAddress) { const cowShedHooks = this.getCowShedHooks(chainId, this.options?.factoryOptions); return cowShedHooks.proxyOf(ownerAddress); } /** * Encodes multiple calls into a single pre-authorized call to the cow-shed factory. * * This single call will create the cow-shed account if it doesn't exist yet, then will execute the calls. * * @returns pre-authorized multicall details */ async signCalls({ calls, signer: signerParam, chainId, nonce = CowShedSdk.getNonce(), deadline = NON_EXPIRING_DEADLINE, defaultGasLimit, signingScheme = contracts_1.SigningScheme.EIP712, }) { // Get the cow-shed for the wallet const cowShedHooks = this.getCowShedHooks(chainId); // Get the signer, or throw an error if not provided const signerLike = signerParam || this.options?.signer; if (!signerLike) { throw new Error('Signer is required'); } const signer = (0, wallet_1.getSigner)(signerLike); const ownerAddress = await signer.getAddress(); const cowShedAccount = cowShedHooks.proxyOf(ownerAddress); // Sign the calls using cow-shed's owner const signature = await cowShedHooks.signCalls(calls, nonce, deadline, signer, signingScheme); // Get the signed transaction's calldata const callData = cowShedHooks.encodeExecuteHooksForFactory(calls, nonce, deadline, ownerAddress, signature); // Estimate the gas limit for the transaction const cowShedFactoryAddress = cowShedHooks.getFactoryAddress(); const factoryCall = { to: cowShedFactoryAddress, data: callData, value: BigInt(0), }; const gasEstimate = await signer.estimateGas(factoryCall).catch((error) => { const factoryCallString = JSON.stringify(factoryCall, serialize_1.jsonWithBigintReplacer, 2); const errorMessage = `Error estimating gas for the cow-shed call: ${factoryCallString}. Review the factory call`; // Return the default gas limit if provided if (defaultGasLimit) { (0, log_1.log)(`${errorMessage}, using the default gas limit.`); return defaultGasLimit; } // Re-throw the error if no default gas limit is provided throw new Error(`${errorMessage}, or provide the defaultGasLimit parameter.`, { cause: error }); }); // Return the details, including the signed transaction data return { cowShedAccount, signedMulticall: factoryCall, gasLimit: BigInt(gasEstimate.toString()), }; } getCowShedHooks(chainId, customOptions) { let cowShedHooks = this.hooksCache.get(chainId); if (cowShedHooks) { // Return cached cow-shed hooks return cowShedHooks; } // Create new cow-shed hooks and cache it cowShedHooks = new CoWShedHooks_1.CowShedHooks(chainId, customOptions); this.hooksCache.set(chainId, cowShedHooks); return cowShedHooks; } static getNonce() { return (0, utils_1.formatBytes32String)(Date.now().toString()); } } exports.CowShedSdk = CowShedSdk; //# sourceMappingURL=CowShedSdk.js.map