Skip to main content

Create a transaction

Overview

This guide describes how to create and send transactions to the blockchain using MPC (multi-party computation).

In the MPC Solution, every transaction is created through an MPC SDK call. The SDK supports the following operation types:

  • Transfer: the default type, used for transferring assets like RON and ERC20 tokens from one source to one destination.
  • Contract call: Directly call any method of a smart contract.
  • Sign: Sign a message with the standard Ethereum prefix.
  • Approve: Approve an ERC20 asset to be spent on another address.

When sending a transaction, you can choose how you want to manage gas fees: let the SDK estimate the fees automatically, or estimate them manually before sending the transaction. For more information, see Estimate gas.

All operations with transactions return a transaction hash. After obtaining it, you need to confirm that the transaction is successful before any further handling.

Prerequisites

  • Complete the steps in Create an MPC wallet.
  • Register the smart contracts with which your game needs to interact. All requests to unregistered contracts are denied.
  • For new developers, the system sets payment limits for specific ERC20 tokens and native tokens. You can increase these limits by implementing additional security measures, such as MFA (multi-factor authentication).

Transfer RON

Transfer the RON token from one address to another.

To transfer RON using mpcCore, call the transferNativeToken function.

const txData = await mpcCore.transferNativeToken(accessToken, privateKeyShard, txJSON);

You can choose to manually construct a txJSON object based on the EVM (Ethereum Virtual Machine) standard, or let the SDK fill in the transaction parameters automatically. For more information, see Transaction JSON.

This example shows how to transfer 10 RON to another address:

const accessToken = `${ACCESS_TOKEN}`;
const privateKeyShard = `${PRIVATE_KEY}`;
const receiver = "0xcd3cf91e7f0601ab98c95dd18b4f99221bcf0b20";
const amount = "0x8ac7230489e80000"; // 10 RON
const toAddress = "0xcd3cf91e7f0601ab98c95dd18b4f99221bcf0b10";
const txJSON = {
to: toAddress,
value: amount
};

const txHash = await mpcCore.transferNativeToken(accessToken, privateKeyShard, txJSON);
console.log(txHash); // Transaction hash

Transfer ERC20 token

Transfer an ERC20 token, such as AXS, from one address to another.

To transfer an ERC20 token using mpcCore, call the transferERC20Token function:

const txData = await mpcCore.transferERC20Token(accessToken, privateKeyShard, contractAddress, receiverAddress, amount)

This example shows how to transfer 1 AXS to another aaddress:

const accessToken : string = `${ACCESS_TOKEN}`;
const privateKeyShard : string = `${PRIVATE_KEY}`;
const contractAddress : string = "0x3c4e17b9056272ce1b49f6900d8cfd6171a1869d"; // AXS contract
const amount : string = "0xde0b6b3a7640000"; // 1 AXS
const address : string = '0xcd3cf91e7f0601ab98c95dd18b4f99221bcf0b20';

const txData = await mpcCore.transferERC20Token(accessToken, privateKeyShard, contractAddress, receiverAddress, amount);

Approve ERC20 token

Authorize a smart contract to spend a specific amount of an ERC20 token, such as AXS.

To approve an ERC20 token using mpcCore, call the approveERC20Token function:

const approveTx = await mpcCore.approveERC20Token(accessToken, privateKeyShard, contractAddress, spenderAddress, amount);

This example shows how to approve a smart contract to spend 1 AXS:

const accessToken : string = `${ACCESS_TOKEN}`;
const privateKeyShard : string = `${PRIVATE_KEY}`;
const contractAddress : string = "0x3c4e17b9056272ce1b49f6900d8cfd6171a1869d"; // AXS contract
const spenderAddress : string = "0xcd3cf91e7f0601ab98c95dd18b4f99221bcf0b20";
const amount : string = "0xde0b6b3a7640000"; // 1 AXS

const approveTx = await mpcCore.approveERC20Token(accessToken, privateKeyShard, contractAddress, spenderAddress, amount);

Call contract function

You can call any function of a smart contract using the MPC SDK.

To invoke a contract call using mpcCore, call the callContract function:

const contract = await mpcCore.callContract(accessToken, privateKeyShard, txJSON);
// Call function in smart contract
contract.functionName(args);

You can choose to manually construct a txJSON object based on the EVM standard, or let the SDK fill in the transaction parameters automatically. For more information, see Transaction JSON.

This example shows how to call a function (submit_numbers(uint8[],uint256[])) from a contract with specific parameters (submit_numbers([12],[1])):

const accessToken = `${ACCESS_TOKEN}`;
const privateKeyShard = `${PRIVATE_KEY}`;

const contractAddress = "0xaeD7F0FaEc15981835aaAeEd9c8D21327385743e";
const ABI = {
...
{
inputs: [
{
internalType: "uint8[]",
name: "numbers",
type: "uint8[]",
},
{
internalType: "uint256[]",
name: "points",
type: "uint256[]",
},
],
name: "submit_numbers",
outputs: [],
stateMutability: "payable",
type: "function"
},
...
};
const voidSigner = new ethers.VoidSigner(contractAddress, mpcProvider);
const contract = new ethers.Contract(contractAddress, ABI, voidSigner);
const unsignTx = await contract.populateTransaction.submit_numbers([1], [1]);

const txJSON = {
"data": unsignTx.data, // unsignTx.data return data hex value from ABI transactsion
"gas": ..,
"gasPrice": ...,
"nonce": ...,
"to": ${Contract_Addess},
"value": "0xde0b6b3a7640000", // 1 RON
"r": "0x0",
"v": "0x0",
"s": "0x0",
"chainId": ...,
};

const txData = await mpcCore.callContract(accessToken, privateKeyShard, txJSON);
console.log(tx.data.txHash);

Sign message

The MPC SDK exclusively supports signing string-type messages.

To sign a message using mpcCore, use the private key shard correspondent to the public address that you generated in Create an MPC wallet, as well as the user's access token after they signed in with their Sky Mavis account.

const result = await mpcCore.signMessage(accessToken, privateKeyShard, message);

Example:

const accessToken = `${ACCESS_TOKEN}`;
const privateKeyShard = `${PRIVATE_KEY}`;
// "Sign this message to breed Axie #223495 with Axie #123232"
const encodedMessage = "U2lnbiB0aGlzIG1lc3NhZ2UgdG8gYnJlZWQgQXhpZSAjMjIzNDk1IHdpdGggQXhpZSAjMTIzMjMy";
const result = await mpcCore.signMessage(accessToken, privateKeyShard, encodedMessage);
//...

Next steps

Recover a wallet from backup

Was this helpful?
Happy React is loading...