Skip to content

Fix single proposal create #35

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Apr 17, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions apps/example-next/next.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ const nextConfig = {
webpack: (config) => {
config.externals.push("pino-pretty", "lokijs", "encoding");
return config;
},
},

// backend expects trailing slashes
trailingSlash: true,
Expand All @@ -42,7 +42,7 @@ const nextConfig = {
// Handle URLs without trailing slash
{
source: "/api/v1/:path*",
destination: "http://api-staging.pwn.xyz/api/v1/:path*",
destination: "https://api-staging.pwn.xyz/api/v1/:path*",
},
// Handle URLs that already have a trailing slash
{
Expand Down
Binary file modified bun.lockb
Binary file not shown.
26 changes: 18 additions & 8 deletions packages/v1-core/src/actions/make-proposal.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import type { UserWithNonceManager } from "@pwndao/sdk-core";
import type { ProposalWithSignature } from "src/models/strategies/types.js";
import invariant from "ts-invariant";
import { createChainLinkElasticProposal } from "../factories/create-chain-link-proposal.js";
import { createElasticProposal } from "../factories/create-elastic-proposal.js";
import { ProposalType } from "../models/proposals/proposal-base.js";
import type { UserWithNonceManager } from "@pwndao/sdk-core";

const proposalTypes = {
[ProposalType.Elastic]: createElasticProposal,
Expand Down Expand Up @@ -44,10 +44,17 @@ export const makeProposal = async <T extends ProposalType>(
typeof createElasticProposal
>[0];
const elasticDeps = deps as Parameters<typeof createElasticProposal>[1];
const proposal = await createElasticProposal(elasticParams, elasticDeps, user);
proposalWithSignature = await elasticDeps.contract.createProposal(proposal, {
persistProposal: elasticDeps.api.persistProposal,
});
const proposal = await createElasticProposal(
elasticParams,
elasticDeps,
user,
);
proposalWithSignature = await elasticDeps.contract.createProposal(
proposal,
{
persistProposal: elasticDeps.api.persistProposal,
},
);
break;
}
case ProposalType.ChainLink: {
Expand All @@ -62,9 +69,12 @@ export const makeProposal = async <T extends ProposalType>(
chainLinkDeps,
user,
);
proposalWithSignature = await chainLinkDeps.contract.createProposal(proposal, {
persistProposal: chainLinkDeps.api.persistProposal,
});
proposalWithSignature = await chainLinkDeps.contract.createProposal(
proposal,
{
persistProposal: chainLinkDeps.api.persistProposal,
},
);
break;
}
case ProposalType.DutchAuction:
Expand Down
182 changes: 176 additions & 6 deletions packages/v1-core/src/actions/make-proposals.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {
type AddressString,
MultiTokenCategory,
SupportedChain,
type UserWithNonceManager,
ZERO_ADDRESS,
ZERO_FINGERPRINT,
getLoanContractAddress,
Expand All @@ -14,10 +14,12 @@ import {
} from "@pwndao/sdk-core";
import type { Config } from "@wagmi/core";
import { LBTC, PYUSD } from "../addresses.js";
import type { CreateElasticProposalParams } from "../factories/create-elastic-proposal.js";
import type { ProposalWithSignature } from "../index.js";
import { ChainLinkProposal } from "../models/proposals/chainlink-proposal.js";
import { ProposalType } from "../models/proposals/proposal-base.js";
import { convertNameIntoDenominator } from "../utils/chainlink-feeds.js";
import { makeProposal } from "./make-proposal.js";
import { makeProposals } from "./make-proposals.js";
import type {
ImplementedProposalTypes,
Expand All @@ -26,6 +28,7 @@ import type {

describe("Test make proposals", () => {
const collateralAddress = LBTC[SupportedChain.Ethereum] as AddressString;
const collateralAddress2 = PYUSD[SupportedChain.Ethereum] as AddressString;
const creditAddress = PYUSD[SupportedChain.Ethereum] as AddressString;

const user_address = generateAddress();
Expand All @@ -39,6 +42,7 @@ describe("Test make proposals", () => {

afterEach(() => {
vi.useRealTimers();
vi.resetAllMocks();
});

const durationDays = 30;
Expand Down Expand Up @@ -397,16 +401,56 @@ describe("Test make proposals", () => {
loanContract: loanContractMock,
},
},
{
type: ProposalType.ChainLink,
params: {
collateral: getMockToken(SupportedChain.Ethereum, collateralAddress2),
credit: creditToken,
creditAmount,
ltv: {
[getUniqueCreditCollateralKey(
creditToken,
getMockToken(SupportedChain.Ethereum, collateralAddress2),
)]: Number(ltv),
},
apr: {
[getUniqueCreditCollateralKey(
creditToken,
getMockToken(SupportedChain.Ethereum, collateralAddress2),
)]: apr,
},
duration: {
days: durationDays,
date: undefined,
},
expirationDays,
utilizedCreditId: generateAddress(),
minCreditAmountPercentage: 0,
minCreditAmount: specificMinCreditAmount, // Use the specific minCreditAmount
relatedStrategyId: "1",
sourceOfFunds: null,
isOffer: true,
},
deps: {
api: {
persistProposal: vi.fn().mockImplementation((p) => p),
persistProposals: vi.fn().mockImplementation((p) => p),
updateNonces: vi.fn().mockImplementation((p) => p),
},
contract: contractMock,
loanContract: loanContractMock,
},
},
];

const proposals = await makeProposals(config, proposalParams, user);

expect(proposals).toHaveLength(1);
expect(proposals).toHaveLength(2);
expect(contractMock.createMultiProposal).toHaveBeenCalled();
expect(loanContractMock.getLenderSpecHash).toHaveBeenCalledTimes(1);

// Verify that the proposal uses the specific minCreditAmount
const proposal = proposals[0];
const proposal = proposals?.[0];
expect(proposal.minCreditAmount).toBe(specificMinCreditAmount);
expect(proposal.proposerSpecHash).toBe(proposerSpecHash);
expect(proposal.collateralAddress).toBe(collateralAddress);
Expand Down Expand Up @@ -457,13 +501,17 @@ describe("Test make proposals", () => {
const proposalParams = [
{
type: "UnsupportedType" as ProposalType,
params: {},
params: {
proposalType: "UnsupportedType",
credit: getMockToken(SupportedChain.Ethereum, creditAddress),
collateral: getMockToken(SupportedChain.Ethereum, collateralAddress),
},
deps: {},
},
] as unknown as ProposalParamWithDeps<ImplementedProposalTypes>[];

await expect(makeProposals(config, proposalParams, user)).rejects.toThrow(
"Not implemented for proposal type UnsupportedType",
"Proposal type UnsupportedType not found",
);
});

Expand All @@ -480,6 +528,7 @@ describe("Test make proposals", () => {
}
return proposals;
}),
createProposal: vi.fn().mockImplementation((p) => p),
};

const loanContractMock = {
Expand Down Expand Up @@ -531,11 +580,50 @@ describe("Test make proposals", () => {
loanContract: loanContractMock,
},
},
{
type: ProposalType.ChainLink,
params: {
collateral: getMockToken(SupportedChain.Ethereum, collateralAddress2),
credit: getMockToken(SupportedChain.Ethereum, creditAddress),
creditAmount,
ltv: {
[getUniqueCreditCollateralKey(
getMockToken(SupportedChain.Ethereum, creditAddress),
getMockToken(SupportedChain.Ethereum, collateralAddress2),
)]: Number(ltv),
},
apr: {
[getUniqueCreditCollateralKey(
getMockToken(SupportedChain.Ethereum, creditAddress),
getMockToken(SupportedChain.Ethereum, collateralAddress2),
)]: apr,
},
duration: {
days: durationDays,
date: undefined,
},
expirationDays,
utilizedCreditId: generateAddress(),
minCreditAmountPercentage: 3,
relatedStrategyId: "1",
isOffer: false, // This is a request, not an offer
sourceOfFunds: null,
},
deps: {
api: {
persistProposal: vi.fn().mockImplementation((p) => p),
persistProposals: vi.fn().mockImplementation((p) => p),
updateNonces: vi.fn().mockImplementation((p) => p),
},
contract: contractMock,
loanContract: loanContractMock,
},
},
];

const proposals = await makeProposals(config, proposalParams, user);

expect(proposals).toHaveLength(1);
expect(proposals).toHaveLength(2);
expect(contractMock.createMultiProposal).toHaveBeenCalled();
expect(loanContractMock.getLenderSpecHash).toHaveBeenCalledTimes(1);

Expand Down Expand Up @@ -566,4 +654,86 @@ describe("Test make proposals", () => {
expect(proposal.feedInvertFlags[1]).toBe(true);
expect(proposal.feedInvertFlags[2]).toBe(true);
});

it("should fallback to makeProposal for single proposal", async () => {
const contractMock = {
createProposal: vi.fn().mockImplementation((p) => {
return {
...p,
signature: "0x456",
};
}),
getCollateralAmount: vi.fn().mockImplementation(() => 0n),
getProposalHash: vi.fn().mockImplementation(() => "0x123"),
};

const loanContractMock = {
getLenderSpecHash: vi
.fn()
.mockImplementation(() => Promise.resolve(proposerSpecHash)),
};

const user = getMockUserWithNonceManager(user_address);
const config = {} as Config;

const elasticParams: CreateElasticProposalParams = {
collateral: getMockToken(SupportedChain.Ethereum, collateralAddress),
credit: getMockToken(SupportedChain.Ethereum, creditAddress),
creditAmount,
ltv: {
[getUniqueCreditCollateralKey(
getMockToken(SupportedChain.Ethereum, creditAddress),
getMockToken(SupportedChain.Ethereum, collateralAddress),
)]: Number(ltv),
},
apr: {
[getUniqueCreditCollateralKey(
getMockToken(SupportedChain.Ethereum, creditAddress),
getMockToken(SupportedChain.Ethereum, collateralAddress),
)]: apr,
},
duration: {
days: durationDays,
date: undefined,
},
expirationDays,
utilizedCreditId: generateAddress(),
minCreditAmountPercentage: 3,
relatedStrategyId: "1",
isOffer: true,
sourceOfFunds: null,
};

const proposalParams: ProposalParamWithDeps<ProposalType.Elastic>[] = [
{
type: ProposalType.Elastic,
params: elasticParams,
deps: {
api: {
getAssetUsdUnitPrice: async () => 1000000000000000000n,
persistProposal: vi.fn().mockImplementation((p) => p),
persistProposals: vi.fn().mockImplementation((p) => p),
updateNonces: vi.fn().mockImplementation((p) => p),
},
contract: contractMock,
loanContract: loanContractMock,
},
},
];

const result = await makeProposals(config, proposalParams, user);

const proposal = result[0] as ProposalWithSignature;


expect(proposal).toBeDefined()
expect(proposal.proposerSpecHash).toBe(proposerSpecHash)
expect(proposal.collateralAddress).toBe(collateralAddress)
expect(proposal.creditAddress).toBe(creditAddress)
expect(proposal.availableCreditLimit).toBe(creditAmount)
expect(proposal.minCreditAmount).toBe(3n * 10n ** (18n - 2n))
expect(proposal.accruingInterestAPR).toBe(apr)
expect(proposal.signature).toBe("0x456")

});
});
Loading
Loading