JSFiddle - React, Tailwind, and code Playground
by marvinirwin
JavaScript
/*const result = await caver.klay.call({
to: ,
data: caver.klay.abi.encodeFunctionCall(
{
name: 'emit_message',
type: 'function',
inputs: ,
},
),
});*/
const contractAddress = "0x9D7EeFEB01D7bAb375cDf254056e0204ac3Bcb41";
const sendTransactionWithKlay = ({
caver,
account,
functionName,
inputs,
contractAddress,
gas,
value,
params,
}) => {
const encodedFunctionCall = caver.klay.abi.encodeFunctionCall(
{
name: functionName,
type: 'function',
inputs: inputs,
},
params
)
return caver.klay.sendTransaction({
type: 'SMART_CONTRACT_EXECUTION',
from: account,
to: contractAddress,
value: value,
gas: gas,
data: encodedFunctionCall,
})
}
const str2ab = (str) => {
var buf = new ArrayBuffer(str.length*2); // 2 bytes for each char
var bufView = new Uint16Array(buf);
for (var i=0, strLen=str.length; i < strLen; i++) {
bufView[i] = str.charCodeAt(i);
}
return buf;
};
const emitMessage = async (content) => {
return new Promise((resolve, reject) => {
const ret = sendTransactionWithKlay({
caver,
account: "0x58e0cc862c13249cbfad5b47f10a0f456cda1470", //(await klaytn.enable())[0]
functionName: "emit_message",
inputs: [
{
type: 'string',
name: 'message_hash',
},
],
contractAddress: contractAddress,
gas: 80000,
params: [content]
}).once('receipt', resolve);
})
};
const verifyTransactionSigned = async ({account}) => {
const contract = new caver.klay.Contract([ { "inputs": [], "stateMutability": "nonpayable", "type": "constructor" }, { "anonymous": false, "inputs": [ { "indexed": true, "internalType": "address", "name": "_from", "type": "address" }, { "indexed": true, "internalType": "string", "name": "message", "type": "string" }, { ...