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>
113 lines
3.2 KiB
JavaScript
113 lines
3.2 KiB
JavaScript
import { ethers } from 'ethers';
|
|
import { formatType } from './eip712';
|
|
|
|
export const PersonalSign = formatType({ prefixed: 'bytes' });
|
|
|
|
export const TypedDataSign = contentsTypeName =>
|
|
formatType({
|
|
contents: contentsTypeName,
|
|
name: 'string',
|
|
version: 'string',
|
|
chainId: 'uint256',
|
|
verifyingContract: 'address',
|
|
salt: 'bytes32',
|
|
});
|
|
|
|
export class ERC7739Signer extends ethers.AbstractSigner {
|
|
#signer;
|
|
#domain;
|
|
|
|
constructor(signer, domain) {
|
|
super(signer.provider);
|
|
this.#signer = signer;
|
|
this.#domain = domain;
|
|
}
|
|
|
|
static from(signer, domain) {
|
|
return new this(signer, domain);
|
|
}
|
|
|
|
get signingKey() {
|
|
return this.#signer.signingKey;
|
|
}
|
|
|
|
get privateKey() {
|
|
return this.#signer.privateKey;
|
|
}
|
|
|
|
async getAddress() {
|
|
return this.#signer.getAddress();
|
|
}
|
|
|
|
connect(provider) {
|
|
this.#signer.connect(provider);
|
|
}
|
|
|
|
async signTransaction(tx) {
|
|
return this.#signer.signTransaction(tx);
|
|
}
|
|
|
|
async signMessage(message) {
|
|
return this.#signer.signTypedData(this.#domain, { PersonalSign }, ERC4337Utils.preparePersonalSign(message));
|
|
}
|
|
|
|
async signTypedData(domain, types, value) {
|
|
const { allTypes, contentsTypeName, contentsDescr } = ERC4337Utils.getContentsDetail(types);
|
|
|
|
return Promise.resolve(
|
|
this.#signer.signTypedData(domain, allTypes, ERC4337Utils.prepareSignTypedData(value, this.#domain)),
|
|
).then(signature =>
|
|
ethers.concat([
|
|
signature,
|
|
ethers.TypedDataEncoder.hashDomain(domain), // appDomainSeparator
|
|
ethers.TypedDataEncoder.hashStruct(contentsTypeName, types, value), // contentsHash
|
|
ethers.toUtf8Bytes(contentsDescr),
|
|
ethers.toBeHex(contentsDescr.length, 2),
|
|
]),
|
|
);
|
|
}
|
|
}
|
|
|
|
export class ERC4337Utils {
|
|
static preparePersonalSign(message) {
|
|
return {
|
|
prefixed: ethers.concat([
|
|
ethers.toUtf8Bytes(ethers.MessagePrefix),
|
|
ethers.toUtf8Bytes(message.length.toString()),
|
|
typeof message === 'string' ? ethers.toUtf8Bytes(message) : message,
|
|
]),
|
|
};
|
|
}
|
|
|
|
static prepareSignTypedData(contents, signerDomain) {
|
|
return {
|
|
name: signerDomain.name ?? '',
|
|
version: signerDomain.version ?? '',
|
|
chainId: signerDomain.chainId ?? 0,
|
|
verifyingContract: signerDomain.verifyingContract ?? ethers.ZeroAddress,
|
|
salt: signerDomain.salt ?? ethers.ZeroHash,
|
|
contents,
|
|
};
|
|
}
|
|
|
|
static getContentsDetail(contentsTypes, contentsTypeName = Object.keys(contentsTypes).at(0)) {
|
|
// Examples values
|
|
//
|
|
// contentsTypeName B
|
|
// typedDataSignType TypedDataSign(B contents,...)A(uint256 v)B(Z z)Z(A a)
|
|
// contentsType A(uint256 v)B(Z z)Z(A a)
|
|
// contentsDescr A(uint256 v)B(Z z)Z(A a)B
|
|
const allTypes = { TypedDataSign: TypedDataSign(contentsTypeName), ...contentsTypes };
|
|
const typedDataSignType = ethers.TypedDataEncoder.from(allTypes).encodeType('TypedDataSign');
|
|
const contentsType = typedDataSignType.slice(typedDataSignType.indexOf(')') + 1); // Remove TypedDataSign (first object)
|
|
const contentsDescr = contentsType + (contentsType.startsWith(contentsTypeName) ? '' : contentsTypeName);
|
|
|
|
return {
|
|
allTypes,
|
|
contentsTypes,
|
|
contentsTypeName,
|
|
contentsDescr,
|
|
};
|
|
}
|
|
}
|