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

Add transferOwnership function to DeWeb SC #186

Closed
wants to merge 2 commits into from
Closed
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
@@ -0,0 +1,44 @@
import { Args } from '@massalabs/as-types';
import {
Address,
get,
getKeys,
resetStorage,
setDeployContext,
} from '@massalabs/massa-as-sdk';
import {
constructor,
transferOwnership,
} from '../../contracts/deweb-interface';
import { OWNER_KEY } from '@massalabs/sc-standards/assembly/contracts/utils/ownership-internal';

const user1 = new Address(
'AU12UBnqTHDQALpocVBnkPNy7y5CndUJQTLutaVDDFgMJcq5kQiKq',
);
const user2 = new Address(
'AU1Sxv7v8KZtYQHvAq1V8fB5z3QJ1Q5t7ccDeASEgbLPmMn4SVO1P',
);

describe('Ownership', () => {
beforeEach(() => {
resetStorage();
setDeployContext(user1.toString());
constructor(new Args().serialize());
});

test('transfer ownership', () => {
transferOwnership(new Args().add(user2).serialize());

const owner = get(OWNER_KEY);

expect(owner).toStrictEqual(user2.toString(), 'Owner should be user2');
});

test('renounce ownership', () => {
transferOwnership(new Args().serialize());

const owner = get(OWNER_KEY);

expect(owner).toHaveLength(0, 'Owner should be empty');
});
});
19 changes: 19 additions & 0 deletions smart-contract/assembly/contracts/deweb-interface.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {
Address,
balance,
Context,
generateEvent,
Expand Down Expand Up @@ -45,6 +46,24 @@ export function constructor(_: StaticArray<u8>): void {
_setOwner(Context.caller().toString());
}

/**
* Change the owner of the smart contract to the address passed as an argument
* @param _binaryArgs - serialized arguments containing the new owner address
* @returns void
*/
export function transferOwnership(_binaryArgs: StaticArray<u8>): void {
_onlyOwner();

const args = new Args(_binaryArgs);
const newOwnerResult = args.nextSerializable<Address>();

const newOwner = newOwnerResult.isOk()
? newOwnerResult.unwrap().toString()
: '';

_setOwner(newOwner);
}

/* -------------------------------------------------------------------------- */
/* FILE INITIALIZATION */
/* -------------------------------------------------------------------------- */
Expand Down
Loading