diff --git a/smart-contract/assembly/contracts/main.ts b/smart-contract/assembly/contracts/main.ts index c468986..e8dd14e 100644 --- a/smart-contract/assembly/contracts/main.ts +++ b/smart-contract/assembly/contracts/main.ts @@ -8,6 +8,7 @@ import { scheduleAllSendFT, pushSchedule, readSchedulesBySpender, + readSchedulesByRecipient, removeSchedule, idCounterKey, readSchedule, @@ -78,6 +79,17 @@ export function getSchedulesBySpender( return readSchedulesBySpender(spender); } +export function getScheduleByRecipient( + binaryArgs: StaticArray, +): StaticArray { + const args = new Args(binaryArgs); + const recipient = args + .nextString() + .expect('Recipient address is missing or invalid'); + + return readSchedulesByRecipient(recipient); +} + export function getSchedule(binaryArgs: StaticArray): StaticArray { const args = new Args(binaryArgs); const spender = args diff --git a/smart-contract/assembly/internal.ts b/smart-contract/assembly/internal.ts index 26533c9..c87069f 100644 --- a/smart-contract/assembly/internal.ts +++ b/smart-contract/assembly/internal.ts @@ -72,6 +72,7 @@ export function scheduleAllSendFT(schedule: Schedule): void { // Storage +const recipientsPrefix = 'RECIPIENT'; const schedulesPrefix = 'SCHEDULE'; export const idCounterKey = stringToBytes('C'); @@ -90,6 +91,14 @@ function incrementIdCounter(): u64 { return inc; } +export function getRecipientPrefix(recipient: string): StaticArray { + return stringToBytes(recipientsPrefix + recipient); +} + +export function getRecipientKey(recipient: string, id: u64): StaticArray { + return getRecipientPrefix(recipient).concat(u64ToBytes(id)); +} + function getSchedulePrefix(spender: string): StaticArray { return stringToBytes(schedulesPrefix + spender); } @@ -102,8 +111,9 @@ export function pushSchedule(schedule: Schedule): void { const id = incrementIdCounter(); const key = getScheduleKey(schedule.spender, id); schedule.id = id; - Storage.set(key, new Args().add(schedule).serialize()); + + Storage.set(getRecipientKey(schedule.recipient, id), key); } export function updateSchedule(schedule: Schedule): void { @@ -113,7 +123,13 @@ export function updateSchedule(schedule: Schedule): void { export function removeSchedule(spender: string, id: u64): void { const key = getScheduleKey(spender, id); + const schedule = new Args(Storage.get(key)) + .nextSerializable() + .unwrap(); Storage.del(key); + + const recipientKey = getRecipientKey(schedule.recipient, id); + Storage.del(recipientKey); } export function readSchedulesBySpender(spender: string): StaticArray { @@ -128,6 +144,19 @@ export function readSchedulesBySpender(spender: string): StaticArray { return new Args().addSerializableObjectArray(schedules).serialize(); } +export function readSchedulesByRecipient(recipient: string): StaticArray { + const keyPrefix = getRecipientPrefix(recipient); + const keys = Storage.getKeys(keyPrefix); + const schedules = new Array(keys.length); + for (let i = 0; i < keys.length; i++) { + const scheduleKey = Storage.get(keys[i]); + const data = Storage.get(scheduleKey); + schedules[i] = new Args(data).nextSerializable().unwrap(); + } + + return new Args().addSerializableObjectArray(schedules).serialize(); +} + export function readSchedule(spender: string, id: u64): StaticArray { const key = getScheduleKey(spender, id); return Storage.get(key);