Skip to content

Commit

Permalink
Add WMAS Standard (#142)
Browse files Browse the repository at this point in the history
* feat: WMAS Standard

* fix: run fmt

* fix param

* fix compilation errors

* feat: WMAS interface & add missing function in wrapper

* fix missing doc

* fix: remove deprecated link

* Update recipient address

Co-authored-by: Senji888 <44082144+Ben-Rey@users.noreply.github.com>

* run fmt

* refactor: inverse ligne

---------

Co-authored-by: Senji888 <44082144+Ben-Rey@users.noreply.github.com>
  • Loading branch information
plouis01 and Ben-Rey authored Mar 12, 2024
1 parent 7da2bc2 commit 604d357
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 1 deletion.
23 changes: 23 additions & 0 deletions smart-contracts/assembly/contracts/FT/IWMAS.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { Args } from '@massalabs/as-types';
import { Address, call } from '@massalabs/massa-as-sdk';
import { TokenWrapper } from './wrapper';
import { u256 } from 'as-bignum/assembly/integer/u256';

export class IWMAS extends TokenWrapper {
init(
name: string = 'Wrapped Massa',
symbol: string = 'WMAS',
decimals: u8 = 9,
supply: u256 = u256.Zero,
): void {
super.init(name, symbol, decimals, supply);
}

deposit(value: u64): void {
call(this._origin, 'deposit', new Args(), value);
}

withdraw(value: u64, to: Address): void {
call(this._origin, 'withdraw', new Args().add(value).add(to), 0);
}
}
41 changes: 41 additions & 0 deletions smart-contracts/assembly/contracts/FT/WMAS.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import { Args, u256ToBytes } from '@massalabs/as-types';
import { Address, Context, transferCoins } from '@massalabs/massa-as-sdk';
import { burn } from './burnable/burn';
import { u256 } from 'as-bignum/assembly/integer/u256';
import { _mint } from './mintable/mint-internal';

export * from './token';

/**
* Wrap wanted value.
*
* @param _ - unused but mandatory.
*/
export function deposit(_: StaticArray<u8>): void {
const amount = Context.transferredCoins();
assert(amount > 0, 'Payment must be more than 0 MAS');
const recipient = Context.caller();

const args = new Args().add(recipient).add(u256.fromU64(amount)).serialize();
_mint(args);
}

/**
* Unwrap wanted value.
*
* @param bs - serialized StaticArray<u8> containing
* - the amount to withdraw (u64)
* - the recipient's account (String).
*/
export function withdraw(bs: StaticArray<u8>): void {
const args = new Args(bs);
const amount = args.nextU64().expect('amount is missing');
const recipient = new Address(
args.nextString().expect('recipient is missing'),
);

assert(amount > 0, 'Payment must be more than 0 WMAS');

burn(u256ToBytes(u256.fromU64(amount)));
transferCoins(recipient, amount);
}
31 changes: 30 additions & 1 deletion smart-contracts/assembly/contracts/FT/wrapper.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
import { Address, call } from '@massalabs/massa-as-sdk';
import { Args, NoArg, bytesToU256, bytesToString } from '@massalabs/as-types';
import {
Args,
NoArg,
bytesToU256,
bytesToString,
byteToU8,
} from '@massalabs/as-types';
import { u256 } from 'as-bignum/assembly';

/**
Expand Down Expand Up @@ -28,6 +34,19 @@ export class TokenWrapper {
this._origin = at;
}

/**
* Initializes the smart contract.
*
* @param name - Name of the token.
* @param symbol - Symbol of the token.
* @param decimals - Number of decimals of the token.
* @param supply - Initial supply of the token.
*/
init(name: string, symbol: string, decimals: u8, supply: u256): void {
const args = new Args().add(name).add(symbol).add(decimals).add(supply);
call(this._origin, 'constructor', args, 0);
}

/**
* Returns the version of the smart contract.
* This versioning is following the best practices defined in https://semver.org/.
Expand Down Expand Up @@ -55,6 +74,16 @@ export class TokenWrapper {
return bytesToString(call(this._origin, 'symbol', NoArg, 0));
}

/**
* Returns the number of decimals of the token.
*
* @returns number of decimals.
*/
decimals(): u8 {
const res = call(this._origin, 'decimals', NoArg, 0);
return byteToU8(res);
}

/**
* Returns the total token supply.
*
Expand Down

0 comments on commit 604d357

Please sign in to comment.