// What the relayer agrees to carry. A signed transaction is read from its bytes and // accepted only if it does one of the few things the Kinetiq Options app needs. // Everything else is refused before anything is run or posted. import { Transaction, Interface, getAddress } from "ethers"; export const OPTIONS_ABI = [ "function write(uint128 amount, uint128 strike, uint64 expiry, uint128 premium) returns (uint256)", "function cancel(uint256 id)", "function reclaim(uint256 id)", "function buy(uint256 id) payable", "function transferOption(uint256 id, address to)", "function exercise(uint256 id)", ]; const options = new Interface(OPTIONS_ABI); const erc20 = new Interface(["function approve(address spender, uint256 value) returns (bool)"]); const wrapper = new Interface(["function deposit() payable", "function withdraw(uint256 value)"]); const refuse = (code, reason) => ({ ok: false, code, reason }); const same = (a, b) => !!a && !!b && getAddress(a) === getAddress(b); /** * @param {string} raw the signed transaction, 0x-prefixed * @param {{chainId:number, options:string, optionsHype?:string, whype?:string, tokens:string[], maxGas?:bigint, maxFeePerGas?:bigint}} cfg * `options` locks kHYPE. `optionsHype`, when set, is the same contract locking wrapped HYPE (`whype`). * @returns {{ok:true, tx:Transaction, from:string, hash:string, call:string} | {ok:false, code:string, reason:string}} */ export function checkTx(raw, cfg) { let tx; try { tx = Transaction.from(raw); } catch { return refuse("malformed", "not a valid signed transaction"); } if (!tx.isSigned() || !tx.from) return refuse("unsigned", "the transaction carries no valid signature"); if (tx.chainId !== BigInt(cfg.chainId)) return refuse("chain", `signed for chain ${tx.chainId}, expected ${cfg.chainId}`); if (tx.type !== 0 && tx.type !== 2) return refuse("type", "only legacy and EIP-1559 transactions are carried"); if (!tx.to) return refuse("scope", "contract creation is not carried"); if (tx.gasLimit > (cfg.maxGas ?? 800_000n)) return refuse("gas", "gas limit above what this app ever needs"); const cap = tx.maxFeePerGas ?? tx.gasPrice ?? 0n; if (cap > (cfg.maxFeePerGas ?? 500_000_000_000n)) return refuse("fee", "fee cap unreasonably high"); const done = (call) => ({ ok: true, tx, from: tx.from, hash: tx.hash, call }); const contracts = [cfg.options, cfg.optionsHype].filter(Boolean); const hypeMarket = !!cfg.optionsHype && !!cfg.whype; if (contracts.some((c) => same(c, tx.to))) { let parsed; try { parsed = options.parseTransaction({ data: tx.data, value: tx.value }); } catch { parsed = null; } if (!parsed) return refuse("scope", "not a call this app makes on the options contract"); if (tx.value !== 0n && parsed.name !== "buy") return refuse("scope", "only buy may carry HYPE"); return done(parsed.name); } // Plain HYPE is wrapped before it is locked, and unwrapped when it comes back. if (hypeMarket && same(tx.to, cfg.whype)) { let w; try { w = wrapper.parseTransaction({ data: tx.data, value: tx.value }); } catch { w = null; } if (w?.name === "deposit") return tx.value > 0n ? done("wrap") : refuse("scope", "wrapping needs an amount of HYPE"); if (w?.name === "withdraw") return tx.value === 0n ? done("unwrap") : refuse("scope", "unwrapping carries no HYPE"); } if ([...(cfg.tokens ?? []), hypeMarket ? cfg.whype : null].some((t) => same(t, tx.to))) { let parsed; try { parsed = erc20.parseTransaction({ data: tx.data, value: tx.value }); } catch { parsed = null; } if (!parsed || tx.value !== 0n) return refuse("scope", "only approve is carried on the tokens"); if (!contracts.some((c) => same(c, parsed.args.spender))) return refuse("scope", "approve must name the options contract as spender"); return done("approve"); } return refuse("scope", "this relayer only carries Kinetiq Options transactions"); }