Skip to content

Commit 46126b0

Browse files
committed
Implement erc20 token contract
1 parent e6d97e8 commit 46126b0

6 files changed

Lines changed: 433 additions & 1 deletion

File tree

pleth/abi.py

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,10 +113,32 @@ def size(self) -> int:
113113
return self.slen
114114

115115

116+
class Uint8:
117+
@classmethod
118+
def decode(cls, reader: io.IOBase) -> int:
119+
origin = int.from_bytes(pleth.io.read_full(reader, 32), 'big')
120+
assert origin >= 0
121+
assert origin <= 0xff
122+
return origin
123+
124+
@classmethod
125+
def encode(cls, origin: int) -> bytearray:
126+
assert origin >= 0
127+
assert origin <= 0xff
128+
return bytearray(origin.to_bytes(32, 'big'))
129+
130+
@classmethod
131+
def size(cls) -> int:
132+
return 32
133+
134+
116135
class Uint256:
117136
@classmethod
118137
def decode(cls, reader: io.IOBase) -> int:
119-
return int.from_bytes(pleth.io.read_full(reader, 32), 'big')
138+
origin = int.from_bytes(pleth.io.read_full(reader, 32), 'big')
139+
assert origin >= 0
140+
assert origin <= 0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff
141+
return origin
120142

121143
@classmethod
122144
def encode(cls, origin: int) -> bytearray:

pleth/wallet.py

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import json
2+
import pleth.abi
23
import pleth.config
34
import pleth.core
45
import pleth.denomination
@@ -67,6 +68,50 @@ def contract_exec(self, addr: bytearray, value: int, data: bytearray) -> bytearr
6768
tx = pleth.core.TxLegacy(self.nonce(), gas_price, gas, addr, value, data)
6869
return self.send(tx)
6970

71+
def erc20_balance(self, addr: bytearray) -> int:
72+
func = pleth.abi.function_selector('balanceOf', ['address'])
73+
args = pleth.abi.argument_encoding([pleth.abi.Address], [self.addr])
74+
data = func + args
75+
rets = self.contract_call(addr, data)
76+
rets = pleth.abi.argument_decoding([pleth.abi.Uint256], rets)
77+
return rets[0]
78+
79+
def erc20_decimals(self, addr: bytearray) -> int:
80+
data = pleth.abi.function_selector('decimals', [])
81+
rets = self.contract_call(addr, data)
82+
rets = pleth.abi.argument_decoding([pleth.abi.Uint8], rets)
83+
return rets[0]
84+
85+
def erc20_transfer(self, addr: bytearray, to: bytearray, value: int) -> bytearray:
86+
func = pleth.abi.function_selector('transfer', ['address', 'uint256'])
87+
args = pleth.abi.argument_encoding([pleth.abi.Address, pleth.abi.Uint256], [to, value])
88+
return self.contract_exec(addr, 0, func + args)
89+
90+
def erc20_transfer_all(self, addr: bytearray, to: bytearray) -> bytearray:
91+
return self.erc20_transfer(addr, to, self.erc20_balance(addr))
92+
93+
def erc20_transfer_from(self, addr: bytearray, spender: bytearray, to: bytearray, value: int) -> bytearray:
94+
func = pleth.abi.function_selector('transferFrom', ['address', 'address', 'uint256'])
95+
args = pleth.abi.argument_encoding(
96+
[pleth.abi.Address, pleth.abi.Address, pleth.abi.Uint256],
97+
[spender, to, value],
98+
)
99+
hash = self.contract_exec(addr, 0, func + args)
100+
return hash
101+
102+
def erc20_approve(self, addr: bytearray, spender: bytearray, value: int) -> bytearray:
103+
func = pleth.abi.function_selector('approve', ['address', 'uint256'])
104+
args = pleth.abi.argument_encoding([pleth.abi.Address, pleth.abi.Uint256], [spender, value])
105+
hash = self.contract_exec(addr, 0, func + args)
106+
return hash
107+
108+
def erc20_allowance(self, addr: bytearray, owner: bytearray, spender: bytearray) -> int:
109+
func = pleth.abi.function_selector('allowance', ['address', 'address'])
110+
args = pleth.abi.argument_encoding([pleth.abi.Address, pleth.abi.Address], [owner, spender])
111+
rets = self.contract_call(addr, func + args)
112+
rets = pleth.abi.argument_decoding([pleth.abi.Uint256], rets)
113+
return rets[0]
114+
70115
def nonce(self) -> int:
71116
return int(pleth.rpc.eth_get_transaction_count(f'0x{self.addr.hex()}', 'pending'), 0)
72117

res/erc20

5.85 KB
Binary file not shown.

0 commit comments

Comments
 (0)