// --- Off-Chain Payment Flow ---
async function offChainPaymentFlow(
licenseeSdk: CulturaSDK,
parentOwnerSdk: CulturaSDK,
sponsorSdk: CulturaSDK,
sponsorAddress: `0x${string}`,
licenseeAddress: `0x${string}`,
parentOwnerAddress: `0x${string}`
) {
try {
// Step 1: Register off-chain royalty period (delegated)
console.log("Registering off-chain royalty period...");
const offchainRoyaltyDue = parseEther("5");
const startDate = BigInt(Math.floor(Date.now() / 1000));
const endDate = startDate + BigInt(365 * 24 * 60 * 60);
const offchainPeriodIndex =
await licenseeSdk.royalty.getRoyaltyInfoCount(licensedAssetId);
const registrationSignature =
await licenseeSdk.royalty.signForDelegatedRegisterRoyaltyDue(
licensedAssetId,
offchainRoyaltyDue,
startDate,
endDate,
offchainPeriodIndex
);
await sponsorSdk.royalty.delegatedRegisterRoyaltyDue(
licensedAssetId,
offchainRoyaltyDue,
startDate,
endDate,
offchainPeriodIndex,
registrationSignature
);
console.log("Off-chain royalty period registered.");
// Step 2: Report off-chain payment (delegated)
console.log("Reporting off-chain payment...");
const paymentAmount = parseEther("1");
const paymentMetadata = JSON.stringify({
paymentMethod: "Bank Transfer",
referenceNumber: "BT-2023-12345",
paymentDate: new Date().toISOString(),
notes: "Payment for Q3 licensing",
});
const reportSignature =
await licenseeSdk.royalty.signForDelegatedOffchainReportRoyalty(
licensedAssetId,
offchainPeriodIndex,
paymentAmount
);
await sponsorSdk.royalty.delegatedReportOffChainPayment(
licensedAssetId,
offchainPeriodIndex,
paymentAmount,
paymentMetadata,
reportSignature
);
console.log("Off-chain payment reported.");
// Step 3: Parent acceptance (sponsored fees)
console.log("Parent accepting off-chain payment...");
// Get parent's allocated amount
const parentAcceptanceInfo =
await licenseeSdk.royalty.getParentAcceptanceInfo(
licensedAssetId,
offchainPeriodIndex,
parent1RbaAddress
);
const allocatedAmount = parentAcceptanceInfo.allocatedAmount;
if (allocatedAmount > 0n) {
// Parent owner signs acceptance
const acceptSignature =
await parentOwnerSdk.royalty.signForAcceptOffChainPaymentByParent(
parent1RbaAddress,
allocatedAmount,
offchainPeriodIndex,
sponsorAddress
);
// Sponsor executes acceptance (paying fees)
await sponsorSdk.royalty.acceptOffChainPaymentByParent(
licensedAssetId,
offchainPeriodIndex,
parent1RbaAddress,
acceptSignature,
parentOwnerAddress,
sponsorAddress
);
console.log("Off-chain payment accepted by parent.");
}
// Step 4: Check acceptance status
const allParentInfo = await licenseeSdk.royalty.getAllParentAcceptanceInfo(
licensedAssetId,
offchainPeriodIndex,
assetContractAddress
);
console.log("Parent acceptance statuses:");
for (const parentInfo of allParentInfo) {
console.log(`- Parent RBA: ${parentInfo.parentBoundAccount}`);
console.log(` Status: ${parentInfo.acceptanceInfo.status}`);
console.log(` Amount: ${parentInfo.acceptanceInfo.allocatedAmount}`);
}
} catch (e) {
console.error("Error in off-chain payment flow:", e);
throw e;
}
}
// --- Alternative: Parent Denial Flow ---
async function denyOffChainPayment(
parentOwnerSdk: CulturaSDK,
sponsorSdk: CulturaSDK,
parentOwnerAddress: `0x${string}`,
parentRbaAddress: `0x${string}`,
periodIndex: bigint
) {
try {
console.log("Parent denying off-chain payment...");
const denySignature =
await parentOwnerSdk.royalty.signForDenyOffChainPaymentByParent(
licensedAssetId,
periodIndex,
parentRbaAddress
);
await sponsorSdk.royalty.denyOffChainPaymentByParent(
licensedAssetId,
periodIndex,
parentRbaAddress,
denySignature,
parentOwnerAddress
);
console.log("Off-chain payment denied by parent.");
} catch (e) {
console.error("Error denying off-chain payment:", e);
throw e;
}
}