-
Notifications
You must be signed in to change notification settings - Fork 3
/
OwnableContract.ts
73 lines (61 loc) · 1.92 KB
/
OwnableContract.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
import { LocalContractStorage, Blockchain, StorageMap, DynamicParameter } from "./System";
import { BigNumber } from "./bignumber";
class OwnableContract {
owner: string;
myAddress: string;
admins: StorageMap<string>;
constructor() {
LocalContractStorage.defineProperties(this, {
owner: null,
myAddress: null
})
LocalContractStorage.defineMapProperties(this, { "admins": null })
}
// Workaround for
// Property 'init' in type 'The Child Class' is not assignable
// to the same property in base type 'The Father Class'
_initOwnableContract() {
const { from } = Blockchain.transaction
this.admins.set(from, "true")
this.owner = from
}
onlyAdmins() {
const { from } = Blockchain.transaction
if (!this.admins.get(from)) {
throw new Error("Sorry, You don't have the permission as admins.")
}
}
onlyContractOwner() {
const { from } = Blockchain.transaction
if (this.owner !== from) {
throw new Error("Sorry, But you don't have the permission as owner.")
}
}
getContractOwner() {
return this.owner
}
getAdmins() {
return this.admins
}
setAdmins(address: string) {
this.onlyContractOwner()
this.admins.set(address, "true")
}
withdraw(value: number | string | BigNumber) {
this.onlyAdmins()
// Only the owner can have the withdraw fund, so be careful
return Blockchain.transfer(this.owner, new BigNumber(value))
}
withdrawAll() {
this.withdraw(this.getContractBalance())
}
setMyAddress() {
this.onlyContractOwner()
this.myAddress = Blockchain.transaction.to
}
getContractBalance() {
var balance = new BigNumber(Blockchain.getAccountState(this.myAddress).balance);
return balance
}
}
export default OwnableContract