Skip to content

3. Connector

LastCeri edited this page Nov 27, 2023 · 4 revisions

What is a Connector in StarkSharp?

Once you have defined your platform, the next step is to create a connector. The connector uses platform specific methods to interact with Starknet.

Creating a Connector

To create a connector in StarkSharp, follow the steps below:

Import Necessary Libraries

Ensure you've imported the required StarkSharp libraries.

using StarkSharp.Connectors;

Instantiate a Connector

Use the Connector class to create an instance of the connector. This class requires the platform instance as its argument.

Connector connector = new Connector(myNewPlatform);

With the code above, you've created a connector object that uses the myNewPlatform platform.

Connect Wallet

To connect a wallet, use connector.ConnectWallet function which expects wallet type, wallet connection success/error actions.

ArgentX

connector.ConnectWallet(WalletType.ArgentX,
    (successMessage) => OnWalletConnectionSuccess(successMessage),
    (errorMessage) => OnWalletConnectionError(errorMessage));

Braavos

connector.ConnectWallet(WalletType.Braavos,
    (successMessage) => OnWalletConnectionSuccess(successMessage),
    (errorMessage) => OnWalletConnectionError(errorMessage));

Supported Wallet Types: ArgentX, Braavos

Create Actions for Success and Error

public void OnWalletConnectionSuccess(string message)
{
    connector.DebugMessage("On Wallet Connection Success: " + message);
}

public void OnWalletConnectionError(string message)
{
    connector.DebugMessage("On Wallet Connection Error: " + message);
}

Send Transaction

To send a transaction, use connector.SendTransaction function which expects a TransactionInteraction class instance. If you are sending in ERC20 or ERC720 standarts, you can call ERC20Standart or ERC721Standart available functions with ERC20Standart.[Function] or ERC721Standart.[Function].

connector.SendTransaction(
    ERC20Standart.TransferToken(contractAddress, recipientAddress, amount),
    (successMessage) => OnSendTransactionSuccess(successMessage),
    (errorMessage) => OnSendTransactionError(errorMessage));

In order to send a custom transaction, you need to create a TransactionInteraction class instance first.

TransactionInteraction transactionInteraction = new TransactionInteraction(senderAddress, contractAddress, functionName, functionArgs, cairoVersion, maxFee, chainId, privateKey, version);

Then you can give contract interaction to the SendTransaction function as a parameter.

connector.SendTransaction(
    transactionInteraction,
    (successMessage) => OnSendTransactionSuccess(successMessage),
    (errorMessage) => OnSendTransactionError(errorMessage));

Create Actions for Success and Error

public void OnSendTransactionSuccess(string message)
{
    connector.DebugMessage("On Send Transaction Success: " + message);
}

public void OnSendTransactionError(string message)
{
    connector.DebugMessage("On Send Transaction Error: " + message);
}

Call Contract

To call a contract, use connector.CallContract function which expects a ContractInteraction class instance. If you are calling in ERC20 or ERC720 standarts, you can call ERC20Standart or ERC721Standart available functions with ERC20Standart.[Function] or ERC721Standart.[Function].

connector.CallContract(
    ERC20Standart.BalanceOf(contractAddress, walletAddress),
    (successMessage) => OnCallContractSuccess(successMessage),
    (errorMessage) => OnCallContractError(errorMessage));

In order to call contract for a non-standart contract, you need to create a ContractInteraction class instance first.

ContractInteraction contractInteraction = new ContractInteraction(contractAddress, entryPoint, callDataJson);

Then you can give contract interaction to the CallContract function as a parameter.

connector.CallContract(
    contractInteraction,
    (successMessage) => OnCallContractSuccess(successMessage),
    (errorMessage) => OnCallContractError(errorMessage));

Create Actions for Success and Error

public void OnCallContractSuccess(string message)
{
    connector.DebugMessage("On Call Contract Success: " + message);
}

public void OnCallContractError(string message)
{
    connector.DebugMessage("On Call Contract Error: " + message);
}
Clone this wiki locally