Skip to content

Commit

Permalink
recipient index
Browse files Browse the repository at this point in the history
  • Loading branch information
Thykof committed Jul 12, 2024
1 parent e375c21 commit f977fc9
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 1 deletion.
12 changes: 12 additions & 0 deletions smart-contract/assembly/contracts/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
scheduleAllSendFT,
pushSchedule,
readSchedulesBySpender,
readSchedulesByRecipient,
removeSchedule,
idCounterKey,
readSchedule,
Expand Down Expand Up @@ -78,6 +79,17 @@ export function getSchedulesBySpender(
return readSchedulesBySpender(spender);
}

export function getScheduleByRecipient(
binaryArgs: StaticArray<u8>,
): StaticArray<u8> {
const args = new Args(binaryArgs);
const recipient = args
.nextString()
.expect('Recipient address is missing or invalid');

return readSchedulesByRecipient(recipient);
}

export function getSchedule(binaryArgs: StaticArray<u8>): StaticArray<u8> {
const args = new Args(binaryArgs);
const spender = args
Expand Down
31 changes: 30 additions & 1 deletion smart-contract/assembly/internal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ export function scheduleAllSendFT(schedule: Schedule): void {

// Storage

const recipientsPrefix = 'RECIPIENT';
const schedulesPrefix = 'SCHEDULE';

export const idCounterKey = stringToBytes('C');
Expand All @@ -90,6 +91,14 @@ function incrementIdCounter(): u64 {
return inc;
}

export function getRecipientPrefix(recipient: string): StaticArray<u8> {
return stringToBytes(recipientsPrefix + recipient);
}

export function getRecipientKey(recipient: string, id: u64): StaticArray<u8> {
return getRecipientPrefix(recipient).concat(u64ToBytes(id));
}

function getSchedulePrefix(spender: string): StaticArray<u8> {
return stringToBytes(schedulesPrefix + spender);
}
Expand All @@ -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 {
Expand All @@ -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<Schedule>()
.unwrap();
Storage.del(key);

const recipientKey = getRecipientKey(schedule.recipient, id);
Storage.del(recipientKey);
}

export function readSchedulesBySpender(spender: string): StaticArray<u8> {
Expand All @@ -128,6 +144,19 @@ export function readSchedulesBySpender(spender: string): StaticArray<u8> {
return new Args().addSerializableObjectArray(schedules).serialize();
}

export function readSchedulesByRecipient(recipient: string): StaticArray<u8> {
const keyPrefix = getRecipientPrefix(recipient);
const keys = Storage.getKeys(keyPrefix);
const schedules = new Array<Schedule>(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<Schedule>().unwrap();
}

return new Args().addSerializableObjectArray(schedules).serialize();
}

export function readSchedule(spender: string, id: u64): StaticArray<u8> {
const key = getScheduleKey(spender, id);
return Storage.get(key);
Expand Down

0 comments on commit f977fc9

Please sign in to comment.