Skip to content

Commit

Permalink
fix: propose options everything base10
Browse files Browse the repository at this point in the history
  • Loading branch information
DaveVodrazka committed Nov 9, 2024
1 parent 4aa2a35 commit 1628d5b
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 13 deletions.
13 changes: 7 additions & 6 deletions src/components/AddProposal/AddProposal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import { ProposalText } from "./ProposalText";
import { proposeOptions } from "./proposeOptions";
import { useAccount, useSendTransaction } from "@starknet-react/core";
import { useOptions } from "../../hooks/useOptions";
import { stringToBigint } from "../../utils/conversions";

const strkUsdcCallPool = new Pool(STRK_ADDRESS, USDC_ADDRESS, OptionType.Call);

Expand Down Expand Up @@ -165,15 +166,15 @@ export const AddProposal = () => {
pool.quoteToken.symbol
}-${pool.typeAsText.toUpperCase()}-`;
return [
name + "LONG",
name + "SHORT",
stringToBigint(name + "LONG").toString(10),
stringToBigint(name + "SHORT").toString(10),
o.maturity.toString(10),
decimalToMath64(o.strike),
"0", // Fixed sign
pool.type,
pool.lpAddress,
pool.quoteToken.address,
pool.baseToken.address,
BigInt(pool.type).toString(10),
BigInt(pool.lpAddress).toString(10),
BigInt(pool.quoteToken.address).toString(10),
BigInt(pool.baseToken.address).toString(10),
decimalToMath64(o.volatility),
"0", // Fixed sign
];
Expand Down
15 changes: 8 additions & 7 deletions src/components/AddProposal/proposeOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,17 @@ export const proposeOptions = async (
args?: Call[]
) => Promise<RequestResult<"wallet_addInvokeTransaction">>
) => {
const calldata = [
"2", // add options custom proposal prop id
options.length + 2, // length of the payload Span<felt252>
AMM_ADDRESS,
options.length / 11, // length of the array of options (each option is 11 fields)
...options,
];
const call = {
contractAddress: GOVERNANCE_ADDRESS,
entrypoint: "submit_custom_proposal",
calldata: [
"0x2", // add options custom proposal prop id
options.length + 2, // length of the payload Span<felt252>
AMM_ADDRESS,
options.length / 11, // length of the array of options (each option is 11 fields)
...options,
],
calldata,
};

debug("Executing add options proposal:", call);
Expand Down
3 changes: 3 additions & 0 deletions src/setupTests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,6 @@
// expect(element).toHaveTextContent(/react/i)
// learn more: https://github.com/testing-library/jest-dom
import "@testing-library/jest-dom";
// src/setupTests.js
global.TextEncoder = require("util").TextEncoder;
global.TextDecoder = require("util").TextDecoder; // Optional if you need TextDecoder as well
21 changes: 21 additions & 0 deletions src/utils/conversions.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { stringToBigint } from "./conversions";

describe("convert text to felt", () => {
const cases: [string, bigint][] = [
["STRK-USDC-CALL-LONG", 1858307286577880742802534252795740158654631495n],
["STRK-USDC-CALL-SHORT", 475726665363937470157448768715709480645533061716n],
["STRK-USDC-PUT-LONG", 7259012838194846651572400367370245491347015n],
["STRK-USDC-PUT-SHORT", 1858307286577880742802534494046782875732234836n],
["ETH-USDC-CALL-LONG", 6039427387912472877549585711181846421655111n],
["ETH-USDC-CALL-SHORT", 1546093411305593056652693942062552713891107412n],
];

test("pool names", () => {
cases.forEach(([text, felt]) => {
const result = stringToBigint(text);
expect(result === felt).toBe(true);
});
});
});

export {};
11 changes: 11 additions & 0 deletions src/utils/conversions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,14 @@ export const bnToOptionSide = (n: BigNumberish): OptionSide =>

export const bnToOptionType = (n: BigNumberish): OptionType =>
BigInt(n) === 1n ? OptionType.Put : OptionType.Call;

export const stringToBigint = (txt: string): bigint => {
const bText = new TextEncoder().encode(txt);
let result = 0n;

for (let byte of bText) {
result = (result << BigInt(8)) + BigInt(byte);
}

return result;
};

0 comments on commit 1628d5b

Please sign in to comment.