Skip to content

Commit

Permalink
Merge pull request #55 from bancorprotocol/new-abi-custom-fees
Browse files Browse the repository at this point in the history
  • Loading branch information
zavelevsky authored Aug 16, 2023
2 parents dd09992 + ae4b558 commit f396497
Show file tree
Hide file tree
Showing 8 changed files with 377 additions and 63 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "@bancor/carbon-sdk",
"type": "module",
"source": "src/index.ts",
"version": "0.0.81-DEV",
"version": "0.0.84-DEV",
"description": "The SDK is a READ-ONLY tool, intended to facilitate working with Carbon contracts. It's a convenient wrapper around our matching algorithm, allowing programs and users get a ready to use transaction data that will allow them to manage strategies and fulfill trades",
"main": "dist/index.js",
"module": "dist/index.js",
Expand Down
93 changes: 88 additions & 5 deletions src/abis/CarbonController.json
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,11 @@
"name": "AlreadyInitialized",
"type": "error"
},
{
"inputs": [],
"name": "BalanceMismatch",
"type": "error"
},
{
"inputs": [],
"name": "DeadlineExpired",
Expand Down Expand Up @@ -193,27 +198,27 @@
},
{
"inputs": [],
"name": "OutDated",
"name": "OrderDisabled",
"type": "error"
},
{
"inputs": [],
"name": "Overflow",
"name": "OutDated",
"type": "error"
},
{
"inputs": [],
"name": "PairAlreadyExists",
"name": "Overflow",
"type": "error"
},
{
"inputs": [],
"name": "PairDoesNotExist",
"name": "PairAlreadyExists",
"type": "error"
},
{
"inputs": [],
"name": "StrategyDoesNotExist",
"name": "PairDoesNotExist",
"type": "error"
},
{
Expand Down Expand Up @@ -300,6 +305,37 @@
"name": "PairCreated",
"type": "event"
},
{
"anonymous": false,
"inputs": [
{
"indexed": true,
"internalType": "Token",
"name": "token0",
"type": "address"
},
{
"indexed": true,
"internalType": "Token",
"name": "token1",
"type": "address"
},
{
"indexed": false,
"internalType": "uint32",
"name": "prevFeePPM",
"type": "uint32"
},
{
"indexed": false,
"internalType": "uint32",
"name": "newFeePPM",
"type": "uint32"
}
],
"name": "PairTradingFeePPMUpdated",
"type": "event"
},
{
"anonymous": false,
"inputs": [
Expand Down Expand Up @@ -1104,6 +1140,30 @@
"stateMutability": "view",
"type": "function"
},
{
"inputs": [
{
"internalType": "Token",
"name": "token0",
"type": "address"
},
{
"internalType": "Token",
"name": "token1",
"type": "address"
}
],
"name": "pairTradingFeePPM",
"outputs": [
{
"internalType": "uint32",
"name": "",
"type": "uint32"
}
],
"stateMutability": "view",
"type": "function"
},
{
"inputs": [],
"name": "pairs",
Expand Down Expand Up @@ -1225,6 +1285,29 @@
"stateMutability": "pure",
"type": "function"
},
{
"inputs": [
{
"internalType": "Token",
"name": "token0",
"type": "address"
},
{
"internalType": "Token",
"name": "token1",
"type": "address"
},
{
"internalType": "uint32",
"name": "newPairTradingFeePPM",
"type": "uint32"
}
],
"name": "setPairTradingFeePPM",
"outputs": [],
"stateMutability": "nonpayable",
"type": "function"
},
{
"inputs": [
{
Expand Down
50 changes: 46 additions & 4 deletions src/chain-cache/ChainCache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import {
import { Logger } from '../common/logger';
const logger = new Logger('ChainCache.ts');

const schemeVersion = 5; // bump this when the serialization format changes
const schemeVersion = 6; // bump this when the serialization format changes

type PairToStrategiesMap = { [key: string]: EncodedStrategy[] };
type StrategyById = { [key: string]: EncodedStrategy };
Expand All @@ -36,6 +36,7 @@ type SerializableDump = {
strategiesByPair: RetypeBigNumberToString<PairToStrategiesMap>;
strategiesById: RetypeBigNumberToString<StrategyById>;
ordersByDirectedPair: RetypeBigNumberToString<PairToDirectedOrdersMap>;
tradingFeePPMByPair: { [key: string]: number };
latestBlockNumber: number;
latestTradesByPair: { [key: string]: TradeData };
latestTradesByDirectedPair: { [key: string]: TradeData };
Expand All @@ -51,6 +52,7 @@ export class ChainCache extends (EventEmitter as new () => TypedEventEmitter<Cac
private _latestTradesByPair: { [key: string]: TradeData } = {};
private _latestTradesByDirectedPair: { [key: string]: TradeData } = {};
private _blocksMetadata: BlockMetadata[] = [];
private _tradingFeePPMByPair: { [key: string]: number } = {};

private _handleCacheMiss:
| ((token0: string, token1: string) => Promise<void>)
Expand Down Expand Up @@ -111,6 +113,7 @@ export class ChainCache extends (EventEmitter as new () => TypedEventEmitter<Cac
return acc;
}, {} as PairToDirectedOrdersMap);

this._tradingFeePPMByPair = parsedCache.tradingFeePPMByPair;
this._latestBlockNumber = parsedCache.latestBlockNumber;
this._latestTradesByPair = parsedCache.latestTradesByPair;
this._latestTradesByDirectedPair = parsedCache.latestTradesByDirectedPair;
Expand Down Expand Up @@ -147,6 +150,7 @@ export class ChainCache extends (EventEmitter as new () => TypedEventEmitter<Cac
},
{} as RetypeBigNumberToString<PairToDirectedOrdersMap>
),
tradingFeePPMByPair: this._tradingFeePPMByPair,
latestBlockNumber: this._latestBlockNumber,
latestTradesByPair: this._latestTradesByPair,
latestTradesByDirectedPair: this._latestTradesByDirectedPair,
Expand Down Expand Up @@ -184,9 +188,6 @@ export class ChainCache extends (EventEmitter as new () => TypedEventEmitter<Cac

//#region public getters

// It's useful to use the cache to store the up to date trading fee
public tradingFeePPM: number | undefined;

public async getStrategiesByPair(
token0: string,
token1: string
Expand Down Expand Up @@ -260,6 +261,15 @@ export class ChainCache extends (EventEmitter as new () => TypedEventEmitter<Cac
return this._latestBlockNumber;
}

public async getTradingFeePPMByPair(
token0: string,
token1: string
): Promise<number | undefined> {
await this._checkAndHandleCacheMiss(token0, token1);
const key = toPairKey(token0, token1);
return this._tradingFeePPMByPair[key];
}

public get blocksMetadata(): BlockMetadata[] {
return this._blocksMetadata;
}
Expand Down Expand Up @@ -310,6 +320,33 @@ export class ChainCache extends (EventEmitter as new () => TypedEventEmitter<Cac
}
}

/**
* This methods allows setting the trading fee of a pair.
* Note that fees can also be updated via `applyBatchedUpdates`.
* This specific method is useful when the fees were fetched from the chain
* as part of initialization or some other operation mode which doesn't
* rely on even processing
*
* @param {string} token0 - address of the first token of the pair
* @param {string} token1 - address of the second token of the pair
* @param tradingFeePPM - the pair's trading fee
*/
public addPairFees(
token0: string,
token1: string,
tradingFeePPM: number
): void {
logger.debug(
'Adding trading fee to pair',
token0,
token1,
'fee',
tradingFeePPM
);
const key = toPairKey(token0, token1);
this._tradingFeePPMByPair[key] = tradingFeePPM;
}

/**
* This method is to be used when events from a range of blocks are fetched
* and are to be applied to the cache.
Expand All @@ -329,13 +366,15 @@ export class ChainCache extends (EventEmitter as new () => TypedEventEmitter<Cac
*/
public applyBatchedUpdates(
latestBlockNumber: number,
latestFeeUpdates: [string, string, number][],
latestTrades: TradeData[],
createdStrategies: EncodedStrategy[],
updatedStrategies: EncodedStrategy[],
deletedStrategies: EncodedStrategy[]
): void {
logger.debug('Applying batched updates to cache', {
latestBlockNumber,
latestFeeUpdates,
latestTrades,
createdStrategies,
updatedStrategies,
Expand All @@ -358,6 +397,9 @@ export class ChainCache extends (EventEmitter as new () => TypedEventEmitter<Cac
this._deleteStrategy(strategy);
affectedPairs.add(toPairKey(strategy.token0, strategy.token1));
});
latestFeeUpdates.forEach(([token0, token1, newFee]) => {
this._tradingFeePPMByPair[toPairKey(token0, token1)] = newFee;
});
this._setLatestBlockNumber(latestBlockNumber);

if (affectedPairs.size > 0) {
Expand Down
Loading

0 comments on commit f396497

Please sign in to comment.