Skip to content

Instantly share code, notes, and snippets.

@rouzwelt
Last active July 13, 2024 02:21
Show Gist options
  • Save rouzwelt/63fd26498b21b7b002f981f81a387715 to your computer and use it in GitHub Desktop.
Save rouzwelt/63fd26498b21b7b002f981f81a387715 to your computer and use it in GitHub Desktop.
Parse and Deploy Rain Order
const { ethers } = require("ethers");
const encodeMeta = (data) => {
return (
"0x" +
BigInt(0xff0a89c674ee7874n).toString(16).toLowerCase() +
ethers.utils.hexlify(ethers.utils.toUtf8Bytes(data)).split("x")[1]
);
};
const allAbis = [
// used for getting parser address if not already known
"function iParser() external view returns (address)",
// used for parsing rainlang for obv3
"function parse(bytes calldata data) external view returns (bytes calldata bytecode, uint256[] calldata constants)",
// orderbook v3 abis for adding order
"function addOrder(((address token, uint8 decimals, uint256 vaultId)[] validInputs, (address token, uint8 decimals, uint256 vaultId)[] validOutputs, (address deployer, bytes bytecode, uint256[] constants) evaluableConfig, bytes meta) config) returns (bool stateChanged)",
];
const deployerContractAddress = "";
const orderbokContractAddress = "";
const provider = new ethers.providers.JsonRpcProvider("chain's rpc url");
const signer = new ethers.Wallet(walletPrivateKey, provider);
const deployerContract = new ethers.Contract(deployerContractAddress, allAbis, provider);
const orderbookContract = new ethers.Contract(orderbokContractAddress, allAbis, provider);
const rainlang = "using-words-from 0x31A76D8644612e0ABD1aF0D42909Ed57F16F608D 0xCE6ad0ba209e7D3B59Ddb8a63595193C11C3B0aB start-time: 1720361228,budget-per-day: 10,budget-per-second: div(budget-per-day 86400),time-elapsed: sub(now() start-time),budget-to-date: mul(time-elapsed budget-per-second),spent-so-far: get(order-hash()),spend-this-time: sub(budget-to-date spent-so-far),flr-usd: ftso-current-price-usd(\"FLR\" 3600),usd-flr: inv(flr-usd),max-output: spend-this-time,io-ratio: mul(0.9 usd-flr),:set(order-hash() add(spent-so-far spend-this-time)); :;";
const rainlangAsBytes = ethers.utils.toUtf8Bytes(rainlang);
// if not known, you can use the iParser() call to get them from deployerContract
// // example: parserContractAddress = await deployerContract.iParser();
const parserContractAddress = "";
const parserContract = new ethers.Contract(parserContractAddress, allAbis, provider);
const { constants, bytecode } = await parserContract.parse(rainlangAsBytes);
const addOrderArgs = {
validInputs: [{
token: "input token address",
decimals: "input token decimals",
vaultId: "input vault id",
}],
validOutputs: [{
token: "output token address",
decimals: "output token decimals",
vaultId: "output vault id",
}],
evaluableConfig: {
deployer: deployerContractAddress,
constants,
bytecode
},
meta: encodeMeta("some meta msg or something or the rainlang original string"),
};
// deploy the order
const tx = await orderbookContract.connect(signer).addOrder(addOrderArgs);
const { ethers } = require("ethers");
const encodeMeta = (data) => {
return (
"0x" +
BigInt(0xff0a89c674ee7874n).toString(16).toLowerCase() +
ethers.utils.hexlify(ethers.utils.toUtf8Bytes(data)).split("x")[1]
);
};
const allAbis = [
// used for getting interpreter and store addresses if not already known
"function iStore() external view returns (address)",
"function iInterpreter() external view returns (address)",
// used for parsing rainlang for obv4
"function parse2(bytes memory data) external view returns (bytes memory bytecode)", // for obv4, eval3
// orderbook v4 abis for adding order
"function addOrder2(((address interpreter, address store, bytes bytecode) evaluable, (address token, uint8 decimals, uint256 vaultId)[] validInputs, (address token, uint8 decimals, uint256 vaultId)[] validOutputs, bytes32 nonce, bytes32 secret, bytes meta) calldata config, ((address interpreter, address store, bytes bytecode) evaluable, (address signer, uint256[] context, bytes signature)[] signedContext)[] calldata post) external returns (bool stateChanged)"
];
const deployerContractAddress = "";
const orderbokContractAddress = "";
const provider = new ethers.providers.JsonRpcProvider("chain's rpc url");
const signer = new ethers.Wallet(walletPrivateKey, provider);
const deployerContract = new ethers.Contract(deployerContractAddress, allAbis, provider);
const orderbookContract = new ethers.Contract(orderbokContractAddress, allAbis, provider);
const rainlang = "using-words-from 0x31A76D8644612e0ABD1aF0D42909Ed57F16F608D 0xCE6ad0ba209e7D3B59Ddb8a63595193C11C3B0aB start-time: 1720361228,budget-per-day: 10,budget-per-second: div(budget-per-day 86400),time-elapsed: sub(now() start-time),budget-to-date: mul(time-elapsed budget-per-second),spent-so-far: get(order-hash()),spend-this-time: sub(budget-to-date spent-so-far),flr-usd: ftso-current-price-usd(\"FLR\" 3600),usd-flr: inv(flr-usd),max-output: spend-this-time,io-ratio: mul(0.9 usd-flr),:set(order-hash() add(spent-so-far spend-this-time)); :;";
const rainlangAsBytes = ethers.utils.toUtf8Bytes(rainlang);
// if not known, you can use the iStore() and iInterpreter() calls to get them from deployerContract
// example: storeContractAddress = await deployerContract.iStore();
const interpreterContractAddress = "";
const storeContractAddress = "";
const bytecode = await deployerContract.parse2(rainlangAsBytes);
const addOrder2Args = {
evaluable: {
interpreter: interpreterContractAddress,
store: storeContractAddress,
bytecode,
},
nonce: "some nonce, should be bytes32",
secret: "some secrete, doesn't matter for non privacy chains, should be bytes32",
validInputs: [{
token: "input token address",
decimals: "input token decimals",
vaultId: "input vault id",
}],
validOutputs: [{
token: "output token address",
decimals: "output token decimals",
vaultId: "output vault id",
}],
meta: encodeMeta("some meta msg or info or the rainlang original string"),
};
// deploy the order
const tx = await orderbookContract.connect(signer).addOrder2(addOrder2Args, []);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment