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

Feat/add data package #17

Merged
merged 4 commits into from
May 20, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
3,561 changes: 0 additions & 3,561 deletions examples/svelte-playground/package-lock.json

This file was deleted.

88 changes: 88 additions & 0 deletions examples/svelte-playground/src/lib/demurrage.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
const DEMURRAGE_WINDOW = 1n; // days
const MAX_VALUE = (1n << 192n) - 1n; // uint192 max value
const GAMMA_64x64 = 18443079296116538654n;
const BETA_64x64 = 18450409579521241655n;
const DECIMALS = 18n;
const EXA = 10n ** DECIMALS;
const ONE_64x64 = 1n << 64n;

// Helper function to calculate power using fixed-point representation
function pow(base: bigint, exponent: bigint): bigint {
let result = ONE_64x64;
let x = base;
while (exponent > 0n) {
if (exponent % 2n == 1n) {
result = (result * x) / ONE_64x64;
}
x = (x * x) / ONE_64x64;
exponent /= 2n;
}
return result;
}

// Calculate discounted balance
function calculateDiscountedBalance(balance: bigint, daysDifference: bigint): bigint {
if (daysDifference === 0n) {
return balance;
}
const r = pow(GAMMA_64x64, daysDifference);
return (balance * r) / ONE_64x64;
}

// Data structure for DiscountedBalance
interface DiscountedBalance {
balance: bigint;
lastUpdatedDay: bigint;
}

// Calculate current balances based on historical balances
function calculateCurrentBalances(
balances: { [account: string]: { [id: number]: DiscountedBalance } },
currentDay: bigint
): { [account: string]: { [id: number]: bigint } } {
const currentBalances: { [account: string]: { [id: number]: bigint } } = {};

for (const account in balances) {
if (!currentBalances[account]) {
currentBalances[account] = {};
}

for (const id in balances[account]) {
const discountedBalance = balances[account][id];
const daysDifference = currentDay - discountedBalance.lastUpdatedDay;
currentBalances[account][id] = calculateDiscountedBalance(
discountedBalance.balance,
daysDifference
);
}
}

return currentBalances;
}

// Example usage

// Example balances data
const balances = {
'0xAccount1': {
1: { balance: 1000000000000000000n, lastUpdatedDay: 10n },
2: { balance: 500000000000000000n, lastUpdatedDay: 12n }
},
'0xAccount2': {
1: { balance: 2000000000000000000n, lastUpdatedDay: 8n }
}
};

// Current day
const currentDay = 20n;

// Calculate current balances
const currentBalances = calculateCurrentBalances(balances, currentDay);

// Log the results
for (const account in currentBalances) {
console.log(`Account: ${account}`);
for (const id in currentBalances[account]) {
console.log(` ID: ${id}, Balance: ${currentBalances[account][id].toString()}`);
}
}
118 changes: 0 additions & 118 deletions examples/svelte-playground/src/lib/taskList.ts

This file was deleted.

52 changes: 42 additions & 10 deletions examples/svelte-playground/src/routes/+page.svelte
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
<script lang="ts">
import { Avatar, Sdk } from '@circles-sdk/sdk/dist';
import AvatarComponent from '../components/avatar/Avatar.svelte';
import { ethers, HDNodeWallet } from 'ethers';
import { BaseWallet, ethers, HDNodeWallet } from 'ethers';
import HorizontalLayout from '../components/common/HorizontalLayout.svelte';
import VerticalCollapsible from '../components/common/VerticalCollapsible.svelte';
import PromiseButton from '../components/common/ActionButton.svelte';
import EventList, { subscribeAvatar } from '../components/EventList.svelte';
import { onMount } from 'svelte';
import HorizontalCollapsible from '../components/common/HorizontalCollapsible.svelte';
import { AvatarState } from '@circles-sdk/sdk/dist/avatar';
import {
PUBLIC_ANVIL_RPC_URL
, PUBLIC_ANVIL_HUB_V1
Expand All @@ -17,11 +15,19 @@
, PUBLIC_GC_RPC_URL
, PUBLIC_GC_HUB_V1
, PUBLIC_GC_HUB_V2
, PUBLIC_GC_PRIVATE_KEY, PUBLIC_ANVIL_MIGRATION_CONTRACT, PUBLIC_GC_MIGRATION_CONTRACT
, PUBLIC_GC_PRIVATE_KEY
, PUBLIC_CHIADO_RPC_URL
, PUBLIC_CHIADO_HUB_V1
, PUBLIC_CHIADO_HUB_V2
, PUBLIC_CHIADO_PRIVATE_KEY
, PUBLIC_ANVIL_MIGRATION_CONTRACT
, PUBLIC_GC_MIGRATION_CONTRACT
} from '$env/static/public';
import { Avatar, AvatarState, Sdk } from '@circles-sdk/sdk/dist';
import type { ChainConfig } from '@circles-sdk/sdk';

let environment = 'gnosisChain';
const environments : {
const environments: {
[key: string]: {
rpcUrl: string,
hubv1Address: string,
Expand All @@ -43,10 +49,17 @@
hubv2Address: PUBLIC_ANVIL_HUB_V2,
migrationContract: PUBLIC_ANVIL_MIGRATION_CONTRACT,
mainWallet: PUBLIC_ANVIL_PRIVATE_KEY
},
chiado: {
rpcUrl: PUBLIC_CHIADO_RPC_URL,
hubv1Address: PUBLIC_CHIADO_HUB_V1,
hubv2Address: PUBLIC_CHIADO_HUB_V2,
migrationContract: PUBLIC_ANVIL_MIGRATION_CONTRACT,
mainWallet: PUBLIC_CHIADO_PRIVATE_KEY
}
};

let chainId: number = 0;
let chainId: bigint = BigInt(0);
let avatars: Avatar[] = [];

async function onEnvironmentChange() {
Expand All @@ -55,7 +68,7 @@
}

function migrateLocalStorage() {
const newEntries = {};
const newEntries: { [x: string]: any } = {};
const newIndex = [];

if (!localStorage.getItem('v0.2')) {
Expand All @@ -64,6 +77,9 @@
if (key === 'avatars') {
continue;
}
if (!key) {
continue;
}
const newKey = key.replace('avatar_', '');
newEntries[`${chainId}_${newKey}`] = localStorage.getItem(key);
newIndex.push(`${chainId}_${newKey}`);
Expand Down Expand Up @@ -94,7 +110,14 @@
}
const jsonRpcProvider = new ethers.JsonRpcProvider(environments[environment].rpcUrl);
const ethersWallet = new ethers.Wallet(avatarRecord.privateKey, jsonRpcProvider);
const sdk = new Sdk(environments[environment].hubv1Address, environments[environment].hubv2Address, environments[environment].migrationContract, ethersWallet);

const chainConfig: ChainConfig = {
circlesRpcUrl: environments[environment].rpcUrl,
v1HubAddress: environments[environment].hubv1Address,
v2HubAddress: environments[environment].hubv2Address,
migrationAddress: environments[environment].migrationContract
};
const sdk = new Sdk(chainConfig, <any>ethersWallet);
const avatar = await sdk.getAvatar(avatarId.replace(`${chainId}_`, ''));
await avatar.initialize();

Expand All @@ -111,7 +134,16 @@
const createAvatar = async () => {
const jsonRpcProvider = new ethers.JsonRpcProvider(environments[environment].rpcUrl);
const subWallet = await randomFundedWallet(jsonRpcProvider);
const sdk = new Sdk(environments[environment].hubv1Address, environments[environment].hubv2Address, environments[environment].migrationContract, subWallet);
subWallet.connect(jsonRpcProvider);

const chainConfig: ChainConfig = {
circlesRpcUrl: environments[environment].rpcUrl,
v1HubAddress: environments[environment].hubv1Address,
v2HubAddress: environments[environment].hubv2Address,
migrationAddress: environments[environment].migrationContract
};

const sdk = new Sdk(chainConfig, <any>subWallet);
const avatar = await sdk.getAvatar(await subWallet.getAddress());
await avatar.initialize();

Expand Down Expand Up @@ -144,7 +176,7 @@
await jsonRpcProvider.send('evm_mine', []);
};

export const randomFundedWallet = async (jsonRpcProvider: ethers.JsonRpcProvider): Promise<HDNodeWallet> => {
export const randomFundedWallet = async (jsonRpcProvider: ethers.JsonRpcProvider): Promise<BaseWallet> => {
const subWallet = ethers.Wallet.createRandom().connect(jsonRpcProvider);
const mainWallet = new ethers.Wallet(environments[environment].mainWallet, jsonRpcProvider);
const tx = await mainWallet.sendTransaction({
Expand Down
Loading
Loading