2

I'm trying to build a dapp using the lens protocol and I'm struggling to find the right way to decode the event logs.

Scenario: I'm trying to publish the post using the POST method in the proxy contract LensHub Proxy. with some insights from a person in the lenshub community I was told that I should use the proxy contract for all txns and use the ABI LensHUb Implementation with it. Therefore as you can see in the below code I have used to do the implementation as the community guidelines.

const contractAddress = "0xf6bf84e5df229029c9d36dc7abacdbe9c0bd7b4f"; // Lenshub Proxy
const proxyLensHubAbi = abi; // abi of implementation
const provider = new ethers.providers.JsonRpcProvider(
  "https://polygon-mumbai.g.alchemy.com/v2/key"
);
const contract = new ethers.Contract(
  contractAddress,
  proxyLensHubAbi,
  provider
);
const signer = new ethers.Wallet(
  "key",
  provider
);
const inputStruct = {
  profileId: 169189,
  contentURI:
    "https://ipfs.fleek.co/ipfs/plantghostplantghostplantghostplant",
  collectModule: "0xedeb556d916d9e6e154a083ac18da52cc5ed8b1c",
  collectModuleData: [],
  referenceModule: "0x0000000000000000000000000000000000000000",
  referenceModuleData: [],
};
const signedContract = await contract.connect(signer);
const result = await signedContract.post(inputStruct);
const receipt = await result.wait();
console.log(receipt.events, "events");
console.log("result", receipt);

The problem:

The problem I'm facing here is with the event emitted with this txn. The "Post created" event is emitted where the pubID and other post-related details are returned but because the data returned are all hex I need to decode them. but when decoding what I get is an empty array. What I'm guessing is the event emitted cannot be found in the contracts abi so, therefore, using the abi for decoding is not working. Is this the reason or am I doing anything working? How can I do this?

I'm Just stuck

The code for decoding

const publishingLogicAbi = publishing;
abiDecoder.addABI(publishingLogicAbi);
const decodedLogs = abiDecoder.decodeLogs(receipt.events);
console.log(decodedLogs);

This is a simulation I did using Tenderly https://dashboard.tenderly.co/h_rrishs_work/project/simulator/2ed296f1-8235-4b68-a41f-662fc7ef1f94

smart contracts in tenderly

As you can see there are three contracts participating in this contract.

events

The topic data is decoded and shown here. The blockchain used here is the Mumbai testnet

1
  • The tenderly link requires a login, so it isn't that useful.
    – Ismael
    Commented May 9, 2022 at 15:31

2 Answers 2

3
+100

The ABI posted to Mumbai implementation contract is probably lacking.

Take transaction 0x32e3230a7478315092a04ef57fc2426947ec5031c6977c7b184e7bcd02be9216 for instance, if you look at Mumbai Explorer that calls the same method 0x963ff141 (post), under logs you'll see log #159 which is submitted by contract 0xf6bf84e5df229029c9d36dc7abacdbe9c0bd7b4f (the proxy) - the first topic 0xc672c38b4d26c3c978228e99164105280410b144af24dd3ed8e4f9d211d96a50 is the event emitted.

If you look at the encoded implementation ABI, there's no method with signature matching to this topic, that's why you're not getting anything.

To back this up, looking at the implementation source code, you can see that post(...) calls PublishingLogic.createPost(...) which in turn emits an event PostCreated - this event is missing from the ABI.

If you add the missing PostCreated event to the ABI manually -

{
    "anonymous": false,
    "inputs": [
        { "indexed": true, "internalType": "uint256", "name": "profileId", "type": "uint256" },
        { "indexed": true, "internalType": "uint256", "name": "pubId", "type": "uint256" },
        { "indexed": false, "internalType": "string", "name": "contentURI", "type": "string" },
        { "indexed": false, "internalType": "address", "name": "collectModule", "type": "address" },
        { "indexed": false, "internalType": "bytes", "name": "collectModuleReturnData", "type": "bytes" },
        { "indexed": false, "internalType": "address", "name": "referenceModule", "type": "address" },
        { "indexed": false, "internalType": "bytes", "name": "referenceModuleReturnData", "type": "bytes" },
        { "indexed": false, "internalType": "uint256", "name": "timestamp", "type": "uint256" }
    ],
    "name": "PostCreated",
    "type": "event"
}

decoding will manage to produce a result -

[{"name":"PostCreated","events":[{"name":"profileId","type":"uint256","value":"129"},{"name":"pubId","type":"uint256","value":"26"},{"name":"contentURI","type":"string","value":"https://theagora.vercel.app/posts/6260ca6331de6ff150e48df2"},{"name":"collectModule","type":"address","value":"0xedeb556d916d9e6e154a083ac18da52cc5ed8b1c"},{"name":"collectModuleReturnData","type":"bytes","value":null},{"name":"referenceModule","type":"address","value":"0x0000000000000000000000000000000000000000"},{"name":"referenceModuleReturnData","type":"bytes","value":null},{"name":"timestamp","type":"uint256","value":"1650510457"}],"address":"0xF6BF84E5df229029C9D36dC7ABaCDBE9c0bd7b4F"}]
1

You get empty array because in your abi missing the events abi.

Try this:

const abi = [
    {
        "anonymous": false,
        "inputs": [
            {
                "indexed": true,
                "internalType": "uint256",
                "name": "profileId",
                "type": "uint256"
            },
            {
                "indexed": true,
                "internalType": "uint256",
                "name": "pubId",
                "type": "uint256"
            },
            {
                "indexed": false,
                "internalType": "string",
                "name": "contentURI",
                "type": "string"
            },
            {
                "indexed": false,
                "internalType": "address",
                "name": "collectModule",
                "type": "address"
            },
            {
                "indexed": false,
                "internalType": "bytes",
                "name": "collectModuleReturnData",
                "type": "bytes"
            },
            {
                "indexed": false,
                "internalType": "address",
                "name": "referenceModule",
                "type": "address"
            },
            {
                "indexed": false,
                "internalType": "bytes",
                "name": "referenceModuleReturnData",
                "type": "bytes"
            },
            {
                "indexed": false,
                "internalType": "uint256",
                "name": "timestamp",
                "type": "uint256"
            }
        ],

        name: "PostCreated",
        type: "event",
    }
]
let txrecepit = await provider.eth.getTransactionReceipt(txhash);
abiDecoder.addABI(abi);
var log = abiDecoder.decodeLogs(txrecepit.logs);
console.log(log[0].events);

Response :

[
  { name: 'profileId', type: 'uint256', value: '129' },
  { name: 'pubId', type: 'uint256', value: '24' },
  {
    name: 'contentURI',
    type: 'string',
    value: 'https://theagora.vercel.app/posts/625775f875f4fe66b1e4c53a'
  },
  {
    name: 'collectModule',
    type: 'address',
    value: '0xedeb556d916d9e6e154a083ac18da52cc5ed8b1c'
  },
  { name: 'collectModuleReturnData', type: 'bytes', value: null },
  {
    name: 'referenceModule',
    type: 'address',
    value: '0x0000000000000000000000000000000000000000'
  },
  { name: 'referenceModuleReturnData', type: 'bytes', value: null },
  { name: 'timestamp', type: 'uint256', value: '1649904205' }
]
1
  • You just copied my answer...
    – Kof
    Commented May 6, 2022 at 18:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.