api icon indicating copy to clipboard operation
api copied to clipboard

How to add custom parameters to an transaction with API, such as to api.tx.balances.transfer

Open gith-u-b opened this issue 1 year ago • 4 comments

I want to implement payment function(dot、ksm),but I don't know how to add custom parameters to an transaction with API, such as to api.tx.balances.transfer, thanks.

gith-u-b avatar Jul 25 '22 02:07 gith-u-b

such as: name and duration params

server.post<{ Body: { name: string, to: string, duration: string } }>("/worker/sendDot", async (request, reply) => {
    let { name, to, duration } = request.body
    const keyring = new Keyring({ type: 'sr25519' });
    const alice = keyring.addFromUri('//Alice');
    console.log('alice.address========', alice.address);

    const cost = 1;

    const txHash = await api.tx.balances
    .transfer(to, cost)
    .signAndSend(alice);

    return { txHash: txHash }
  });

gith-u-b avatar Jul 25 '22 02:07 gith-u-b

substrate doesn't have the concept of random attributes on an extrinsic. Best you can do id send a batch with a transfer and a remark.

jacogr avatar Jul 25 '22 09:07 jacogr

基板没有外部随机属性的概念。最好你可以发送一个带有转移和备注的批次。

I see a issue: https://github.com/polkadot-js/api/issues/3363

So I try:

const txHash = api.tx.utility.batchAll([
      api.tx.balances.transfer(to, price),
      api.tx.system.remark([name, duration])
    ]).signAndSend(alice)

use batch or batchAll is error:

Cannot read properties of undefined (reading 'batchAll')

gith-u-b avatar Jul 26 '22 02:07 gith-u-b

I try:

const hash = await Promise.all([
  api.tx.balances.transfer(polkaPnsAddr, amount * 1e10).signAndSend(alice),
  api.tx.system.remark('i am a remark')
])

is ok

gith-u-b avatar Jul 27 '22 07:07 gith-u-b

This issue has been open for 21 days with no activity and is not labelled as an enhancement. It will be closed in 7 days.

polkadot-js-bot avatar Aug 17 '22 08:08 polkadot-js-bot

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue if you think you have a related problem or query.

polkadot-js-bot avatar Aug 31 '22 10:08 polkadot-js-bot