Skip to content

Commit

Permalink
fix(gm): Update contracts
Browse files Browse the repository at this point in the history
  • Loading branch information
0xmDreamy committed Jul 22, 2024
1 parent 0055052 commit 92c908c
Show file tree
Hide file tree
Showing 13 changed files with 1,697 additions and 439 deletions.
12 changes: 6 additions & 6 deletions scripts/create-gm-task.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,15 @@ const main = async () => {
maxSwapSlippageBips: 100,
maxDepositSlippageBips: 50,
dataStoreAddress: "0xFD70de6b91282D8017aA4E741e9Ae325CAb992d8",
gmReaderAddress: "0xf60becbba223EEA9495Da3f606753867eC10d139",
gmReaderAddress: "0x5Ca84c34a381434786738735265b9f3FD814b824",
} satisfies Partial<Web3FunctionUserArgs>;
const configs = [
{
...commonBatchTaskOptions,
name: "gmARB",
web3FunctionArgs: {
...commonWeb3FunctionArgs,
execAddress: "0x39c54BD10261D42EE1838D5Fc71DD307Dcb39001",
execAddress: "0x892dDc36214fB833E702f53B612e2608E14EE27C",
rewardToken: "0x912CE59144191C1204E64559FE8253a0e49E6548",
marketInputToken: "0x912CE59144191C1204E64559FE8253a0e49E6548",
stakingAddress: "0xAF4FDCAa6d9D5Be4Acd8FCE02fA37f72b31a74cb",
Expand All @@ -56,7 +56,7 @@ const main = async () => {
name: "gmBTC",
web3FunctionArgs: {
...commonWeb3FunctionArgs,
execAddress: "0xb4fC7be1FC0a6d7b6D5d509C622F56d719Cd1373",
execAddress: "0x0D609C455f3bBD9971e37aDF41cDb342C32Ac801",
rewardToken: "0x912CE59144191C1204E64559FE8253a0e49E6548",
marketInputToken: "0xaf88d065e77c8cC2239327C5EDb3A432268e5831",
stakingAddress: "0xeB0DeaB1099DD5A7d499b89a6f47cEF8F08c5680",
Expand All @@ -68,7 +68,7 @@ const main = async () => {
name: "gmETH",
web3FunctionArgs: {
...commonWeb3FunctionArgs,
execAddress: "0xf53a003e863BA83424048d729460fba056c06b80",
execAddress: "0x60a816a7a5387D59D073d9d7441E03A278cFad30",
rewardToken: "0x912CE59144191C1204E64559FE8253a0e49E6548",
marketInputToken: "0xaf88d065e77c8cC2239327C5EDb3A432268e5831",
stakingAddress: "0xa7940dcB17214faBCE26E146613804308C01c295",
Expand All @@ -80,7 +80,7 @@ const main = async () => {
name: "gmLINK",
web3FunctionArgs: {
...commonWeb3FunctionArgs,
execAddress: "0x25aC30195F5b7653DdD7eb93Cae6ff5D924cDAF4",
execAddress: "0x3142aB041cF9Db9b1b22D9f55716e83B1eA178f1",
rewardToken: "0x912CE59144191C1204E64559FE8253a0e49E6548",
marketInputToken: "0xaf88d065e77c8cC2239327C5EDb3A432268e5831",
stakingAddress: "0x5b51f27c279aEecc8352688B69d55B533417e263",
Expand All @@ -92,7 +92,7 @@ const main = async () => {
name: "gmSOL",
web3FunctionArgs: {
...commonWeb3FunctionArgs,
execAddress: "0x9F026f9edc92150076bB8A0aC44c14A8412C1639",
execAddress: "0x19366CAf3991EBB48566ccde140eeeb3D514D9e5",
rewardToken: "0x912CE59144191C1204E64559FE8253a0e49E6548",
marketInputToken: "0xaf88d065e77c8cC2239327C5EDb3A432268e5831",
stakingAddress: "0x18F7cCa3D98aD96cf26DBDA1Db3Fd71e30D32d31",
Expand Down
161 changes: 87 additions & 74 deletions utils/gm.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,18 @@
import { BigNumber, Contract, type providers } from "ethers";
import ky from "ky";
import type { Address, Hex, PublicClient } from "viem";

import gmDataStoreAbi from "./gmDataStoreAbi";
import gmReaderAbi from "./gmReaderAbi";
import type { Hex } from "./types";
import { gmReaderAbi } from "./gmReaderAbi";

const ZERO_ADDRESS: Hex = "0x0000000000000000000000000000000000000000";
const ZERO_ADDRESS: Address = "0x0000000000000000000000000000000000000000";

const DEPOSIT_SIGNLE_TOKEN_GAS_LIMIT_KEY: Hex =
"0xefc0960e00ee78ec9c4ac47dfe361c3ed2dc14c6be6004a1e6593b843b045001"; // DEPOSIT_GAS_LIMIT
const ESTIMATED_GAS_FEE_BASE_AMOUNT_KEY: Hex =
"0xb240624f82b02b1a8e07fd5d67821e9664f273e0dc86415a33c1f3f444c81db4"; // ESTIMATED_GAS_FEE_BASE_AMOUNT
const ESTIMATED_GAS_FEE_MULTIPLIER_FACTOR_KEY: Hex =
"0xce135f2a886cf6d862269f215b1e64498fa09cb04f90b771b163399df2a82b81"; // ESTIMATED_GAS_FEE_MULTIPLIER_FACTOR;
const ESTIMATED_GAS_FEE_MULTIPLIER_FACTOR_PRECISION =
BigNumber.from(10).pow(30);
const ESTIMATED_GAS_FEE_MULTIPLIER_FACTOR_PRECISION = 10n ** 30n;
const SINGLE_SWAP_GAS_LIMIT_KEY: Hex =
"0x3be28fb346f7abc4a956a16d3739c8c4bfcca9385988c64bf86cdd16638c1f81"; // SINGLE_SWAP_GAS_LIMIT

Expand Down Expand Up @@ -44,32 +42,36 @@ export async function getTickers({
}

export type Market = {
marketToken: Hex;
indexToken: Hex;
longToken: Hex;
shortToken: Hex;
marketToken: Address;
indexToken: Address;
longToken: Address;
shortToken: Address;
};

export type GetMarketParameters = {
provider: providers.Provider;
readerAddress: Hex;
marketAddress: Hex;
dataStoreAddress: Hex;
client: PublicClient;
readerAddress: Address;
marketAddress: Address;
dataStoreAddress: Address;
};

export function getMarket({
provider,
client,
readerAddress,
marketAddress,
dataStoreAddress,
}: GetMarketParameters): Promise<Market> {
const readerContract = new Contract(readerAddress, gmReaderAbi, provider);
return readerContract.getMarket(dataStoreAddress, marketAddress);
return client.readContract({
abi: gmReaderAbi,
address: readerAddress,
functionName: "getMarket",
args: [dataStoreAddress, marketAddress],
});
}

export type Price = {
min: BigNumber;
max: BigNumber;
min: bigint;
max: bigint;
};

export type MarketPrices = {
Expand All @@ -86,7 +88,7 @@ export function getMarketPrices({
market,
tickers,
}: GetMarketPrices): MarketPrices {
const getTokenPrice = (tokenAddress: Hex) => {
const getTokenPrice = (tokenAddress: Address) => {
const ticker = tickers.find(
(ticker) =>
ticker.tokenAddress.toLowerCase() === tokenAddress.toLowerCase(),
Expand All @@ -95,8 +97,8 @@ export function getMarketPrices({
throw Error(`tickers does not have token: ${tokenAddress}`);
}
return {
min: BigNumber.from(ticker.minPrice),
max: BigNumber.from(ticker.maxPrice),
min: BigInt(ticker.minPrice),
max: BigInt(ticker.maxPrice),
};
};
return {
Expand All @@ -107,92 +109,103 @@ export function getMarketPrices({
}

export type getDepositAmountParameters = {
longTokenAmount: BigNumber;
shortTokenAmount: BigNumber;
longTokenAmount: bigint;
shortTokenAmount: bigint;
} & GetTickersParameters &
GetMarketParameters;

export async function getDepositAmountOut({
longTokenAmount,
shortTokenAmount,
endpoint,
provider,
client,
readerAddress,
marketAddress,
dataStoreAddress,
}: getDepositAmountParameters): Promise<BigNumber> {
}: getDepositAmountParameters): Promise<bigint> {
const [market, tickers] = await Promise.all([
getMarket({ provider, readerAddress, marketAddress, dataStoreAddress }),
getMarket({ client, readerAddress, marketAddress, dataStoreAddress }),
getTickers({ endpoint }),
]);
const marketPrices = getMarketPrices({ market, tickers });

const readerContract = new Contract(readerAddress, gmReaderAbi, provider);
return readerContract.getDepositAmountOut(
dataStoreAddress,
market,
marketPrices,
longTokenAmount,
shortTokenAmount,
ZERO_ADDRESS,
);
return client.readContract({
abi: gmReaderAbi,
address: readerAddress,
functionName: "getDepositAmountOut",
args: [
dataStoreAddress,
market,
marketPrices,
longTokenAmount,
shortTokenAmount,
ZERO_ADDRESS,
0, // TwoStep
true, // includeVirtualInventoryImpact = true
],
});
}

export type GasLimitParameters = {
provider: providers.Provider;
dataStoreAddress: Hex;
client: PublicClient;
dataStoreAddress: Address;
};

export function getDepositSingleTokenGasLimit({
provider,
client,
dataStoreAddress,
}: GasLimitParameters): Promise<BigNumber> {
const dataStoreContract = new Contract(
dataStoreAddress,
gmDataStoreAbi,
provider,
);
return dataStoreContract.getUint(DEPOSIT_SIGNLE_TOKEN_GAS_LIMIT_KEY);
}: GasLimitParameters): Promise<bigint> {
return client.readContract({
abi: gmDataStoreAbi,
address: dataStoreAddress,
functionName: "getUint",
args: [DEPOSIT_SIGNLE_TOKEN_GAS_LIMIT_KEY],
});
}

export function getSingleSwapGasLimit({
provider,
client,
dataStoreAddress,
}: GasLimitParameters): Promise<BigNumber> {
const dataStoreContract = new Contract(
dataStoreAddress,
gmDataStoreAbi,
provider,
);
return dataStoreContract.getUint(SINGLE_SWAP_GAS_LIMIT_KEY);
}: GasLimitParameters): Promise<bigint> {
return client.readContract({
abi: gmDataStoreAbi,
address: dataStoreAddress,
functionName: "getUint",
args: [SINGLE_SWAP_GAS_LIMIT_KEY],
});
}

export type GetExecutionFeeParameters = {
provider: providers.Provider;
dataStoreAddress: Hex;
gasLimit: BigNumber;
gasPrice: BigNumber;
client: PublicClient;
dataStoreAddress: Address;
gasLimit: bigint;
gasPrice: bigint;
};

export async function getExecutionFee({
provider,
client,
dataStoreAddress,
gasLimit,
gasPrice,
}: GetExecutionFeeParameters): Promise<BigNumber> {
const dataStoreContract = new Contract(
dataStoreAddress,
gmDataStoreAbi,
provider,
);
}: GetExecutionFeeParameters): Promise<bigint> {
const [estimatedGasFeeBaseAmount, estimatedGasFeeMultiplierFactor] =
(await Promise.all([
dataStoreContract.getUint(ESTIMATED_GAS_FEE_BASE_AMOUNT_KEY),
dataStoreContract.getUint(ESTIMATED_GAS_FEE_MULTIPLIER_FACTOR_KEY),
])) as [BigNumber, BigNumber];
const adjustedGasLimit = gasLimit
.mul(estimatedGasFeeMultiplierFactor)
.div(ESTIMATED_GAS_FEE_MULTIPLIER_FACTOR_PRECISION)
.add(estimatedGasFeeBaseAmount);
return adjustedGasLimit.mul(gasPrice);
await Promise.all([
client.readContract({
abi: gmDataStoreAbi,
address: dataStoreAddress,
functionName: "getUint",
args: [ESTIMATED_GAS_FEE_BASE_AMOUNT_KEY],
}),
client.readContract({
abi: gmDataStoreAbi,
address: dataStoreAddress,
functionName: "getUint",
args: [ESTIMATED_GAS_FEE_MULTIPLIER_FACTOR_KEY],
}),
]);
const adjustedGasLimit =
(gasLimit * estimatedGasFeeMultiplierFactor) /
ESTIMATED_GAS_FEE_MULTIPLIER_FACTOR_PRECISION +
estimatedGasFeeBaseAmount;
return adjustedGasLimit * gasPrice;
}
Loading

0 comments on commit 92c908c

Please sign in to comment.