Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix/mark pools as existing #500

Merged
merged 14 commits into from
Oct 6, 2023
Merged
300 changes: 300 additions & 0 deletions src/abi/pancakeswap-v3/PancakeswapV3Factory.abi.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,300 @@
[
{
"inputs": [
{ "internalType": "address", "name": "_poolDeployer", "type": "address" }
],
"stateMutability": "nonpayable",
"type": "constructor"
},
{
"anonymous": false,
"inputs": [
{
"indexed": true,
"internalType": "uint24",
"name": "fee",
"type": "uint24"
},
{
"indexed": true,
"internalType": "int24",
"name": "tickSpacing",
"type": "int24"
}
],
"name": "FeeAmountEnabled",
"type": "event"
},
{
"anonymous": false,
"inputs": [
{
"indexed": true,
"internalType": "uint24",
"name": "fee",
"type": "uint24"
},
{
"indexed": false,
"internalType": "bool",
"name": "whitelistRequested",
"type": "bool"
},
{
"indexed": false,
"internalType": "bool",
"name": "enabled",
"type": "bool"
}
],
"name": "FeeAmountExtraInfoUpdated",
"type": "event"
},
{
"anonymous": false,
"inputs": [
{
"indexed": true,
"internalType": "address",
"name": "oldOwner",
"type": "address"
},
{
"indexed": true,
"internalType": "address",
"name": "newOwner",
"type": "address"
}
],
"name": "OwnerChanged",
"type": "event"
},
{
"anonymous": false,
"inputs": [
{
"indexed": true,
"internalType": "address",
"name": "token0",
"type": "address"
},
{
"indexed": true,
"internalType": "address",
"name": "token1",
"type": "address"
},
{
"indexed": true,
"internalType": "uint24",
"name": "fee",
"type": "uint24"
},
{
"indexed": false,
"internalType": "int24",
"name": "tickSpacing",
"type": "int24"
},
{
"indexed": false,
"internalType": "address",
"name": "pool",
"type": "address"
}
],
"name": "PoolCreated",
"type": "event"
},
{
"anonymous": false,
"inputs": [
{
"indexed": true,
"internalType": "address",
"name": "lmPoolDeployer",
"type": "address"
}
],
"name": "SetLmPoolDeployer",
"type": "event"
},
{
"anonymous": false,
"inputs": [
{
"indexed": true,
"internalType": "address",
"name": "user",
"type": "address"
},
{
"indexed": false,
"internalType": "bool",
"name": "verified",
"type": "bool"
}
],
"name": "WhiteListAdded",
"type": "event"
},
{
"inputs": [
{ "internalType": "address", "name": "pool", "type": "address" },
{ "internalType": "address", "name": "recipient", "type": "address" },
{
"internalType": "uint128",
"name": "amount0Requested",
"type": "uint128"
},
{
"internalType": "uint128",
"name": "amount1Requested",
"type": "uint128"
}
],
"name": "collectProtocol",
"outputs": [
{ "internalType": "uint128", "name": "amount0", "type": "uint128" },
{ "internalType": "uint128", "name": "amount1", "type": "uint128" }
],
"stateMutability": "nonpayable",
"type": "function"
},
{
"inputs": [
{ "internalType": "address", "name": "tokenA", "type": "address" },
{ "internalType": "address", "name": "tokenB", "type": "address" },
{ "internalType": "uint24", "name": "fee", "type": "uint24" }
],
"name": "createPool",
"outputs": [
{ "internalType": "address", "name": "pool", "type": "address" }
],
"stateMutability": "nonpayable",
"type": "function"
},
{
"inputs": [
{ "internalType": "uint24", "name": "fee", "type": "uint24" },
{ "internalType": "int24", "name": "tickSpacing", "type": "int24" }
],
"name": "enableFeeAmount",
"outputs": [],
"stateMutability": "nonpayable",
"type": "function"
},
{
"inputs": [{ "internalType": "uint24", "name": "", "type": "uint24" }],
"name": "feeAmountTickSpacing",
"outputs": [{ "internalType": "int24", "name": "", "type": "int24" }],
"stateMutability": "view",
"type": "function"
},
{
"inputs": [{ "internalType": "uint24", "name": "", "type": "uint24" }],
"name": "feeAmountTickSpacingExtraInfo",
"outputs": [
{ "internalType": "bool", "name": "whitelistRequested", "type": "bool" },
{ "internalType": "bool", "name": "enabled", "type": "bool" }
],
"stateMutability": "view",
"type": "function"
},
{
"inputs": [
{ "internalType": "address", "name": "", "type": "address" },
{ "internalType": "address", "name": "", "type": "address" },
{ "internalType": "uint24", "name": "", "type": "uint24" }
],
"name": "getPool",
"outputs": [{ "internalType": "address", "name": "", "type": "address" }],
"stateMutability": "view",
"type": "function"
},
{
"inputs": [],
"name": "lmPoolDeployer",
"outputs": [{ "internalType": "address", "name": "", "type": "address" }],
"stateMutability": "view",
"type": "function"
},
{
"inputs": [],
"name": "owner",
"outputs": [{ "internalType": "address", "name": "", "type": "address" }],
"stateMutability": "view",
"type": "function"
},
{
"inputs": [],
"name": "poolDeployer",
"outputs": [{ "internalType": "address", "name": "", "type": "address" }],
"stateMutability": "view",
"type": "function"
},
{
"inputs": [
{ "internalType": "uint24", "name": "fee", "type": "uint24" },
{ "internalType": "bool", "name": "whitelistRequested", "type": "bool" },
{ "internalType": "bool", "name": "enabled", "type": "bool" }
],
"name": "setFeeAmountExtraInfo",
"outputs": [],
"stateMutability": "nonpayable",
"type": "function"
},
{
"inputs": [
{ "internalType": "address", "name": "pool", "type": "address" },
{ "internalType": "uint32", "name": "feeProtocol0", "type": "uint32" },
{ "internalType": "uint32", "name": "feeProtocol1", "type": "uint32" }
],
"name": "setFeeProtocol",
"outputs": [],
"stateMutability": "nonpayable",
"type": "function"
},
{
"inputs": [
{ "internalType": "address", "name": "pool", "type": "address" },
{ "internalType": "address", "name": "lmPool", "type": "address" }
],
"name": "setLmPool",
"outputs": [],
"stateMutability": "nonpayable",
"type": "function"
},
{
"inputs": [
{
"internalType": "address",
"name": "_lmPoolDeployer",
"type": "address"
}
],
"name": "setLmPoolDeployer",
"outputs": [],
"stateMutability": "nonpayable",
"type": "function"
},
{
"inputs": [
{ "internalType": "address", "name": "_owner", "type": "address" }
],
"name": "setOwner",
"outputs": [],
"stateMutability": "nonpayable",
"type": "function"
},
{
"inputs": [
{ "internalType": "address", "name": "user", "type": "address" },
{ "internalType": "bool", "name": "verified", "type": "bool" }
],
"name": "setWhiteListAddress",
"outputs": [],
"stateMutability": "nonpayable",
"type": "function"
}
]
4 changes: 4 additions & 0 deletions src/dex-helper/dummy-dex-helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,10 @@ class DummyCache implements ICache {
return 0;
}

async zrem(key: string, membersKeys: string[]): Promise<number> {
return 0;
}

async zadd(key: string, bulkItemsToAdd: (number | string)[], option?: 'NX') {
return 0;
}
Expand Down
2 changes: 2 additions & 0 deletions src/dex-helper/icache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ export interface ICache {

zremrangebyscore(key: string, min: number, max: number): Promise<number>;

zrem(key: string, membersKeys: string[]): Promise<number>;

zscore(setKey: string, key: string): Promise<string | null>;

sismember(setKey: string, key: string): Promise<boolean>;
Expand Down
73 changes: 73 additions & 0 deletions src/dex/algebra/algebra-factory.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
import { Interface } from '@ethersproject/abi';
import { DeepReadonly } from 'ts-essentials';
import FactoryABI from '../../abi/algebra/AlgebraFactory-v1_1.abi.json';
import { IDexHelper } from '../../dex-helper/idex-helper';
import { StatefulEventSubscriber } from '../../stateful-event-subscriber';
import { Address, Log, Logger } from '../../types';
import { LogDescription } from 'ethers/lib/utils';
import { FactoryState } from './types';

export type OnPoolCreatedCallback = ({
token0,
token1,
}: {
token0: string;
token1: string;
}) => FactoryState | null;

/*
* "Stateless" event subscriber in order to capture "PoolCreated" event on new pools created.
* State is present, but it's a placeholder to actually make the events reach handlers (if there's no previous state - `processBlockLogs` is not called)
*/
export class AlgebraFactory extends StatefulEventSubscriber<FactoryState> {
handlers: {
[event: string]: (event: any) => DeepReadonly<FactoryState> | null;
} = {};

logDecoder: (log: Log) => any;

public readonly factoryIface = new Interface(FactoryABI);

constructor(
readonly dexHelper: IDexHelper,
parentName: string,
protected readonly factoryAddress: Address,
logger: Logger,
protected readonly onPoolCreated: OnPoolCreatedCallback,
mapKey: string = '',
) {
super(parentName, `${parentName} Factory`, dexHelper, logger, true, mapKey);

this.addressesSubscribed = [factoryAddress];

this.logDecoder = (log: Log) => this.factoryIface.parseLog(log);

this.handlers['Pool'] = this.handleNewPool.bind(this);
}

generateState(): FactoryState {
JackieJoo marked this conversation as resolved.
Show resolved Hide resolved
return {
token0: '',
token1: '',
};
}

protected processLog(
_: DeepReadonly<FactoryState>,
log: Readonly<Log>,
): DeepReadonly<FactoryState> | null {
const event = this.logDecoder(log);
if (event.name in this.handlers) {
return this.handlers[event.name](event);
}

return null;
}

handleNewPool(event: LogDescription) {
const token0 = event.args.token0;
const token1 = event.args.token1;

return this.onPoolCreated({ token0, token1 });
}
}
Loading
Loading