The Tomo Telegram SDK provides the SUI provider for getting addresses, signing, and sending transactions.
Copy // get provider
const sui= window.tomo_sui;
Before using the provider, the user needs to log in to the wallet through the modal by following the method:
Copy // connect wallet
await sui?.connectWallet();
Copy const address = sui.getAddress();
Copy const response = await sui.signMessage(signMsg);
Copy // define transaction body
export const getSendSuiCoinTx = async ({
fromAddress,
toAddress,
amount,
coinType,
}: {
fromAddress: string;
toAddress: string;
amount: string;
coinType?: string;
}): Promise<{ transaction: string; bytes: Uint8Array } | null> => {
const suiClient = getSuiClient();
const txb = new Transaction();
txb.transferObjects(
[coinWithBalance({ balance: Number(amount), type: coinType || undefined })],
toAddress
);
try {
txb.setSender(fromAddress);
const transaction = (await txb.toJSON({ client: suiClient })).toString();
const bytes = await txb.build({ client: suiClient });
console.log('bytes', transaction, bytes);
return { transaction, bytes };
} catch (e) {
console.warn(e);
return null;
}
};
// create transaction body for concrete transaction
const res = await getSendSuiCoinTx({
fromAddress: addr,
toAddress: toAddr2,
amount: +toValue2 * 10 ** 6 + '',
coinType: contractAddr,
});
// create transaction
const { transaction, bytes } = res || {};
const input = {
transactionBlock: transaction,
chain: 'sui:mainnet',
txBytes: bytes,
options: {
showEffects: true,
},
};
// sign transaction
const signed transaction= await sui.signTransaction(input);
// or you can use sign and send transaction at the same time
const res = await sui.signAndExecuteTransaction(input);
After signing the transaction, you can send it to your client or add other signatures. In addition, you can use signAndExecuteTransaction
the method to send the signed transaction directly.