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.3 to 0.1.4

dist/index.cjs

717

dist/index.js

@@ -1,10 +0,709 @@

export { MaxUint256, MaxUint128, MaxUint64, MaxUint32, MaxUint16, MaxUint8, } from "./constants.js";
export { isCurrencyAmount, makeCurrencyAmountFromString, makeCurrencyAmountFromFraction, makeCurrencyAmountFromRaw, currencyAmountAdd, currencyAmountSubtract, currencyAmountMultiply, currencyAmountDivide, currencyAmountLessThan, currencyAmountEqualTo, currencyAmountGreaterThan, } from "./currencyAmountUtils.js";
export { isToken, isNativeCurrency, currencyEqualTo, currencySortsBefore, } from "./currencyUtils.js";
export { isFraction, makeFraction, fractionQuotient, fractionRemainder, fractionInvert, fractionAdd, fractionSubtract, fractionMultiply, fractionDivide, fractionLessThan, fractionEqualTo, fractionGreaterThan, } from "./fractionUtils.js";
export { isPrice, makePriceFromFraction, makePriceFromAmounts, makePrice, priceInvert, priceAdd, priceSubtract, priceMultiply, priceDivide, priceLessThan, priceEqualTo, priceGreaterThan, priceQuote, rawPrice, adjustedPrice, } from "./priceUtils.js";
export { readAndParse } from "./readUtils.js";
export { erc20BalanceOf, erc20Allowance, erc20TotalSupply, erc20Name, erc20Symbol, erc20Decimals, erc20GetToken, } from "./erc20/reads.js";
export { erc20Transfer, erc20Approve, erc20TransferFrom, } from "./erc20/writes.js";
export { nativeBalance } from "./native/reads.js";
// src/constants.ts
var MaxUint256 = 2n ** 256n - 1n;
var MaxUint128 = 2n ** 128n - 1n;
var MaxUint64 = 2n ** 64n - 1n;
var MaxUint32 = 2n ** 32n - 1n;
var MaxUint16 = 2n ** 16n - 1n;
var MaxUint8 = 2n ** 8n - 1n;
// src/currencyUtils.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 makeCurrencyAmountFromFraction = (currency, amount) => ({
type: "currencyAmount",
currency,
amount: scaleUp(currency, amount.numerator) / amount.denominator
});
var makeCurrencyAmountFromRaw = (currency, amount) => ({
type: "currencyAmount",
currency,
amount
});
var currencyAmountAdd = (a, b) => {
if (isCurrencyAmount(b)) {
invariant2(currencyEqualTo(a.currency, b.currency));
}
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))
};
};
var currencyAmountSubtract = (a, b) => {
if (isCurrencyAmount(b)) {
invariant2(currencyEqualTo(a.currency, b.currency));
}
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))
};
};
var currencyAmountMultiply = (a, b) => {
if (isCurrencyAmount(b)) {
invariant2(currencyEqualTo(a.currency, b.currency));
}
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)
};
};
var currencyAmountDivide = (a, b) => {
if (isCurrencyAmount(b)) {
invariant2(currencyEqualTo(a.currency, b.currency));
}
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)
};
};
var currencyAmountLessThan = (a, b) => {
if (isCurrencyAmount(b)) {
invariant2(currencyEqualTo(a.currency, b.currency));
}
return isCurrencyAmount(b) ? a.amount < b.amount : a.amount < scaleUp(a.currency, BigInt(b));
};
var currencyAmountEqualTo = (a, b) => {
if (isCurrencyAmount(b)) {
invariant2(currencyEqualTo(a.currency, b.currency));
}
return isCurrencyAmount(b) ? a.amount === b.amount : a.amount === scaleUp(a.currency, BigInt(b));
};
var currencyAmountGreaterThan = (a, b) => {
if (isCurrencyAmount(b)) {
invariant2(currencyEqualTo(a.currency, b.currency));
}
return isCurrencyAmount(b) ? a.amount > b.amount : a.amount > scaleUp(a.currency, BigInt(b));
};
// src/fractionUtils.ts
import invariant3 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");
return { type: "fraction", numerator: BigInt(numerator), denominator: d };
};
var fractionQuotient = (fraction) => fraction.numerator / fraction.denominator;
var fractionRemainder = (fraction) => fraction.numerator % fraction.denominator;
var fractionInvert = (fraction) => {
invariant3(fraction.numerator !== 0n, "Fraction: denominator equal to 0");
return {
type: "fraction",
numerator: fraction.denominator,
denominator: fraction.numerator
};
};
var fractionAdd = (a, b) => isFraction(b) ? {
type: "fraction",
numerator: a.numerator * b.denominator + b.numerator * a.denominator,
denominator: a.denominator * b.denominator
} : {
type: "fraction",
numerator: a.numerator + a.denominator * BigInt(b),
denominator: a.denominator
};
var fractionSubtract = (a, b) => isFraction(b) ? {
type: "fraction",
numerator: a.numerator * b.denominator - b.numerator * a.denominator,
denominator: a.denominator * b.denominator
} : {
type: "fraction",
numerator: a.numerator - a.denominator * BigInt(b),
denominator: a.denominator
};
var fractionMultiply = (a, b) => isFraction(b) ? {
type: "fraction",
numerator: a.numerator * b.numerator,
denominator: a.denominator * b.denominator
} : {
type: "fraction",
numerator: a.numerator * BigInt(b),
denominator: a.denominator
};
var fractionDivide = (a, b) => isFraction(b) ? {
type: "fraction",
numerator: a.numerator * b.denominator,
denominator: a.denominator * b.numerator
} : {
type: "fraction",
numerator: a.numerator,
denominator: a.denominator * BigInt(b)
};
var fractionLessThan = (a, b) => isFraction(b) ? a.numerator * b.denominator < a.denominator * b.numerator : a.numerator < a.denominator * BigInt(b);
var fractionGreaterThan = (a, b) => isFraction(b) ? a.numerator * b.denominator > a.denominator * b.numerator : a.numerator > a.denominator * BigInt(b);
var fractionEqualTo = (a, b) => isFraction(b) ? a.numerator * b.denominator === a.denominator * b.numerator : a.numerator === a.denominator * BigInt(b);
// src/priceUtils.ts
import invariant4 from "tiny-invariant";
var isPrice = (x) => typeof x === "object" && "type" in x && x.type === "price";
var makePriceFromFraction = (quote, base, price) => ({
type: "price",
quote,
base,
numerator: price.numerator * 10n ** BigInt(quote.decimals),
denominator: price.denominator * 10n ** BigInt(base.decimals)
});
var makePriceFromAmounts = (quote, base) => ({
type: "price",
quote: quote.currency,
base: base.currency,
numerator: base.amount,
denominator: quote.amount
});
var makePrice = (quote, base, numerator, denominator = 1) => ({
type: "price",
quote,
base,
numerator: BigInt(numerator) * 10n ** BigInt(quote.decimals),
denominator: BigInt(denominator) * 10n ** BigInt(base.decimals)
});
var priceInvert = (price) => ({
...fractionInvert(rawPrice(price)),
type: "price",
quote: price.base,
base: price.quote
});
var priceAdd = (a, b) => {
if (isPrice(b))
invariant4(
currencyEqualTo(a.base, b.base) && currencyEqualTo(a.quote, b.quote)
);
return isPrice(b) ? {
...fractionAdd(rawPrice(a), rawPrice(b)),
type: "price",
quote: a.quote,
base: a.base
} : makePriceFromFraction(a.quote, a.base, fractionAdd(adjustedPrice(a), b));
};
var priceSubtract = (a, b) => {
if (isPrice(b))
invariant4(
currencyEqualTo(a.base, b.base) && currencyEqualTo(a.quote, b.quote)
);
return isPrice(b) ? {
...fractionSubtract(rawPrice(a), rawPrice(b)),
type: "price",
quote: a.quote,
base: a.base
} : makePriceFromFraction(
a.quote,
a.base,
fractionSubtract(adjustedPrice(a), b)
);
};
var priceMultiply = (a, b) => {
if (isPrice(b))
invariant4(
currencyEqualTo(a.base, b.base) && currencyEqualTo(a.quote, b.quote)
);
return isPrice(b) ? {
type: "price",
quote: a.quote,
base: a.base,
numerator: scaleUp(
a.base,
fractionMultiply(rawPrice(a), rawPrice(b)).numerator
),
denominator: scaleUp(
a.quote,
fractionMultiply(rawPrice(a), rawPrice(b)).denominator
)
} : makePriceFromFraction(
a.quote,
a.base,
fractionMultiply(adjustedPrice(a), b)
);
};
var priceDivide = (a, b) => {
if (isPrice(b))
invariant4(
currencyEqualTo(a.base, b.base) && currencyEqualTo(a.quote, b.quote)
);
return isPrice(b) ? {
type: "price",
quote: a.quote,
base: a.base,
numerator: scaleUp(
a.quote,
fractionDivide(rawPrice(a), rawPrice(b)).numerator
),
denominator: scaleUp(
a.base,
fractionDivide(rawPrice(a), rawPrice(b)).denominator
)
} : makePriceFromFraction(
a.quote,
a.base,
fractionDivide(adjustedPrice(a), b)
);
};
var priceLessThan = (a, b) => {
if (isPrice(b))
invariant4(
currencyEqualTo(a.base, b.base) && currencyEqualTo(a.quote, b.quote)
);
return isPrice(b) ? fractionLessThan(rawPrice(a), rawPrice(b)) : fractionLessThan(adjustedPrice(a), b);
};
var priceEqualTo = (a, b) => {
if (isPrice(b))
invariant4(
currencyEqualTo(a.base, b.base) && currencyEqualTo(a.quote, b.quote)
);
return isPrice(b) ? fractionEqualTo(rawPrice(a), rawPrice(b)) : fractionEqualTo(adjustedPrice(a), b);
};
var priceGreaterThan = (a, b) => {
if (isPrice(b))
invariant4(
currencyEqualTo(a.base, b.base) && currencyEqualTo(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(
price.quote,
price.numerator * currencyAmount.amount / price.denominator
);
};
var rawPrice = (price) => ({
type: "fraction",
numerator: price.numerator,
denominator: price.denominator
});
var adjustedPrice = (price) => ({
type: "fraction",
numerator: price.numerator * 10n ** BigInt(price.base.decimals),
denominator: price.denominator * 10n ** BigInt(price.quote.decimals)
});
// src/readUtils.ts
var readAndParse = async (reverseMirageRead) => {
return reverseMirageRead.parse(await reverseMirageRead.read());
};
// src/erc20/erc20Abi.ts
var erc20ABI = [
{
type: "event",
name: "Approval",
inputs: [
{
indexed: true,
name: "owner",
type: "address"
},
{
indexed: true,
name: "spender",
type: "address"
},
{
indexed: false,
name: "value",
type: "uint256"
}
]
},
{
type: "event",
name: "Transfer",
inputs: [
{
indexed: true,
name: "from",
type: "address"
},
{
indexed: true,
name: "to",
type: "address"
},
{
indexed: false,
name: "value",
type: "uint256"
}
]
},
{
type: "function",
name: "allowance",
stateMutability: "view",
inputs: [
{
name: "owner",
type: "address"
},
{
name: "spender",
type: "address"
}
],
outputs: [
{
name: "",
type: "uint256"
}
]
},
{
type: "function",
name: "approve",
stateMutability: "nonpayable",
inputs: [
{
name: "spender",
type: "address"
},
{
name: "amount",
type: "uint256"
}
],
outputs: [
{
name: "",
type: "bool"
}
]
},
{
type: "function",
name: "balanceOf",
stateMutability: "view",
inputs: [
{
name: "account",
type: "address"
}
],
outputs: [
{
name: "",
type: "uint256"
}
]
},
{
type: "function",
name: "decimals",
stateMutability: "view",
inputs: [],
outputs: [
{
name: "",
type: "uint8"
}
]
},
{
type: "function",
name: "name",
stateMutability: "view",
inputs: [],
outputs: [
{
name: "",
type: "string"
}
]
},
{
type: "function",
name: "symbol",
stateMutability: "view",
inputs: [],
outputs: [
{
name: "",
type: "string"
}
]
},
{
type: "function",
name: "totalSupply",
stateMutability: "view",
inputs: [],
outputs: [
{
name: "",
type: "uint256"
}
]
},
{
type: "function",
name: "transfer",
stateMutability: "nonpayable",
inputs: [
{
name: "recipient",
type: "address"
},
{
name: "amount",
type: "uint256"
}
],
outputs: [
{
name: "",
type: "bool"
}
]
},
{
type: "function",
name: "transferFrom",
stateMutability: "nonpayable",
inputs: [
{
name: "sender",
type: "address"
},
{
name: "recipient",
type: "address"
},
{
name: "amount",
type: "uint256"
}
],
outputs: [
{
name: "",
type: "bool"
}
]
}
];
// src/erc20/reads.ts
var erc20BalanceOf = (publicClient, args) => {
return {
read: () => publicClient.readContract({
abi: erc20ABI,
address: args.token.address,
functionName: "balanceOf",
args: [args.address]
}),
parse: (data) => makeCurrencyAmountFromRaw(args.token, data)
};
};
var erc20Allowance = (publicClient, args) => {
return {
read: () => publicClient.readContract({
abi: erc20ABI,
address: args.token.address,
functionName: "allowance",
args: [args.address, args.spender]
}),
parse: (data) => makeCurrencyAmountFromRaw(args.token, data)
};
};
var erc20TotalSupply = (publicClient, args) => {
return {
read: () => publicClient.readContract({
abi: erc20ABI,
address: args.token.address,
functionName: "totalSupply"
}),
parse: (data) => makeCurrencyAmountFromRaw(args.token, data)
};
};
var erc20Name = (publicClient, args) => {
return {
read: () => publicClient.readContract({
abi: erc20ABI,
address: args.token.address,
functionName: "name"
}),
parse: (data) => data
};
};
var erc20Symbol = (publicClient, args) => {
return {
read: () => publicClient.readContract({
abi: erc20ABI,
address: args.token.address,
functionName: "symbol"
}),
parse: (data) => data
};
};
var erc20Decimals = (publicClient, args) => {
return {
read: () => publicClient.readContract({
abi: erc20ABI,
address: args.token.address,
functionName: "decimals"
}),
parse: (data) => data
};
};
var erc20GetToken = (publicClient, args) => {
return {
read: () => Promise.all([
erc20Name(publicClient, args).read(),
erc20Symbol(publicClient, args).read(),
erc20Decimals(publicClient, args).read()
]),
parse: (data) => ({
name: data[0],
symbol: data[1],
decimals: data[2],
...args.token
})
};
};
// src/erc20/writes.ts
var erc20Transfer = async (publicClient, walletClient, account, args) => {
const { request, result } = await publicClient.simulateContract({
address: args.amount.currency.address,
abi: erc20ABI,
functionName: "transfer",
args: [args.to, args.amount.amount],
account
});
const hash = await walletClient.writeContract(request);
return { hash, result, request };
};
var erc20Approve = async (publicClient, walletClient, account, args) => {
const { request, result } = await publicClient.simulateContract({
address: args.amount.currency.address,
abi: erc20ABI,
functionName: "approve",
args: [args.spender, args.amount.amount],
account
});
const hash = await walletClient.writeContract(request);
return { hash, result, request };
};
var erc20TransferFrom = async (publicClient, walletClient, account, args) => {
const { request, result } = await publicClient.simulateContract({
address: args.amount.currency.address,
abi: erc20ABI,
functionName: "transferFrom",
args: [args.to, args.from, args.amount.amount],
account
});
const hash = await walletClient.writeContract(request);
return { hash, result, request };
};
// src/native/reads.ts
var nativeBalance = (publicClient, args) => {
return {
read: () => publicClient.getBalance({ address: args.address }),
parse: (data) => makeCurrencyAmountFromRaw(args.nativeCurrency, data)
};
};
export {
MaxUint128,
MaxUint16,
MaxUint256,
MaxUint32,
MaxUint64,
MaxUint8,
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
};
//# sourceMappingURL=index.js.map

12

package.json
{
"name": "reverse-mirage",
"version": "0.1.3",
"version": "0.1.4",
"description": "",

@@ -34,3 +34,4 @@ "type": "module",

"devDependencies": {
"@uniswap/sdk-core": "^4.0.3",
"@changesets/cli": "^2.26.2",
"@uniswap/sdk-core": "^4.0.6",
"@viem/anvil": "^0.0.6",

@@ -40,4 +41,5 @@ "@wagmi/cli": "^1.3.0",

"rome": "^12.1.3",
"typescript": "^5.0.4",
"viem": "^1.3.0",
"tsup": "^7.1.0",
"typescript": "^5.1.6",
"viem": "^1.4.0",
"vitest": "^0.33.0"

@@ -48,3 +50,3 @@ },

"bench:ci": "CI=true vitest bench",
"build": "tsc --outdir ./dist",
"build": "tsup",
"format": "rome format . --write",

@@ -51,0 +53,0 @@ "lint": "rome check .",

@@ -10,2 +10,2 @@ # Reverse Mirage [![GitHub Actions][gha-badge]][gha] [![npm version][npm-badge]][npm] [![npm bundle size][bundle-badge]][bundle]

Application level typescript utilities for the Ethereum Virtual Machine
Application level typescript utilities for the Ethereum Virtual Machine.

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