mirror of
https://github.com/orionprotocol/sdk.git
synced 2026-04-03 11:38:09 +03:00
added PMM
This commit is contained in:
63
src/Unit/Pmm/abi/OrionRFQ.ts
Normal file
63
src/Unit/Pmm/abi/OrionRFQ.ts
Normal file
@@ -0,0 +1,63 @@
|
||||
export const orionRFQContractABI =
|
||||
[
|
||||
{
|
||||
"inputs": [
|
||||
{
|
||||
"components": [
|
||||
{
|
||||
"internalType": "uint256",
|
||||
"name": "info",
|
||||
"type": "uint256"
|
||||
},
|
||||
{
|
||||
"internalType": "address",
|
||||
"name": "makerAsset",
|
||||
"type": "address"
|
||||
},
|
||||
{
|
||||
"internalType": "address",
|
||||
"name": "takerAsset",
|
||||
"type": "address"
|
||||
},
|
||||
{
|
||||
"internalType": "address",
|
||||
"name": "maker",
|
||||
"type": "address"
|
||||
},
|
||||
{
|
||||
"internalType": "address",
|
||||
"name": "allowedSender",
|
||||
"type": "address"
|
||||
},
|
||||
{
|
||||
"internalType": "uint256",
|
||||
"name": "makingAmount",
|
||||
"type": "uint256"
|
||||
},
|
||||
{
|
||||
"internalType": "uint256",
|
||||
"name": "takingAmount",
|
||||
"type": "uint256"
|
||||
}
|
||||
],
|
||||
"internalType": "struct OrderRFQLib.OrderRFQ",
|
||||
"name": "order",
|
||||
"type": "tuple"
|
||||
},
|
||||
{
|
||||
"internalType": "bytes",
|
||||
"name": "signature",
|
||||
"type": "bytes"
|
||||
},
|
||||
{
|
||||
"internalType": "uint256",
|
||||
"name": "flagsAndAmount",
|
||||
"type": "uint256"
|
||||
}
|
||||
],
|
||||
"name": "fillOrderRFQ",
|
||||
"outputs": [],
|
||||
"stateMutability": "nonpayable",
|
||||
"type": "function"
|
||||
}
|
||||
];
|
||||
78
src/Unit/Pmm/index.ts
Normal file
78
src/Unit/Pmm/index.ts
Normal file
@@ -0,0 +1,78 @@
|
||||
import type Unit from '../index';
|
||||
import { z } from 'zod';
|
||||
import {pmmOrderSchema} from "./schemas/order";
|
||||
import {simpleFetch} from "simple-typed-fetch";
|
||||
import {ethers, Wallet} from "ethers";
|
||||
import {BigNumber} from "bignumber.js";
|
||||
import { ERC20__factory } from '@orionprotocol/contracts/lib/ethers-v6/index.js';
|
||||
import {orionRFQContractABI} from "./abi/OrionRFQ";
|
||||
|
||||
export default class Pmm {
|
||||
private readonly unit: Unit;
|
||||
private readonly provider: ethers.Provider;
|
||||
private contractAddress: string;
|
||||
|
||||
constructor(unit: Unit) {
|
||||
this.unit = unit;
|
||||
this.provider = unit.provider;
|
||||
this.contractAddress = '';
|
||||
}
|
||||
|
||||
private isInitialized() : boolean {
|
||||
return this.contractAddress !== '';
|
||||
}
|
||||
|
||||
public async init() {
|
||||
if(this.isInitialized())
|
||||
return;
|
||||
const { orionPMMRouterContractAddress } = await simpleFetch(this.unit.blockchainService.getPmmInfo)();
|
||||
this.contractAddress = orionPMMRouterContractAddress;
|
||||
}
|
||||
|
||||
public async setAllowance(token: string, amount: string, signer: Wallet) {
|
||||
await this.init();
|
||||
|
||||
const bnTargetAmount = new BigNumber(amount);
|
||||
const walletAddress = await signer.getAddress();
|
||||
|
||||
const tokenContract = ERC20__factory
|
||||
.connect(token, this.unit.provider);
|
||||
|
||||
const unsignedApproveTx = await tokenContract
|
||||
.approve.populateTransaction(
|
||||
this.contractAddress,
|
||||
bnTargetAmount.toString()
|
||||
);
|
||||
const nonce = await this.provider.getTransactionCount(walletAddress, 'pending');
|
||||
const { gasPrice, maxFeePerGas } = await this.provider.getFeeData();
|
||||
const network = await this.provider.getNetwork();
|
||||
|
||||
if (gasPrice !== null)
|
||||
unsignedApproveTx.gasPrice = gasPrice;
|
||||
|
||||
if(maxFeePerGas !== null)
|
||||
unsignedApproveTx.maxFeePerGas = maxFeePerGas;
|
||||
|
||||
unsignedApproveTx.chainId = network.chainId;
|
||||
unsignedApproveTx.nonce = nonce;
|
||||
unsignedApproveTx.from = walletAddress;
|
||||
const gasLimit = await this.provider.estimateGas(unsignedApproveTx);
|
||||
unsignedApproveTx.gasLimit = gasLimit;
|
||||
|
||||
const signedTx = await signer.signTransaction(unsignedApproveTx);
|
||||
const txResponse = await this.provider.broadcastTransaction(signedTx);
|
||||
await txResponse.wait();
|
||||
}
|
||||
|
||||
public async FillRFQOrder(order : z.infer<typeof pmmOrderSchema>, signer: Wallet) {
|
||||
await this.init();
|
||||
|
||||
if(!order.success)
|
||||
throw Error("Invalid order provided");
|
||||
|
||||
const contract = new ethers.Contract(this.contractAddress, orionRFQContractABI, signer);
|
||||
|
||||
// @ts-ignore
|
||||
return contract.fillOrderRFQ(order.quotation, order.signature, BigInt(0));
|
||||
}
|
||||
}
|
||||
18
src/Unit/Pmm/schemas/order.ts
Normal file
18
src/Unit/Pmm/schemas/order.ts
Normal file
@@ -0,0 +1,18 @@
|
||||
import {z} from "zod";
|
||||
|
||||
export const pmmOrderQuotationSchema = z.object({
|
||||
info: z.string().default(''),
|
||||
makerAsset: z.string().default(''),
|
||||
takerAsset: z.string().default(''),
|
||||
maker: z.string().default(''),
|
||||
allowedSender: z.string().default(''),
|
||||
makingAmount: z.string().default(''),
|
||||
takingAmount: z.string().default(''),
|
||||
});
|
||||
|
||||
export const pmmOrderSchema = z.object({
|
||||
quotation: pmmOrderQuotationSchema.default({}),
|
||||
signature: z.string().default(''),
|
||||
success: z.boolean().default(false),
|
||||
error: z.string().default(''),
|
||||
});
|
||||
@@ -11,6 +11,7 @@ import Exchange from './Exchange/index.js';
|
||||
import { chains, envs } from '../config';
|
||||
import type { networkCodes } from '../constants/index.js';
|
||||
import { IndexerService } from '../services/Indexer';
|
||||
import Pmm from "./Pmm";
|
||||
|
||||
type KnownConfig = {
|
||||
env: KnownEnv
|
||||
@@ -30,6 +31,8 @@ export default class Unit {
|
||||
|
||||
public readonly aggregator: Aggregator;
|
||||
|
||||
public readonly pmm: Pmm;
|
||||
|
||||
public readonly priceFeed: PriceFeed;
|
||||
|
||||
public readonly exchange: Exchange;
|
||||
@@ -122,5 +125,6 @@ export default class Unit {
|
||||
this.config.basicAuth
|
||||
);
|
||||
this.exchange = new Exchange(this);
|
||||
this.pmm = new Pmm(this);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user