-
Notifications
You must be signed in to change notification settings - Fork 77
/
Copy pathWMAS.ts
65 lines (60 loc) · 1.76 KB
/
WMAS.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import { Args, u256ToBytes } from '@massalabs/as-types';
import {
Address,
Context,
Storage,
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';
import { balanceKey } from './MRC20-internals';
export * from './MRC20';
const STORAGE_BYTE_COST = 100_000;
const STORAGE_PREFIX_LENGTH = 4;
const BALANCE_KEY_PREFIX_LENGTH = 7;
/**
* Wrap wanted value.
*
* @param _ - unused but mandatory.
*/
export function deposit(_: StaticArray<u8>): void {
const recipient = Context.caller();
const amount = Context.transferredCoins();
const storageCost = computeMintStorageCost(recipient);
assert(
amount > storageCost,
'Transferred amount is not enough to cover storage cost',
);
_mint(
new Args()
.add(recipient)
.add(u256.fromU64(amount - storageCost))
.serialize(),
);
}
/**
* 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'),
);
burn(u256ToBytes(u256.fromU64(amount)));
transferCoins(recipient, amount);
}
export function computeMintStorageCost(receiver: Address): u64 {
if (Storage.has(balanceKey(receiver))) {
return 0;
}
const baseLength = STORAGE_PREFIX_LENGTH;
const keyLength = BALANCE_KEY_PREFIX_LENGTH + receiver.toString().length;
const valueLength = 4 * sizeof<u64>();
return (baseLength + keyLength + valueLength) * STORAGE_BYTE_COST;
}