/
main.sw
77 lines (67 loc) · 2.13 KB
/
main.sw
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
contract;
use std::{
context::{
balance_of,
msg_amount,
},
token::{
burn,
force_transfer_to_contract,
mint,
mint_to_address,
mint_to_contract,
transfer_to_address,
},
};
abi MyToken {
// Mint to this contract
fn mint(amount: u64);
// Burn from this contract
fn burn(amount: u64);
fn mint_to_address(amount: u64, to: Address);
fn mint_to_contract(amount: u64, to: ContractId);
fn transfer_to_address(amount: u64, asset_id: ContractId, to: Address);
fn force_transfer_to_contract(amount: u64, asset_id: ContractId, contract_id: ContractId);
// Deposit any token back to this contract
#[payable]
fn deposit();
fn get_balance_of_contract(contract_id: ContractId, asset_id: ContractId) -> u64;
// TODO: get balance of account?
}
impl MyToken for Contract {
// Mint to this contract
// Like ERC20.mint(amount, address(this))
fn mint(amount: u64) {
mint(amount);
}
// Burn from this contract
// Like ERC20.burn(amount, address(this))
fn burn(amount: u64) {
burn(amount);
}
// Like ERC20.mint(to, amount)
fn mint_to_address(amount: u64, to: Address) {
// TODO: what happens if to is contact?
mint_to_address(amount, to);
}
// Like ERC20.mint(to, amount)
fn mint_to_contract(amount: u64, to: ContractId) {
mint_to_contract(amount, to);
}
// Like ERC20(token).transferFrom(address(this), to, amount)
fn transfer_to_address(amount: u64, asset_id: ContractId, to: Address) {
transfer_to_address(amount, asset_id, to);
}
// Like ERC20(token).transferFrom(address(this), to, amount)
fn force_transfer_to_contract(amount: u64, asset_id: ContractId, contract_id: ContractId) {
force_transfer_to_contract(amount, asset_id, contract_id)
}
// Like ERC20(token).transferFrom(msg.sender, address(this), amount)
#[payable]
fn deposit() {
assert(msg_amount() > 0)
}
fn get_balance_of_contract(contract_id: ContractId, asset_id: ContractId) -> u64 {
balance_of(contract_id, asset_id)
}
}