-
Notifications
You must be signed in to change notification settings - Fork 77
/
Copy pathMRC20-internals.ts
81 lines (73 loc) · 2.35 KB
/
MRC20-internals.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
import { stringToBytes } from '@massalabs/as-types';
import { bytesToU256, u256ToBytes } from '@massalabs/as-types';
import { Address, Storage } from '@massalabs/massa-as-sdk';
import { u256 } from 'as-bignum/assembly';
export const BALANCE_KEY_PREFIX = 'BALANCE';
export const ALLOWANCE_KEY_PREFIX = 'ALLOWANCE';
/**
* Theses function are intended to be used in different token types (mintable, burnable...).
* We define them and export in this file to avoid exporting them in the contract entry file,
* making them callable from the outside world
*
*/
/**
* Returns the balance of a given address.
*
* @param address - address to get the balance for
*/
export function _balance(address: Address): u256 {
const key = balanceKey(address);
if (Storage.has(key)) {
return bytesToU256(Storage.get(key));
}
return u256.Zero;
}
/**
* Sets the balance of a given address.
*
* @param address - address to set the balance for
* @param balance -
*/
export function _setBalance(address: Address, balance: u256): void {
Storage.set(balanceKey(address), u256ToBytes(balance));
}
/**
* @param address -
* @returns the key of the balance in the storage for the given address
*/
export function balanceKey(address: Address): StaticArray<u8> {
return stringToBytes(BALANCE_KEY_PREFIX + address.toString());
}
/**
* Sets the allowance of the spender on the owner's account.
*
* @param owner - owner address
* @param spender - spender address
* @param amount - amount to set an allowance for
*/
export function _approve(owner: Address, spender: Address, amount: u256): void {
const key = allowanceKey(owner, spender);
Storage.set(key, u256ToBytes(amount));
}
/**
* Returns the allowance set on the owner's account for the spender.
*
* @param owner - owner's id
* @param spender - spender's id
*
* @returns the allowance
*/
export function _allowance(owner: Address, spender: Address): u256 {
const key = allowanceKey(owner, spender);
return Storage.has(key) ? bytesToU256(Storage.get(key)) : u256.Zero;
}
/**
* @param owner - address of the token owner
* @param spender - address of the token spender
* @returns the key of the allowance in the storage for the given addresses
*/
function allowanceKey(owner: Address, spender: Address): StaticArray<u8> {
return stringToBytes(
ALLOWANCE_KEY_PREFIX + owner.toString().concat(spender.toString()),
);
}