order placement works, including automatic vault creation
This commit is contained in:
114
src/blockchain/orderlib.js
Normal file
114
src/blockchain/orderlib.js
Normal file
@@ -0,0 +1,114 @@
|
||||
|
||||
import {uint32max, uint64max} from "@/misc.js";
|
||||
import {ethers} from "ethers";
|
||||
|
||||
export const NO_CHAIN = uint64max;
|
||||
export const NO_OCO = uint64max;
|
||||
|
||||
// struct SwapOrder {
|
||||
// address tokenIn;
|
||||
// address tokenOut;
|
||||
// Route route;
|
||||
// uint256 amount;
|
||||
// bool amountIsInput;
|
||||
// bool outputDirectlyToOwner;
|
||||
// uint64 chainOrder; // use NO_CHAIN for no chaining. chainOrder index must be < than this order's index for safety (written first) and chainOrder state must be Template
|
||||
// Tranche[] tranches;
|
||||
// }
|
||||
// struct Route {
|
||||
// Exchange exchange;
|
||||
// uint24 fee;
|
||||
// }
|
||||
export function newOrder(tokenIn, tokenOut, exchange, fee, amount, amountIsInput, tranches,
|
||||
outputToOwner=false, chainOrder=NO_CHAIN) {
|
||||
if(!tranches)
|
||||
tranches = [newTranche(1,[])] // todo this is just a swap: issue warning?
|
||||
return [
|
||||
tokenIn, tokenOut, [exchange,fee], amount, amountIsInput, outputToOwner, chainOrder, tranches
|
||||
]
|
||||
}
|
||||
|
||||
// struct Tranche {
|
||||
// uint64 fraction; // 18-decimal fraction of the order amount which is available to this tranche. must be <= 1
|
||||
// Constraint[] constraints;
|
||||
// }
|
||||
export function newTranche(amountRatio, constraints) {
|
||||
return [
|
||||
BigInt(Math.ceil(amountRatio * 10**18)), // we use ceil to make sure the sum of tranche fractions doesn't round below 1
|
||||
constraints
|
||||
]
|
||||
}
|
||||
|
||||
// enum Exchange {
|
||||
// UniswapV2,
|
||||
// UniswapV3
|
||||
// }
|
||||
export const Exchange = {
|
||||
UniswapV2: 0,
|
||||
UniswapV3: 1,
|
||||
}
|
||||
|
||||
// enum ConstraintMode {
|
||||
// Time, // 0
|
||||
// Limit, // 1
|
||||
// Trailing, // 2
|
||||
// Barrier, // 3
|
||||
// Line // 4
|
||||
// }
|
||||
export const ConstraintMode = {
|
||||
Time: 0,
|
||||
Limit: 1,
|
||||
Trailing: 2,
|
||||
Barrier: 3,
|
||||
Line: 4,
|
||||
}
|
||||
|
||||
// struct Constraint {
|
||||
// ConstraintMode mode;
|
||||
// bytes constraint; // abi-encoded constraint struct
|
||||
// }
|
||||
|
||||
function encodeConstraint( constraintMode, types, values ) {
|
||||
return [constraintMode, ethers.AbiCoder.defaultAbiCoder().encode(types,values)]
|
||||
}
|
||||
|
||||
|
||||
export const TimeMode = {
|
||||
Timestamp:0,
|
||||
SinceOrderStart:1,
|
||||
}
|
||||
|
||||
export const DISTANT_PAST = 0
|
||||
export const DISTANT_FUTURE = uint32max
|
||||
|
||||
// struct Time {
|
||||
// TimeMode mode;
|
||||
// uint32 time;
|
||||
// }
|
||||
// struct TimeConstraint {
|
||||
// Time earliest;
|
||||
// Time latest;
|
||||
// }
|
||||
export function newTimeConstraint(startMode, start, endMode, end) {
|
||||
// absolute time
|
||||
return encodeConstraint(
|
||||
ConstraintMode.Time,
|
||||
['uint8', 'uint32', 'uint8', 'uint32'],
|
||||
[startMode, start, endMode, end]
|
||||
)
|
||||
}
|
||||
|
||||
// struct PriceConstraint {
|
||||
// bool isAbove;
|
||||
// bool isRatio;
|
||||
// uint160 valueSqrtX96;
|
||||
// }
|
||||
|
||||
// struct LineConstraint {
|
||||
// bool isAbove;
|
||||
// bool isRatio;
|
||||
// uint32 time;
|
||||
// uint160 valueSqrtX96;
|
||||
// int160 slopeSqrtX96; // price change per second
|
||||
// }
|
||||
|
||||
Reference in New Issue
Block a user