π IntegrationsWagmi - Viem Viem Viem version of the connect sdk
Copy npm i @cometh/connect-sdk-viem wagmi viem
Init a Connect Viem Client
After creating (or reconnecting) to a connect wallet, you can now create a connectViemClient:
Copy import {
ComethWallet ,
ConnectAdaptor ,
SupportedNetworks
} from '@cometh/connect-sdk'
import { getConnectViemClient } from '@cometh/connect-sdk-viem'
const walletAdaptor = new ConnectAdaptor ({
chainId : SupportedNetworks . POLYGON ,
apiKey : API_KEY
})
const wallet = new ComethWallet ({
authAdapter : walletAdaptor ,
apiKey : API_KEY ,
rpcUrl : RPC_URL
})
await wallet .connect ()
const connectViemClient = getConnectViemClient (wallet)
Client methods
All Public actions available through a Public viem Client are available. (see viem docs ). On top of that, we added 2 custom functions that you can call through the connectViemClient:
Copy import { encodeFunctionData } from 'viem'
const txCallData = encodeFunctionData ({
abi : CONTRACT_ABI ,
functionName : CONTRACT_METHOD
})
const tx = { to : DESTINATION , value : VALUE , data : txCallData }
const hash = await connectViemClient .sendTransaction (tx)
Copy const txBatch = [
{ to : DESTINATION , value : VALUE , data : txCallData } ,
{ to : DESTINATION , value : VALUE , data : txCallData }
]
const hash = await connectViemClient .sendTransaction (txBatch)
Copy const receipt = await connectViemClient .getTransaction (hash)
Copy import { getContract } from 'viem'
const connectViemClient = getConnectViemClient (wallet)
const counterContract = getContract ({
address : COUNTER_CONTRACT_ADDRESS ,
abi : countContractAbi ,
walletClient : connectClient
});
const hash = await counterContract . write .count ();
Init a Connect Viem Account
Copy import {
ComethWallet ,
ConnectAdaptor ,
SupportedNetworks
} from '@cometh/connect-sdk'
import { getConnectViemAccount } from '@cometh/connect-sdk-viem'
const walletAdaptor = new ConnectAdaptor ({
chainId : SupportedNetworks . POLYGON ,
apiKey : API_KEY
})
const wallet = new ComethWallet ({
authAdapter : walletAdaptor ,
apiKey : API_KEY ,
rpcUrl : RPC_URL
})
await wallet .connect ()
const connectViemAccount = getConnectViemAccount (wallet)
Account Methods
Copy const signature = await connectViemAccount .signMessage ({ message : 'hello' })
Copy const tx = { to : DESTINATION , value : VALUE , data : txCallData }
const signature = await connectViemAccount .signTransaction (tx)
Last updated 6 months ago