casper-ecosystem / cep18

Implementation of ERC20 token for the CasperLabs platform.
https://casper.network/docs/dapp-dev-guide
Apache License 2.0
26 stars 47 forks source link

How to send cep18 token to a smart contract? #110

Closed Enflow-io closed 6 months ago

Enflow-io commented 8 months ago

Hello!

Is it possible to keep some token on a smart contract balance? And how to implement such behaviour?

gRoussac commented 6 months ago

Hi @Enflow-io thanks for your contact but we don't give support on github.

You can transfer a token to a contract by wrapping the contract hash into a Casper Key type. https://github.com/casper-ecosystem/cep18/blob/dev/tests/src/transfer.rs#L205-L221 . Casper Client CLI will let you do that.

With the js client you probably need to convert the contract hash to a byte array then to a key.

const convertHashStringToBuffer = (hashStr: string): Buffer => {
  const hashHex = hashStr.startsWith("hash-") ? hashStr.slice(5) : hashStr;
  return Buffer.from(hashHex, "hex");
};
....
// in the transfer request arguments 
    const runtimeArgs = RuntimeArgs.fromMap({});
    if (contract_hash) {
        CLValueBuilder.key(
          CLValueBuilder.byteArray(convertHashStringToBuffer(contract_hash))
        )
     }
....

Please go on the Casper discord https://discord.com/invite/casperblockchain to get adequate support.

Regards