Skip to content
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

Send excess coins back to owner #205

Merged
merged 2 commits into from
Jan 3, 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
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { stringToBytes, Args } from '@massalabs/as-types';
import { sha256, Storage } from '@massalabs/massa-as-sdk';
import { sha256, Storage, balance } from '@massalabs/massa-as-sdk';
import { FileDelete } from '../../../contracts/serializable/FileDelete';
import { deleteFiles, purge } from '../../../contracts/deweb-interface';
import {
Expand Down Expand Up @@ -81,3 +81,8 @@ export function _assertHasNoChunk(locationHash: StaticArray<u8>): void {
);
assert(chunkKeys.length === 0, 'Chunks should not be stored');
}

export function _assertHasNoCoins(): void {
assert(balance() === 0, 'Balance should be zero');
}

Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
_assertPurged,
_deleteFiles,
_purge,
_assertHasNoCoins,
} from './helpers/delete-file';
import { Metadata } from '../../contracts/serializable/Metadata';

Expand Down Expand Up @@ -52,6 +53,7 @@ describe('Upload files', () => {

_assertFilesAreNotPresent([file1Path, file2Path, file3Path]);
_assertFilesArePresent([file4Path]);
_assertHasNoCoins();
});

throws('if try to remove a non-existing file', () => {
Expand All @@ -70,6 +72,7 @@ describe('Upload files', () => {

_assertFilesAreNotPresent([file1Path, file2Path, file3Path]);
_assertFilesArePresent([file4Path]);
_assertHasNoCoins();
});

test('Test purge', () => {
Expand All @@ -91,5 +94,6 @@ describe('Upload files', () => {
_purge();

_assertPurged();
_assertHasNoCoins();
});
});
27 changes: 27 additions & 0 deletions smart-contract/assembly/contracts/deweb-interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,9 @@ export function filesInit(_binaryArgs: StaticArray<u8>): void {
for (let i = 0; i < files.length; i++) {
_fileInit(files[i].location, files[i].totalChunk, files[i].metadata);
}

// Send the freed coins back to the caller
transferCoins(Context.caller(), balance());
}

/* -------------------------------------------------------------------------- */
Expand All @@ -116,6 +119,9 @@ export function uploadFileChunks(_binaryArgs: StaticArray<u8>): void {
for (let i = 0; i < chunks.length; i++) {
_setFileChunk(chunks[i].location, chunks[i].index, chunks[i].data);
}

// Send the freed coins back to the caller
transferCoins(Context.caller(), balance());
}

export function removeFileChunkRange(_binaryArgs: StaticArray<u8>): void {
Expand All @@ -129,6 +135,9 @@ export function removeFileChunkRange(_binaryArgs: StaticArray<u8>): void {
const end = args.next<u32>().expect('Invalid end');

_removeChunksRange(hashLocation, start, end);

// Send the freed coins back to the caller
transferCoins(Context.caller(), balance());
}

/**
Expand All @@ -150,6 +159,9 @@ export function deleteFiles(_binaryArgs: StaticArray<u8>): void {

_deleteFile(files[i].hashLocation);
}

// Send the freed coins back to the caller
transferCoins(Context.caller(), balance());
}

/* -------------------------------------------------------------------------- */
Expand All @@ -176,6 +188,9 @@ export function setMetadataGlobal(_binaryArgs: StaticArray<u8>): void {
stringToBytes(metadata[i].value),
);
}

// Send the freed coins back to the caller
transferCoins(Context.caller(), balance());
}

/**
Expand All @@ -193,6 +208,9 @@ export function removeMetadataGlobal(_binaryArgs: StaticArray<u8>): void {
for (let i = 0; i < key.length; i++) {
_removeGlobalMetadata(stringToBytes(key[i]));
}

// Send the freed coins back to the caller
transferCoins(Context.caller(), balance());
}

/* -------------------------------------------------------------------------- */
Expand Down Expand Up @@ -222,6 +240,9 @@ export function setMetadataFile(_binaryArgs: StaticArray<u8>): void {
hashLocation,
);
}

// Send the freed coins back to the caller
transferCoins(Context.caller(), balance());
}

/**
Expand All @@ -243,6 +264,9 @@ export function removeMetadataFile(_binaryArgs: StaticArray<u8>): void {
for (let i = 0; i < metadata.length; i++) {
_removeFileMetadata(stringToBytes(metadata[i]), hashLocation);
}

// Send the freed coins back to the caller
transferCoins(Context.caller(), balance());
}

/* -------------------------------------------------------------------------- */
Expand Down Expand Up @@ -282,6 +306,9 @@ export function receiveCoins(): void {
export function upgradeSC(args: StaticArray<u8>): void {
_onlyOwner();
setBytecode(args);

// Send the freed coins back to the caller
transferCoins(Context.caller(), balance());
}


Expand Down
Loading