Overview
Web3 transactions typically involve interacting with smart contracts on the blockchain, with applications extending to decentralized applications (dApps), non-fungible tokens (NFTs), and beyond. This guide will outline how developers can create Web3 transactions in MPC Wallets using the APIs provided by Cobo.
Note: The code samples below are only applicable to Web3 Wallets under the MPC Wallets.
Code Samples
Initiating a withdraw transaction using Web3 Wallets (e.g., sending 0.01 ETH)
Step 1: Initialize the mpc_client
Step 2: Create the withdraw transaction
from cobo_custody. client. mpc_client import MPCClient
from cobo_custody. config import DEVELOP_ENV
from cobo_custody. signer. local_signer import LocalSigner
import time
api_secret = "your api secret"
chain_code = "ETH"
coin_code = "ETH"
amount = "100000000000000000"
from_address = "your mpc wallet address"
to_address = "to address"
mpc_client = MPCClient( signer= LocalSigner( api_secret) , env= DEVELOP_ENV, debug= False )
request_id = f"MPCTransaction- { int ( time. time( ) * 1000 ) } "
response = mpc_client. create_transaction(
coin= coin_code,
request_id= request_id,
amount= amount,
from_addr= from_address,
to_addr= to_address,
gas_price= 6500000000 ,
gas_limit= 21000 ,
)
print ( f"Withdraw: { response. result} " )
Interacting with a smart contract using Web3 Wallets
Step 1: Initialize the mpc_client
Step 2: Pass in calldata to call a smart contract
from cobo_custody. client. mpc_client import MPCClient
from cobo_custody. config import DEVELOP_ENV
from cobo_custody. signer. local_signer import LocalSigner
import time
api_secret = "your api secret"
chain_code = "ETH"
coin_code = "ETH"
amount = "0"
from_address = "your mpc wallet address"
to_address = "contract address"
extra_parameters = '{"calldata": "0xa8059cbb000000000000000000000000971948873f869e4517311b190d7eb31e30eba0ef000000000000000000000000000000000000000000000000002386f26fc10000"}'
mpc_client = MPCClient( signer= LocalSigner( api_secret) , env= DEVELOP_ENV, debug= False )
request_id = f"MPCTransaction- { int ( time. time( ) * 1000 ) } "
response = mpc_client. create_transaction(
coin= coin_code,
request_id= request_id,
amount= amount,
from_addr= from_address,
to_addr= to_address,
gas_price= 6500000000 ,
gas_limit= 22385 ,
extra_parameters= extra_parameters,
)
print ( f"call contract: { response. result} " )
For more information on the ‘create_transaction’ endpoint, please click here .