Skip to content

Commit

Permalink
Merge pull request #106 from clober-dex/test/vault-for-user
Browse files Browse the repository at this point in the history
Test/Vault for user action
  • Loading branch information
graykode authored Sep 26, 2024
2 parents 706b668 + 393f982 commit 353dc51
Show file tree
Hide file tree
Showing 15 changed files with 1,249 additions and 24 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# Based on https://raw.githubusercontent.com/github/gitignore/main/Node.gitignore
test.ts
.graphclient/
docs

Expand Down
2 changes: 1 addition & 1 deletion examples/cancel-order.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ const main = async () => {
const walletClient = createWalletClient({
chain: arbitrumSepolia,
account: privateKeyToAccount(
(process.env.DEV_PRIVATE_KEY || '0x') as `0x${string}`,
(process.env.TESTNET_PRIVATE_KEY || '0x') as `0x${string}`,
),
transport: http(),
})
Expand Down
2 changes: 1 addition & 1 deletion examples/claim-order.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ const main = async () => {
const walletClient = createWalletClient({
chain: arbitrumSepolia,
account: privateKeyToAccount(
(process.env.DEV_PRIVATE_KEY || '0x') as `0x${string}`,
(process.env.TESTNET_PRIVATE_KEY || '0x') as `0x${string}`,
),
transport: http(),
})
Expand Down
2 changes: 1 addition & 1 deletion examples/limit-order.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ const main = async () => {
const walletClient = createWalletClient({
chain: arbitrumSepolia,
account: privateKeyToAccount(
(process.env.DEV_PRIVATE_KEY || '0x') as `0x${string}`,
(process.env.TESTNET_PRIVATE_KEY || '0x') as `0x${string}`,
),
transport: http(),
})
Expand Down
2 changes: 1 addition & 1 deletion examples/market-order.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ const main = async () => {
const walletClient = createWalletClient({
chain: arbitrumSepolia,
account: privateKeyToAccount(
(process.env.DEV_PRIVATE_KEY || '0x') as `0x${string}`,
(process.env.TESTNET_PRIVATE_KEY || '0x') as `0x${string}`,
),
transport: http(),
})
Expand Down
48 changes: 48 additions & 0 deletions examples/open-market.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import { createWalletClient, http, parseUnits } from 'viem'
import { arbitrumSepolia } from 'viem/chains'
import { privateKeyToAccount } from 'viem/accounts'
import * as dotenv from 'dotenv'
import { getMarket, openMarket } from '@clober/v2-sdk'

dotenv.config()

const main = async () => {
const walletClient = createWalletClient({
chain: arbitrumSepolia,
account: privateKeyToAccount(
(process.env.TESTNET_PRIVATE_KEY || '0x') as `0x${string}`,
),
transport: http(),
})

const transaction1 = await openMarket({
chainId: arbitrumSepolia.id,
inputToken: '0x00bfd44e79fb7f6dd5887a9426c8ef85a0cd23e0',
outputToken: '0xF2e615A933825De4B39b497f6e6991418Fb31b78',
})
const hash1 = await walletClient.sendTransaction({
...transaction1,
gasPrice: parseUnits('1', 9),
})
console.log(`open market hash: ${hash1}`)

const transaction2 = await openMarket({
chainId: arbitrumSepolia.id,
inputToken: '0xF2e615A933825De4B39b497f6e6991418Fb31b78',
outputToken: '0x00bfd44e79fb7f6dd5887a9426c8ef85a0cd23e0',
})
const hash2 = await walletClient.sendTransaction({
...transaction2,
gasPrice: parseUnits('1', 9),
})
console.log(`open market hash: ${hash2}`)

const market = await getMarket({
chainId: arbitrumSepolia.id,
token0: '0x00bfd44e79fb7f6dd5887a9426c8ef85a0cd23e0',
token1: '0xF2e615A933825De4B39b497f6e6991418Fb31b78',
})
console.log(`market: `, market)
}

main()
33 changes: 33 additions & 0 deletions examples/pool/open.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import * as dotenv from 'dotenv'
import { createWalletClient, http, parseUnits, zeroHash } from 'viem'
import { arbitrumSepolia } from 'viem/chains'
import { privateKeyToAccount } from 'viem/accounts'

import { openPool } from '../../src'

dotenv.config()

const main = async () => {
const walletClient = createWalletClient({
chain: arbitrumSepolia,
account: privateKeyToAccount(
(process.env.TESTNET_PRIVATE_KEY || '0x') as `0x${string}`,
),
transport: http(),
})

const transaction = await openPool({
chainId: arbitrumSepolia.id,
userAddress: '0x5F79EE8f8fA862E98201120d83c4eC39D9468D49',
tokenA: '0x00BFD44e79FB7f6dd5887A9426c8EF85A0CD23e0',
tokenB: '0xF2e615A933825De4B39b497f6e6991418Fb31b78',
salt: zeroHash,
})
const hash = await walletClient.sendTransaction({
...transaction,
gasPrice: parseUnits('1', 9),
})
console.log(`open pool hash: ${hash}`)
}

main()
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@clober/v2-sdk",
"version": "0.0.66",
"version": "0.0.67",
"description": "🛠 An SDK for building applications on top of Clober V2",
"files": [
"dist"
Expand Down
8 changes: 4 additions & 4 deletions src/apis/odos.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ export async function fetchQuote({
tokenOut: Currency
slippageLimitPercent: number
userAddress: `0x${string}`
testnetPrice?: number // tokenOutAmount per tokenIn, for testnet chains only
testnetPrice?: number | undefined // tokenOutAmount per tokenIn, for testnet chains only
}): Promise<{
amountOut: bigint
pathId: string
Expand All @@ -49,7 +49,7 @@ export async function fetchQuote({
amountOut: parseUnits(
(
Number(formatUnits(amountIn, tokenIn.decimals)) * testnetPrice
).toFixed(),
).toString(),
tokenOut.decimals,
),
pathId: '0x',
Expand Down Expand Up @@ -106,7 +106,7 @@ export async function fetchCallData({
tokenOut: Currency
slippageLimitPercent: number
userAddress: `0x${string}`
testnetPrice?: number // tokenOutAmount per tokenIn, for testnet chains only
testnetPrice?: number | undefined // tokenOutAmount per tokenIn, for testnet chains only
}): Promise<{
amountOut: bigint
data: `0x${string}`
Expand All @@ -119,7 +119,7 @@ export async function fetchCallData({
const amountOut = parseUnits(
(
Number(formatUnits(amountIn, tokenIn.decimals)) * testnetPrice
).toFixed(),
).toString(),
tokenOut.decimals,
)

Expand Down
32 changes: 23 additions & 9 deletions src/call.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ import {
parsePrice,
} from './utils/prices'
import { invertTick, toPrice } from './utils/tick'
import { getExpectedInput, getExpectedOutput } from './view'
import { getExpectedInput, getExpectedOutput, getQuoteToken } from './view'
import { toBookId } from './utils/book-id'
import { fetchIsApprovedForAll } from './utils/approval'
import { fetchOnChainOrders } from './utils/order'
Expand Down Expand Up @@ -1097,9 +1097,9 @@ export const addLiquidity = async ({
options?: {
slippage?: number
disableSwap?: boolean
testnetPrice?: string // token1 amount per token0
token0PermitParams?: ERC20PermitParam
token1PermitParams?: ERC20PermitParam
testnetPrice?: number
useSubgraph?: boolean
} & DefaultWriteContractOptions
}): Promise<{
Expand All @@ -1110,6 +1110,12 @@ export const addLiquidity = async ({
lpCurrency: Currency6909Flow
}
}> => {
if (
isAddressEqual(token0, zeroAddress) ||
isAddressEqual(token1, zeroAddress)
) {
throw new Error('ETH is not supported for adding liquidity')
}
const publicClient = createPublicClient({
chain: CHAIN_MAP[chainId],
transport: options?.rpcUrl ? http(options.rpcUrl) : http(),
Expand Down Expand Up @@ -1178,12 +1184,18 @@ export const addLiquidity = async ({
}

if (!disableSwap) {
const token0Price = Number(
options?.testnetPrice ? options.testnetPrice : '1',
)
const currencyBPerCurrencyA = isAddressEqual(token1, pool.currencyB.address)
? token0Price
: 1 / token0Price
const currencyBPerCurrencyA = options?.testnetPrice
? isAddressEqual(
getQuoteToken({
chainId,
token0,
token1,
}),
pool.currencyA.address,
)
? 1 / Number(options.testnetPrice)
: Number(options.testnetPrice)
: undefined
const swapAmountA = parseUnits('1', pool.currencyA.decimals)
const { amountOut: swapAmountB } = await fetchQuote({
chainId,
Expand Down Expand Up @@ -1228,7 +1240,9 @@ export const addLiquidity = async ({
tokenOut: pool.currencyA,
slippageLimitPercent,
userAddress: CONTRACT_ADDRESSES[chainId]!.Minter,
testnetPrice: 1 / currencyBPerCurrencyA,
testnetPrice: currencyBPerCurrencyA
? 1 / currencyBPerCurrencyA
: undefined,
})
swapParams.data = calldata
amountA += actualDeltaA
Expand Down
6 changes: 4 additions & 2 deletions src/constants/permit.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import { zeroHash } from 'viem'

export const emptyERC20PermitParams = {
permitAmount: 0n,
signature: {
deadline: 0n,
v: 0,
r: '0x',
s: '0x',
r: zeroHash,
s: zeroHash,
},
}
Loading

0 comments on commit 353dc51

Please sign in to comment.