Initial build for QIE Mainnet Hackathon 3.0 (DeFi & Financial Infrastructure track). Includes: - packages/contracts: constant-product AMM (factory/pair/router), over-collateralized lending vault, and an oracle adapter wrapping QIE's real deployed price-feed contracts. 25/25 Foundry tests pass locally. Clean-room: no existing DEX/lending protocol source was read or copied. - packages/core: TypeScript SDK (viem-based) for QIE's chain, the real (corrected) oracle ABI, a fail-closed QUSDC helper, a Blockscout explorer client, and QIE Wallet connection constants. Live-verified against real testnet (chain 1983) and mainnet (chain 1990) RPCs and the real oracle contracts. - packages/cli: a small CLI built on @qie-forge/core. Real findings from building this, documented in README.md and docs/STATUS.md: the previously-documented testnet RPC URL is dead (replacement found and verified live); QIE's own oracle docs describe a getPrice(string) function that reverts on the real deployed contracts, which actually implement a Chainlink AggregatorV3-style interface (verified live via raw eth_call); the oracle is deployed on Mainnet only, not Testnet. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
235 lines
8.0 KiB
JavaScript
235 lines
8.0 KiB
JavaScript
import { ethers } from 'ethers';
|
|
import { ValidationRange } from './enums';
|
|
import * as random from './random';
|
|
|
|
export const SIG_VALIDATION_SUCCESS = '0x0000000000000000000000000000000000000000';
|
|
export const SIG_VALIDATION_FAILURE = '0x0000000000000000000000000000000000000001';
|
|
const PAYMASTER_SIG_MAGIC = '0x22e325a297439656';
|
|
|
|
const BLOCK_RANGE_FLAG = 0x800000000000n;
|
|
const BLOCK_RANGE_MASK = 0x7fffffffffffn;
|
|
|
|
function getAddress(account) {
|
|
return account.target ?? account.address ?? account;
|
|
}
|
|
|
|
function pack(left, right) {
|
|
return ethers.solidityPacked(['uint128', 'uint128'], [left, right]);
|
|
}
|
|
|
|
export function packValidationData(validAfter, validUntil, authorizer, range = undefined) {
|
|
// if range is not specified, use the value as provided,
|
|
// otherwise, clean the values (& BLOCK_RANGE_MASK) and set the flag if corresponding to the range.
|
|
// in Block range, `validUntil == 0` is left as 0 so the decoder's `validUntil == 0 -> max` rule applies.
|
|
return ethers.solidityPacked(
|
|
['uint48', 'uint48', 'address'],
|
|
[
|
|
range === undefined
|
|
? BigInt(validAfter)
|
|
: (BigInt(validAfter) & BLOCK_RANGE_MASK) | (range == ValidationRange.Block ? BLOCK_RANGE_FLAG : 0n),
|
|
range === undefined
|
|
? BigInt(validUntil)
|
|
: range == ValidationRange.Block && (BigInt(validUntil) & BLOCK_RANGE_MASK) == 0n
|
|
? 0n
|
|
: (BigInt(validUntil) & BLOCK_RANGE_MASK) | (range == ValidationRange.Block ? BLOCK_RANGE_FLAG : 0n),
|
|
typeof authorizer == 'boolean'
|
|
? authorizer
|
|
? SIG_VALIDATION_SUCCESS
|
|
: SIG_VALIDATION_FAILURE
|
|
: getAddress(authorizer),
|
|
],
|
|
);
|
|
}
|
|
|
|
export function packInitCode(factory, factoryData) {
|
|
return ethers.solidityPacked(['address', 'bytes'], [getAddress(factory), factoryData]);
|
|
}
|
|
|
|
export function packPaymasterAndData(
|
|
paymaster,
|
|
paymasterVerificationGasLimit,
|
|
paymasterPostOpGasLimit,
|
|
paymasterData,
|
|
signature = undefined,
|
|
) {
|
|
return ethers.concat([
|
|
ethers.solidityPacked(
|
|
['address', 'uint128', 'uint128', 'bytes'],
|
|
[getAddress(paymaster), paymasterVerificationGasLimit, paymasterPostOpGasLimit, paymasterData],
|
|
),
|
|
signature === undefined
|
|
? '0x'
|
|
: ethers.solidityPacked(
|
|
['bytes', 'uint16', 'bytes8'],
|
|
[signature, ethers.dataLength(signature), PAYMASTER_SIG_MAGIC],
|
|
),
|
|
]);
|
|
}
|
|
|
|
/// Represent one user operation
|
|
export class UserOperation {
|
|
constructor(params) {
|
|
this.sender = getAddress(params.sender);
|
|
this.nonce = params.nonce;
|
|
this.factory = params.factory ?? undefined;
|
|
this.factoryData = params.factoryData ?? '0x';
|
|
this.callData = params.callData ?? '0x';
|
|
this.verificationGas = params.verificationGas ?? 10_000_000n;
|
|
this.callGas = params.callGas ?? 100_000n;
|
|
this.preVerificationGas = params.preVerificationGas ?? 100_000n;
|
|
this.maxPriorityFee = params.maxPriorityFee ?? 100_000n;
|
|
this.maxFeePerGas = params.maxFeePerGas ?? 100_000n;
|
|
this.paymaster = params.paymaster ?? undefined;
|
|
this.paymasterVerificationGasLimit = params.paymasterVerificationGasLimit ?? 0n;
|
|
this.paymasterPostOpGasLimit = params.paymasterPostOpGasLimit ?? 0n;
|
|
this.paymasterData = params.paymasterData ?? '0x';
|
|
this.paymasterSignature = params.paymasterSignature ?? undefined;
|
|
this.signature = params.signature ?? '0x';
|
|
}
|
|
|
|
get packed() {
|
|
return {
|
|
sender: this.sender,
|
|
nonce: this.nonce,
|
|
initCode: this.factory ? packInitCode(this.factory, this.factoryData) : '0x',
|
|
callData: this.callData,
|
|
accountGasLimits: pack(this.verificationGas, this.callGas),
|
|
preVerificationGas: this.preVerificationGas,
|
|
gasFees: pack(this.maxPriorityFee, this.maxFeePerGas),
|
|
paymasterAndData: this.paymaster
|
|
? packPaymasterAndData(
|
|
this.paymaster,
|
|
this.paymasterVerificationGasLimit,
|
|
this.paymasterPostOpGasLimit,
|
|
this.paymasterData,
|
|
this.paymasterSignature,
|
|
)
|
|
: '0x',
|
|
signature: this.signature,
|
|
};
|
|
}
|
|
|
|
hash(entrypoint, overrides = {}) {
|
|
return entrypoint.getUserOpHash({ ...this.packed, ...overrides });
|
|
}
|
|
}
|
|
|
|
const parseInitCode = initCode => ({
|
|
factory: ethers.dataSlice(initCode, 0, 20),
|
|
factoryData: ethers.dataSlice(initCode, 20),
|
|
});
|
|
|
|
/// Global ERC-4337 environment helper.
|
|
export class ERC4337Helper {
|
|
constructor(connection) {
|
|
this.connection = connection;
|
|
this.factoryAsPromise = connection.ethers.deployContract('$Create2');
|
|
}
|
|
|
|
async wait() {
|
|
this.factory = await this.factoryAsPromise;
|
|
return this;
|
|
}
|
|
|
|
async newAccount(name, extraArgs = [], params = {}) {
|
|
const env = {
|
|
entrypoint: params.entrypoint ?? this.connection.ethers.predeploy.entrypoint.v09,
|
|
senderCreator: params.senderCreator ?? this.connection.ethers.predeploy.senderCreator.v09,
|
|
};
|
|
|
|
const { factory } = await this.wait();
|
|
|
|
const accountFactory = await this.connection.ethers.getContractFactory(name);
|
|
|
|
if (params.eip7702signer) {
|
|
const delegate = await accountFactory.deploy(...extraArgs);
|
|
const instance = await params.eip7702signer.getAddress().then(address => accountFactory.attach(address));
|
|
const authorization = await params.eip7702signer.authorize({ address: delegate.target });
|
|
return new EIP7702SmartAccount(instance, authorization, env, delegate);
|
|
} else {
|
|
const initCode = await accountFactory
|
|
.getDeployTransaction(...extraArgs)
|
|
.then(tx => factory.interface.encodeFunctionData('$deploy', [0, params.salt ?? random.bytes32(), tx.data]))
|
|
.then(deployCode => ethers.concat([factory.target, deployCode]));
|
|
|
|
const instance = await this.connection.ethers.provider
|
|
.call({
|
|
from: env.entrypoint,
|
|
to: env.senderCreator,
|
|
data: env.senderCreator.interface.encodeFunctionData('createSender', [initCode]),
|
|
})
|
|
.then(result => ethers.getAddress(ethers.dataSlice(result, -20)))
|
|
.then(address => accountFactory.attach(address));
|
|
|
|
return new SmartAccount(instance, initCode, env);
|
|
}
|
|
}
|
|
}
|
|
|
|
/// Represent one ERC-4337 account contract.
|
|
class SmartAccount extends ethers.BaseContract {
|
|
constructor(instance, initCode, env) {
|
|
super(instance.target, instance.interface, instance.runner, instance.deployTx);
|
|
this.address = instance.target;
|
|
this.initCode = initCode;
|
|
this.env = env;
|
|
}
|
|
|
|
async deploy(account = this.runner) {
|
|
const { factory: to, factoryData: data } = parseInitCode(this.initCode);
|
|
this.deployTx = await account.sendTransaction({ to, data });
|
|
return this;
|
|
}
|
|
|
|
async createUserOp(userOp = {}) {
|
|
userOp.sender ??= this;
|
|
userOp.nonce ??= await this.env.entrypoint.getNonce(userOp.sender, 0);
|
|
if (ethers.isAddressable(userOp.paymaster)) {
|
|
userOp.paymaster = await ethers.resolveAddress(userOp.paymaster);
|
|
userOp.paymasterVerificationGasLimit ??= 100_000n;
|
|
userOp.paymasterPostOpGasLimit ??= 100_000n;
|
|
}
|
|
return new UserOperationWithContext(userOp);
|
|
}
|
|
}
|
|
|
|
class EIP7702SmartAccount extends SmartAccount {
|
|
constructor(instance, authorization, env, delegate) {
|
|
super(instance, undefined, env);
|
|
this.authorization = authorization;
|
|
this.delegate = delegate;
|
|
}
|
|
|
|
deploy() {
|
|
return this.runner
|
|
.sendTransaction({
|
|
to: ethers.ZeroAddress,
|
|
authorizationList: [this.authorization],
|
|
})
|
|
.then(() => this);
|
|
}
|
|
}
|
|
|
|
class UserOperationWithContext extends UserOperation {
|
|
constructor(userOp) {
|
|
super(userOp);
|
|
this._sender = userOp.sender;
|
|
}
|
|
|
|
addInitCode() {
|
|
if (this._sender?.initCode) {
|
|
return Object.assign(this, parseInitCode(this._sender.initCode));
|
|
} else throw new Error('No init code available for the sender of this user operation');
|
|
}
|
|
|
|
getAuthorization() {
|
|
if (this._sender?.authorization) {
|
|
return this._sender.authorization;
|
|
} else throw new Error('No EIP-7702 authorization available for the sender of this user operation');
|
|
}
|
|
|
|
hash(overrides = {}) {
|
|
return super.hash(this._sender.env.entrypoint, overrides);
|
|
}
|
|
}
|