From 604d35739aa3663c53434cb69ac62c7b6131ef1d Mon Sep 17 00:00:00 2001 From: Pierre-Louis <93529605+plouis01@users.noreply.github.com> Date: Tue, 12 Mar 2024 02:31:59 -0700 Subject: [PATCH] Add WMAS Standard (#142) * 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> --- .../assembly/contracts/FT/IWMAS.ts | 23 +++++++++++ smart-contracts/assembly/contracts/FT/WMAS.ts | 41 +++++++++++++++++++ .../assembly/contracts/FT/wrapper.ts | 31 +++++++++++++- 3 files changed, 94 insertions(+), 1 deletion(-) create mode 100644 smart-contracts/assembly/contracts/FT/IWMAS.ts create mode 100644 smart-contracts/assembly/contracts/FT/WMAS.ts diff --git a/smart-contracts/assembly/contracts/FT/IWMAS.ts b/smart-contracts/assembly/contracts/FT/IWMAS.ts new file mode 100644 index 0000000..8337b85 --- /dev/null +++ b/smart-contracts/assembly/contracts/FT/IWMAS.ts @@ -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); + } +} diff --git a/smart-contracts/assembly/contracts/FT/WMAS.ts b/smart-contracts/assembly/contracts/FT/WMAS.ts new file mode 100644 index 0000000..7f96908 --- /dev/null +++ b/smart-contracts/assembly/contracts/FT/WMAS.ts @@ -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): 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 containing + * - the amount to withdraw (u64) + * - the recipient's account (String). + */ +export function withdraw(bs: StaticArray): 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); +} diff --git a/smart-contracts/assembly/contracts/FT/wrapper.ts b/smart-contracts/assembly/contracts/FT/wrapper.ts index 0d98631..61e6168 100644 --- a/smart-contracts/assembly/contracts/FT/wrapper.ts +++ b/smart-contracts/assembly/contracts/FT/wrapper.ts @@ -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'; /** @@ -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/. @@ -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. *