Skip to content

aura-nw/horoscope-track-node-example

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 

Repository files navigation

Top swap AURA

On this project, we will create an example to track all address on Aura Network Xstaxy to calculate total aura swapped to usdc coin by each address

Contract address need to track: aura1fgfnuru6krewgt9zzu7nzercz007928uzrw2t2tl6hl3ec50me2q3ankr2

Setup project

# generate code from template
git clone https://github.com/aura-nw/horoscope-track-node.git  
cd horoscope-track-node
git checkout 4c2ff5398f41e7c0d33c75435c62522170e63cdc

# or use horoscope-cli
npm install @aura-nw/horoscope-cli
horoscope-cli init

# install package node
npm install

# create env
cp .env-sample .env

# run docker compose to run redis, postgres, hasura
docker-compose up

Create model and migration

Create model in schema.prisma:

model Account {
  id         Int      @id @default(autoincrement())
  address    String
  amount     BigInt
  time       DateTime
}

Then run db migration

npm run prisma:migrate

Update business logic

Update project.yaml to filter message by condition

- handler: handleMessages
  type: message
  filter:
  - type: "/cosmwasm.wasm.v1.MsgExecuteContract" 
      contract: "aura1fgfnuru6krewgt9zzu7nzercz007928uzrw2t2tl6hl3ec50me2q3ankr2" 
      contractCall: "execute_swap_operations"    

Update code to handle message in src/mappings/mappingHandlers.ts

export async function handleMessages(
  msg: IMessageResponse,
  trx: any,
): Promise<void> {
  console.log('catched message:');
  // console.log(JSON.stringify(msg));

  const sender = msg.sender;
  const funds: any[] = msg.content.funds;
  let time = new Date();
  if (msg.block?.time) {
    time = new Date(msg.block?.time);
  }
  time.setHours(0, 0, 0, 0);
  const amountAura = funds.find((x: any) => x.denom === 'uaura').amount;
  const accountInDB = await prisma.account.findFirst({
    where: {
      address: sender,
      time: {
        equals: time,
      },
    },
  });
  if (!accountInDB) {
    const resultInsert = await prisma.account.create({
      // eslint-disable-next-line @typescript-eslint/ban-ts-comment
      // @ts-ignore
      data: { address: sender, amount: amountAura, time: time },
    });
    console.log('result insert: ', resultInsert.id);
  } else {
    accountInDB.amount = BigInt(accountInDB.amount) + BigInt(amountAura);
    const resultUpdate = await prisma.account.update({
      where: {
        id: accountInDB.id,
      },
      data: {
        amount: accountInDB.amount,
      },
    });
    console.log('result update: ', resultUpdate.id);
  }
}

To start crawling

npm run start:dev

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published