From 6e9c9826477e974fb39c30802926dd33269f7d7d Mon Sep 17 00:00:00 2001 From: damip Date: Fri, 3 Jan 2025 15:00:32 +0100 Subject: [PATCH 1/2] Send excess coins back to owner --- .../assembly/contracts/deweb-interface.ts | 27 +++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/smart-contract/assembly/contracts/deweb-interface.ts b/smart-contract/assembly/contracts/deweb-interface.ts index 1403812..6ec1768 100644 --- a/smart-contract/assembly/contracts/deweb-interface.ts +++ b/smart-contract/assembly/contracts/deweb-interface.ts @@ -93,6 +93,9 @@ export function filesInit(_binaryArgs: StaticArray): 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()); } /* -------------------------------------------------------------------------- */ @@ -116,6 +119,9 @@ export function uploadFileChunks(_binaryArgs: StaticArray): 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): void { @@ -129,6 +135,9 @@ export function removeFileChunkRange(_binaryArgs: StaticArray): void { const end = args.next().expect('Invalid end'); _removeChunksRange(hashLocation, start, end); + + // Send the freed coins back to the caller + transferCoins(Context.caller(), balance()); } /** @@ -150,6 +159,9 @@ export function deleteFiles(_binaryArgs: StaticArray): void { _deleteFile(files[i].hashLocation); } + + // Send the freed coins back to the caller + transferCoins(Context.caller(), balance()); } /* -------------------------------------------------------------------------- */ @@ -176,6 +188,9 @@ export function setMetadataGlobal(_binaryArgs: StaticArray): void { stringToBytes(metadata[i].value), ); } + + // Send the freed coins back to the caller + transferCoins(Context.caller(), balance()); } /** @@ -193,6 +208,9 @@ export function removeMetadataGlobal(_binaryArgs: StaticArray): 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()); } /* -------------------------------------------------------------------------- */ @@ -222,6 +240,9 @@ export function setMetadataFile(_binaryArgs: StaticArray): void { hashLocation, ); } + + // Send the freed coins back to the caller + transferCoins(Context.caller(), balance()); } /** @@ -243,6 +264,9 @@ export function removeMetadataFile(_binaryArgs: StaticArray): 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()); } /* -------------------------------------------------------------------------- */ @@ -282,6 +306,9 @@ export function receiveCoins(): void { export function upgradeSC(args: StaticArray): void { _onlyOwner(); setBytecode(args); + + // Send the freed coins back to the caller + transferCoins(Context.caller(), balance()); } From 55d9a105f7a228b7b25ae8875fb455deda78208b Mon Sep 17 00:00:00 2001 From: damip Date: Fri, 3 Jan 2025 15:08:38 +0100 Subject: [PATCH 2/2] update tests --- .../__tests__/deweb-interface/helpers/delete-file.ts | 7 ++++++- .../__tests__/deweb-interface/remove-files.spec.ts | 4 ++++ 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/smart-contract/assembly/__tests__/deweb-interface/helpers/delete-file.ts b/smart-contract/assembly/__tests__/deweb-interface/helpers/delete-file.ts index f5534d3..5ca878c 100644 --- a/smart-contract/assembly/__tests__/deweb-interface/helpers/delete-file.ts +++ b/smart-contract/assembly/__tests__/deweb-interface/helpers/delete-file.ts @@ -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 { @@ -81,3 +81,8 @@ export function _assertHasNoChunk(locationHash: StaticArray): void { ); assert(chunkKeys.length === 0, 'Chunks should not be stored'); } + +export function _assertHasNoCoins(): void { + assert(balance() === 0, 'Balance should be zero'); +} + diff --git a/smart-contract/assembly/__tests__/deweb-interface/remove-files.spec.ts b/smart-contract/assembly/__tests__/deweb-interface/remove-files.spec.ts index c9fc886..ee46e65 100644 --- a/smart-contract/assembly/__tests__/deweb-interface/remove-files.spec.ts +++ b/smart-contract/assembly/__tests__/deweb-interface/remove-files.spec.ts @@ -8,6 +8,7 @@ import { _assertPurged, _deleteFiles, _purge, + _assertHasNoCoins, } from './helpers/delete-file'; import { Metadata } from '../../contracts/serializable/Metadata'; @@ -52,6 +53,7 @@ describe('Upload files', () => { _assertFilesAreNotPresent([file1Path, file2Path, file3Path]); _assertFilesArePresent([file4Path]); + _assertHasNoCoins(); }); throws('if try to remove a non-existing file', () => { @@ -70,6 +72,7 @@ describe('Upload files', () => { _assertFilesAreNotPresent([file1Path, file2Path, file3Path]); _assertFilesArePresent([file4Path]); + _assertHasNoCoins(); }); test('Test purge', () => { @@ -91,5 +94,6 @@ describe('Upload files', () => { _purge(); _assertPurged(); + _assertHasNoCoins(); }); });