-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.ts
118 lines (102 loc) · 3.11 KB
/
index.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
import {
Address,
Bytes,
Context,
Host,
debug,
Balance,
PersistentMap,
util,
} from "idena-sdk-as"
export class IRC20 {
balances: PersistentMap<Address, Balance>
approves: PersistentMap<string, Balance>
_name: string = "TOKEN NAME"
_symbol: string = "TOKEN SYMBOL"
_decimals: u8 = 18
_totalSupply: Balance = Balance.fromString("1000000000000000000000000000") // 1000000000 tokens with 18 decimal places
constructor() {
this.balances = PersistentMap.withStringPrefix<Address, Balance>("b:");
this.approves = PersistentMap.withStringPrefix<string, Balance>("a:");
const caller = Context.caller()
this.balances.set(caller, this._totalSupply);
this.emitTransferEvent(new Address(0), caller, this._totalSupply)
}
@view
getBalance(owner: Address): Balance {
return this.balances.get(owner, Balance.from(0))
}
transfer(to: Address, amount: Balance): void {
let sender = Context.caller()
debug(
"transfer from: " +
sender.toHex() +
" to: " +
to.toHex() +
" amount: " +
amount.value.toString()
)
const fromAmount = this.getBalance(sender)
util.assert(fromAmount >= amount, "not enough tokens on account")
this.balances.set(sender, fromAmount - amount)
let destBalance = this.getBalance(to)
this.balances.set(to, destBalance + amount)
this.emitTransferEvent(sender, to, amount)
}
approve(spender: Address, amount: Balance): void {
let sender = Context.caller()
this.approves.set(sender.toHex() + ":" + spender.toHex(), amount)
this.emitApprovalEvent(sender, spender, amount)
}
@view
allowance(tokenOwner: Address, spender: Address): Balance {
const key = tokenOwner.toHex() + ":" + spender.toHex()
log(`key hex=${util.toHexString(util.stringToBytes(key))}`)
return this.approves.get(key, Balance.from(0))
}
@view
totalSupply(): Balance {
return this._totalSupply
}
@view
name(): string {
return this._name
}
@view
symbol(): string {
return this._symbol
}
@view
decimals(): u8 {
return this._decimals
}
transferFrom(from: Address, to: Address, amount: Balance): void {
let caller = Context.caller()
const fromAmount = this.getBalance(from)
util.assert(fromAmount >= amount, "not enough tokens on account")
const approvedAmount = this.allowance(from, caller)
util.assert(
amount <= approvedAmount,
"not enough tokens approved to transfer"
)
this.approves.set(from.toHex() + ":" + caller.toHex(), approvedAmount - amount)
this.balances.set(from, fromAmount - amount)
let destBalance = this.getBalance(to)
this.balances.set(to, destBalance + amount)
this.emitTransferEvent(from, to, amount)
}
private emitTransferEvent(from: Address, to: Address, amount: Balance): void {
Host.emitEvent("transfer", [
from,
to,
Bytes.fromBytes(amount.toBytes()),
])
}
private emitApprovalEvent(owner: Address, spender: Address, amount: Balance): void{
Host.emitEvent("approval", [
owner,
spender,
Bytes.fromBytes(amount.toBytes())
])
}
}