-
Notifications
You must be signed in to change notification settings - Fork 63
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Interact with Smart Contrat #246
Comments
will follow up |
Hello! Tokens are smart contracts so in order to do what you are asking for you should leverage the transfer/transferFrom functions from Privateum smart contract https://bscscan.com/address/0xb10be3f4c7e1f870d86ed6cfd412fed6615feb6f#writeContract As per how to interact with smart contracts, there's no direct way on our SDK but you can do it through Here it is an example directly from Here it is an example from Celo academy https://celo.academy/t/interact-with-celo-blockchain-using-web3dart/183 And here it is a really simple example on how to read from a Contract using I hope this helps for now. We are working on an more user friendly interaction with contracts from our own SDKs! |
Thank you for your response and solution that you suggested., How can we avoid using private key? |
Hello @david-dyn! As of today, the only way to interact with a Smart Contract is through web3dart, which indeed uses private key. We will start soon to work on our own way to interact with Smart Contract without relying on web3dart. In the meantime you can refer to this Bob's answer here as well #219 (comment) |
So sorry, but why I can't user WalletConnectEip155Credentials? Can I know what your package is? |
I was integrated WalletConnectEip155Credentials in wallet connect example project, and it worked perfectly, web3dart package. |
I tried, but it does not direct me to metamask with w3mService.launchConnectedWallet();
|
Before transaction you need to connect wallet to your dApp, you can check example project. |
I don't see the comment quoted by @Mash-Woo anymore, wasn't it good? |
I was delete it, tomorrow I will refactor it, that everyone can understand it 🙃 |
Thank you. Now, I fixed a bit and it worked with my code, but do you know how to let user automatically change the chain to the chain that deployed the contract. My contract is on Arbitrum but when I click send transaction it often direct user to Ethereum chain |
Hi @quetool , I tried many different ways, but if I use web3dart to send transactions, how can I get user's private key with web3modal, I am seeing that I can't get the user's private key if they use a decentralized wallet such as Metamask |
class CustomCredentialsSender extends CustomTransactionSender {
CustomCredentialsSender({
required this.signingEngine,
required this.sessionTopic,
required this.chainId,
required this.credentialsAddress,
});
final ISignEngine signingEngine;
final String sessionTopic;
final String chainId;
final EthereumAddress credentialsAddress;
@override
EthereumAddress get address => credentialsAddress;
@override
Future<String> sendTransaction(Transaction transaction) async {
if (kDebugMode) {
print(
'CustomCredentialsSender: sendTransaction - transaction: ${transaction.prettyPrint}');
}
if (!signingEngine.getActiveSessions().keys.contains(sessionTopic)) {
if (kDebugMode) {
print(
'sendTransaction - called with invalid sessionTopic: $sessionTopic');
}
return 'Internal Error - sendTransaction - called with invalid sessionTopic';
}
SessionRequestParams sessionRequestParams = SessionRequestParams(
method: 'eth_sendTransaction',
params: [
{
'from': transaction.from?.hex ?? credentialsAddress.hex,
'to': transaction.to?.hex,
'data':
(transaction.data != null) ? bytesToHex(transaction.data!) : null,
if (transaction.value != null)
'value':
'0x${transaction.value?.getInWei.toRadixString(16) ?? '0'}',
if (transaction.maxGas != null)
'gas': '0x${transaction.maxGas?.toRadixString(16)}',
if (transaction.gasPrice != null)
'gasPrice': '0x${transaction.gasPrice?.getInWei.toRadixString(16)}',
if (transaction.nonce != null) 'nonce': transaction.nonce,
}
],
);
if (kDebugMode) {
print(
'CustomCredentialsSender: sendTransaction - blockchain $chainId, sessionRequestParams: ${sessionRequestParams.toJson()}');
}
final hash = await signingEngine.request(
topic: sessionTopic,
chainId: chainId,
request: sessionRequestParams,
);
return hash;
}
@override
Future<EthereumAddress> extractAddress() {
// TODO: implement extractAddress
throw UnimplementedError();
}
@override
Future<MsgSignature> signToSignature(Uint8List payload,
{int? chainId, bool isEIP1559 = false}) {
// TODO: implement signToSignature
throw UnimplementedError();
}
@override
MsgSignature signToEcSignature(Uint8List payload,
{int? chainId, bool isEIP1559 = false}) {
// TODO: implement signToEcSignature
throw UnimplementedError();
}
} You can use this custom credentials class instead of using private key, you need to create this class object in your transfer function like this: Credentials credentials = CustomCredentialsSender(
signingEngine: w3mService.web3App!.signEngine,
sessionTopic: w3mService.session!.topic!,
chainId: w3mService.selectedChain!.namespace,
credentialsAddress: EthereumAddress.fromHex(w3mService.session!.address!),
); After it you can use this credentials in your sendTransaction method. |
Thank you, I tested it, and it can be sent. However, instead of sending my token according to my contract, it sends Eth. And besides that, if the user rejects it, it will cause bugs. Exception has occurred. |
Are you use contract abi, and your custom token contract address? Can you share your transfer method code here? |
Yes, I tried to use contract details like you, but it does not work for me, so now I am using this final scAddress = EthereumAddress.fromHex(dotenv.env['WEB3_SC_ADDRESS']!);
Future<void> transferWalletConnect() async {
String abi = await rootBundle.loadString('backend/contract/namecontract.abi.json');
final contract =
DeployedContract(ContractAbi.fromJson(abi, 'nameToken'), scAddress);
final transfer = contract.function('safeTransferFromFee'); //function from the contract
log('Check contract ${contract.address}');
Credentials credentials = CustomCredentialsSender(
signEngine: w3mService.web3App!.signEngine,
sessionTopic: w3mService.session!.topic!,
chainId: w3mService.selectedChain!.namespace,
credentialAddress:
EthereumAddress.fromHex(w3mService.address!));
final transaction = Transaction.callContract(
contract: contract,
function: transfer,
parameters: [
EthereumAddress.fromHex('$myaddress'),
EthereumAddress.fromHex('$receiveraddress'),
BigInt.parse(authProvider.userData!.profile!.mintId!.toString()),
BigInt.one,
Uint8List.fromList([])
]);
log('Check credentials $credentials');
log('Check transaction $transaction');
final result = ethClient.sendTransaction(credentials, transaction,
chainId: int.parse(w3mService.selectedChain!.chainId));
w3mService.launchConnectedWallet();
w3mService.addListener(() {
result;
});
w3mService.notifyListeners();
} |
if your abi is json, use
And try to use w3mService.session!.chainId instead of w3mService.selectedChain!.chainId if these steps don't help you, please share your contract address of token I will try reproduce it. |
Somehow my package only can use w3mService.selectedChain!.chainId or maybe I will set a String such as '42161' the chain id of Arbitrum |
Where is your ethClient init? And can you share rpcUrl that you use? |
I put it in main, my rpcUrl, I am facing this Error Exception has occurred. |
Your chain id is 42161, use it instead of eip155:42161 |
I changed but still facing this problem |
@Mash-Woo could you share the contract address and your code where you send the transaction? |
This is my set-up config void initializedW3MService() async {
W3MChainPresets.chains.putIfAbsent(
'eip155:${dotenv.get('CHAIN_ID')}',
() => W3MChainInfo(
chainName: 'Arbitrum Sepolia Testnet',
chainId: dotenv.get('CHAIN_ID'),
namespace: 'eip155:${dotenv.get('CHAIN_ID')}',
tokenName: 'ETH',
rpcUrl: 'https://sepolia-rollup.arbitrum.io/rpc',
blockExplorer: W3MBlockExplorer(
name: 'Arbitrum Sepolia Testnet',
url: 'https://sepolia.arbiscan.io/',
),
),
);
await w3mService.init();
w3mService.notifyListeners();
} I just want to add this config and use it, it can work on metamask but not on trust wallet |
In Metamask, I am facing this #176 |
I meant a whole reproducible code, including contract Abi, contract address, and everything that could be useful to test. it's pretty difficult otherwise. |
Also, is your custom chain (Arbitrum Sepolia) added in your Wallet? If not, are you requesting |
I added it to my wallet |
Hmm, actually I completed solving this, and now my bug is Exception has occurred. _$WalletConnectErrorImpl (WalletConnectError(code: 5100, message: Unsupported chains. The chain eip155:421614 is not supported, data: null)) It cause when I work with Trust Wallet, except for Metamask |
It means Trust wallet does not support that chain. Try a different wallet or a different chain on Trust. |
Thank you, I changed to another network and it worked, but sometime, if user rejected transaction, I will meet this problem Exception has occurred. |
Indeed that means the user rejected the request |
Would you happen to have any solution, such as if the user rejects the request, it will redirect to my app? |
It's up to the wallet to redirect to your dapp. You can't do anything from your dapp to get back to your dapp from the wallet. In any case I just realized you are implementing Web3Modal and this is WalletConnectFlutterV2 repo so please if you have any other issue open a new one in the proper repository. Thanks! |
Hello all! Glad to announce that a new beta is out for WalletConnectFlutterV2 with Smart Contract interaction based on the proposed solution from @bobwith2bees. Thank you Bob! And thank you @Rudo-dyn for bringing this to my attention! For any issue with this beta please follow up on this new thread In the PR description you will find how to interact with the new functionality, hopefully it's easy enough. For the time being, if you are willing to try this with Web3Modal (such as your case @Mash-Woo), you'll need to override dependency_overrides:
walletconnect_flutter_v2: ^2.2.0-beta01 And then make your requests leveraging return w3mService.web3App!.requestWriteContract(.....)
// OR
return w3mService.web3App!.requestReadContract(.....) Bear in mind that this is a beta release and things might slightly change in the near future. |
Hello
I need to send transaction with currency other then default in network
![IMG_20240110_103401](https://private-user-images.githubusercontent.com/71083267/295472042-7f42d987-d093-47e4-814c-bd42b798f2b0.jpg?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3MzkzMDE0MTUsIm5iZiI6MTczOTMwMTExNSwicGF0aCI6Ii83MTA4MzI2Ny8yOTU0NzIwNDItN2Y0MmQ5ODctZDA5My00N2U0LTgxNGMtYmQ0MmI3OThmMmIwLmpwZz9YLUFtei1BbGdvcml0aG09QVdTNC1ITUFDLVNIQTI1NiZYLUFtei1DcmVkZW50aWFsPUFLSUFWQ09EWUxTQTUzUFFLNFpBJTJGMjAyNTAyMTElMkZ1cy1lYXN0LTElMkZzMyUyRmF3czRfcmVxdWVzdCZYLUFtei1EYXRlPTIwMjUwMjExVDE5MTE1NVomWC1BbXotRXhwaXJlcz0zMDAmWC1BbXotU2lnbmF0dXJlPTg4NTk3ODRhNjI0N2EzNjI4ZDg5YzJiZDk2MDBkYzk3ZjQzNjJmZjVlYTIyYTEyZDAzMDRjZWI4MmI0ODk4YmEmWC1BbXotU2lnbmVkSGVhZGVycz1ob3N0In0.Zlp1n12pHHgMUzrucn07teDBJoJUepZ6Jv5VWO17sVo)
For example Pri in Binance Smart Chain
I need to send 0.001 PRI not 0.001 BNB
The text was updated successfully, but these errors were encountered: