Skip to content

Commit

Permalink
More isolated declarations in typescript
Browse files Browse the repository at this point in the history
  • Loading branch information
paulmillr committed Jan 19, 2025
1 parent 2fc44dc commit bcb2407
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 12 deletions.
9 changes: 8 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
{
"typescript.tsdk": "node_modules\\typescript\\lib"
"files.exclude": {
"*.{js,d.ts,js.map}": true,
"esm/*.{js,d.ts,js.map}": true,
"audit": true,
"coverage": true,
"esm": true,
"{LICENSE,SECURITY.md,.gitignore,.prettierrc.json,package-lock.json}": true
}
}
2 changes: 1 addition & 1 deletion jsr.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@paulmillr/micro-eth-signer",
"version": "0.13.3-pre",
"version": "0.13.3",
"exports": {
".": "./src/index.ts",
"./abi": "./src/abi/index.ts",
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "micro-eth-signer",
"version": "0.13.2",
"version": "0.13.3",
"description": "Minimal library for Ethereum transactions, addresses and smart contracts",
"files": [
"*.js",
Expand Down
14 changes: 13 additions & 1 deletion src/net/uniswap-common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,19 @@ export async function awaitDeep<T, E extends boolean | undefined>(
return trBack(out);
}

export const COMMON_BASES = ['WETH', 'DAI', 'USDC', 'USDT', 'COMP', 'MKR', 'WBTC', 'AMPL']
export type CommonBase = {
contract: string;
} & import('../abi/decoder.js').ContractInfo;
export const COMMON_BASES: CommonBase[] = [
'WETH',
'DAI',
'USDC',
'USDT',
'COMP',
'MKR',
'WBTC',
'AMPL',
]
.map((i) => tokenFromSymbol(i))
.filter((i) => !!i);
export const WETH: string = tokenFromSymbol('WETH')!.contract;
Expand Down
20 changes: 15 additions & 5 deletions src/net/uniswap-v2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,12 @@ const PAIR_CONTRACT = [
},
] as const;

export function create2(from: Uint8Array, salt: Uint8Array, initCodeHash: Uint8Array) {
export function create2(from: Uint8Array, salt: Uint8Array, initCodeHash: Uint8Array): string {
const cat = concatBytes(new Uint8Array([255]), from, salt, initCodeHash);
return ethHex.encode(keccak_256(cat).slice(12));
}

export function pairAddress(a: string, b: string, factory: string = FACTORY_ADDRESS) {
export function pairAddress(a: string, b: string, factory: string = FACTORY_ADDRESS): string {
// This is completely broken: '0x11' '0x11' will return '0x1111'. But this is how it works in sdk.
const data = concatBytes(...uni.sortTokens(a, b).map((i) => ethHex.decode(i)));
return create2(ethHex.decode(factory), keccak_256(data), INIT_CODE_HASH);
Expand Down Expand Up @@ -140,7 +140,17 @@ export function txData(
slippagePercent: number;
feeOnTransfer: boolean;
} = TX_DEFAULT_OPT
) {
): {
to: string;
value: bigint;
data: any;
allowance:
| {
token: string;
amount: bigint;
}
| undefined;
} {
opt = { ...TX_DEFAULT_OPT, ...opt };
if (!uni.isValidUniAddr(input) || !uni.isValidUniAddr(output) || !uni.isValidEthAddr(to))
throw new Error('Invalid address');
Expand Down Expand Up @@ -182,8 +192,8 @@ export function txData(
// Here goes Exchange API. Everything above is SDK. Supports almost everything from official sdk except liquidity stuff.
export default class UniswapV2 extends uni.UniswapAbstract {
name = 'Uniswap V2';
contract = UNISWAP_V2_ROUTER_CONTRACT;
bestPath(fromCoin: string, toCoin: string, inputAmount: bigint) {
contract: string = UNISWAP_V2_ROUTER_CONTRACT;
bestPath(fromCoin: string, toCoin: string, inputAmount: bigint): Promise<Path> {
return bestPath(this.net, fromCoin, toCoin, inputAmount);
}
txData(
Expand Down
16 changes: 13 additions & 3 deletions src/net/uniswap-v3.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,17 @@ export function txData(
amountIn?: bigint,
amountOut?: bigint,
opt: TxOpt = uni.DEFAULT_SWAP_OPT
) {
): {
to: string;
value: bigint;
data: Uint8Array;
allowance:
| {
token: string;
amount: bigint;
}
| undefined;
} {
opt = { ...uni.DEFAULT_SWAP_OPT, ...opt };
const err = 'Uniswap v3: ';
if (!uni.isValidUniAddr(input)) throw new Error(err + 'invalid input address');
Expand Down Expand Up @@ -198,8 +208,8 @@ export function txData(
// Here goes Exchange API. Everything above is SDK.
export default class UniswapV3 extends uni.UniswapAbstract {
name = 'Uniswap V3';
contract = UNISWAP_V3_ROUTER_CONTRACT;
bestPath(fromCoin: string, toCoin: string, inputAmount: bigint) {
contract: string = UNISWAP_V3_ROUTER_CONTRACT;
bestPath(fromCoin: string, toCoin: string, inputAmount: bigint): Promise<Route> {
return bestPath(this.net, fromCoin, toCoin, inputAmount);
}
txData(
Expand Down

0 comments on commit bcb2407

Please sign in to comment.