Skip to content

Commit

Permalink
Fix subgraph asset ID calculation and padding
Browse files Browse the repository at this point in the history
  • Loading branch information
solimander committed Jan 25, 2024
1 parent 22860e1 commit 593daf3
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 9 deletions.
16 changes: 12 additions & 4 deletions packages/prop-house-subgraph/src/lib/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,13 +59,13 @@ export function computeAssetID(asset: AssetStruct): string {
case AssetType.NATIVE:
return ZERO_BYTES_32;
case AssetType.ERC20:
return `0x${asset.assetType.toString(16)}${asset.token.toHex().substring(2)}`.padEnd(66, '0');
return `0x0${asset.assetType.toString(16)}${asset.token.toHex().substring(2)}`.padEnd(66, '0');
default:
const paddedToken = asset.token.toHex().substring(2).padStart(64, '0');
const token = asset.token.toHex().substring(2);
const paddedIdentifier = asset.identifier.toHex().substring(2).padStart(64, '0');
const keccakHash = crypto.keccak256(ByteArray.fromHexString(`${paddedToken}${paddedIdentifier}`)).toHex().substring(2);
const keccakHash = crypto.keccak256(ByteArray.fromHexString(`${token}${paddedIdentifier}`)).toHex().substring(2);

return `0x${asset.assetType.toString(16)}${keccakHash}`.slice(0, 66);
return `0x0${asset.assetType.toString(16)}${keccakHash}`.slice(0, 66);
}
}

Expand Down Expand Up @@ -110,3 +110,11 @@ export function getGovPowerStrategyType(addr: string): string {
}
return GovPowerStrategyType.UNKNOWN;
}

/**
* Pad a hex string to 32 bytes
* @param hex The hex string to pad
*/
export function padHexTo32Bytes(hex: string): string {
return `0x${hex.substring(2).padStart(64, '0')}`;
}
14 changes: 9 additions & 5 deletions packages/prop-house-subgraph/src/timed-round.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { log, BigInt } from '@graphprotocol/graph-ts';
import { AssetClaimed, RoundCancelled, RoundFinalized, RoundRegistered, TransferBatch, TransferSingle } from '../generated/templates/TimedRound/TimedRound';
import { Account, Asset, Award, Balance, Claim, Reclaim, Round, RoundVotingStrategy, TimedRoundConfig, Transfer, GovPowerStrategy, RoundProposingStrategy } from '../generated/schema';
import { AssetStruct, computeAssetID, computeGovPowerStrategyID, get2DArray, getAssetTypeString, getGovPowerStrategyType } from './lib/utils';
import { AssetStruct, computeAssetID, computeGovPowerStrategyID, get2DArray, getAssetTypeString, getGovPowerStrategyType, padHexTo32Bytes } from './lib/utils';
import { RoundEventState, BIGINT_ONE, ZERO_ADDRESS, BIGINT_4_WEEKS_IN_SECONDS, BIGINT_ZERO } from './lib/constants';

export function storeGovPowerStrategy(addresses: BigInt[], params2D: BigInt[][], index: i32): string {
Expand Down Expand Up @@ -176,6 +176,8 @@ export function handleAssetClaimed(event: AssetClaimed): void {
claimer.save();
}

const assetId = padHexTo32Bytes(event.params.asset.assetId.toHex());

const claim = new Claim(
`${event.transaction.hash.toHex()}-${event.logIndex.toString()}`,
);
Expand All @@ -184,7 +186,7 @@ export function handleAssetClaimed(event: AssetClaimed): void {
claim.claimedAt = event.block.timestamp;
claim.recipient = event.params.recipient;
claim.proposalId = event.params.proposalId;
claim.asset = event.params.asset.assetId.toHex();
claim.asset = assetId;
claim.amount = event.params.asset.amount;
claim.round = round.id;
claim.save();
Expand All @@ -200,14 +202,16 @@ export function handleSingleTransfer(event: TransferSingle): void {
]);
return;
}


const assetId = padHexTo32Bytes(event.params.id.toHex());

const reclaim = new Reclaim(
`${event.transaction.hash.toHex()}-${event.logIndex.toString()}`,
);
reclaim.txHash = event.transaction.hash;
reclaim.reclaimer = reclaimer.id;
reclaim.reclaimedAt = event.block.timestamp;
reclaim.asset = event.params.id.toHex();
reclaim.asset = assetId;
reclaim.amount = event.params.value;
reclaim.round = event.address.toHex();
reclaim.save();
Expand All @@ -216,7 +220,7 @@ export function handleSingleTransfer(event: TransferSingle): void {
let balance = Balance.load(balanceId);
if (!balance) {
balance = new Balance(balanceId);
balance.asset = event.params.id.toHex();
balance.asset = assetId;
balance.round = event.address.toHex();
balance.balance = BIGINT_ZERO;
}
Expand Down

0 comments on commit 593daf3

Please sign in to comment.