Skip to content

Commit

Permalink
v4.0.0
Browse files Browse the repository at this point in the history
Fix tron objects descrialization
  • Loading branch information
mrtnetwork committed Aug 29, 2024
1 parent fbade03 commit dff8fef
Show file tree
Hide file tree
Showing 105 changed files with 2,054 additions and 476 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 4.0.0

- Fix tron objects descrialization

## 3.9.0

- Fix issue with Ethereum RLP encoding related to leading zero in signature S.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ void main() async {
],
"stateMutability": "pure",
"type": "function"
}, false),
}),
params: [
BigInt.from(12),
BigInt.from(150),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ void main() async {
"name": "transfer",
"stateMutability": "nonpayable",
"type": "function"
}, false);
});

/// Request EIP-1559 historical fee data from the RPC service
final eip1559HistoricalFee = await rpc.request(RPCGetFeeHistory(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ void main() async {
"name": "transfer",
"stateMutability": "nonpayable",
"type": "function"
}, false);
});

/// Request gas price from the RPC service
final gasPrice = await rpc.request(RPCGetGasPrice());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ void main() async {
"name": "transfer",
"stateMutability": "nonpayable",
"type": "function"
}, false);
});

/// Build an Ethereum transaction for a contract call (transfer)
final tr = ETHTransactionBuilder.contract(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import 'tron_test_abi.dart';
void main() async {
/// For Tron: If the output parameters include an address, set isTron to true.
/// If it doesn't, set isTron to false to receive an ETH address instead of a Tron address.
final contract = ContractABI.fromJson(tronContract["entrys"]!, isTron: true);
final contract = ContractABI.fromJson(tronContract["entrys"]!);
final rpc = EVMRPC(RPCHttpService("https://api.shasta.trongrid.io/jsonrpc"));
final call1 = await rpc.request(RPCCall.fromMethod(
contractAddress:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,7 @@ void main() async {
final rpc =
TronProvider(TronHTTPProvider(url: "https://api.shasta.trongrid.io"));

final contract =
ContractABI.fromJson(payableContractTest["entrys"]!, isTron: true);
final contract = ContractABI.fromJson(payableContractTest["entrys"]!);

final function = contract.functionFromName("PayWithTrc10");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import 'tron_test_abi.dart';
void main() async {
/// For Tron: If the output parameters include an address, set isTron to true.
/// If it doesn't, set isTron to false to receive an ETH address instead of a Tron address.
final contract = ContractABI.fromJson(tronContract["entrys"]!, isTron: true);
final contract = ContractABI.fromJson(tronContract["entrys"]!);
final rpc =
TronProvider(TronHTTPProvider(url: "https://api.shasta.trongrid.io"));
final call1 = await rpc.request(TronRequestTriggerConstantContract.fromMethod(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ void main() async {
int bandWidthNeed = 0;
int energyNeed = 0;

final contract = ContractABI.fromJson(trc20Abi, isTron: true);
final contract = ContractABI.fromJson(trc20Abi);

final function = contract.functionFromName("transfer");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ void main() async {
final rpc =
TronProvider(TronHTTPProvider(url: "https://api.shasta.trongrid.io"));

final contract = ContractABI.fromJson(trc20Abi, isTron: true);
final contract = ContractABI.fromJson(trc20Abi);

final function = contract.functionFromName("transfer");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@ void main() async {
final rpc =
TronProvider(TronHTTPProvider(url: "https://api.shasta.trongrid.io"));

final contract =
ContractABI.fromJson(payableContractTest["entrys"]!, isTron: true);
final contract = ContractABI.fromJson(payableContractTest["entrys"]!);

final function = contract.functionFromName("PayWithTRX");

Expand Down
21 changes: 1 addition & 20 deletions example/test/widget_test.dart
Original file line number Diff line number Diff line change
@@ -1,20 +1 @@
void main() async {
// /// WebSocket RPC Service
// final websocketRpc = await RPCWebSocketService.connect(
// "wss://go.getblock.io/b9c91d92aaeb4e5ba2d4cca664ab708c", onEvents: (p0) {
// print("on event $p0");
// }, onClose: (p0) {});
// // Establish a WebSocket RPC connection to the specified endpoint for real-time updates.

// /// Ethereum RPC
// final rpc = EVMRPC(websocketRpc);

// final getblock = await rpc.request(RPCETHSubscribe());
// print("block $getblock");
// Timer.periodic(const Duration(seconds: 5), (s) async {
// // final re = await rpc.request(RPCETHSubscribe());
// // print("re $re");
// });
// print("get block $getblock");
// final changed = await Future.delayed(const Duration(seconds: 60));
}
void main() {}
23 changes: 9 additions & 14 deletions lib/ethereum/src/address/evm_address.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,19 @@ import 'package:on_chain/solidity/address/core.dart';
import 'package:blockchain_utils/bip/address/eth_addr.dart';
import 'package:blockchain_utils/blockchain_utils.dart';

extension ToEthereumAddress on SolidityAddress {
ETHAddress toEthereumAddress() {
if (this is ETHAddress) return this as ETHAddress;
return ETHAddress(toHex());
}
}

/// Class representing an Ethereum address, implementing the [SolidityAddress] interface.
class ETHAddress implements SolidityAddress {
/// The Ethereum address string.
class ETHAddress extends SolidityAddress {
final String address;

/// Private constructor for creating an instance of [ETHAddress] with a given Ethereum address
const ETHAddress._(this.address);
const ETHAddress._(this.address) : super.unsafe(address);

/// Creates an [ETHAddress] instance from a public key represented as a bytes.
factory ETHAddress.fromPublicKey(List<int> keyBytes) {
Expand Down Expand Up @@ -39,20 +45,9 @@ class ETHAddress implements SolidityAddress {
return ETHAddress(BytesUtils.toHexString(addrBytes, prefix: "0x"));
}

/// convert address to bytes
@override
List<int> toBytes() {
return BytesUtils.fromHexString(address);
}

/// Constant representing the length of the ETH address in bytes
static const int lengthInBytes = 20;

@override
String toHex() {
return address;
}

@override
String toString() {
return address;
Expand Down
2 changes: 1 addition & 1 deletion lib/ethereum/src/models/block.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import 'package:on_chain/ethereum/src/models/transaction.dart';
import 'package:on_chain/utils/number_utils.dart';
import 'package:on_chain/utils/utils/number_utils.dart';

/// Represents a withdrawal in the context of Ethereum block.
class Withdrawal {
Expand Down
2 changes: 1 addition & 1 deletion lib/ethereum/src/models/fee_history.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import 'package:on_chain/utils/number_utils.dart';
import 'package:on_chain/utils/utils/number_utils.dart';

/// Represents the fee history in the context of Ethereum, including base fee per gas, gas used ratio, oldest block, and reward details.
class FeeHistory {
Expand Down
2 changes: 1 addition & 1 deletion lib/ethereum/src/models/log_entry.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import 'package:on_chain/utils/number_utils.dart';
import 'package:on_chain/utils/utils/number_utils.dart';

/// Represents an entry in Ethereum transaction logs.
class LogEntry {
Expand Down
2 changes: 1 addition & 1 deletion lib/ethereum/src/models/transaction.dart
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import 'package:on_chain/ethereum/src/address/evm_address.dart';
import 'package:on_chain/ethereum/src/models/models.dart';
import 'package:on_chain/ethereum/src/transaction/eth_transaction.dart';
import 'package:on_chain/utils/number_utils.dart';
import 'package:on_chain/utils/utils/number_utils.dart';
import 'package:blockchain_utils/blockchain_utils.dart';

/// Represents information about an Ethereum transaction.
Expand Down
2 changes: 1 addition & 1 deletion lib/ethereum/src/models/transaction_receipt.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import 'package:on_chain/ethereum/src/models/log_entry.dart';
import 'package:on_chain/utils/number_utils.dart';
import 'package:on_chain/utils/utils/number_utils.dart';

/// Represents the receipt of an Ethereum transaction.
class TransactionReceipt {
Expand Down
2 changes: 1 addition & 1 deletion lib/ethereum/src/transaction/eth_transaction.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import 'package:on_chain/ethereum/src/address/evm_address.dart';
import 'package:on_chain/ethereum/src/rlp/decode.dart';
import 'package:on_chain/ethereum/src/rlp/encode.dart';
import 'package:on_chain/ethereum/src/models/access_list.dart';
import 'package:on_chain/utils/number_utils.dart';
import 'package:on_chain/utils/utils/number_utils.dart';
import 'package:blockchain_utils/blockchain_utils.dart';

/// Represents the type of an Ethereum transaction.
Expand Down
1 change: 0 additions & 1 deletion lib/solidity/abi/abi.dart
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
library abi;

import 'package:on_chain/solidity/address/core.dart';
import 'package:on_chain/ethereum/src/address/evm_address.dart';
import 'package:on_chain/tron/src/address/tron_address.dart';
import 'package:blockchain_utils/blockchain_utils.dart';

Expand Down
8 changes: 3 additions & 5 deletions lib/solidity/abi/core/abi.dart
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ class AbiParameter {
final String type;

/// Flag indicating whether Tron types are used.
final bool tronTypes;
// final bool tronTypes;

/// The base type, if applicable.
final String? baseType;
Expand All @@ -84,7 +84,6 @@ class AbiParameter {
/// The name of the parameter.
required this.name,
required this.type,
this.tronTypes = false,
this.baseType,
this.indexed = false,
this.components = const [],
Expand Down Expand Up @@ -113,17 +112,16 @@ class AbiParameter {
}

/// Factory method to create an AbiParameter instance from a JSON representation.
factory AbiParameter.fromJson(Map<String, dynamic> json, bool tronTypes) {
factory AbiParameter.fromJson(Map<String, dynamic> json) {
final List<dynamic> inputs = json["components"] ?? [];
final String name = json["name"] ?? "";
return AbiParameter(
name: name.isEmpty ? null : name,
type: json["type"],
internalType: json["internalType"],
indexed: json["indexed"] ?? false,
tronTypes: tronTypes,
components: List<AbiParameter>.unmodifiable(
inputs.map((e) => AbiParameter.fromJson(e, tronTypes)).toList()),
inputs.map((e) => AbiParameter.fromJson(e)).toList()),
);
}

Expand Down
30 changes: 15 additions & 15 deletions lib/solidity/abi/eip712/utils.dart
Original file line number Diff line number Diff line change
Expand Up @@ -81,11 +81,13 @@ class _EIP712Utils {
}
switch (type) {
case "address":
if (value is TronAddress) {
return value.toAddress();
} else {
return (value as ETHAddress).address;
if (value is String) {
return value;
}
if (value is SolidityAddress) {
return value.toHex();
}
break;
case "bool":
case "string":
return value;
Expand All @@ -100,19 +102,17 @@ class _EIP712Utils {
/// If the value is a string, it is parsed to create an ETHAddress or TronAddress.
/// Throws a [SolidityAbiException] for invalid input.
static SolidityAddress ensureIsAddress(dynamic value) {
if (value is ETHAddress) return value;
if (value is List<int>) {
if (value.length == TronAddress.lengthInBytes) {
return TronAddress.fromBytes(value);
}
return ETHAddress.fromBytes(value);
} else if (value is String) {
try {
return ETHAddress(value);
} catch (e) {
try {
if (value is SolidityAddress) return value;
if (value is List<int>) {
return SolidityAddress.fromBytes(value);
} else if (value is String) {
if (StringUtils.isHexBytes(value)) {
return SolidityAddress(value);
}
return TronAddress(value);
}
}
} catch (_) {}
throw SolidityAbiException("Invalid data provided for address codec.",
details: {"input": value});
}
Expand Down
9 changes: 9 additions & 0 deletions lib/solidity/abi/exception/abi_exception.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,13 @@ class SolidityAbiException extends BlockchainUtilsException {
final String message;

const SolidityAbiException(this.message, {this.details});

@override
String toString() {
String msg = message;
if (details?.isNotEmpty ?? false) {
msg += ' Details: $details';
}
return msg;
}
}
4 changes: 1 addition & 3 deletions lib/solidity/abi/types/address.dart
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,7 @@ class AddressCoder implements ABICoder<SolidityAddress> {
final addrBytes = bytes.sublist(
ABIConst.uintBytesLength - addrLength, ABIConst.uintBytesLength);
return DecoderResult(
result: params.tronTypes
? TronAddress.fromEthAddress(addrBytes)
: ETHAddress.fromBytes(addrBytes),
result: SolidityAddress.fromBytes(addrBytes),
consumed: ABIConst.uintBytesLength,
name: params.name);
}
Expand Down
5 changes: 1 addition & 4 deletions lib/solidity/abi/utils/utils.dart
Original file line number Diff line number Diff line change
Expand Up @@ -78,10 +78,7 @@ class _ABIUtils {
}
return Tuple(
AbiParameter(
type: arrayParamType,
name: '',
components: abi.components,
tronTypes: abi.tronTypes),
type: arrayParamType, name: '', components: abi.components),
size);
}
}
Expand Down
35 changes: 32 additions & 3 deletions lib/solidity/address/core.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
import 'package:blockchain_utils/bip/address/eth_addr.dart';
import 'package:blockchain_utils/bip/coin_conf/coins_conf.dart';
import 'package:blockchain_utils/blockchain_utils.dart';

/// An abstract class representing a hexadecimal address in solidity smart conteract system.
/// such as Ethereum and Tron (visible address).
///
Expand All @@ -8,9 +12,34 @@
/// Implementations for specific blockchain addresses, such as Ethereum
/// (`ETHAddress`) and Tron (`TronAddress`), will provide concrete
/// implementations for these methods.
abstract class SolidityAddress {
class SolidityAddress {
final String _hexAddress;
const SolidityAddress.unsafe(this._hexAddress);
factory SolidityAddress(String address, {bool skipChecksum = true}) {
address = StringUtils.strip0x(address);
if (address.length > EthAddrConst.addrLen &&
address.toLowerCase().startsWith("41")) {
address = address.substring(2);
}
EthAddrDecoder().decodeAddr(
"${CoinsConf.ethereum.params.addrPrefix}$address",
{"skip_chksum_enc": skipChecksum});
return SolidityAddress.unsafe(EthAddrUtils.toChecksumAddress(address));
}
factory SolidityAddress.fromBytes(List<int> bytes,
{bool skipChecksum = true}) {
return SolidityAddress(BytesUtils.toHexString(bytes));
}

/// Converts the hexadecimal address to a bytes.
List<int> toBytes();
List<int> toBytes() {
return BytesUtils.fromHexString(_hexAddress);
}

String toHex() => _hexAddress;

String toHex();
@override
String toString() {
return _hexAddress;
}
}
5 changes: 2 additions & 3 deletions lib/solidity/contract/contract_abi.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,10 @@ class ContractABI {
ContractABI._(this.fragments);

/// Factory method to create a ContractABI instance from JSON.
factory ContractABI.fromJson(List<Map<String, dynamic>> abi,
{bool isTron = false}) {
factory ContractABI.fromJson(List<Map<String, dynamic>> abi) {
try {
final fragments = abi.map((e) {
return AbiBaseFragment.fromJson(e, isTron);
return AbiBaseFragment.fromJson(e);
}).toList();
return ContractABI._(fragments);
} catch (e) {
Expand Down
Loading

0 comments on commit dff8fef

Please sign in to comment.