Skip to content

Commit

Permalink
fix: null error in unlock transfer handler
Browse files Browse the repository at this point in the history
  • Loading branch information
kyriediculous committed Jun 7, 2024
1 parent d231744 commit 2517406
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 18 deletions.
24 changes: 10 additions & 14 deletions packages/subgraph/src/mappings/helpers.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { BigDecimal, BigInt, Address, ByteArray, Bytes, ethereum } from '@graphprotocol/graph-ts'
import { BigDecimal, BigInt, Address, ByteArray, Bytes, ethereum, log } from '@graphprotocol/graph-ts'
import { UniswapQuoter } from '../types/templates/SwapPool/UniswapQuoter'

export const ADDRESS_ZERO = Address.fromString('0x0000000000000000000000000000000000000000')

export const BD_ZERO = BigDecimal.fromString('0')
export const BI_ZERO = BigInt.fromI32(0)
export const BI_ONE = BigInt.fromI32(1)
Expand Down Expand Up @@ -102,26 +104,20 @@ export const getUsdPrice = (token: Address): BigDecimal => {
}
// Define the return type
class DecodedTokenId {
tenderizer: Address;
tenderizer: string;
id: BigInt;

constructor(tenderizer: Address, id: BigInt) {
constructor(tenderizer: string, id: BigInt) {
this.tenderizer = tenderizer;
this.id = id;
}
}

export const decodeTokenId = (tokenId: BigInt): DecodedTokenId => {
// Convert BigInt to bytes
let bytes = ByteArray.fromBigInt(tokenId)

// Extract the address (first 20 bytes)
let addressBytes = Bytes.fromUint8Array(bytes.subarray(0, 20));
let address = ethereum.decode("address", addressBytes)!.toAddress();

// Extract the uint96 (last 12 bytes)
let uint96Bytes = Bytes.fromUint8Array(bytes.subarray(20, 32));
let uint96 = BigInt.fromUnsignedBytes(uint96Bytes);
let hexId = tokenId.toHexString()
let address = "0x" + hexId.substring(0, 20)
let uint96 = BigInt.fromUnsignedBytes(Bytes.fromHexString(hexId.substring(20, 32)))

// Return the decoded values
return new DecodedTokenId(address, uint96);
}
}
16 changes: 12 additions & 4 deletions packages/subgraph/src/mappings/unlocks.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,24 @@

import { Unlock } from "../types/schema";
import { Unlock, User } from "../types/schema";
import { Transfer as UnlockTransferEvent } from "../types/Unlocks/Unlocks";
import { decodeTokenId } from "./helpers";
import { ADDRESS_ZERO, decodeTokenId } from "./helpers";

export function handleUnlockTransfer(event: UnlockTransferEvent): void {
if (event.params.from === ADDRESS_ZERO) return;

const decoded = decodeTokenId(event.params.id)
const idAsString = decoded.id.toString()
let unlockID = decoded.tenderizer.toHex().concat('0'.repeat(24 - idAsString.length).concat(idAsString))
let unlockID = decoded.tenderizer.concat('0'.repeat(24 - idAsString.length).concat(idAsString))

let u = Unlock.load(unlockID);
if (u == null) return;
u.user = event.params.to.toHex();
let receiver = event.params.to.toHex();
let user = User.load(receiver)
if (user == null) {
user = new User(receiver)
user.save()
}

u.user = receiver;
u.save();
}

0 comments on commit 2517406

Please sign in to comment.