Skip to content

Commit

Permalink
fix: emit loan example working
Browse files Browse the repository at this point in the history
  • Loading branch information
mishuagopian committed Feb 23, 2024
1 parent 9db5e89 commit 6a728aa
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 29 deletions.
33 changes: 16 additions & 17 deletions examples/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,13 @@ import { privateKeyToAccount } from 'viem/accounts';
dotenv.config();

const RPC = process.env.RPC_URL;
const MULTI_SOURCE_LOAN_CONTRACT_V5 = process.env.MULTI_SOURCE_LOAN_CONTRACT_V5 ?? '';
const SEAPORT_CONTRACT_ADDRESS = '0x00000000000000ADc04C56Bf30aC9d3c0aAF14dC';
const LEVERAGE_CONTRACT = process.env.LEVERAGE_ADDRESS ?? '';
const MULTI_SOURCE_LOAN_CONTRACT_V6 = process.env.MULTI_SOURCE_LOAN_CONTRACT_V6 ?? '';

export const MAX_NUMBER =
115792089237316195423570985008687907853269984665640564039457584007913129639935n;

if (!isAddress(MULTI_SOURCE_LOAN_CONTRACT_V5)) {
throw new Error('invalid MULTI_SOURCE_LOAN_CONTRACT_V5 address');
if (!isAddress(MULTI_SOURCE_LOAN_CONTRACT_V6)) {
throw new Error('invalid MULTI_SOURCE_LOAN_CONTRACT_V6 address');
}

if (!RPC) throw new Error('RPC_URL is not set');
Expand Down Expand Up @@ -92,6 +90,7 @@ const offerInput = {
expirationTime: BigInt(expirationTimeSeconds),
duration: 1294967295n,
borrowerAddress: zeroAddress,
maxTrancheFloor: 0n,
};

export const testCollectionOfferInput = {
Expand Down Expand Up @@ -142,21 +141,21 @@ const approveForUser = async (user: Gondi, to: Address) => {
await approveNFT(user, to);
};

const MULTI_SOURCE_LOAN_CONTRACT_V4 = process.env.MULTI_SOURCE_LOAN_CONTRACT_V4 ?? '';

const SEAPORT_CONTRACT_ADDRESS = '0x00000000000000ADc04C56Bf30aC9d3c0aAF14dC';
const CONTRACTS = [
MULTI_SOURCE_LOAN_CONTRACT_V6,
process.env.MULTI_SOURCE_LOAN_CONTRACT_V5 ?? '',
process.env.MULTI_SOURCE_LOAN_CONTRACT_V4 ?? '',
process.env.LEVERAGE_ADDRESS ?? '',
SEAPORT_CONTRACT_ADDRESS,
];
for (const [i, user] of users.entries()) {
console.log(`approving tokens for user ${i}`);
await approveForUser(user, MULTI_SOURCE_LOAN_CONTRACT_V5);

if (isAddress(MULTI_SOURCE_LOAN_CONTRACT_V4)) {
await approveForUser(user, MULTI_SOURCE_LOAN_CONTRACT_V4);
for (const contract in CONTRACTS) {
if (isAddress(contract)) {
await approveForUser(user, contract);
}
}

if (isAddress(LEVERAGE_CONTRACT)) {
await approveForUser(user, LEVERAGE_CONTRACT);
}

await approveForUser(user, SEAPORT_CONTRACT_ADDRESS);
}

// Assuming MSL contract default: 3 days (seconds)
Expand Down
30 changes: 20 additions & 10 deletions examples/emitSingleNFTOfferLoan.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,24 @@ const emitAndRepayLoan = async (contract?: Address) => {
console.log(`offer placed successfully: ${contractVersionString}`);

const emitLoan = await users[1].emitLoan({
offer: signedOffer,
offerExecution: [
{
offer: {
...signedOffer,
maxTrancheFloor: signedOffer.maxTrancheFloor ?? 0n,
},
lenderOfferSignature: signedOffer.signature,
},
],
duration: signedOffer.duration,
tokenId: testTokenId,
});
const { loan } = await emitLoan.waitTxInBlock();
const { loan, loanId } = await emitLoan.waitTxInBlock();
console.log(`loan emitted: ${contractVersionString}`);

await sleep(3000);

const repayLoan = await users[1].repayLoan({
loan,
loanId: loan.source[0].loanId,
});
const repayLoan = await users[1].repayLoan({ loan, loanId });
await repayLoan.waitTxInBlock();
console.log(`loan repaid: ${contractVersionString}`);
};
Expand All @@ -28,10 +34,14 @@ async function main() {
try {
await emitAndRepayLoan();

const MULTI_SOURCE_LOAN_CONTRACT_V4 = process.env.MULTI_SOURCE_LOAN_CONTRACT_V4 ?? '';

if (isAddress(MULTI_SOURCE_LOAN_CONTRACT_V4)) {
await emitAndRepayLoan(MULTI_SOURCE_LOAN_CONTRACT_V4);
const oldContracts = [
process.env.MULTI_SOURCE_LOAN_CONTRACT_V5 ?? '',
process.env.MULTI_SOURCE_LOAN_CONTRACT_V4 ?? '',
];
for (const contract of oldContracts) {
if (isAddress(contract)) {
await emitAndRepayLoan(contract);
}
}
} catch (e) {
console.log('Error:');
Expand Down
5 changes: 3 additions & 2 deletions src/contracts/MslV6.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ export class MslV6 extends BaseContract<typeof multiSourceLoanAbiV6> {
private getDomain() {
return {
name: CONTRACT_DOMAIN_NAME,
version: '2',
version: '3',
chainId: this.wallet.chain.id,
verifyingContract: this.address,
};
Expand Down Expand Up @@ -158,6 +158,7 @@ export class MslV6 extends BaseContract<typeof multiSourceLoanAbiV6> {
offerExecution,
tokenId,
duration,
principalReceiver,
expirationTime,
}: EmitLoanArgs) {
const executionData = {
Expand All @@ -173,7 +174,7 @@ export class MslV6 extends BaseContract<typeof multiSourceLoanAbiV6> {
tokenId,
duration,
expirationTime: expirationTime ?? BigInt(millisToSeconds(Date.now()) + SECONDS_IN_DAY),
principalReceiver: this.wallet.account.address,
principalReceiver: principalReceiver ?? this.wallet.account.address,
callbackData: '0x' as Hash, // No callback data is expected here, only for BNPL [Levearage call]
};

Expand Down
1 change: 1 addition & 0 deletions src/gondi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -828,5 +828,6 @@ export interface EmitLoanArgs {
}[];
tokenId: bigint;
duration: bigint;
principalReceiver?: Address;
expirationTime?: bigint;
}

0 comments on commit 6a728aa

Please sign in to comment.