Skip to content

Commit

Permalink
* update serum and psyoptions packages (#445)
Browse files Browse the repository at this point in the history
* add single TX multi mint support
  • Loading branch information
tomjohn1028 authored Jul 10, 2021
1 parent 72083dc commit a5964f1
Show file tree
Hide file tree
Showing 8 changed files with 166 additions and 237 deletions.
40 changes: 15 additions & 25 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@
"@ledgerhq/hw-transport-webusb": "^5.48.0",
"@material-ui/core": "^4.11.3",
"@material-ui/icons": "^4.11.2",
"@mithraic-labs/psyoptions": "^0.5.2",
"@mithraic-labs/serum": "^0.0.3",
"@mithraic-labs/psyoptions": "^0.7.1",
"@mithraic-labs/serum": "^0.0.5",
"@project-serum/sol-wallet-adapter": "0.2.0",
"@project-serum/tokens": "0.0.7",
"@sentry/react": "^6.2.1",
Expand Down
16 changes: 8 additions & 8 deletions scripts/seedLocalNet.js
Original file line number Diff line number Diff line change
Expand Up @@ -135,32 +135,32 @@ const trustWalletERC20Icon = (address) =>
// Generate sample assets from local net data and random logos
const localSPLData = [
{
decimals: 8,
mintAddress: splMint4.publicKey.toString(),
tokenSymbol: 'BTC',
tokenName: 'Bitcoin',
icon:
'https://raw.githubusercontent.com/trustwallet/assets/master/blockchains/bitcoin/info/logo.png',
icon: 'https://raw.githubusercontent.com/trustwallet/assets/master/blockchains/bitcoin/info/logo.png',
},
{
decimals: 8,
mintAddress: splMint1.publicKey.toString(),
tokenSymbol: 'LSRM',
tokenName: 'Serum',
icon:
'https://raw.githubusercontent.com/trustwallet/assets/master/blockchains/ethereum/assets/0x476c5E26a75bd202a9683ffD34359C0CC15be0fF/logo.png',
icon: 'https://raw.githubusercontent.com/trustwallet/assets/master/blockchains/ethereum/assets/0x476c5E26a75bd202a9683ffD34359C0CC15be0fF/logo.png',
},
{
decimals: 8,
mintAddress: splMint3.publicKey.toString(),
tokenSymbol: 'ETH',
tokenName: 'Ethereum',
icon:
'https://raw.githubusercontent.com/trustwallet/assets/master/blockchains/ethereum/assets/0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2/logo.png',
icon: 'https://raw.githubusercontent.com/trustwallet/assets/master/blockchains/ethereum/assets/0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2/logo.png',
},
{
decimals: 8,
mintAddress: splMint2.publicKey.toString(),
tokenSymbol: 'USDC',
tokenName: 'USDC',
icon:
'https://raw.githubusercontent.com/trustwallet/assets/f3ffd0b9ae2165336279ce2f8db1981a55ce30f8/blockchains/ethereum/assets/0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48/logo.png',
icon: 'https://raw.githubusercontent.com/trustwallet/assets/f3ffd0b9ae2165336279ce2f8db1981a55ce30f8/blockchains/ethereum/assets/0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48/logo.png',
},
]
fs.writeFileSync(
Expand Down
26 changes: 18 additions & 8 deletions scripts/setupMarketsFromMetaData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { Token, TOKEN_PROGRAM_ID } from '@solana/spl-token'
import {
initializeAccountsForMarket,
initializeMarketInstruction,
Market,
} from '@mithraic-labs/psyoptions'
import BigNumber from 'bignumber.js'
import { getSolanaConfig } from './helpers'
Expand Down Expand Up @@ -64,7 +65,6 @@ const OPTION_PROGRAM_ID = new PublicKey(
const {
transaction: createAccountsTx,
signers,
optionMarketKey,
optionMintKey,
writerTokenMintKey,
quoteAssetPoolKey,
Expand All @@ -85,6 +85,13 @@ const OPTION_PROGRAM_ID = new PublicKey(
},
)

const underlyingAmountPerContract = new BigNumber(
marketMeta.underlyingAssetPerContract,
).toNumber()
const quoteAmountPerContract = new BigNumber(
marketMeta.quoteAssetPerContract,
).toNumber()

// create and send transaction for initializing the option market
const initializeMarketIx = await initializeMarketInstruction({
programId: OPTION_PROGRAM_ID,
Expand All @@ -93,15 +100,10 @@ const OPTION_PROGRAM_ID = new PublicKey(
quoteAssetMintKey,
optionMintKey,
writerTokenMintKey,
optionMarketKey,
underlyingAssetPoolKey,
quoteAssetPoolKey,
underlyingAmountPerContract: new BigNumber(
marketMeta.underlyingAssetPerContract,
).toNumber(),
quoteAmountPerContract: new BigNumber(
marketMeta.quoteAssetPerContract,
).toNumber(),
underlyingAmountPerContract,
quoteAmountPerContract,
expirationUnixTimestamp: marketMeta.expiration,
})
const transaction = new Transaction()
Expand All @@ -118,6 +120,14 @@ const OPTION_PROGRAM_ID = new PublicKey(
},
)
console.log(`* confirmed mint TX id: ${txId}`)
const [optionMarketKey] = await Market.getDerivedAddressFromParams({
programId: OPTION_PROGRAM_ID,
underlyingAssetMintKey,
quoteAssetMintKey,
underlyingAmountPerContract,
quoteAmountPerContract,
expirationUnixTimestamp: marketMeta.expiration,
})
newOptionMarketAddresses.push(optionMarketKey.toString())
})()
}, starterPromise)
Expand Down
24 changes: 20 additions & 4 deletions src/hooks/useInitializeMarkets.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import BigNumber from 'bignumber.js'
import {
initializeAccountsForMarket,
initializeMarketInstruction,
Market,
} from '@mithraic-labs/psyoptions'
import useConnection from './useConnection'
import useNotifications from './useNotifications'
Expand Down Expand Up @@ -48,22 +49,22 @@ export const useInitializeMarkets = (): ((
qAssetDecimals,
}: InitMarketParams) => {
try {
const programId = new PublicKey(endpoint.programId)
const results = await Promise.all(
quoteAmountsPerContract.map(async (qAmount) => {
// Create and send transaction for creating/initializing accounts needed
// for option market
const {
transaction: createAccountsTx,
signers,
optionMarketKey,
optionMintKey,
writerTokenMintKey,
quoteAssetPoolKey,
underlyingAssetPoolKey,
} = await initializeAccountsForMarket({
connection,
payerKey: pubKey,
programId: endpoint.programId,
programId,
})
await sendTransaction({
transaction: createAccountsTx,
Expand All @@ -86,15 +87,21 @@ export const useInitializeMarkets = (): ((
const underlyingAssetMintKey = new PublicKey(uAssetMint)
const quoteAssetMintKey = new PublicKey(qAssetMint)

console.log(
'UI log',
amountPerContract.toString(),
amountPerContractU64,
uAssetDecimals,
)

// create and send transaction for initializing the option market
const initializeMarketIx = await initializeMarketInstruction({
programId: new PublicKey(endpoint.programId),
programId,
fundingAccountKey: pubKey,
underlyingAssetMintKey,
quoteAssetMintKey,
optionMintKey,
writerTokenMintKey,
optionMarketKey,
underlyingAssetPoolKey,
quoteAssetPoolKey,
underlyingAmountPerContract: amountPerContractU64,
Expand All @@ -113,6 +120,15 @@ export const useInitializeMarkets = (): ((
successMessage: 'Confirmed: Initialize Market',
})

const [optionMarketKey] = await Market.getDerivedAddressFromParams({
programId,
underlyingAssetMintKey,
quoteAssetMintKey,
underlyingAmountPerContract: amountPerContractU64,
quoteAmountPerContract: quoteAmountPerContractU64,
expirationUnixTimestamp: expiration,
})

const marketData: OptionMarket = {
key: `${expiration}-${uAssetSymbol}-${qAssetSymbol}-${amountPerContract.toString()}-${amountPerContract.toString()}/${qAmount.toString()}`,
amountPerContract,
Expand Down
6 changes: 3 additions & 3 deletions src/hooks/usePlaceBuyOrder.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ const usePlaceBuyOrder = (
}
// place the buy order
const {
createdOpenOrdersKey,
openOrdersAddress,
transaction: placeOrderTx,
signers: placeOrderSigners,
} = await serumMarket.market.makePlaceOrderTransaction(connection, {
Expand All @@ -64,8 +64,8 @@ const usePlaceBuyOrder = (
transaction.add(placeOrderTx)
signers = [...signers, ...placeOrderSigners]

if (createdOpenOrdersKey) {
createAdHocOpenOrdersSub(createdOpenOrdersKey)
if (openOrdersAddress) {
createAdHocOpenOrdersSub(openOrdersAddress)
}

await sendTransaction({
Expand Down
Loading

0 comments on commit a5964f1

Please sign in to comment.