Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

reverse-mirage

Package Overview
Dependencies
Maintainers
1
Versions
19
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

reverse-mirage - npm Package Compare versions

Comparing version 0.1.4 to 0.1.5

src/amountUtils.ts

248

dist/index.d.ts

@@ -1,3 +0,3 @@

import { Address, Abi, Hash, SimulateContractReturnType, PublicClient, WalletClient, Account } from 'viem';
import { Address as Address$1 } from 'viem/accounts';
import { Abi, Hash, SimulateContractReturnType, PublicClient, Address as Address$1, WalletClient, Account } from 'viem';
import { Address } from 'viem/accounts';

@@ -11,2 +11,3 @@ declare const MaxUint256: bigint;

type BigIntIsh = bigint | string | number;
type Fraction = {

@@ -17,21 +18,13 @@ type: "fraction";

};
type BigIntIsh = bigint | string | number;
type NativeCurrency = {
type: "nativeCurrency";
type Token<TType extends string = string> = {
type: TType;
name: string;
symbol: string;
decimals: number;
chainID: number;
};
type Token = Omit<NativeCurrency, "type"> & {
type: "token";
address: Address;
};
type Currency = NativeCurrency | Token;
type CurrencyAmount<TCurrency extends Currency> = {
type: "currencyAmount";
currency: TCurrency;
amount: bigint;
};
type Price<TQuoteCurrency extends Currency, TBaseCurrency extends Currency> = Omit<Fraction, "type"> & {
type TokenData<TToken extends Token, TData extends object> = {
type: `${TToken["type"]}${string}`;
token: TToken;
} & TData;
type Price<TQuoteCurrency extends Token, TBaseCurrency extends Token> = Omit<Fraction, "type"> & {
type: "price";

@@ -51,32 +44,40 @@ quote: TQuoteCurrency;

declare const isCurrencyAmount: <TCurrency extends Currency>(x: BigIntIsh | CurrencyAmount<TCurrency>) => x is CurrencyAmount<TCurrency>;
declare const makeCurrencyAmountFromString: <TCurrency extends Currency>(currency: TCurrency, amount: string) => CurrencyAmount<TCurrency>;
declare const makeCurrencyAmountFromFraction: <TCurrency extends Currency>(currency: TCurrency, amount: Fraction) => CurrencyAmount<TCurrency>;
declare const makeCurrencyAmountFromRaw: <TCurrency extends Currency>(currency: TCurrency, amount: bigint) => CurrencyAmount<TCurrency>;
declare const currencyAmountAdd: <TCurrency extends Currency>(a: CurrencyAmount<TCurrency>, b: BigIntIsh | CurrencyAmount<TCurrency>) => CurrencyAmount<TCurrency>;
declare const currencyAmountSubtract: <TCurrency extends Currency>(a: CurrencyAmount<TCurrency>, b: BigIntIsh | CurrencyAmount<TCurrency>) => CurrencyAmount<TCurrency>;
declare const currencyAmountMultiply: <TCurrency extends Currency>(a: CurrencyAmount<TCurrency>, b: BigIntIsh | CurrencyAmount<TCurrency>) => CurrencyAmount<TCurrency>;
declare const currencyAmountDivide: <TCurrency extends Currency>(a: CurrencyAmount<TCurrency>, b: BigIntIsh | CurrencyAmount<TCurrency>) => CurrencyAmount<TCurrency>;
declare const currencyAmountLessThan: <TCurrency extends Currency>(a: CurrencyAmount<TCurrency>, b: BigIntIsh | CurrencyAmount<TCurrency>) => boolean;
declare const currencyAmountEqualTo: <TCurrency extends Currency>(a: CurrencyAmount<TCurrency>, b: BigIntIsh | CurrencyAmount<TCurrency>) => boolean;
declare const currencyAmountGreaterThan: <TCurrency extends Currency>(a: CurrencyAmount<TCurrency>, b: BigIntIsh | CurrencyAmount<TCurrency>) => boolean;
type Amount<TToken extends Token = Token> = TokenData<TToken & {
decimals?: number;
}, {
amount: bigint;
}>;
declare const isAmount: <TAmount extends Amount<Token>>(x: BigIntIsh | TAmount) => x is TAmount;
declare const makeAmountFromString: <TToken extends Token & {
decimals?: number;
}>(token: TToken, amount: string) => Amount<TToken>;
declare const makeAmountFromFraction: <TToken extends Token & {
decimals?: number;
}>(token: TToken, amount: Fraction) => Amount<TToken>;
declare const makeAmountFromRaw: <TToken extends Token & {
decimals?: number;
}>(token: TToken, amount: bigint) => Amount<TToken>;
declare const amountAdd: <TToken extends Token & {
decimals?: number;
}>(a: Amount<TToken>, b: BigIntIsh | Amount<TToken>) => Amount<TToken>;
declare const amountSubtract: <TToken extends Token & {
decimals?: number;
}>(a: Amount<TToken>, b: BigIntIsh | Amount<TToken>) => Amount<TToken>;
declare const amountMultiply: <TToken extends Token & {
decimals?: number;
}>(a: Amount<TToken>, b: BigIntIsh | Amount<TToken>) => Amount<TToken>;
declare const amountDivide: <TToken extends Token & {
decimals?: number;
}>(a: Amount<TToken>, b: BigIntIsh | Amount<TToken>) => Amount<TToken>;
declare const amountLessThan: <TToken extends Token & {
decimals?: number;
}>(a: Amount<TToken>, b: BigIntIsh | Amount<TToken>) => boolean;
declare const amountEqualTo: <TToken extends Token & {
decimals?: number;
}>(a: Amount<TToken>, b: BigIntIsh | Amount<TToken>) => boolean;
declare const amountGreaterThan: <TToken extends Token & {
decimals?: number;
}>(a: Amount<TToken>, b: BigIntIsh | Amount<TToken>) => boolean;
/**
* Determines if x is a token type
*/
declare const isToken: (x: Currency) => x is Token;
/**
* Determines if x is a native currency type
*/
declare const isNativeCurrency: (x: Currency) => x is NativeCurrency;
/**
* Determines if currency a is equal to currency b
*/
declare const currencyEqualTo: (a: Currency, b: Currency) => boolean;
/**
* Determines if the address of a is less than the address of b
*/
declare const currencySortsBefore: (a: Token, b: Token) => boolean;
/**
* Determines if x is a fraction type

@@ -130,43 +131,111 @@ */

declare const isPrice: <TQuoteCurrency extends Currency, TBaseCurrency extends Currency>(x: BigIntIsh | Price<TQuoteCurrency, TBaseCurrency>) => x is Price<TQuoteCurrency, TBaseCurrency>;
declare const makePriceFromFraction: <TQuoteCurrency extends Currency, TBaseCurrency extends Currency>(quote: TQuoteCurrency, base: TBaseCurrency, price: Fraction) => Price<TQuoteCurrency, TBaseCurrency>;
declare const makePriceFromAmounts: <TQuoteCurrency extends Currency, TBaseCurrency extends Currency>(quote: CurrencyAmount<TQuoteCurrency>, base: CurrencyAmount<TBaseCurrency>) => Price<TQuoteCurrency, TBaseCurrency>;
declare const makePrice: <TQuoteCurrency extends Currency, TBaseCurrency extends Currency>(quote: TQuoteCurrency, base: TBaseCurrency, numerator: BigIntIsh, denominator?: BigIntIsh) => Price<TQuoteCurrency, TBaseCurrency>;
declare const priceInvert: <TQuoteCurrency extends Currency, TBaseCurrency extends Currency>(price: Price<TQuoteCurrency, TBaseCurrency>) => Price<TBaseCurrency, TQuoteCurrency>;
declare const priceAdd: <TBaseCurrency extends Currency, TQuoteCurrency extends Currency>(a: Price<TQuoteCurrency, TBaseCurrency>, b: BigIntIsh | Price<TQuoteCurrency, TBaseCurrency>) => Price<TQuoteCurrency, TBaseCurrency>;
declare const priceSubtract: <TQuoteCurrency extends Currency, TBaseCurrency extends Currency>(a: Price<TQuoteCurrency, TBaseCurrency>, b: BigIntIsh | Price<TQuoteCurrency, TBaseCurrency>) => Price<TQuoteCurrency, TBaseCurrency>;
declare const priceMultiply: <TQuoteCurrency extends Currency, TBaseCurrency extends Currency>(a: Price<TQuoteCurrency, TBaseCurrency>, b: BigIntIsh | Price<TQuoteCurrency, TBaseCurrency>) => Price<TQuoteCurrency, TBaseCurrency>;
declare const priceDivide: <TQuoteCurrency extends Currency, TBaseCurrency extends Currency>(a: Price<TQuoteCurrency, TBaseCurrency>, b: BigIntIsh | Price<TQuoteCurrency, TBaseCurrency>) => Price<TQuoteCurrency, TBaseCurrency>;
declare const priceLessThan: <TQuoteCurrency extends Currency, TBaseCurrency extends Currency>(a: Price<TQuoteCurrency, TBaseCurrency>, b: BigIntIsh | Price<TQuoteCurrency, TBaseCurrency>) => boolean;
declare const priceEqualTo: <TQuoteCurrency extends Currency, TBaseCurrency extends Currency>(a: Price<TQuoteCurrency, TBaseCurrency>, b: BigIntIsh | Price<TQuoteCurrency, TBaseCurrency>) => boolean;
declare const priceGreaterThan: <TQuoteCurrency extends Currency, TBaseCurrency extends Currency>(a: Price<TQuoteCurrency, TBaseCurrency>, b: BigIntIsh | Price<TQuoteCurrency, TBaseCurrency>) => boolean;
declare const priceQuote: <TQuoteCurrency extends Currency, TBaseCurrency extends Currency>(price: Price<TQuoteCurrency, TBaseCurrency>, currencyAmount: CurrencyAmount<TBaseCurrency>) => CurrencyAmount<TQuoteCurrency>;
declare const rawPrice: <TQuoteCurrency extends Currency, TBaseCurrency extends Currency>(price: Price<TQuoteCurrency, TBaseCurrency>) => Fraction;
declare const adjustedPrice: <TQuoteCurrency extends Currency, TBaseCurrency extends Currency>(price: Price<TQuoteCurrency, TBaseCurrency>) => Fraction;
declare const isPrice: <TQuoteCurrency extends Token & {
decimals?: number;
}, TBaseCurrency extends Token & {
decimals?: number;
}>(x: BigIntIsh | Price<TQuoteCurrency, TBaseCurrency>) => x is Price<TQuoteCurrency, TBaseCurrency>;
declare const makePriceFromFraction: <TQuoteCurrency extends Token & {
decimals?: number;
}, TBaseCurrency extends Token & {
decimals?: number;
}>(quote: TQuoteCurrency, base: TBaseCurrency, price: Fraction) => Price<TQuoteCurrency, TBaseCurrency>;
declare const makePriceFromAmounts: <TQuoteCurrency extends Token & {
decimals?: number;
}, TBaseCurrency extends Token & {
decimals?: number;
}>(quote: Amount<TQuoteCurrency>, base: Amount<TBaseCurrency>) => Price<TQuoteCurrency, TBaseCurrency>;
declare const makePrice: <TQuoteCurrency extends Token & {
decimals?: number;
}, TBaseCurrency extends Token & {
decimals?: number;
}>(quote: TQuoteCurrency, base: TBaseCurrency, numerator: BigIntIsh, denominator?: BigIntIsh) => Price<TQuoteCurrency, TBaseCurrency>;
declare const priceInvert: <TQuoteCurrency extends Token & {
decimals?: number;
}, TBaseCurrency extends Token & {
decimals?: number;
}>(price: Price<TQuoteCurrency, TBaseCurrency>) => Price<TBaseCurrency, TQuoteCurrency>;
declare const priceAdd: <TBaseCurrency extends Token & {
decimals?: number;
}, TQuoteCurrency extends Token & {
decimals?: number;
}>(a: Price<TQuoteCurrency, TBaseCurrency>, b: BigIntIsh | Price<TQuoteCurrency, TBaseCurrency>) => Price<TQuoteCurrency, TBaseCurrency>;
declare const priceSubtract: <TQuoteCurrency extends Token & {
decimals?: number;
}, TBaseCurrency extends Token & {
decimals?: number;
}>(a: Price<TQuoteCurrency, TBaseCurrency>, b: BigIntIsh | Price<TQuoteCurrency, TBaseCurrency>) => Price<TQuoteCurrency, TBaseCurrency>;
declare const priceMultiply: <TQuoteCurrency extends Token & {
decimals?: number;
}, TBaseCurrency extends Token & {
decimals?: number;
}>(a: Price<TQuoteCurrency, TBaseCurrency>, b: BigIntIsh | Price<TQuoteCurrency, TBaseCurrency>) => Price<TQuoteCurrency, TBaseCurrency>;
declare const priceDivide: <TQuoteCurrency extends Token & {
decimals?: number;
}, TBaseCurrency extends Token & {
decimals?: number;
}>(a: Price<TQuoteCurrency, TBaseCurrency>, b: BigIntIsh | Price<TQuoteCurrency, TBaseCurrency>) => Price<TQuoteCurrency, TBaseCurrency>;
declare const priceLessThan: <TQuoteCurrency extends Token & {
decimals?: number;
}, TBaseCurrency extends Token & {
decimals?: number;
}>(a: Price<TQuoteCurrency, TBaseCurrency>, b: BigIntIsh | Price<TQuoteCurrency, TBaseCurrency>) => boolean;
declare const priceEqualTo: <TQuoteCurrency extends Token & {
decimals?: number;
}, TBaseCurrency extends Token & {
decimals?: number;
}>(a: Price<TQuoteCurrency, TBaseCurrency>, b: BigIntIsh | Price<TQuoteCurrency, TBaseCurrency>) => boolean;
declare const priceGreaterThan: <TQuoteCurrency extends Token & {
decimals?: number;
}, TBaseCurrency extends Token & {
decimals?: number;
}>(a: Price<TQuoteCurrency, TBaseCurrency>, b: BigIntIsh | Price<TQuoteCurrency, TBaseCurrency>) => boolean;
declare const priceQuote: <TQuoteCurrency extends Token & {
decimals?: number;
}, TBaseCurrency extends Token & {
decimals?: number;
}>(price: Price<TQuoteCurrency, TBaseCurrency>, amount: Amount<TBaseCurrency>) => Amount<TQuoteCurrency>;
declare const rawPrice: <TQuoteCurrency extends Token & {
decimals?: number;
}, TBaseCurrency extends Token & {
decimals?: number;
}>(price: Price<TQuoteCurrency, TBaseCurrency>) => Fraction;
declare const adjustedPrice: <TQuoteCurrency extends Token & {
decimals?: number;
}, TBaseCurrency extends Token & {
decimals?: number;
}>(price: Price<TQuoteCurrency, TBaseCurrency>) => Fraction;
declare const readAndParse: <TRet, TParse>(reverseMirageRead: ReverseMirageRead<TRet, TParse>) => Promise<TParse>;
type ERC20 = Token<"erc20"> & {
address: Address;
decimals: number;
};
type ERC20Amount<TERC20 extends ERC20> = TokenData<TERC20, {
amount: bigint;
}>;
declare const erc20BalanceOf: (publicClient: PublicClient, args: {
token: Token;
address: Address;
erc20: ERC20;
address: Address$1;
}) => {
read: () => Promise<bigint>;
parse: (data: bigint) => CurrencyAmount<Token>;
parse: (data: bigint) => Amount<ERC20>;
};
declare const erc20Allowance: (publicClient: PublicClient, args: {
token: Token;
address: Address;
spender: Address;
erc20: ERC20;
address: Address$1;
spender: Address$1;
}) => {
read: () => Promise<bigint>;
parse: (data: bigint) => CurrencyAmount<Token>;
parse: (data: bigint) => Amount<ERC20>;
};
declare const erc20TotalSupply: (publicClient: PublicClient, args: {
token: Token;
erc20: ERC20;
}) => {
read: () => Promise<bigint>;
parse: (data: bigint) => CurrencyAmount<Token>;
parse: (data: bigint) => Amount<ERC20>;
};
declare const erc20Name: (publicClient: PublicClient, args: {
token: Pick<Token, "address">;
erc20: Pick<ERC20, "address">;
}) => {

@@ -177,3 +246,3 @@ read: () => Promise<string>;

declare const erc20Symbol: (publicClient: PublicClient, args: {
token: Pick<Token, "address">;
erc20: Pick<ERC20, "address">;
}) => {

@@ -184,3 +253,3 @@ read: () => Promise<string>;

declare const erc20Decimals: (publicClient: PublicClient, args: {
token: Pick<Token, "address">;
erc20: Pick<ERC20, "address">;
}) => {

@@ -191,3 +260,3 @@ read: () => Promise<number>;

declare const erc20GetToken: (publicClient: PublicClient, args: {
token: Pick<Token, "address" | "chainID">;
erc20: Pick<ERC20, "address" | "chainID">;
}) => {

@@ -349,24 +418,31 @@ read: () => Promise<[string, string, number]>;

declare const erc20Transfer: (publicClient: PublicClient, walletClient: WalletClient, account: Account | Address$1, args: {
to: Address$1;
amount: CurrencyAmount<Token>;
declare const erc20Transfer: (publicClient: PublicClient, walletClient: WalletClient, account: Account | Address, args: {
to: Address;
amount: ERC20Amount<ERC20>;
}) => Promise<ReverseMirageWrite<typeof erc20ABI, "transfer">>;
declare const erc20Approve: (publicClient: PublicClient, walletClient: WalletClient, account: Account | Address$1, args: {
spender: Address$1;
amount: CurrencyAmount<Token>;
declare const erc20Approve: (publicClient: PublicClient, walletClient: WalletClient, account: Account | Address, args: {
spender: Address;
amount: ERC20Amount<ERC20>;
}) => Promise<ReverseMirageWrite<typeof erc20ABI, "approve">>;
declare const erc20TransferFrom: (publicClient: PublicClient, walletClient: WalletClient, account: Account | Address$1, args: {
from: Address$1;
to: Address$1;
amount: CurrencyAmount<Token>;
declare const erc20TransferFrom: (publicClient: PublicClient, walletClient: WalletClient, account: Account | Address, args: {
from: Address;
to: Address;
amount: ERC20Amount<ERC20>;
}) => Promise<ReverseMirageWrite<typeof erc20ABI, "transferFrom">>;
type NativeCurrency = Token<"nativeCurrency"> & {
decimals: number;
};
type NativeCurrencyAmount<TNativeCurrency extends NativeCurrency> = TokenData<TNativeCurrency, {
amount: bigint;
}>;
declare const nativeBalance: (publicClient: PublicClient, args: {
nativeCurrency: NativeCurrency;
address: Address;
address: Address$1;
}) => {
read: () => Promise<bigint>;
parse: (data: bigint) => CurrencyAmount<NativeCurrency>;
parse: (data: bigint) => Amount<NativeCurrency>;
};
export { BigIntIsh, Currency, CurrencyAmount, Fraction, MaxUint128, MaxUint16, MaxUint256, MaxUint32, MaxUint64, MaxUint8, NativeCurrency, Price, ReverseMirageRead, ReverseMirageWrite, Token, adjustedPrice, currencyAmountAdd, currencyAmountDivide, currencyAmountEqualTo, currencyAmountGreaterThan, currencyAmountLessThan, currencyAmountMultiply, currencyAmountSubtract, currencyEqualTo, currencySortsBefore, erc20Allowance, erc20Approve, erc20BalanceOf, erc20Decimals, erc20GetToken, erc20Name, erc20Symbol, erc20TotalSupply, erc20Transfer, erc20TransferFrom, fractionAdd, fractionDivide, fractionEqualTo, fractionGreaterThan, fractionInvert, fractionLessThan, fractionMultiply, fractionQuotient, fractionRemainder, fractionSubtract, isCurrencyAmount, isFraction, isNativeCurrency, isPrice, isToken, makeCurrencyAmountFromFraction, makeCurrencyAmountFromRaw, makeCurrencyAmountFromString, makeFraction, makePrice, makePriceFromAmounts, makePriceFromFraction, nativeBalance, priceAdd, priceDivide, priceEqualTo, priceGreaterThan, priceInvert, priceLessThan, priceMultiply, priceQuote, priceSubtract, rawPrice, readAndParse };
export { BigIntIsh, ERC20, ERC20Amount, Fraction, MaxUint128, MaxUint16, MaxUint256, MaxUint32, MaxUint64, MaxUint8, NativeCurrency, NativeCurrencyAmount, Price, ReverseMirageRead, ReverseMirageWrite, Token, TokenData, adjustedPrice, amountAdd, amountDivide, amountEqualTo, amountGreaterThan, amountLessThan, amountMultiply, amountSubtract, erc20Allowance, erc20Approve, erc20BalanceOf, erc20Decimals, erc20GetToken, erc20Name, erc20Symbol, erc20TotalSupply, erc20Transfer, erc20TransferFrom, fractionAdd, fractionDivide, fractionEqualTo, fractionGreaterThan, fractionInvert, fractionLessThan, fractionMultiply, fractionQuotient, fractionRemainder, fractionSubtract, isAmount, isFraction, isPrice, makeAmountFromFraction, makeAmountFromRaw, makeAmountFromString, makeFraction, makePrice, makePriceFromAmounts, makePriceFromFraction, nativeBalance, priceAdd, priceDivide, priceEqualTo, priceGreaterThan, priceInvert, priceLessThan, priceMultiply, priceQuote, priceSubtract, rawPrice, readAndParse };

@@ -9,120 +9,72 @@ // src/constants.ts

// src/currencyUtils.ts
// src/amountUtils.ts
import invariant from "tiny-invariant";
import { isAddressEqual } from "viem";
var isToken = (x) => x.type === "token";
var isNativeCurrency = (x) => x.type === "nativeCurrency";
var currencyEqualTo = (a, b) => {
return isToken(a) === isToken(b) && a.chainID === b.chainID && a.decimals === b.decimals && (isToken(a) ? isAddressEqual(a.address, b.address) : true);
};
var currencySortsBefore = (a, b) => {
invariant(a.chainID === b.chainID, "chain mismatch");
const aAddress = a.address.toLowerCase();
const bAddress = b.address.toLowerCase();
invariant(aAddress !== bAddress, "addresses equal");
return aAddress < bAddress;
};
// src/currencyAmountUtils.ts
import invariant2 from "tiny-invariant";
import { parseUnits } from "viem/utils";
var scaleUp = (currency, amount) => amount * 10n ** BigInt(currency.decimals);
var scaleDown = (currency, amount) => amount / 10n ** BigInt(currency.decimals);
var isCurrencyAmount = (x) => typeof x === "object" && "type" in x && x.type === "currencyAmount";
var makeCurrencyAmountFromString = (currency, amount) => ({
type: "currencyAmount",
currency,
amount: parseUnits(amount, currency.decimals)
var scaleUp = (token, amount) => token.decimals ? amount * 10n ** BigInt(token.decimals) : amount;
var scaleDown = (token, amount) => token.decimals ? amount / 10n ** BigInt(token.decimals) : amount;
var isAmount = (x) => typeof x === "object" && "amount" in x;
var makeAmountFromString = (token, amount) => ({
type: `${token.type}Amount`,
token,
amount: token.decimals ? parseUnits(amount, token.decimals) : BigInt(amount)
});
var makeCurrencyAmountFromFraction = (currency, amount) => ({
type: "currencyAmount",
currency,
amount: scaleUp(currency, amount.numerator) / amount.denominator
var makeAmountFromFraction = (token, amount) => ({
type: `${token.type}Amount`,
token,
amount: scaleUp(token, amount.numerator) / amount.denominator
});
var makeCurrencyAmountFromRaw = (currency, amount) => ({
type: "currencyAmount",
currency,
var makeAmountFromRaw = (token, amount) => ({
type: `${token.type}Amount`,
token,
amount
});
var currencyAmountAdd = (a, b) => {
if (isCurrencyAmount(b)) {
invariant2(currencyEqualTo(a.currency, b.currency));
var amountAdd = (a, b) => {
if (isAmount(b)) {
invariant(a.token === b.token);
}
return isCurrencyAmount(b) ? {
type: "currencyAmount",
currency: a.currency,
amount: a.amount + b.amount
} : {
type: "currencyAmount",
currency: a.currency,
amount: a.amount + scaleUp(a.currency, BigInt(b))
};
return isAmount(b) ? makeAmountFromRaw(a.token, a.amount + b.amount) : makeAmountFromRaw(a.token, a.amount + scaleUp(a.token, BigInt(b)));
};
var currencyAmountSubtract = (a, b) => {
if (isCurrencyAmount(b)) {
invariant2(currencyEqualTo(a.currency, b.currency));
var amountSubtract = (a, b) => {
if (isAmount(b)) {
invariant(a.token === b.token);
}
return isCurrencyAmount(b) ? {
type: "currencyAmount",
currency: a.currency,
amount: a.amount - b.amount
} : {
type: "currencyAmount",
currency: a.currency,
amount: a.amount - scaleUp(a.currency, BigInt(b))
};
return isAmount(b) ? makeAmountFromRaw(a.token, a.amount - b.amount) : makeAmountFromRaw(a.token, a.amount - scaleUp(a.token, BigInt(b)));
};
var currencyAmountMultiply = (a, b) => {
if (isCurrencyAmount(b)) {
invariant2(currencyEqualTo(a.currency, b.currency));
var amountMultiply = (a, b) => {
if (isAmount(b)) {
invariant(a.token === b.token);
}
return isCurrencyAmount(b) ? {
type: "currencyAmount",
currency: a.currency,
amount: scaleDown(a.currency, a.amount * b.amount)
} : {
type: "currencyAmount",
currency: a.currency,
amount: a.amount * BigInt(b)
};
return isAmount(b) ? makeAmountFromRaw(a.token, scaleDown(a.token, a.amount * b.amount)) : makeAmountFromRaw(a.token, a.amount * BigInt(b));
};
var currencyAmountDivide = (a, b) => {
if (isCurrencyAmount(b)) {
invariant2(currencyEqualTo(a.currency, b.currency));
var amountDivide = (a, b) => {
if (isAmount(b)) {
invariant(a.token === b.token);
}
return isCurrencyAmount(b) ? {
type: "currencyAmount",
currency: a.currency,
amount: scaleUp(a.currency, a.amount) / b.amount
} : {
type: "currencyAmount",
currency: a.currency,
amount: a.amount / BigInt(b)
};
return isAmount(b) ? makeAmountFromRaw(a.token, scaleUp(a.token, a.amount) / b.amount) : makeAmountFromRaw(a.token, a.amount / BigInt(b));
};
var currencyAmountLessThan = (a, b) => {
if (isCurrencyAmount(b)) {
invariant2(currencyEqualTo(a.currency, b.currency));
var amountLessThan = (a, b) => {
if (isAmount(b)) {
invariant(a.token === b.token);
}
return isCurrencyAmount(b) ? a.amount < b.amount : a.amount < scaleUp(a.currency, BigInt(b));
return isAmount(b) ? a.amount < b.amount : a.amount < scaleUp(a.token, BigInt(b));
};
var currencyAmountEqualTo = (a, b) => {
if (isCurrencyAmount(b)) {
invariant2(currencyEqualTo(a.currency, b.currency));
var amountEqualTo = (a, b) => {
if (isAmount(b)) {
invariant(a.token === b.token);
}
return isCurrencyAmount(b) ? a.amount === b.amount : a.amount === scaleUp(a.currency, BigInt(b));
return isAmount(b) ? a.amount === b.amount : a.amount === scaleUp(a.token, BigInt(b));
};
var currencyAmountGreaterThan = (a, b) => {
if (isCurrencyAmount(b)) {
invariant2(currencyEqualTo(a.currency, b.currency));
var amountGreaterThan = (a, b) => {
if (isAmount(b)) {
invariant(a.token === b.token);
}
return isCurrencyAmount(b) ? a.amount > b.amount : a.amount > scaleUp(a.currency, BigInt(b));
return isAmount(b) ? a.amount > b.amount : a.amount > scaleUp(a.token, BigInt(b));
};
// src/fractionUtils.ts
import invariant3 from "tiny-invariant";
import invariant2 from "tiny-invariant";
var isFraction = (x) => typeof x === "object" && "type" in x && x.type === "fraction";
var makeFraction = (numerator, denominator = 1) => {
const d = BigInt(denominator);
invariant3(d !== 0n, "Fraction: denominator equal to 0");
invariant2(d !== 0n, "Fraction: denominator equal to 0");
return { type: "fraction", numerator: BigInt(numerator), denominator: d };

@@ -133,3 +85,3 @@ };

var fractionInvert = (fraction) => {
invariant3(fraction.numerator !== 0n, "Fraction: denominator equal to 0");
invariant2(fraction.numerator !== 0n, "Fraction: denominator equal to 0");
return {

@@ -182,3 +134,3 @@ type: "fraction",

// src/priceUtils.ts
import invariant4 from "tiny-invariant";
import invariant3 from "tiny-invariant";
var isPrice = (x) => typeof x === "object" && "type" in x && x.type === "price";

@@ -189,9 +141,9 @@ var makePriceFromFraction = (quote, base, price) => ({

base,
numerator: price.numerator * 10n ** BigInt(quote.decimals),
denominator: price.denominator * 10n ** BigInt(base.decimals)
numerator: quote.decimals ? price.numerator * 10n ** BigInt(quote.decimals) : price.numerator,
denominator: base.decimals ? price.denominator * 10n ** BigInt(base.decimals) : price.denominator
});
var makePriceFromAmounts = (quote, base) => ({
type: "price",
quote: quote.currency,
base: base.currency,
quote: quote.token,
base: base.token,
numerator: base.amount,

@@ -204,4 +156,4 @@ denominator: quote.amount

base,
numerator: BigInt(numerator) * 10n ** BigInt(quote.decimals),
denominator: BigInt(denominator) * 10n ** BigInt(base.decimals)
numerator: quote.decimals ? BigInt(numerator) * 10n ** BigInt(quote.decimals) : BigInt(numerator),
denominator: base.decimals ? BigInt(denominator) * 10n ** BigInt(base.decimals) : BigInt(denominator)
});

@@ -216,5 +168,3 @@ var priceInvert = (price) => ({

if (isPrice(b))
invariant4(
currencyEqualTo(a.base, b.base) && currencyEqualTo(a.quote, b.quote)
);
invariant3(a.base === b.base && a.quote === b.quote);
return isPrice(b) ? {

@@ -229,5 +179,3 @@ ...fractionAdd(rawPrice(a), rawPrice(b)),

if (isPrice(b))
invariant4(
currencyEqualTo(a.base, b.base) && currencyEqualTo(a.quote, b.quote)
);
invariant3(a.base === b.base && a.quote === b.quote);
return isPrice(b) ? {

@@ -246,5 +194,3 @@ ...fractionSubtract(rawPrice(a), rawPrice(b)),

if (isPrice(b))
invariant4(
currencyEqualTo(a.base, b.base) && currencyEqualTo(a.quote, b.quote)
);
invariant3(a.base === b.base && a.quote === b.quote);
return isPrice(b) ? {

@@ -270,5 +216,3 @@ type: "price",

if (isPrice(b))
invariant4(
currencyEqualTo(a.base, b.base) && currencyEqualTo(a.quote, b.quote)
);
invariant3(a.base === b.base && a.quote === b.quote);
return isPrice(b) ? {

@@ -294,5 +238,3 @@ type: "price",

if (isPrice(b))
invariant4(
currencyEqualTo(a.base, b.base) && currencyEqualTo(a.quote, b.quote)
);
invariant3(a.base === b.base && a.quote === b.quote);
return isPrice(b) ? fractionLessThan(rawPrice(a), rawPrice(b)) : fractionLessThan(adjustedPrice(a), b);

@@ -302,5 +244,3 @@ };

if (isPrice(b))
invariant4(
currencyEqualTo(a.base, b.base) && currencyEqualTo(a.quote, b.quote)
);
invariant3(a.base === b.base && a.quote === b.quote);
return isPrice(b) ? fractionEqualTo(rawPrice(a), rawPrice(b)) : fractionEqualTo(adjustedPrice(a), b);

@@ -310,12 +250,10 @@ };

if (isPrice(b))
invariant4(
currencyEqualTo(a.base, b.base) && currencyEqualTo(a.quote, b.quote)
);
invariant3(a.base === b.base && a.quote === b.quote);
return isPrice(b) ? fractionGreaterThan(rawPrice(a), rawPrice(b)) : fractionGreaterThan(adjustedPrice(a), b);
};
var priceQuote = (price, currencyAmount) => {
invariant4(currencyEqualTo(price.base, currencyAmount.currency));
return makeCurrencyAmountFromRaw(
var priceQuote = (price, amount) => {
invariant3(price.base === amount.token);
return makeAmountFromRaw(
price.quote,
price.numerator * currencyAmount.amount / price.denominator
price.numerator * amount.amount / price.denominator
);

@@ -330,4 +268,4 @@ };

type: "fraction",
numerator: price.numerator * 10n ** BigInt(price.base.decimals),
denominator: price.denominator * 10n ** BigInt(price.quote.decimals)
numerator: price.base.decimals ? price.numerator * 10n ** BigInt(price.base.decimals) : price.numerator,
denominator: price.quote.decimals ? price.denominator * 10n ** BigInt(price.quote.decimals) : price.denominator
});

@@ -544,7 +482,7 @@

abi: erc20ABI,
address: args.token.address,
address: args.erc20.address,
functionName: "balanceOf",
args: [args.address]
}),
parse: (data) => makeCurrencyAmountFromRaw(args.token, data)
parse: (data) => makeAmountFromRaw(args.erc20, data)
};

@@ -556,7 +494,7 @@ };

abi: erc20ABI,
address: args.token.address,
address: args.erc20.address,
functionName: "allowance",
args: [args.address, args.spender]
}),
parse: (data) => makeCurrencyAmountFromRaw(args.token, data)
parse: (data) => makeAmountFromRaw(args.erc20, data)
};

@@ -568,6 +506,6 @@ };

abi: erc20ABI,
address: args.token.address,
address: args.erc20.address,
functionName: "totalSupply"
}),
parse: (data) => makeCurrencyAmountFromRaw(args.token, data)
parse: (data) => makeAmountFromRaw(args.erc20, data)
};

@@ -579,3 +517,3 @@ };

abi: erc20ABI,
address: args.token.address,
address: args.erc20.address,
functionName: "name"

@@ -590,3 +528,3 @@ }),

abi: erc20ABI,
address: args.token.address,
address: args.erc20.address,
functionName: "symbol"

@@ -601,3 +539,3 @@ }),

abi: erc20ABI,
address: args.token.address,
address: args.erc20.address,
functionName: "decimals"

@@ -619,3 +557,3 @@ }),

decimals: data[2],
...args.token
...args.erc20
})

@@ -628,3 +566,3 @@ };

const { request, result } = await publicClient.simulateContract({
address: args.amount.currency.address,
address: args.amount.token.address,
abi: erc20ABI,

@@ -640,3 +578,3 @@ functionName: "transfer",

const { request, result } = await publicClient.simulateContract({
address: args.amount.currency.address,
address: args.amount.token.address,
abi: erc20ABI,

@@ -652,3 +590,3 @@ functionName: "approve",

const { request, result } = await publicClient.simulateContract({
address: args.amount.currency.address,
address: args.amount.token.address,
abi: erc20ABI,

@@ -667,3 +605,3 @@ functionName: "transferFrom",

read: () => publicClient.getBalance({ address: args.address }),
parse: (data) => makeCurrencyAmountFromRaw(args.nativeCurrency, data)
parse: (data) => makeAmountFromRaw(args.nativeCurrency, data)
};

@@ -679,11 +617,9 @@ };

adjustedPrice,
currencyAmountAdd,
currencyAmountDivide,
currencyAmountEqualTo,
currencyAmountGreaterThan,
currencyAmountLessThan,
currencyAmountMultiply,
currencyAmountSubtract,
currencyEqualTo,
currencySortsBefore,
amountAdd,
amountDivide,
amountEqualTo,
amountGreaterThan,
amountLessThan,
amountMultiply,
amountSubtract,
erc20Allowance,

@@ -709,10 +645,8 @@ erc20Approve,

fractionSubtract,
isCurrencyAmount,
isAmount,
isFraction,
isNativeCurrency,
isPrice,
isToken,
makeCurrencyAmountFromFraction,
makeCurrencyAmountFromRaw,
makeCurrencyAmountFromString,
makeAmountFromFraction,
makeAmountFromRaw,
makeAmountFromString,
makeFraction,

@@ -719,0 +653,0 @@ makePrice,

{
"name": "reverse-mirage",
"version": "0.1.4",
"version": "0.1.5",
"description": "",

@@ -42,3 +42,3 @@ "type": "module",

"typescript": "^5.1.6",
"viem": "^1.4.0",
"viem": "^1.4.1",
"vitest": "^0.33.0"

@@ -45,0 +45,0 @@ },

@@ -1,4 +0,5 @@

import { makeCurrencyAmountFromRaw } from "../currencyAmountUtils.js";
import { makeAmountFromRaw } from "../amountUtils.js";
import { erc20ABI } from "../erc20/erc20Abi.js";
import type { ReverseMirageRead, Token } from "../types.js";
import type { ReverseMirageRead } from "../types.js";
import type { ERC20 } from "./types.js";
import type { Address, PublicClient } from "viem";

@@ -8,3 +9,3 @@

publicClient: PublicClient,
args: { token: Token; address: Address },
args: { erc20: ERC20; address: Address },
) => {

@@ -15,7 +16,7 @@ return {

abi: erc20ABI,
address: args.token.address,
address: args.erc20.address,
functionName: "balanceOf",
args: [args.address],
}),
parse: (data) => makeCurrencyAmountFromRaw(args.token, data),
parse: (data) => makeAmountFromRaw(args.erc20, data),
} satisfies ReverseMirageRead<bigint>;

@@ -26,3 +27,3 @@ };

publicClient: PublicClient,
args: { token: Token; address: Address; spender: Address },
args: { erc20: ERC20; address: Address; spender: Address },
) => {

@@ -33,7 +34,7 @@ return {

abi: erc20ABI,
address: args.token.address,
address: args.erc20.address,
functionName: "allowance",
args: [args.address, args.spender],
}),
parse: (data) => makeCurrencyAmountFromRaw(args.token, data),
parse: (data) => makeAmountFromRaw(args.erc20, data),
} satisfies ReverseMirageRead<bigint>;

@@ -44,3 +45,3 @@ };

publicClient: PublicClient,
args: { token: Token },
args: { erc20: ERC20 },
) => {

@@ -51,6 +52,6 @@ return {

abi: erc20ABI,
address: args.token.address,
address: args.erc20.address,
functionName: "totalSupply",
}),
parse: (data) => makeCurrencyAmountFromRaw(args.token, data),
parse: (data) => makeAmountFromRaw(args.erc20, data),
} satisfies ReverseMirageRead<bigint>;

@@ -61,3 +62,3 @@ };

publicClient: PublicClient,
args: { token: Pick<Token, "address"> },
args: { erc20: Pick<ERC20, "address"> },
) => {

@@ -68,3 +69,3 @@ return {

abi: erc20ABI,
address: args.token.address,
address: args.erc20.address,
functionName: "name",

@@ -78,3 +79,3 @@ }),

publicClient: PublicClient,
args: { token: Pick<Token, "address"> },
args: { erc20: Pick<ERC20, "address"> },
) => {

@@ -85,3 +86,3 @@ return {

abi: erc20ABI,
address: args.token.address,
address: args.erc20.address,
functionName: "symbol",

@@ -95,3 +96,3 @@ }),

publicClient: PublicClient,
args: { token: Pick<Token, "address"> },
args: { erc20: Pick<ERC20, "address"> },
) => {

@@ -102,3 +103,3 @@ return {

abi: erc20ABI,
address: args.token.address,
address: args.erc20.address,
functionName: "decimals",

@@ -112,3 +113,3 @@ }),

publicClient: PublicClient,
args: { token: Pick<Token, "address" | "chainID"> },
args: { erc20: Pick<ERC20, "address" | "chainID"> },
) => {

@@ -126,5 +127,5 @@ return {

decimals: data[2],
...args.token,
...args.erc20,
}),
} satisfies ReverseMirageRead<[string, string, number]>;
};

@@ -1,3 +0,4 @@

import type { CurrencyAmount, ReverseMirageWrite, Token } from "../types.js";
import type { ReverseMirageWrite } from "../types.js";
import { erc20ABI } from "./erc20Abi.js";
import type { ERC20, ERC20Amount } from "./types.js";
import type { Account, PublicClient, WalletClient } from "viem";

@@ -12,7 +13,7 @@ import type { Address } from "viem/accounts";

to: Address;
amount: CurrencyAmount<Token>;
amount: ERC20Amount<ERC20>;
},
): Promise<ReverseMirageWrite<typeof erc20ABI, "transfer">> => {
const { request, result } = await publicClient.simulateContract({
address: args.amount.currency.address,
address: args.amount.token.address,
abi: erc20ABI,

@@ -33,7 +34,7 @@ functionName: "transfer",

spender: Address;
amount: CurrencyAmount<Token>;
amount: ERC20Amount<ERC20>;
},
): Promise<ReverseMirageWrite<typeof erc20ABI, "approve">> => {
const { request, result } = await publicClient.simulateContract({
address: args.amount.currency.address,
address: args.amount.token.address,
abi: erc20ABI,

@@ -55,7 +56,7 @@ functionName: "approve",

to: Address;
amount: CurrencyAmount<Token>;
amount: ERC20Amount<ERC20>;
},
): Promise<ReverseMirageWrite<typeof erc20ABI, "transferFrom">> => {
const { request, result } = await publicClient.simulateContract({
address: args.amount.currency.address,
address: args.amount.token.address,
abi: erc20ABI,

@@ -62,0 +63,0 @@ functionName: "transferFrom",

@@ -11,23 +11,16 @@ export {

export {
isCurrencyAmount,
makeCurrencyAmountFromString,
makeCurrencyAmountFromFraction,
makeCurrencyAmountFromRaw,
currencyAmountAdd,
currencyAmountSubtract,
currencyAmountMultiply,
currencyAmountDivide,
currencyAmountLessThan,
currencyAmountEqualTo,
currencyAmountGreaterThan,
} from "./currencyAmountUtils.js";
isAmount,
makeAmountFromString,
makeAmountFromFraction,
makeAmountFromRaw,
amountAdd,
amountSubtract,
amountMultiply,
amountDivide,
amountLessThan,
amountEqualTo,
amountGreaterThan,
} from "./amountUtils.js";
export {
isToken,
isNativeCurrency,
currencyEqualTo,
currencySortsBefore,
} from "./currencyUtils.js";
export {
isFraction,

@@ -83,11 +76,13 @@ makeFraction,

export type { ERC20, ERC20Amount } from "./erc20/types.js";
export { nativeBalance } from "./native/reads.js";
export type { NativeCurrency, NativeCurrencyAmount } from "./native/types.js";
export type {
BigIntIsh,
Fraction,
BigIntIsh,
NativeCurrency,
Token,
Currency,
CurrencyAmount,
TokenData,
Price,

@@ -94,0 +89,0 @@ ReverseMirageRead,

@@ -1,3 +0,4 @@

import { makeCurrencyAmountFromRaw } from "../currencyAmountUtils.js";
import type { NativeCurrency, ReverseMirageRead } from "../types.js";
import { makeAmountFromRaw } from "../amountUtils.js";
import type { ReverseMirageRead } from "../types.js";
import type { NativeCurrency } from "./types.js";
import type { Address, PublicClient } from "viem";

@@ -11,4 +12,4 @@

read: () => publicClient.getBalance({ address: args.address }),
parse: (data) => makeCurrencyAmountFromRaw(args.nativeCurrency, data),
parse: (data) => makeAmountFromRaw(args.nativeCurrency, data),
} satisfies ReverseMirageRead<bigint>;
};

@@ -1,3 +0,2 @@

import { makeCurrencyAmountFromRaw, scaleUp } from "./currencyAmountUtils.js";
import { currencyEqualTo } from "./currencyUtils.js";
import { type Amount, makeAmountFromRaw, scaleUp } from "./amountUtils.js";
import {

@@ -13,14 +12,8 @@ fractionAdd,

} from "./fractionUtils.js";
import type {
BigIntIsh,
Currency,
CurrencyAmount,
Fraction,
Price,
} from "./types.js";
import type { BigIntIsh, Fraction, Price } from "./types.js";
import invariant from "tiny-invariant";
export const isPrice = <
TQuoteCurrency extends Currency,
TBaseCurrency extends Currency,
TQuoteCurrency extends Amount["token"],
TBaseCurrency extends Amount["token"],
>(

@@ -32,4 +25,4 @@ x: Price<TQuoteCurrency, TBaseCurrency> | BigIntIsh,

export const makePriceFromFraction = <
TQuoteCurrency extends Currency,
TBaseCurrency extends Currency,
TQuoteCurrency extends Amount["token"],
TBaseCurrency extends Amount["token"],
>(

@@ -43,16 +36,20 @@ quote: TQuoteCurrency,

base,
numerator: price.numerator * 10n ** BigInt(quote.decimals),
denominator: price.denominator * 10n ** BigInt(base.decimals),
numerator: quote.decimals
? price.numerator * 10n ** BigInt(quote.decimals)
: price.numerator,
denominator: base.decimals
? price.denominator * 10n ** BigInt(base.decimals)
: price.denominator,
});
export const makePriceFromAmounts = <
TQuoteCurrency extends Currency,
TBaseCurrency extends Currency,
TQuoteCurrency extends Amount["token"],
TBaseCurrency extends Amount["token"],
>(
quote: CurrencyAmount<TQuoteCurrency>,
base: CurrencyAmount<TBaseCurrency>,
quote: Amount<TQuoteCurrency>,
base: Amount<TBaseCurrency>,
): Price<TQuoteCurrency, TBaseCurrency> => ({
type: "price",
quote: quote.currency,
base: base.currency,
quote: quote.token,
base: base.token,
numerator: base.amount,

@@ -63,4 +60,4 @@ denominator: quote.amount,

export const makePrice = <
TQuoteCurrency extends Currency,
TBaseCurrency extends Currency,
TQuoteCurrency extends Amount["token"],
TBaseCurrency extends Amount["token"],
>(

@@ -75,9 +72,13 @@ quote: TQuoteCurrency,

base,
numerator: BigInt(numerator) * 10n ** BigInt(quote.decimals),
denominator: BigInt(denominator) * 10n ** BigInt(base.decimals),
numerator: quote.decimals
? BigInt(numerator) * 10n ** BigInt(quote.decimals)
: BigInt(numerator),
denominator: base.decimals
? BigInt(denominator) * 10n ** BigInt(base.decimals)
: BigInt(denominator),
});
export const priceInvert = <
TQuoteCurrency extends Currency,
TBaseCurrency extends Currency,
TQuoteCurrency extends Amount["token"],
TBaseCurrency extends Amount["token"],
>(

@@ -93,4 +94,4 @@ price: Price<TQuoteCurrency, TBaseCurrency>,

export const priceAdd = <
TBaseCurrency extends Currency,
TQuoteCurrency extends Currency,
TBaseCurrency extends Amount["token"],
TQuoteCurrency extends Amount["token"],
>(

@@ -100,6 +101,3 @@ a: Price<TQuoteCurrency, TBaseCurrency>,

): Price<TQuoteCurrency, TBaseCurrency> => {
if (isPrice(b))
invariant(
currencyEqualTo(a.base, b.base) && currencyEqualTo(a.quote, b.quote),
);
if (isPrice(b)) invariant(a.base === b.base && a.quote === b.quote);

@@ -117,4 +115,4 @@ return isPrice(b)

export const priceSubtract = <
TQuoteCurrency extends Currency,
TBaseCurrency extends Currency,
TQuoteCurrency extends Amount["token"],
TBaseCurrency extends Amount["token"],
>(

@@ -124,6 +122,3 @@ a: Price<TQuoteCurrency, TBaseCurrency>,

): Price<TQuoteCurrency, TBaseCurrency> => {
if (isPrice(b))
invariant(
currencyEqualTo(a.base, b.base) && currencyEqualTo(a.quote, b.quote),
);
if (isPrice(b)) invariant(a.base === b.base && a.quote === b.quote);

@@ -145,4 +140,4 @@ return isPrice(b)

export const priceMultiply = <
TQuoteCurrency extends Currency,
TBaseCurrency extends Currency,
TQuoteCurrency extends Amount["token"],
TBaseCurrency extends Amount["token"],
>(

@@ -152,6 +147,3 @@ a: Price<TQuoteCurrency, TBaseCurrency>,

): Price<TQuoteCurrency, TBaseCurrency> => {
if (isPrice(b))
invariant(
currencyEqualTo(a.base, b.base) && currencyEqualTo(a.quote, b.quote),
);
if (isPrice(b)) invariant(a.base === b.base && a.quote === b.quote);

@@ -180,4 +172,4 @@ return isPrice(b)

export const priceDivide = <
TQuoteCurrency extends Currency,
TBaseCurrency extends Currency,
TQuoteCurrency extends Amount["token"],
TBaseCurrency extends Amount["token"],
>(

@@ -187,6 +179,3 @@ a: Price<TQuoteCurrency, TBaseCurrency>,

): Price<TQuoteCurrency, TBaseCurrency> => {
if (isPrice(b))
invariant(
currencyEqualTo(a.base, b.base) && currencyEqualTo(a.quote, b.quote),
);
if (isPrice(b)) invariant(a.base === b.base && a.quote === b.quote);

@@ -215,4 +204,4 @@ return isPrice(b)

export const priceLessThan = <
TQuoteCurrency extends Currency,
TBaseCurrency extends Currency,
TQuoteCurrency extends Amount["token"],
TBaseCurrency extends Amount["token"],
>(

@@ -222,6 +211,3 @@ a: Price<TQuoteCurrency, TBaseCurrency>,

): boolean => {
if (isPrice(b))
invariant(
currencyEqualTo(a.base, b.base) && currencyEqualTo(a.quote, b.quote),
);
if (isPrice(b)) invariant(a.base === b.base && a.quote === b.quote);

@@ -234,4 +220,4 @@ return isPrice(b)

export const priceEqualTo = <
TQuoteCurrency extends Currency,
TBaseCurrency extends Currency,
TQuoteCurrency extends Amount["token"],
TBaseCurrency extends Amount["token"],
>(

@@ -241,6 +227,3 @@ a: Price<TQuoteCurrency, TBaseCurrency>,

): boolean => {
if (isPrice(b))
invariant(
currencyEqualTo(a.base, b.base) && currencyEqualTo(a.quote, b.quote),
);
if (isPrice(b)) invariant(a.base === b.base && a.quote === b.quote);

@@ -253,4 +236,4 @@ return isPrice(b)

export const priceGreaterThan = <
TQuoteCurrency extends Currency,
TBaseCurrency extends Currency,
TQuoteCurrency extends Amount["token"],
TBaseCurrency extends Amount["token"],
>(

@@ -260,6 +243,3 @@ a: Price<TQuoteCurrency, TBaseCurrency>,

): boolean => {
if (isPrice(b))
invariant(
currencyEqualTo(a.base, b.base) && currencyEqualTo(a.quote, b.quote),
);
if (isPrice(b)) invariant(a.base === b.base && a.quote === b.quote);

@@ -272,13 +252,13 @@ return isPrice(b)

export const priceQuote = <
TQuoteCurrency extends Currency,
TBaseCurrency extends Currency,
TQuoteCurrency extends Amount["token"],
TBaseCurrency extends Amount["token"],
>(
price: Price<TQuoteCurrency, TBaseCurrency>,
currencyAmount: CurrencyAmount<TBaseCurrency>,
): CurrencyAmount<TQuoteCurrency> => {
invariant(currencyEqualTo(price.base, currencyAmount.currency));
amount: Amount<TBaseCurrency>,
): Amount<TQuoteCurrency> => {
invariant(price.base === amount.token);
return makeCurrencyAmountFromRaw(
return makeAmountFromRaw(
price.quote,
(price.numerator * currencyAmount.amount) / price.denominator,
(price.numerator * amount.amount) / price.denominator,
);

@@ -288,4 +268,4 @@ };

export const rawPrice = <
TQuoteCurrency extends Currency,
TBaseCurrency extends Currency,
TQuoteCurrency extends Amount["token"],
TBaseCurrency extends Amount["token"],
>(

@@ -300,4 +280,4 @@ price: Price<TQuoteCurrency, TBaseCurrency>,

export const adjustedPrice = <
TQuoteCurrency extends Currency,
TBaseCurrency extends Currency,
TQuoteCurrency extends Amount["token"],
TBaseCurrency extends Amount["token"],
>(

@@ -307,4 +287,8 @@ price: Price<TQuoteCurrency, TBaseCurrency>,

type: "fraction",
numerator: price.numerator * 10n ** BigInt(price.base.decimals),
denominator: price.denominator * 10n ** BigInt(price.quote.decimals),
numerator: price.base.decimals
? price.numerator * 10n ** BigInt(price.base.decimals)
: price.numerator,
denominator: price.quote.decimals
? price.denominator * 10n ** BigInt(price.quote.decimals)
: price.denominator,
});

@@ -311,0 +295,0 @@

@@ -1,3 +0,5 @@

import type { Abi, Address, Hash, SimulateContractReturnType } from "viem";
import type { Abi, Hash, SimulateContractReturnType } from "viem";
export type BigIntIsh = bigint | string | number;
export type Fraction = {

@@ -9,28 +11,17 @@ type: "fraction";

export type BigIntIsh = bigint | string | number;
export type NativeCurrency = {
type: "nativeCurrency";
export type Token<TType extends string = string> = {
type: TType;
name: string;
symbol: string;
decimals: number;
chainID: number;
};
export type Token = Omit<NativeCurrency, "type"> & {
type: "token";
address: Address;
};
export type TokenData<TToken extends Token, TData extends object> = {
type: `${TToken["type"]}${string}`;
token: TToken;
} & TData;
export type Currency = NativeCurrency | Token;
export type CurrencyAmount<TCurrency extends Currency> = {
type: "currencyAmount";
currency: TCurrency;
amount: bigint;
};
export type Price<
TQuoteCurrency extends Currency,
TBaseCurrency extends Currency,
TQuoteCurrency extends Token,
TBaseCurrency extends Token,
> = Omit<Fraction, "type"> & {

@@ -37,0 +28,0 @@ type: "price";

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc