This repository has been archived by the owner on Jun 11, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #57 from LiskHQ/feature/intro-auxillary-functions
Implement internal auxiliary functions - Closes #30
- Loading branch information
Showing
13 changed files
with
303 additions
and
32 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,16 @@ | ||
import { Application, PartialApplicationConfig } from 'lisk-sdk'; | ||
import { registerModules } from './modules'; | ||
import { registerPlugins } from './plugins'; | ||
|
||
import { Application, PartialApplicationConfig, TokenModule, ValidatorsModule } from 'lisk-sdk'; | ||
|
||
import { DexModule } from './modules'; | ||
|
||
export const getApplication = (config: PartialApplicationConfig): Application => { | ||
const { app } = Application.defaultApplication(config); | ||
registerModules(app); | ||
registerPlugins(app); | ||
const dexModule = new DexModule(); | ||
const tokenModule = new TokenModule(); | ||
const validatorModule = new ValidatorsModule(); | ||
|
||
dexModule.addDependencies(tokenModule.api, validatorModule.api); | ||
app.registerModule(dexModule); | ||
|
||
return app; | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1 @@ | ||
/* eslint-disable @typescript-eslint/no-empty-function */ | ||
import { Application } from 'lisk-sdk'; | ||
import { DexModule } from './modules/dex/module'; | ||
|
||
export const registerModules = (app: Application): void => { | ||
app.registerModule(new DexModule()); | ||
}; | ||
export { DexModule } from './modules/dex/module'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
/* | ||
* Copyright © 2022 Lisk Foundation | ||
* | ||
* See the LICENSE file at the top-level directory of this distribution | ||
* for licensing information. | ||
* | ||
* Unless otherwise agreed in a custom licensing agreement with the Lisk Foundation, | ||
* no part of this software, including this file, may be copied, modified, | ||
* propagated, or distributed except according to the terms contained in the | ||
* LICENSE file. | ||
* | ||
* Removal or modification of this copyright notice is prohibited. | ||
*/ | ||
import { ModuleConfig, ModuleConfigJSON } from './types'; | ||
|
||
export function getModuleConfig(config: ModuleConfigJSON): ModuleConfig { | ||
return { | ||
...config, | ||
tokenIDReclaim: Buffer.from(config.tokenIDReclaim, 'hex'), | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
/* | ||
* Copyright © 2022 Lisk Foundation | ||
* | ||
* See the LICENSE file at the top-level directory of this distribution | ||
* for licensing information. | ||
* | ||
* Unless otherwise agreed in a custom licensing agreement with the Lisk Foundation, | ||
* no part of this software, including this file, may be copied, modified, | ||
* propagated, or distributed except according to the terms contained in the | ||
* LICENSE file. | ||
* | ||
* Removal or modification of this copyright notice is prohibited. | ||
*/ | ||
|
||
|
||
import { | ||
TokenAPI | ||
} from 'lisk-sdk'; | ||
|
||
import { | ||
utils | ||
} from '@liskhq/lisk-cryptography'; | ||
|
||
import { | ||
NUM_BYTES_ADDRESS, | ||
NUM_BYTES_TOKEN_ID, | ||
NUM_BYTES_POSITION_ID, | ||
MODULE_ID_DEX | ||
} from '../constants'; | ||
|
||
import { | ||
uint32beInv | ||
} from "./bigEndian"; | ||
|
||
import { | ||
PoolID, | ||
PositionID, | ||
Address, | ||
TokenID, | ||
ModuleConfig | ||
} from "../types"; | ||
|
||
export const poolIdToAddress = (poolId: PoolID): Address => { | ||
const _address: Buffer = utils.hash(poolId); | ||
return _address.slice(0, NUM_BYTES_ADDRESS); | ||
} | ||
|
||
export const getToken0Id = (poolId: PoolID): TokenID => poolId.slice(0, NUM_BYTES_TOKEN_ID + 1); | ||
|
||
export const getToken1Id = (poolId: PoolID): TokenID => poolId.slice(NUM_BYTES_TOKEN_ID, (2 * NUM_BYTES_TOKEN_ID) + 1); | ||
|
||
export const getFeeTier = (poolId: PoolID): number => { | ||
const _buffer: Buffer = poolId.slice(-4); | ||
const _hexBuffer: string = _buffer.toString('hex'); | ||
|
||
return uint32beInv(_hexBuffer); | ||
} | ||
|
||
export const getPositionIndex = (positionId: PositionID): number => { | ||
const _buffer: Buffer = positionId.slice(2 * NUM_BYTES_POSITION_ID, NUM_BYTES_ADDRESS); | ||
const _hexBuffer: string = _buffer.toString('hex'); | ||
|
||
return uint32beInv(_hexBuffer); | ||
} | ||
|
||
export const transferToPool = async (tokenAPI: TokenAPI, apiContext, senderAddress: Address, poolId: PoolID, tokenId: TokenID, amount: bigint): Promise < void > => { | ||
const poolAddress = poolIdToAddress(poolId); | ||
await tokenAPI.transfer(apiContext, senderAddress, poolAddress, tokenId, amount); | ||
await tokenAPI.lock(apiContext, poolAddress, MODULE_ID_DEX, tokenId, amount); | ||
} | ||
|
||
export const transferFromPool = async ( | ||
tokenAPI: TokenAPI, | ||
apiContext, | ||
poolId: PoolID, | ||
recipientAddress: Address, | ||
tokenId: TokenID, | ||
amount: bigint | ||
): Promise < void > => { | ||
const poolAddress = poolIdToAddress(poolId) | ||
await tokenAPI.unlock(apiContext, poolAddress, MODULE_ID_DEX, tokenId, amount) | ||
await tokenAPI.transfer(apiContext, poolAddress, recipientAddress, tokenId, amount) | ||
} | ||
|
||
export const transferPoolToPool = async ( | ||
tokenAPI: TokenAPI, | ||
apiContext, | ||
poolIdSend: PoolID, | ||
poolIdReceive: PoolID, | ||
tokenId: TokenID, | ||
amount: bigint | ||
): Promise < void > => { | ||
const poolAddressSend = poolIdToAddress(poolIdSend) | ||
const poolAddressReceive = poolIdToAddress(poolIdReceive) | ||
await tokenAPI.unlock(apiContext, poolAddressSend, MODULE_ID_DEX, tokenId, amount) | ||
await tokenAPI.transfer(apiContext, poolAddressSend, poolAddressReceive, tokenId, amount) | ||
await tokenAPI.lock(apiContext, poolAddressReceive, MODULE_ID_DEX, tokenId, amount) | ||
} | ||
|
||
export const transferToProtocolFeeAccount = async ( | ||
tokenAPI: TokenAPI, | ||
apiContext, | ||
settings: ModuleConfig, | ||
senderAddress: Address, | ||
tokenId: TokenID, | ||
amount: bigint | ||
): Promise < void > => { | ||
await tokenAPI.transfer(apiContext, senderAddress, settings.protocolFeeAddress, tokenId, amount) | ||
} | ||
|
||
// Convert a hex string to a byte array | ||
export const hexToBytes = (hex) => { | ||
const bytes: number[] = []; | ||
for (let c = 0; c < hex.length; c += 2) | ||
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access | ||
bytes.push(parseInt(hex.substr(c, 2), 16)); | ||
return bytes; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
/* | ||
* Copyright © 2022 Lisk Foundation | ||
* | ||
* See the LICENSE file at the top-level directory of this distribution | ||
* for licensing information. | ||
* | ||
* Unless otherwise agreed in a custom licensing agreement with the Lisk Foundation, | ||
* no part of this software, including this file, may be copied, modified, | ||
* propagated, or distributed except according to the terms contained in the | ||
* LICENSE file. | ||
* | ||
* Removal or modification of this copyright notice is prohibited. | ||
*/ | ||
|
||
import JSBI from 'jsbi'; | ||
import {toBigIntBE, toBigIntLE, toBufferBE, toBufferLE} from 'bigint-buffer'; | ||
|
||
import { NUM_BYTES_Q96, PRICE_VALUE_FOR_BIT_POSITION_IN_Q96, MIN_TICK, MAX_TICK } from '../constants'; | ||
import { sqrt } from "./math_constants"; | ||
|
||
import { Q96 } from '../types'; | ||
import { qn_r, mul_n, inv_n } from './q96'; | ||
|
||
export const computeSqrtPrice = (a: JSBI): Buffer => { | ||
const sq_a = sqrt(a); | ||
const a_hex: string = sq_a.toString(16); | ||
|
||
return toBufferBE(BigInt(a_hex), NUM_BYTES_Q96); | ||
} | ||
|
||
export const tickToPrice = (tickValue: number): Q96 => { | ||
|
||
if(tickValue < MIN_TICK || tickValue > MAX_TICK){ | ||
//return; | ||
} | ||
|
||
let absTick: number = Math.abs(tickValue) | ||
let sqrtPrice: JSBI = qn_r(1); | ||
|
||
PRICE_VALUE_FOR_BIT_POSITION_IN_Q96.forEach((e, i) => { | ||
if ((absTick >> i) & 1){ | ||
sqrtPrice = mul_n(sqrtPrice, e); | ||
} | ||
}); | ||
|
||
if(tickValue > 0) | ||
sqrtPrice = inv_n(sqrtPrice) | ||
|
||
return sqrtPrice | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import { | ||
getToken0Id, | ||
getToken1Id, | ||
getFeeTier, | ||
hexToBytes | ||
} from '../../../../src/app/modules/dex/utils/auxiliaryFunctions'; | ||
|
||
import { | ||
PoolID, TokenID | ||
} from '../../../../src/app/modules/dex/types'; | ||
|
||
describe('dex:auxiliaryFunctions', () => { | ||
describe('constructor', () => { | ||
const poolId: PoolID = Buffer.from(hexToBytes('0x00000000000000000000000100000000000000c8')); | ||
const token0Id: TokenID = Buffer.from(hexToBytes('0x0000000000000000')); | ||
const token1Id: TokenID = Buffer.from(hexToBytes('0x0000000100000000')); | ||
it('getToken0Id', async () => { | ||
expect(getToken0Id(poolId)).toEqual(token0Id); | ||
}); | ||
it('getToken1Id', async () => { | ||
expect(getToken1Id(poolId)).toEqual(token1Id); | ||
}); | ||
it('getFeeTier', async () => { | ||
expect(getFeeTier(poolId)).toEqual(Number('0x000000c8')); | ||
}); | ||
}); | ||
}); |
Oops, something went wrong.