sdk icon indicating copy to clipboard operation
sdk copied to clipboard

Difference between one and 0x address when calling transfer and contract methods respectively.

Open mattlockyer opened this issue 5 years ago • 3 comments

Two functions: one to transfer ONE tokens and the other to transfer HRC-20 (HRC) smart contract tokens.

I feel like developers will have some trouble switching between bech32Address and address formats and fields in our SDK.

Is there any way to abstract away the ambiguity? Feel free to chat with me on discord mattlock.

export const transferONE = ({ amount, address }) => async (dispatch, getState) => {
    const tx = hmy.transactions.newTx({
        to: address,  // <--- address is of one format
        value: new hmy.utils.Unit(amount).asEther().toWei(),
        gasLimit: '210000',
        shardID: 0,
        toShardID: 0,
        gasPrice: new hmy.utils.Unit('10').asGwei().toWei(),
    });
    ...
}

export const transferHRC = ({ amount, address }) => async (dispatch, getState) => {
    const tx = contract.methods.transfer(address, parseInt(amount)).send({
        from: active.address, // <--- address is of 0x format
        gasLimit: '1000000',
        gasPrice: new hmy.utils.Unit('10').asGwei().toWei(),
    })
    ...
}

mattlockyer avatar Dec 16 '19 16:12 mattlockyer

in newTx. We accept both 0xchecksumed and bech32 addr. In contract params, currently it supports 0xchecksumed addr.

To convert address format for now. we can use crypto.getAddress(addr).checksum to convert.

I can convert the address internally when constructing the params. However there are risks such as user input raw base16(without checksumed).

I need further discussion about this issue, because currently encoding solidity contract is using eth‘s method.

neeboo avatar Dec 17 '19 12:12 neeboo

one more thing.


const tx = contract.methods.transfer(address, parseInt(amount)).send({
        from: active.address, // <--- address is of 0x format
        gasLimit: '1000000',
        gasPrice: new hmy.utils.Unit('10').asGwei().toWei(),
    })

The from here is not necessarily needed. Because when the txn is signed, it will be replaced as signer’s address.

neeboo avatar Dec 17 '19 18:12 neeboo

@mattlockyer would you try this branch to see if it works for you.

https://github.com/harmony-one/sdk/tree/neeboo/contract

neeboo avatar Dec 18 '19 09:12 neeboo