|
| 1 | +import { |
| 2 | + ComputeBudgetProgram, |
| 3 | + Connection, |
| 4 | + PublicKey, |
| 5 | +} from "@solana/web3.js"; |
| 6 | +import "dotenv/config"; |
| 7 | +import { uint64ToBN } from "@wormhole-foundation/example-liquidity-layer-solana/common"; |
| 8 | +import { AuctionParameters, MatchingEngineProgram } from "@wormhole-foundation/example-liquidity-layer-solana/matchingEngine"; |
| 9 | +import { LoggerFn, connectionCommitmentLevel, getChainConfig, getContractAddress, ledgerSignAndSend, runOnSolana } from "../../helpers"; |
| 10 | +import { MatchingEngineConfiguration } from "../../config/config-types"; |
| 11 | +import { ProgramId } from "@wormhole-foundation/example-liquidity-layer-solana/matchingEngine"; |
| 12 | +import { SolanaLedgerSigner } from "@xlabs-xyz/ledger-signer-solana"; |
| 13 | +import { circle } from "@wormhole-foundation/sdk-base"; |
| 14 | + |
| 15 | + |
| 16 | +const AUCTION_PARAMS: AuctionParameters = { |
| 17 | + userPenaltyRewardBps: 400000, // 40% |
| 18 | + initialPenaltyBps: 250000, // 25% |
| 19 | + duration: 5, // slots |
| 20 | + gracePeriod: 10, // slots |
| 21 | + penaltyPeriod: 20, // slots |
| 22 | + minOfferDeltaBps: 50000, // 5% |
| 23 | + securityDepositBase: uint64ToBN(1000000n), // 1 USDC |
| 24 | + securityDepositBps: 5000, // 0.5% |
| 25 | +}; |
| 26 | + |
| 27 | +runOnSolana("deploy-matching-engine", async (chain, signer, log) => { |
| 28 | + const config = await getChainConfig<MatchingEngineConfiguration>("matching-engine", chain.chainId); |
| 29 | + const matchingEngineId = getContractAddress("MatchingEngine", chain.chainId) as ProgramId; |
| 30 | + |
| 31 | + const env = "Mainnet"; |
| 32 | + const usdcMint = new PublicKey(circle.usdcContract(env, "Solana")); |
| 33 | + const connection = new Connection(chain.rpc, connectionCommitmentLevel); |
| 34 | + const matchingEngine = new MatchingEngineProgram(connection, matchingEngineId, usdcMint); |
| 35 | + |
| 36 | + await initialize(matchingEngine, signer, log, config, usdcMint); |
| 37 | +}); |
| 38 | + |
| 39 | +async function initialize(matchingEngine: MatchingEngineProgram, signer: SolanaLedgerSigner, log: LoggerFn, config: MatchingEngineConfiguration, usdcMint: PublicKey) { |
| 40 | + const connection = matchingEngine.program.provider.connection; |
| 41 | + |
| 42 | + const custodian = matchingEngine.custodianAddress(); |
| 43 | + log("custodian", custodian.toString()); |
| 44 | + |
| 45 | + const exists = await connection.getAccountInfo(custodian).then((acct) => acct != null); |
| 46 | + if (exists) { |
| 47 | + log("already initialized"); |
| 48 | + return; |
| 49 | + } |
| 50 | + |
| 51 | + const signerPubkey = new PublicKey(await signer.getAddress()); |
| 52 | + const initializeIx = await matchingEngine.initializeIx( |
| 53 | + { |
| 54 | + owner: signerPubkey, |
| 55 | + ownerAssistant: signerPubkey, |
| 56 | + feeRecipient: signerPubkey, |
| 57 | + }, |
| 58 | + AUCTION_PARAMS, |
| 59 | + ); |
| 60 | + |
| 61 | + const splToken = await import("@solana/spl-token"); |
| 62 | + const assocciatedTokenProgramId = splToken.ASSOCIATED_TOKEN_PROGRAM_ID; |
| 63 | + const associatedToken = splToken.getAssociatedTokenAddressSync(usdcMint, signerPubkey, undefined, usdcMint, assocciatedTokenProgramId); |
| 64 | + const createAtaInstructions = []; |
| 65 | + createAtaInstructions.push(splToken.createAssociatedTokenAccountInstruction(signerPubkey, associatedToken, signerPubkey, usdcMint)); |
| 66 | + createAtaInstructions.push(ComputeBudgetProgram.setComputeUnitPrice({ microLamports: 1 })); |
| 67 | + |
| 68 | + const createAtaTxid = await ledgerSignAndSend(connection, createAtaInstructions, []); |
| 69 | + log(`CreateAtaTxid ${createAtaTxid}`); |
| 70 | + |
| 71 | + const initializeTxid = await ledgerSignAndSend(connection, [initializeIx], []); |
| 72 | + log(`InitializeTxid ${initializeTxid}`); |
| 73 | +} |
| 74 | + |
0 commit comments