Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: ERC-20 deposit #21

Merged
merged 5 commits into from
Jan 16, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
75 changes: 35 additions & 40 deletions src/handlers/InputAdded.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,40 +33,45 @@ export default class InputAdded implements Handler {
private inputStorage: Map<String, Input>,
) {}

async handlePayload(
private async prepareErc20Deposit(
input: Input,
block: BlockData,
ctx: DataHandlerContext<Store>,
opts: {
inputId: string;
},
) {
if (input.msgSender == ERC20PortalAddress) {
const success = getNumber(dataSlice(input.payload, 0, 1)) == 1; // 1 byte for boolean (not used?)
const tokenAddress = dataSlice(input.payload, 1, 21).toLowerCase(); // 20 bytes for address
const from = dataSlice(input.payload, 21, 41).toLowerCase(); // 20 bytes for address
const amount = getUint(dataSlice(input.payload, 41, 73)); // 32 bytes for uint256

let token = this.tokenStorage.get(tokenAddress) as Token;
if (!token) {
const contract = new ERC20(ctx, block.header, tokenAddress);
const name = await contract.name();
const symbol = await contract.symbol();
const decimals = await contract.decimals();
token = new Token({ id: tokenAddress, name, symbol, decimals });
this.tokenStorage.set(tokenAddress, token);
ctx.log.info(`${tokenAddress} (Token) stored`);
}
const deposit = new Erc20Deposit({
id: input.id,
amount,
from,
token,
});
return deposit;
if (input.msgSender !== ERC20PortalAddress) return undefined;

const success = getNumber(dataSlice(input.payload, 0, 1)) == 1; // 1 byte for boolean (not used?)
brunomenezes marked this conversation as resolved.
Show resolved Hide resolved
const tokenAddress = dataSlice(input.payload, 1, 21).toLowerCase(); // 20 bytes for address
const from = dataSlice(input.payload, 21, 41).toLowerCase(); // 20 bytes for address
const amount = getUint(dataSlice(input.payload, 41, 73)); // 32 bytes for uint256

let token = this.tokenStorage.get(tokenAddress) as Token;
if (!token) {
const contract = new ERC20(ctx, block.header, tokenAddress);
const name = await contract.name();
const symbol = await contract.symbol();
const decimals = await contract.decimals();
token = new Token({ id: tokenAddress, name, symbol, decimals });
this.tokenStorage.set(tokenAddress, token);
ctx.log.info(`${tokenAddress} (Token) stored`);
}
const deposit = new Erc20Deposit({
id: input.id,
amount,
from,
token,
});

this.depositStorage.set(opts.inputId, deposit);
ctx.log.info(`${opts.inputId} (Erc20Deposit) stored`);

return undefined;
return deposit;
}

async prepareErc721Deposit(
private async prepareErc721Deposit(
input: Input,
block: BlockData,
ctx: DataHandlerContext<Store>,
Expand Down Expand Up @@ -143,21 +148,11 @@ export default class InputAdded implements Handler {
transactionHash: log.transaction?.hash,
});

const erc20Deposit = await this.handlePayload(input, block, ctx);
if (erc20Deposit) {
this.depositStorage.set(inputId, erc20Deposit);
ctx.log.info(`${inputId} (Erc20Deposit) stored`);
input.erc20Deposit = erc20Deposit;
}
const params = [input, block, ctx, { inputId }] as const;

input.erc20Deposit = await this.prepareErc20Deposit(...params);

input.erc721Deposit = await this.prepareErc721Deposit(
input,
block,
ctx,
{
inputId,
},
);
input.erc721Deposit = await this.prepareErc721Deposit(...params);

this.inputStorage.set(inputId, input);
ctx.log.info(`${inputId} (Input) stored`);
Expand Down
9 changes: 4 additions & 5 deletions src/handlers/OwnershipTransferred.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,10 @@ export default class OwnershipTransferred implements Handler {

async handle(log: Log, _block: BlockData, ctx: DataHandlerContext<Store>) {
if (log.topics[0] === events.OwnershipTransferred.topic) {
const appId = log.transaction?.to?.toLowerCase();
const application = appId
? this.applicationStorage.get(appId) ??
(await ctx.store.get(Application, appId))
: undefined;
const appId = log.transaction?.to?.toLowerCase() ?? '';
const application =
this.applicationStorage.get(appId) ??
(await ctx.store.get(Application, appId));

if (application) {
// decode event
Expand Down
Loading