Skip to content

Commit

Permalink
Merge pull request #56 from Once-Upon/jordan/ou-1238-dont-use-decode-…
Browse files Browse the repository at this point in the history
…in-ens-but-use-this-approach-instead

Use ABI style decode vs. relying on Once Upon API.
  • Loading branch information
pcowgill authored Dec 5, 2023
2 parents 404af17 + 017e006 commit 60cccde
Show file tree
Hide file tree
Showing 6 changed files with 105 additions and 72 deletions.
49 changes: 5 additions & 44 deletions src/helpers/constants.ts
Original file line number Diff line number Diff line change
@@ -1,52 +1,13 @@
import { InterfaceAbi } from '../types/Abi';

export const TOKEN_SWAP_CONTRACTS = [
'0xe592427a0aece92de3edee1f18e0157c05861564', // Uniswap V3 Router
'0x68b3465833fb72a70ecdf485e0e4c7bd8665fc45', // Uniswap V3router2
'0x7a250d5630b4cf539739df2c5dacb4c659f2488d', // Uniswap V2 Router2
'0xef1c6e67703c7bd7107eed8303fbe6ec2554bf6b', // Uniswap Universal Router1
'0x3fc91a3afd70395cd496c647d5a6cc9d4b2b7fad', // Uniswap Universal Router2
'0xd9e1ce17f2641f24ae83637ab66a2cca9c378b9f', // Sushiswap Router
'0x1111111254fb6c44bac0bed2854e76f90643097d', // 1inch Router
'0x881d40237659c251811cec9c364ef91dc08d300c', // Metamask Swap Router
'0xe66b31678d6c16e9ebf358268a790b763c133750', // Coinbase Wallet Swapper
'0x00000000009726632680fb29d3f7a9734e3010e2', // Rainbow Router
'0xdef1c0ded9bec7f1a1670819833240f027b25eff', // 0x Exchange Proxy. NOTE - This is both an erc20 swap and erc721 swap contract. This address is in both contract lists.
export const WETH_ADDRESSES = [
'0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2', // Ethereum
'0x4200000000000000000000000000000000000006', // OP Stack
'0xe5d7c2a44ffddf6b295a15c148167daaaf5cf34f', // Linea
'0x0000000000a39bb272e79075ade125fd351887ac', // Blur
];

export const KNOWN_ADDRESSES = {
NULL: '0x0000000000000000000000000000000000000000',
WETH: '0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2',
};

export const WETH_ADDRESSES = [
'0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2', // Ethereum
'0x4200000000000000000000000000000000000006', // Optimism
'0xe5d7c2a44ffddf6b295a15c148167daaaf5cf34f', // Linea
'0x0000000000a39bb272e79075ade125fd351887ac', // Blur
];
export const WETH_ABI: InterfaceAbi = [
{
constant: false,
inputs: [],
name: 'deposit',
outputs: [],
payable: true,
stateMutability: 'payable',
type: 'function',
},
{
constant: false,
inputs: [
{
name: 'wad',
type: 'uint256',
},
],
name: 'withdraw',
outputs: [],
payable: false,
stateMutability: 'nonpayable',
type: 'function',
},
];
35 changes: 30 additions & 5 deletions src/protocol/ens/constants.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,32 @@
export const ENSContracts = {
RegistrarV2: '0x283af0b28c62c092c9727f1ee09c02ca627eb7f5',
RegistrarV3: '0x253553366da8546fc250f225fe3d25d0c782303b',
ReverseRegistrar: '0x084b1c3c81545d370f3634392de611caabff8148',
Token: '0xc18360217d8f7ab5e7c516566761ea12ce7f9d72',
Reverse: '0x084b1c3c81545d370f3634392de611caabff8148',
RegistrarV2: {
address: '0x283af0b28c62c092c9727f1ee09c02ca627eb7f5',
abi: [
'function commit(bytes32 commitment)',
'function register(string name, address owner, uint256 duration, bytes32 secret)',
'function registerWithConfig(string name, address owner, uint256 duration, bytes32 secret, address resolver, address addr)',
'function renew(string name, uint256 duration)',
],
},
RegistrarV3: {
address: '0x253553366da8546fc250f225fe3d25d0c782303b',
abi: [
'function commit(bytes32 commitment)',
'function register(string name, address owner, uint256 duration, bytes32 secret)',
'function registerWithConfig(string name, address owner, uint256 duration, bytes32 secret, address resolver, address addr)',
'function renew(string name, uint256 duration)',
],
},
ReverseRegistrar: {
address: '0x084b1c3c81545d370f3634392de611caabff8148',
abi: [],
},
Token: {
address: '0xc18360217d8f7ab5e7c516566761ea12ce7f9d72',
abi: [],
},
Reverse: {
address: '0x084b1c3c81545d370f3634392de611caabff8148',
abi: ['function setName(string name)'],
},
};
39 changes: 25 additions & 14 deletions src/protocol/ens/registrar.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Transaction } from '../../types';
import { ENSContracts } from './constants';
import { decodeTransactionInput } from '../../helpers/utils';

export const ensContextualizer = (transaction: Transaction): Transaction => {
const isENS = detectENS(transaction);
Expand All @@ -9,22 +10,28 @@ export const ensContextualizer = (transaction: Transaction): Transaction => {
};

export const detectENS = (transaction: Transaction): boolean => {
if (transaction.decode === null) {
if (
transaction.to !== ENSContracts.RegistrarV2.address &&
transaction.to !== ENSContracts.RegistrarV3.address
) {
return false;
}

if (
transaction.to !== ENSContracts.RegistrarV2 &&
transaction.to !== ENSContracts.RegistrarV3
) {
let decode;
try {
decode = decodeTransactionInput(
transaction.input,
ENSContracts.RegistrarV2.abi,
);
} catch (e) {
return false;
}

if (
transaction.decode.name !== 'registerWithConfig' &&
transaction.decode.name !== 'register' &&
transaction.decode.name !== 'commit' &&
transaction.decode.name !== 'renew'
decode.name !== 'registerWithConfig' &&
decode.name !== 'register' &&
decode.name !== 'commit' &&
decode.name !== 'renew'
) {
return false;
}
Expand All @@ -34,11 +41,15 @@ export const detectENS = (transaction: Transaction): boolean => {

// Contextualize for mined txs
export const generateENSContext = (transaction: Transaction): Transaction => {
switch (transaction.decode.name) {
const decode = decodeTransactionInput(
transaction.input,
ENSContracts.RegistrarV2.abi,
);
switch (decode.name) {
case 'registerWithConfig':
case 'register': {
const name = transaction.decode.args[0];
const duration = parseInt(transaction.decode.args[2]);
const name = decode.args[0];
const duration = parseInt(decode.args[2]);
const durationInDays = Math.trunc(duration / 60 / 60 / 24);

transaction.context = {
Expand Down Expand Up @@ -101,8 +112,8 @@ export const generateENSContext = (transaction: Transaction): Transaction => {
}

case 'renew': {
const name = transaction.decode.args[0];
const duration = parseInt(transaction.decode.args[1]);
const name = decode.args[0];
const duration = parseInt(decode.args[1]);
const durationInDays = Math.trunc(duration / 60 / 60 / 24);

transaction.context = {
Expand Down
23 changes: 16 additions & 7 deletions src/protocol/ens/reverse.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Transaction } from '../../types';
import { ENSContracts } from './constants';
import { decodeTransactionInput } from '../../helpers/utils';

export const ensReverseContextualizer = (
transaction: Transaction,
Expand All @@ -11,29 +12,37 @@ export const ensReverseContextualizer = (
};

export const detectReverseENS = (transaction: Transaction): boolean => {
if (transaction.decode === null) {
if (transaction.to !== ENSContracts.Reverse.address) {
return false;
}

if (transaction.to !== ENSContracts.Reverse) {
let decode;
try {
decode = decodeTransactionInput(
transaction.input,
ENSContracts.Reverse.abi,
);
} catch (e) {
return false;
}

if (transaction.decode.name !== 'setName') {
if (decode.name !== 'setName') {
return false;
}

return true;
};

// Contextualize for mined txs
export const generateENSReverseContext = (
transaction: Transaction,
): Transaction => {
// Note: This isn't necessary now that we check for this in detect, but that's okay for now
switch (transaction.decode.name) {
const decode = decodeTransactionInput(
transaction.input,
ENSContracts.Reverse.abi,
);
switch (decode.name) {
case 'setName': {
const name = transaction.decode.args[0];
const name = decode.args[0];
transaction.context = {
summaries: {
category: 'IDENTITY',
Expand Down
27 changes: 27 additions & 0 deletions src/protocol/weth/constants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { InterfaceAbi } from 'src/types/Abi';

export const WETH_ABI: InterfaceAbi = [
{
constant: false,
inputs: [],
name: 'deposit',
outputs: [],
payable: true,
stateMutability: 'payable',
type: 'function',
},
{
constant: false,
inputs: [
{
name: 'wad',
type: 'uint256',
},
],
name: 'withdraw',
outputs: [],
payable: false,
stateMutability: 'nonpayable',
type: 'function',
},
];
4 changes: 2 additions & 2 deletions src/protocol/weth/weth.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { ethers } from 'ethers';
import { Transaction } from '../../types';
import { WETH_ADDRESSES, WETH_ABI } from '../../helpers/constants';
import { WETH_ADDRESSES } from '../../helpers/constants';
import { WETH_ABI } from './constants';
import { decodeTransactionInput } from '../../helpers/utils';

export const wethContextualizer = (transaction: Transaction): Transaction => {
Expand Down

0 comments on commit 60cccde

Please sign in to comment.