|
1 | 1 | import json |
| 2 | +import pleth.abi |
2 | 3 | import pleth.config |
3 | 4 | import pleth.core |
4 | 5 | import pleth.denomination |
@@ -67,6 +68,50 @@ def contract_exec(self, addr: bytearray, value: int, data: bytearray) -> bytearr |
67 | 68 | tx = pleth.core.TxLegacy(self.nonce(), gas_price, gas, addr, value, data) |
68 | 69 | return self.send(tx) |
69 | 70 |
|
| 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 | + |
70 | 115 | def nonce(self) -> int: |
71 | 116 | return int(pleth.rpc.eth_get_transaction_count(f'0x{self.addr.hex()}', 'pending'), 0) |
72 | 117 |
|
|
0 commit comments