sui icon indicating copy to clipboard operation
sui copied to clipboard

[DynamicField] Make hash_type_and_key public

Open icodezjb opened this issue 3 years ago • 1 comments

This function should be public for smart contracts to use.

sui::bag::add does not return the id of the newly created object.

We hope to calculate the id of the new object through hash_type_and_key and store it in the event.

icodezjb avatar Nov 16 '22 04:11 icodezjb

If sui does not provide a way to traverse table or bag, then should expose hash_type_and_key, make table or bag keys computable on-chain and off-chain.

icodezjb avatar Nov 21 '22 08:11 icodezjb

Could you talk a bit more about what it is you are trying to do? If you look at the underlying implementation of table and bag. They are fairly simple wrappers. You could make a version of table and bag that basically have a linked list of previous/next entries, by wrapping the value to also store these prev/next keys.

But if you want some additional knowledge off chain, we might want to expose that in a different way.

tnowacki avatar Nov 21 '22 21:11 tnowacki

You could make a version of table and bag that basically have a linked list of previous/next entries, by wrapping the value to also store these prev/next keys.

Good idea.

But if you want some additional knowledge off chain, we might want to expose that in a different way.

I just found out that we can get all keys of table and bag using getObjectsOwnedByObject off-chain.

const {JsonRpcProvider} = require("@mysten/sui.js");

(async ()=> {
    const TESTNET_URL = "https://fullnode.testnet.sui.io:443";

    let client = new JsonRpcProvider(TESTNET_URL);
    let bagId = "0x9f7e2cd16df60de1ba4425053a87e284706256db";
    let keys = await client.getObjectsOwnedByObject(bagId);

    console.log(keys);
})()

result

[
  {
    objectId: '0x86d10e108652104d452eb04e69886628f165150f',
    version: 5,
    digest: 'maF5i2c9WnU/Vg2L2LXM1vdPYZWanl4Ep6pgHNadXw0=',
    type: '0x2::dynamic_field::Field<0x1::ascii::String, 0x2::balance::Balance<0x985c26f5edba256380648d4ad84b202094a4ade3::xbtc::XBTC>>',
    owner: { ObjectOwner: '0x9f7e2cd16df60de1ba4425053a87e284706256db' },
    previousTransaction: 'iRboBzZ4y6ThzL2KGnpgC8PKnvDCPt/5m/ulJLRRONs='
  },
  {
    objectId: '0xd834b6228e1d2e47af5da7c842e8f9e4d292af95',
    version: 779,
    digest: 'ZX357up9GpFVUNxWW1IrV0Jl9uukcZcseJ0XkMa888U=',
    type: '0x2::dynamic_field::Field<0x1::ascii::String, 0x2::balance::Balance<0x985c26f5edba256380648d4ad84b202094a4ade3::usdt::USDT>>',
    owner: { ObjectOwner: '0x9f7e2cd16df60de1ba4425053a87e284706256db' },
    previousTransaction: 'RkFevqOUg212OeuN+8MfgMtRgcDUeFmG6vH2Qkg4EI4='
  }
]

icodezjb avatar Nov 22 '22 08:11 icodezjb