Skip to content

Commit

Permalink
Rename ethDecimal and gweiDecimal to weieth and weigwei
Browse files Browse the repository at this point in the history
  • Loading branch information
paulmillr committed May 18, 2024
1 parent 8525027 commit 648602d
Show file tree
Hide file tree
Showing 8 changed files with 39 additions and 40 deletions.
7 changes: 3 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,14 +45,13 @@ If you don't like NPM, a standalone [eth-signer.js](https://github.com/paulmillr
### Transactions: create, sign

```ts
import { addr, Transaction } from 'micro-eth-signer';
import { gweiDecimal, ethDecimal } from 'micro-eth-signer/utils';
import { addr, Transaction, weigwei, weieth } from 'micro-eth-signer';
const privateKey = '0x6b911fd37cdf5c81d4c0adb1ab7fa822ed253ab0ad9aa18d77257c88b29b718e';
const senderAddr = addr.fromPrivateKey(privateKey);
const unsignedTx = Transaction.prepare({
to: '0xdf90dea0e0bf5ca6d2a7f0cb86874ba6714f463e',
maxFeePerGas: gweiDecimal.decode('100'), // 100 gwei in wei
value: ethDecimal.decode('1.1'), // 1.1 eth in wei
maxFeePerGas: weigwei.decode('100'), // 100 gwei in wei
value: weieth.decode('1.1'), // 1.1 eth in wei
nonce: 0n,
});
const tx = unsignedTx.signBy(privateKey); // Uint8Array is also accepted
Expand Down
18 changes: 7 additions & 11 deletions src/abi/uniswap-v2.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { createDecimal, ethDecimal } from '../utils.js';
import { createDecimal, weieth } from '../utils.js';
import { HintOpt } from './decoder.js';
import { addHints } from './common.js';

Expand All @@ -25,45 +25,41 @@ const hints = {
swapETHForExactTokens(v: any, opt: HintOpt) {
const last = uniToken(v.path, opt, v.amountOut, false);
if (!opt.amount || !last) throw new Error('Not enough info');
return `Swap up to ${ethDecimal.encode(opt.amount)} ETH for exact ${last}. ${uniTs(
v.deadline
)}`;
return `Swap up to ${weieth.encode(opt.amount)} ETH for exact ${last}. ${uniTs(v.deadline)}`;
},

swapExactETHForTokens(v: any, opt: HintOpt) {
const last = uniToken(v.path, opt, v.amountOutMin, false);
if (!opt.amount || !last) throw new Error('Not enough info');
return `Swap ${ethDecimal.encode(opt.amount)} ETH for at least ${last}. ${uniTs(v.deadline)}`;
return `Swap ${weieth.encode(opt.amount)} ETH for at least ${last}. ${uniTs(v.deadline)}`;
},

swapExactETHForTokensSupportingFeeOnTransferTokens(v: any, opt: HintOpt) {
const last = uniToken(v.path, opt, v.amountOutMin, false);
if (!opt.amount || !last) throw new Error('Not enough info');
return `Swap ${ethDecimal.encode(opt.amount)} ETH for at least ${last}. ${uniTs(v.deadline)}`;
return `Swap ${weieth.encode(opt.amount)} ETH for at least ${last}. ${uniTs(v.deadline)}`;
},

swapExactTokensForETH(v: any, opt: HintOpt) {
const first = uniToken(v.path, opt, v.amountIn, true);
if (!first) throw new Error('Not enough info');
return `Swap exact ${first} for at least ${ethDecimal.encode(v.amountOutMin)} ETH. ${uniTs(
return `Swap exact ${first} for at least ${weieth.encode(v.amountOutMin)} ETH. ${uniTs(
v.deadline
)}`;
},

swapExactTokensForETHSupportingFeeOnTransferTokens(v: any, opt: HintOpt) {
const first = uniToken(v.path, opt, v.amountIn, true);
if (!first) throw new Error('Not enough info');
return `Swap exact ${first} for at least ${ethDecimal.encode(v.amountOutMin)} ETH. ${uniTs(
return `Swap exact ${first} for at least ${weieth.encode(v.amountOutMin)} ETH. ${uniTs(
v.deadline
)}`;
},

swapTokensForExactETH(v: any, opt: HintOpt) {
const first = uniToken(v.path, opt, v.amountInMax, true);
if (!first) throw new Error('Not enough info');
return `Swap up to ${first} for exact ${ethDecimal.encode(v.amountOut)} ETH. ${uniTs(
v.deadline
)}`;
return `Swap up to ${first} for exact ${weieth.encode(v.amountOut)} ETH. ${uniTs(v.deadline)}`;
},

swapExactTokensForTokens(v: any, opt: HintOpt) {
Expand Down
10 changes: 5 additions & 5 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ import {
} from './tx.js';
// prettier-ignore
import {
amounts, astr, add0x, ethHex, ethHexNoLeadingZero, strip0x, ethDecimal, cloneDeep,
amounts, astr, add0x, ethHex, ethHexNoLeadingZero, strip0x, weieth, weigwei, cloneDeep,
} from './utils.js';
export { addr };
export { addr, weigwei, weieth };

// The file exports Transaction, but actual (RLP) parsing logic is done in `./tx`

Expand Down Expand Up @@ -216,9 +216,9 @@ export class Transaction<T extends TxType> {
const amountWithFee = fee + amount;
const wei = { amount, fee, amountWithFee };
const humanized = {
amount: ethDecimal.encode(amount),
fee: ethDecimal.encode(fee),
amountWithFee: ethDecimal.encode(amountWithFee),
amount: weieth.encode(amount),
fee: weieth.encode(fee),
amountWithFee: weieth.encode(amountWithFee),
};
return { wei, humanized };
}
Expand Down
4 changes: 2 additions & 2 deletions src/net/uniswap-common.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Web3Provider, ethHex, ethDecimal, isBytes, createDecimal } from '../utils.js';
import { Web3Provider, ethHex, weieth, isBytes, createDecimal } from '../utils.js';
import { addr } from '../index.js';
import { tokenFromSymbol } from '../abi/index.js';

Expand Down Expand Up @@ -162,7 +162,7 @@ export abstract class UniswapAbstract {
opt
);
return {
amount: ethDecimal.encode(txUni.value),
amount: weieth.encode(txUni.value),
address: txUni.to,
expectedAmount,
data: ethHex.encode(txUni.data),
Expand Down
10 changes: 7 additions & 3 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129,14 +129,18 @@ export function zip<A, B>(a: A[], b: B[]): [A, B][] {
}

export const createDecimal = coders.decimal;
export const ethDecimal = createDecimal(ETH_PRECISION);
export const gweiDecimal = createDecimal(GWEI_PRECISION);
export const weieth = createDecimal(ETH_PRECISION);
export const weigwei = createDecimal(GWEI_PRECISION);

// legacy. TODO: remove
export const ethDecimal = weieth;
export const gweiDecimal = weigwei;

export const formatters = {
// returns decimal that costs exactly $0.01 in given precision (using price)
// formatDecimal(perCentDecimal(prec, price), prec) * price == '0.01'
perCentDecimal(precision: number, price: number): bigint {
const fiatPrec = ethDecimal;
const fiatPrec = weieth;
//x * price = 0.01
//x = 0.01/price = 1/100 / price = 1/(100*price)
// float does not have enough precision
Expand Down
22 changes: 11 additions & 11 deletions test/fee.test.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { deepStrictEqual } from 'node:assert';
import { describe, should } from 'micro-should';
import { Transaction } from '../esm/index.js';
import { ethDecimal, gweiDecimal } from '../esm/utils.js';
import { weieth, weigwei } from '../esm/utils.js';

// NOTE: other libraries doesn't support fee estimation, so there is no crosstests for now :(
// But we need some tests to avoid accidental breakage.
Expand All @@ -11,8 +11,8 @@ describe('Fees', () => {
type: 'legacy',
to: '0x27b1fdb04752bbc536007a920d24acb045561c26',
nonce: 1n,
value: ethDecimal.decode('1'),
gasPrice: gweiDecimal.decode('2'),
value: weieth.decode('1'),
gasPrice: weigwei.decode('2'),
});
// 21k * 2 = 42
deepStrictEqual(tx.calcAmounts(), {
Expand All @@ -27,8 +27,8 @@ describe('Fees', () => {
type: 'legacy',
to: '0x27b1fdb04752bbc536007a920d24acb045561c26',
nonce: 1n,
value: ethDecimal.decode('1.23'),
gasPrice: gweiDecimal.decode('55.3'),
value: weieth.decode('1.23'),
gasPrice: weigwei.decode('55.3'),
});
deepStrictEqual(tx2.calcAmounts(), {
wei: {
Expand All @@ -43,9 +43,9 @@ describe('Fees', () => {
const tx = Transaction.prepare({
to: '0x27b1fdb04752bbc536007a920d24acb045561c26',
nonce: 1n,
value: ethDecimal.decode('1'),
maxFeePerGas: gweiDecimal.decode('2'),
maxPriorityFeePerGas: gweiDecimal.decode('1'),
value: weieth.decode('1'),
maxFeePerGas: weigwei.decode('2'),
maxPriorityFeePerGas: weigwei.decode('1'),
});
// 21k * 2 = 42
deepStrictEqual(tx.calcAmounts(), {
Expand All @@ -59,9 +59,9 @@ describe('Fees', () => {
const tx2 = Transaction.prepare({
to: '0x27b1fdb04752bbc536007a920d24acb045561c26',
nonce: 1n,
value: ethDecimal.decode('1.23'),
maxFeePerGas: gweiDecimal.decode('55.3'),
maxPriorityFeePerGas: gweiDecimal.decode('2'),
value: weieth.decode('1.23'),
maxFeePerGas: weigwei.decode('55.3'),
maxPriorityFeePerGas: weigwei.decode('2'),
});
// 21k * 2 = 42
deepStrictEqual(tx2.calcAmounts(), {
Expand Down
4 changes: 2 additions & 2 deletions test/net.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
Chainlink,
UniswapV3,
} from '../esm/net/index.js';
import { ethDecimal, numberTo0xHex } from '../esm/utils.js';
import { weieth, numberTo0xHex } from '../esm/utils.js';
// These real network responses from real nodes, captured by fetchReplay
import { default as NET_TX_REPLAY } from './vectors/rpc/transactions.js';
import { default as NET_ENS_REPLAY } from './vectors/rpc/ens.js';
Expand Down Expand Up @@ -112,7 +112,7 @@ describe('Network', () => {
const gasLimit = await provider.estimateGas({
from: '0xd8da6bf26964af9d7eed9e03e53415d37aa96045',
to: '0xe592427a0aece92de3edee1f18e0157c05861564',
value: numberTo0xHex(ethDecimal.decode('1.23')),
value: numberTo0xHex(weieth.decode('1.23')),
data: '0xc04b8d59000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000a0000000000000000000000000d8da6bf26964af9d7eed9e03e53415d37aa960450000000000000000000000000000000000000000000000000000019077fd30000000000000000000000000000000000000000000000000001111d67bb1bb0000000000000000000000000000000000000000000000000102d6906ca33403f40b0000000000000000000000000000000000000000000000000000000000000042c02aaa39b223fe8d0a0e5c4f27ead9083c756cc20001f4a0b86991c6218b36c1d19d4a2e9eb0ce3606eb480001f46b175474e89094c44da98b954eedeac495271d0f000000000000000000000000000000000000000000000000000000000000',
});
deepStrictEqual(gasLimit, 236082n);
Expand Down
4 changes: 2 additions & 2 deletions test/uniswap.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { describe, should } from 'micro-should';
import { bytesToHex, concatBytes, hexToBytes } from '@noble/hashes/utils';
import * as uniswap2 from '../esm/net/uniswap-v2.js';
import * as uniswap3 from '../esm/net/uniswap-v3.js';
import { ethDecimal, strip0x } from '../esm/utils.js';
import { weieth, strip0x } from '../esm/utils.js';

const vitalik = '0xd8da6bf26964af9d7eed9e03e53415d37aa96045';
const TS = 1876543210;
Expand Down Expand Up @@ -56,7 +56,7 @@ describe('uniswap', () => {
);
});
should('amount', () => {
const dec = (n) => ethDecimal.encode(n);
const dec = (n) => weieth.encode(n);
const [rA, rB] = [296640198432632702024n, 365918988101684615226n];
deepStrictEqual(dec(uniswap2.amount(rA, rB, 1000000000000000000n)), '1.225724583682887052');
deepStrictEqual(dec(uniswap2.amount(rA, rB, 100000000000000000000n)), '92.047496716230633056');
Expand Down

0 comments on commit 648602d

Please sign in to comment.