Skip to content

Commit

Permalink
V2.0.0
Browse files Browse the repository at this point in the history
Added support for the Solana network.
  • Loading branch information
mrtnetwork committed Feb 29, 2024
1 parent 7e7df54 commit 0b07cde
Show file tree
Hide file tree
Showing 1,380 changed files with 65,351 additions and 698 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,4 @@ doc/api/

.flutter-plugins
.flutter-plugins-dependencies
*.zip
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
## 2.0.0
- Added support for the Solana network.

## 1.0.0
- Resolved encoding and decoding issues with Tron operations..
- Corrected Eip1559 fee calculation in ETHTransactionBuilder.
Expand Down
215 changes: 213 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,45 @@ Dive into the Tron Virtual Machine (TVM) networks with confidence. The Onchain P

- All Features of Tron: Harness the full potential of Tron's blockchain by leveraging all its features, including shielded transfers, market transactions, resource delegation, contract management, and more. Empower your Dart applications with comprehensive functionality for a rich and dynamic Tron blockchain experience.

### Solana

Delve confidently into the Solana blockchain ecosystem with the Onchain Plugin for Dart, your gateway to seamless integration with Solana's powerful network. Empower your Dart applications to navigate the Solana landscape with ease, from account creation to asset transfers and execution of a diverse range of smart contracts. Uncover the full potential of Solana's network, harnessing its scalability and performance to drive innovation and growth in your projects. Explore the multitude of Solana contracts, including smart contracts, and unlock new possibilities for your Dart applications to flourish within the dynamic Solana blockchain ecosystem.

- Transaction: Versioned Transaction Generation, Serialization, and Deserialization.

- Sign: Effortlessly Sign Transactions

- Instructions: The plugin offers numerous pre-built instructions, simplifying the process of creating your own transactions. Here are some examples:

- addressLockupTable
- associatedTokenAccount
- computeBudget
- ed25519
- memo
- nameService
- secp256k1
- splToken
- splTokenMetaData
- splTokenSwap
- stake
- stakePool
- system
- tokenLending
- vote
- Metaplex
- auctionHouse
- auctioneer
- bubblegum
- candyMachineCore
- fixedPriceSale
- gumdrop
- hydra
- nftPacks
- tokenEntangler
- tokenMetaData

- Custom Programs: The plugin facilitates the Solana Buffer layout structure, enabling effortless encoding and decoding of pertinent data

## EXAMPLES

### Key and addresses
Expand All @@ -62,6 +101,7 @@ Dive into the Tron Virtual Machine (TVM) networks with confidence. The Onchain P
/// Derive the Ethereum address associated with the public key.
final EthereumAddress ethereumAddress = publicKey.toAddress();
/// Tron
/// Initialize a Tron private key.
final TronPrivateKey tronPrivateKey = TronPrivateKey("...");
Expand All @@ -86,6 +126,24 @@ Dive into the Tron Virtual Machine (TVM) networks with confidence. The Onchain P
/// Convert the Tron address to a hexadecimal format.
final String hexTronAddress = tronAddress.toString(false);
/// Solana
/// Initialize a Solana private key.
final SolanaPrivateKey solanaPrivateKey = SolanaPrivateKey.fromSeedHex("...");
/// Generate a cryptographic signature for a solana transaction serialized.
final String solanaSign = solanaPrivateKey.sign("txDigestBytes");
/// Obtain the corresponding Solana public key.
final SolanaPublicKey solanaPublicKey = solanaPrivateKey.publicKey();
/// Verify signature.
final bool verifySignature =
solanaPublicKey.verify("messageBytes", "signatureBytes");
/// Derive the Solana address associated with the public key.
final SolAddress solanaAddress = solanaPublicKey.toAddress();
```
### Transaction
Expand Down Expand Up @@ -343,6 +401,54 @@ Dive into the Tron Virtual Machine (TVM) networks with confidence. The Onchain P
await rpc.request(TronRequestBroadcastHex(transaction: transaction.toHex));
```
- Solana transaction
Check out all the examples at the provided [link](https://github.com/mrtnetwork/On_chain/tree/main/example/lib/example/solana).
Transfer SOL
```
/// Set up the RPC service with the Solana devnet endpoint.
final service = RPCHttpService("https://api.devnet.solana.com");
/// Initialize the Solana RPC client.
final rpc = SolanaRPC(service);
/// Define the owner's private key and derive the owner's public key.
final ownerPrivateKey = SolanaPrivateKey.fromSeedHex(
"4e27902b3df33d7857dc9d218a3b34a6550e9c7621a6d601d06240a517d22017");
final owner = ownerPrivateKey.publicKey().toAddress();
/// Define the recipient's address.
final receiver = SolAddress("9eaiUBgyT7EY1go2qrCmdRZMisYkGdtrrem3TgP9WSDb");
/// Retrieve the latest block hash.
final blockHash = await rpc.request(const SolanaRPCGetLatestBlockhash());
/// Create a transfer instruction to move funds from the owner to the receiver.
final transferInstruction = SystemProgram.transfer(
from: owner,
layout: SystemTransferLayout(lamports: SolanaUtils.toLamports("0.001")),
to: receiver);
/// Construct a Solana transaction with the transfer instruction.
final transaction = SolanaTransaction(
instructions: [transferInstruction],
recentBlockhash: blockHash.blockhash,
payerKey: owner,
type: TransactionType.v0);
/// Sign the transaction with the owner's private key.
final ownerSignature = ownerPrivateKey.sign(transaction.serializeMessage());
transaction.addSignature(owner, ownerSignature);
/// Serialize the transaction.
final serializedTransaction = transaction.serializeString();
/// Send the transaction to the Solana network.
await rpc.request(
SolanaRPCSendTransaction(encodedTransaction: serializedTransaction));
```
### EIP712
- EIP-712
Expand Down Expand Up @@ -395,12 +501,61 @@ Dive into the Tron Virtual Machine (TVM) networks with confidence. The Onchain P
/// Sign the encoded types with the private key, setting hashMessage to false
final _ = privateKey.sign(encodeTypes, hashMessage: false).toHex();
```
### Solana-Specific
The plugin offers extensive support for pre-built programs, each comprising four key sections:
1. Layouts: These receive the desired data for each program and decode it for transaction processing.
2. Programs: These are responsible for generating instructions.
3. Account: This section manages accounts associated with the program.
4. RPC: Utilized for obtaining accounts linked to the program.
5. Utils: Provides various program utilities.
Example:
```
/// Define the layout for initializing the candy machine.
final initializeCandyMachineLayout =
MetaplexCandyMachineInitializeCandyMachineLayout(
data: CandyMachineData(
itemsAvailable: BigInt.two,
symbol: "MRT",
sellerFeeBasisPoints: 100,
maxSupply: BigInt.from(1000),
isMutable: false,
creators: [Creator(address: address, verified: true, share: 0)]));

/// Find the mint counter PDA.
final mint = MetaplexCandyMachineProgramUtils.findMintCounterPda(
id: id, user: user, candyGuard: candyGuard, candyMachine: candyMachine);

/// Initialize the candy machine.
final instruction = MetaplexCandyMachineCoreProgram.initializeCandyMachine(
candyMachine: candyMachine,
authorityPda: authorityPda,
authority: authority,
payer: payer,
collectionMetadata: collectionMetadata,
collectionMint: collectionMint,
collectionMasterEdition: collectionMasterEdition,
collectionUpdateAuthority: collectionUpdateAuthority,
collectionAuthorityRecord: collectionAuthorityRecord,
tokenMetadataProgram: tokenMetadataProgram,
layout: layout);

/// Request the candy machine account information from the RPC.
final CandyMachineAccount account =
await rpc.request(SolanaRPCGetCandyMachineAccount(account: account));

/// Extract the authority from the candy machine account.
final authority = account.authority;

```
### JSON-RPC
- JSON-RPC
Discover the full spectrum of methods in the [link](https://github.com/mrtnetwork/On_chain/tree/main/lib/ethereum/rpc/methds).
Discover the full spectrum of methods in the [link](https://github.com/mrtnetwork/On_chain/tree/main/lib/ethereum/src/rpc/methds).
```
/// HTTP RPC Service
Expand Down Expand Up @@ -443,7 +598,7 @@ Dive into the Tron Virtual Machine (TVM) networks with confidence. The Onchain P
- Tron Full-Http node
Discover the full spectrum of methods in the [link](https://github.com/mrtnetwork/On_chain/tree/main/lib/tron/provider/methods).
Discover the full spectrum of methods in the [link](https://github.com/mrtnetwork/On_chain/tree/main/lib/tron/src/provider/methods).
```
/// Tron Provider Initialization
Expand All @@ -467,6 +622,62 @@ Dive into the Tron Virtual Machine (TVM) networks with confidence. The Onchain P
/// Get Latest Block Information
final block = await rpc.request(TronRequestGetNowBlock());
```
- Solana RPC
Discover the full spectrum of methods in the [link](https://github.com/mrtnetwork/On_chain/tree/main/lib/tron/provider/methods).
```
/// Initialize the Solana RPC client with the devnet endpoint.
final service = SolanaRPC(RPCHttpService("https://api.devnet.solana.com"));
/// Retrieve the account information for a specific address.
final accountModel = await service.request(const SolanaRPCGetAccountInfo(
account: SolAddress.unchecked(
"527pWSWfeQGLM7SoyVXjCRkrSZBtDkH6ShEBJB3nUDkA")));
/// Retrieve the account information for a specific address with context.
final accountModelWithContext = await service.requestWithContext(
const SolanaRPCGetAccountInfo(
account: SolAddress.unchecked(
"527pWSWfeQGLM7SoyVXjCRkrSZBtDkH6ShEBJB3nUDkA")));
/// Retrieve the account information for a specific address and return as JSON.
final accountResponseInJson = await service.requestDynamic(
const SolanaRPCGetAccountInfo(
account: SolAddress.unchecked(
"527pWSWfeQGLM7SoyVXjCRkrSZBtDkH6ShEBJB3nUDkA")));
```
### BIP-39, Addresses, and HD Wallet Key Management Process
```
/// Generate a 12-word mnemonic phrase.
final mnemonic =
Bip39MnemonicGenerator().fromWordsNumber(Bip39WordsNum.wordsNum12);

/// Generate a seed from the mnemonic phrase with a specific passphrase.
final seed = Bip39SeedGenerator(mnemonic).generate("MRTNETWORK");

/// Define the cryptocurrency coins.
final solanaCoin = Bip44Coins.solana;
final tronCoin = Bip44Coins.tron;
final ethereumCoin = Bip44Coins.ethereum;

/// Derive the default derivation paths for Solana, Tron, and Ethereum.
final solanaDefaultPath = Bip44.fromSeed(seed, solanaCoin).deriveDefaultPath;
final tronDefaultPath = Bip44.fromSeed(seed, tronCoin).deriveDefaultPath;
final ethereumDefaultPath =
Bip44.fromSeed(seed, ethereumCoin).deriveDefaultPath;

/// Generate private keys for Solana, Tron, and Ethereum from their respective derivation paths.
final solanaPrivateKey =
SolanaPrivateKey.fromSeed(solanaDefaultPath.privateKey.raw);
final tronPrivateKey =
TronPrivateKey.fromBytes(tronDefaultPath.privateKey.raw);
final ethereumPrivateKey =
ETHPrivateKey.fromBytes(ethereumDefaultPath.privateKey.raw);

```
## Contributing
Expand Down
26 changes: 24 additions & 2 deletions analysis_options.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,26 @@
# This file configures the analyzer to use the lint rule set from `package:lint`

include: package:lints/recommended.yaml # For production apps
# include: package:lint/casual.yaml # For code samples, hackathons and other non-production code
# include: package:lint/package.yaml # Use this for packages with public API

# Additional information about this file can be found at
# https://dart.dev/guides/language/analysis-options

# You might want to exclude auto-generated files from dart analysis
analyzer:
exclude:
#- '**.freezed.dart'
#- '**.g.dart'

# You can customize the lint rules set to your own liking. A list of all rules
# can be found at https://dart-lang.github.io/linter/lints/options/options.html
linter:
rules:
# Util classes are awesome!
# avoid_classes_with_only_static_members: false

# Make constructors the first thing in every class
# sort_constructors_first: true

# Choose wisely, but you don't have to
# prefer_double_quotes: true
# prefer_single_quotes: true
5 changes: 3 additions & 2 deletions example/lib/example/contract/call_example.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// ignore_for_file: unused_local_variable

import 'package:blockchain_utils/exception/exceptions.dart';
import 'package:example/example/contract/abi.dart';
import 'package:example/example/ethereum/rpc/http_service.dart';
import 'package:on_chain/on_chain.dart';
Expand Down Expand Up @@ -95,7 +96,7 @@ void main() async {
function: contract.functionFromName("checkError"),
params: [],
));
} on RPCException catch (e) {
} on RPCError catch (e) {
final revertErrors = contract.decodeError(e.data);
}

Expand All @@ -112,7 +113,7 @@ void main() async {
]
],
));
} on RPCException catch (e) {
} on RPCError catch (e) {
final revertErrors = contract.decodeError(e.data);
}

Expand Down
2 changes: 1 addition & 1 deletion example/lib/example/eip_712/v4_example.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import 'package:on_chain/abi/abi.dart';
import 'package:on_chain/ethereum/keys/private_key.dart';
import 'package:on_chain/ethereum/src/keys/private_key.dart';

void main() {
/// Create an EIP-712 message
Expand Down
4 changes: 3 additions & 1 deletion example/lib/example/ethereum/rpc/custom_request_example.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import 'package:on_chain/ethereum/ethereum.dart';
import 'package:on_chain/ethereum/src/models/models.dart';
import 'package:on_chain/ethereum/src/rpc/rpc.dart';
import 'package:on_chain/ethereum/src/utils/helper.dart';

import 'http_service.dart';

Expand Down
4 changes: 2 additions & 2 deletions example/lib/example/ethereum/rpc/http_service.dart
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import 'dart:convert';

import 'package:on_chain/ethereum/rpc/core/core.dart';
import 'package:on_chain/ethereum/rpc/core/service.dart';
import 'package:on_chain/ethereum/src/rpc/core/core.dart';
import 'package:on_chain/ethereum/src/rpc/core/service.dart';
import 'package:http/http.dart';

class RPCHttpService with JSONRPCService {
Expand Down
4 changes: 2 additions & 2 deletions example/lib/example/ethereum/rpc/socket_service.dart
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import 'dart:async';
import 'dart:convert';
import 'package:on_chain/ethereum/rpc/core/core.dart';
import 'package:on_chain/ethereum/rpc/core/service.dart';
import 'package:on_chain/ethereum/src/rpc/core/core.dart';
import 'package:on_chain/ethereum/src/rpc/core/service.dart';
import 'package:web_socket_channel/web_socket_channel.dart';

enum WebsocketStatus { connecting, connect, discounnect }
Expand Down
Loading

0 comments on commit 0b07cde

Please sign in to comment.