I’m facing a issue when trying to call contract method “send”.
What I understand is that I can send a transaction to call un function on my contract with a caver.contract instance. I works correctly for a “call” method, but no chance with a “send” method.
In order to avoid this error, I have to create a transaction from scratch with my input data. But I suppose that I missing something.
Thank you.
Here the code:
const caver = new Caver(new Caver.providers.HttpProvider("https://node-api.klaytnapi.com/v1/klaytn", option));
caver.klay.accounts.wallet.add(myPrivateKey, "0xef4aa0fbbfcd09b23980a582034a5302c9c5851e");
runContract();
async function runContract() {
//instance of a new contract object
//getABI() return json ABI of contract
const contractInstance = new caver.contract(getABI(), "0x37aE77269B244304480296d23DEFaa01BA644D6E");
//call non-modifier function
contractInstance.methods.symbol().call().then(console.log);
//>>Success: return "mySymbol"
//***** First method (NOT WORKING): use built-in function send of caver.contract *****//
//call modifier function (create an entry with an address and a name)
contractInstance.methods.awardItem("0xeF4Aa0fbBFcD09b23980A582034A5302c9c5851e", "test2").send({from: "0xeF4Aa0fbBFcD09b23980A582034A5302c9c5851e", gas: 2000000}).then(console.log);
//>> Error: {"code":1034210,"message":"Unsupported method - klay_sendTransaction"}
//***** Second method (WORKING): create a transaction from scratch *****//
//Get ABI Input data
const methodInput = contractInstance.methods.awardItem("0xeF4Aa0fbBFcD09b23980A582034A5302c9c5851e", "test3").encodeABI();
//Write sendTransation with data
caver.klay.sendTransaction({
from: '0xef4aa0fbbfcd09b23980a582034a5302c9c5851e',
to: '0x37aE77269B244304480296d23DEFaa01BA644D6E',
gas: 2000000,
input: methodInput
}).then(function(receipt){
console.log(receipt);
});
//>>Success
};
From caver-js-ext-kas v1.0.2, Contract, KIP7, and KIP17 all operate using KAS Wallet API. Therefore, 0xef4aa0fbbfcd09b23980a582034a5302c9c5851e account must be an account managed inside the KAS Wallet API service.
If you want to send via contract with the address and private key that you are managing yourself, you need to change the wallet used in the contract instance. Please change the source code based on the code below.
And the code uploaded above is a mix of old and new caver code. In this case, it may not work properly. The code below is written using the latest code that is newly supported after the common architecture.
// caver.contract will work with KAS Wallet API or KeyringContainer.
const keyringContainer = new caver.keyringContainer()
const keyring = keyringContainer.keyring.createWithSingleKey('0xeF4Aa0fbBFcD09b23980A582034A5302c9c5851e', myPrivateKey)
keyringContainer.add(keyring)
const contract = new caver.contract(getABI(), '0x37aE77269B244304480296d23DEFaa01BA644D6E')
contract.methods.symbol().call().then(console.log)
// Use KeyringContainer instead of KAS Wallet API
// If you want to use an account in KAS Wallet API, then please skip this. (also keyringContainer code above)
contract.setWallet(keyringContainer)
contract.methods.awardItem('0xeF4Aa0fbBFcD09b23980A582034A5302c9c5851e', 'test2').send({from: '0xeF4Aa0fbBFcD09b23980A582034A5302c9c5851e', gas: 2000000}).then(console.log)
If you have additional questions, please feel free to leave a question. Thanks =)
Hi Julien.
i exactly had the same error with you
i’m also using caver-js, not caver-js-ext-kas and i create accounts address by using kaikas wallet.
i don’t want to use kas!! im working with kaikas wallet.
so, would you tell me how to fixed it please??
this groundx team doesn’t show me the way how to do it ;-(
Hi, what i replied above is only for caver-js-ext-kas which is KAS SDK.
So for you, you need to approach with different way.
Please upload the question with guide line for accurate reply.
And also there is sample project, so please refer to this.