Skip to content

Commit

Permalink
plugins pull addrs from config
Browse files Browse the repository at this point in the history
  • Loading branch information
luketchang committed Sep 12, 2023
1 parent bb2cb5f commit e833d12
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 19 deletions.
19 changes: 13 additions & 6 deletions packages/op-request-plugins/src/UniswapV3Plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import {
import { ethers } from "ethers";
import ERC20_ABI from "./abis/ERC20.json";

const SWAP_ROUTER_ADDRESS = "0xE592427A0AEce92De3Edee1F18E0157C05861564";
const UniswapV3_NAME = "uniswapV3";

export interface UniswapV3PluginMethods {
getSwapRouter(): AlphaRouter;
Expand Down Expand Up @@ -71,6 +71,14 @@ export function UniswapV3Plugin<EInner extends BaseOpRequestBuilder>(
const prom = new Promise<BuilderItemToProcess>(
async (resolve, reject) => {
try {
const swapRouterAddress =
this.config.protocolAllowlist.get(UniswapV3_NAME)?.address;
if (!swapRouterAddress) {
throw new Error(
`UniswapV3 not supported on chain with id: ${this._op.chainId}`
);
}

const router = this.getSwapRouter();
const handlerAddress = this.config.handlerAddress();

Expand Down Expand Up @@ -128,7 +136,7 @@ export function UniswapV3Plugin<EInner extends BaseOpRequestBuilder>(
};

const swapAction: Action = {
contractAddress: SWAP_ROUTER_ADDRESS,
contractAddress: swapRouterAddress,
encodedFunction: route.methodParameters!.calldata,
};

Expand All @@ -145,15 +153,14 @@ export function UniswapV3Plugin<EInner extends BaseOpRequestBuilder>(
// If router contract doesn't have high enough allowance, set to max for handler ->
// router. Anyone can set allowance on handler so might as well set to max.
if (
(
await erc20InContract.allowance(SWAP_ROUTER_ADDRESS)
).toBigInt() < inAmount
(await erc20InContract.allowance(swapRouterAddress)).toBigInt() <
inAmount
) {
const approveAction: Action = {
contractAddress: tokenIn,
encodedFunction: erc20InContract.interface.encodeFunctionData(
"approve",
[SWAP_ROUTER_ADDRESS, ethers.constants.MaxUint256]
[swapRouterAddress, ethers.constants.MaxUint256]
),
};

Expand Down
37 changes: 24 additions & 13 deletions packages/op-request-plugins/src/WstethAdapterPlugin.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import {
Action,
Address,
Asset,
AssetTrait,
BaseOpRequestBuilder,
Expand All @@ -12,6 +11,8 @@ import {
} from "@nocturne-xyz/core";
import { WstethAdapter__factory } from "@nocturne-xyz/contracts";

const WSTETH_ADAPTER_NAME = "wstethAdapter";

export interface WstethAdapterPluginMethods {
// adds an ERC20 transfer to the operation
// handles encoding, unwrapping, and metadata
Expand All @@ -38,18 +39,28 @@ export function WstethAdapterPlugin<EInner extends BaseOpRequestBuilder>(
use: use,
convertWethToWsteth(amount: bigint) {
const prom = new Promise<BuilderItemToProcess>((resolve) => {
const chainId = this._op.chainId;

let wethAddress: Address;
let wstethAdapterAddress: Address;
let wstethAddress: Address;
if (chainId === 1n) {
// mainnet
wethAddress = "0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2";
wstethAdapterAddress = "0x7f39C581F595B53c5cb19bD0b3f8dA6c935E2Ca0"; // TODO: fill with real address
wstethAddress = "0x7f39C581F595B53c5cb19bD0b3f8dA6c935E2Ca0";
} else {
throw new Error(`wsteth not supported on chain with id: ${chainId}`);
const wstethAdapterAddress =
this.config.protocolAllowlist.get(WSTETH_ADAPTER_NAME)?.address;
if (!wstethAdapterAddress) {
throw new Error(
`WstethAdapter not supported on chain with id: ${this._op.chainId}`
);
}

const wethAddress =
this.config.erc20s.get(WSTETH_ADAPTER_NAME)?.address;
if (!wethAddress) {
throw new Error(
`Weth not supported on chain with id: ${this._op.chainId}`
);
}

const wstethAddress =
this.config.erc20s.get(WSTETH_ADAPTER_NAME)?.address;
if (!wstethAddress) {
throw new Error(
`Wsteth not supported on chain with id: ${this._op.chainId}`
);
}

const encodedWeth = AssetTrait.erc20AddressToAsset(wethAddress);
Expand Down

0 comments on commit e833d12

Please sign in to comment.