-
Notifications
You must be signed in to change notification settings - Fork 68
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: native and erc20 balances monitoring transitions declarative in…
…terfaces. File restructuring. Fix multiple transitions in same state
- Loading branch information
1 parent
1f76cc6
commit 720cce3
Showing
4 changed files
with
275 additions
and
65 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
import { ethers } from 'ethers'; | ||
|
||
import { LitContracts } from '@lit-protocol/contracts-sdk'; | ||
import { LitNodeClient } from '@lit-protocol/lit-node-client'; | ||
|
||
import { BaseTransitionParams } from './transitions'; | ||
|
||
export type Address = `0x${string}`; | ||
|
||
export interface StateDefinition { | ||
key: string; | ||
} | ||
|
||
export interface OnEvmChainEvent { | ||
evmChainId: number; | ||
} | ||
|
||
export interface IntervalTransitionDefinition { | ||
interval?: number; | ||
} | ||
|
||
export interface BaseBalanceTransitionDefinition | ||
extends IntervalTransitionDefinition, | ||
OnEvmChainEvent { | ||
address: Address; | ||
comparator: '>' | '>=' | '=' | '!=' | '<=' | '<'; | ||
amount: string; | ||
} | ||
|
||
export interface NativeBalanceTransitionDefinition | ||
extends BaseBalanceTransitionDefinition { | ||
type: 'native'; | ||
} | ||
|
||
export interface ERC20BalanceTransitionDefinition | ||
extends BaseBalanceTransitionDefinition { | ||
type: 'ERC20'; | ||
tokenAddress: string; | ||
tokenDecimals: number; | ||
} | ||
|
||
// TODO add ERC721 and ERC1155 | ||
export type BalanceTransitionDefinition = | ||
| NativeBalanceTransitionDefinition | ||
| ERC20BalanceTransitionDefinition; | ||
|
||
export interface TimerTransitionDefinition | ||
extends IntervalTransitionDefinition { | ||
offset?: number; | ||
step?: number; | ||
until: number; | ||
} | ||
|
||
export interface EvmContractEventTransitionDefinition extends OnEvmChainEvent { | ||
contractAddress: string; | ||
abi: ethers.ContractInterface; | ||
eventName: string; | ||
eventParams?: any; | ||
} | ||
|
||
export interface TransitionDefinition { | ||
balances?: BalanceTransitionDefinition[]; | ||
evmContractEvent?: EvmContractEventTransitionDefinition; | ||
fromState: string; | ||
timer?: TimerTransitionDefinition; | ||
toState: string; | ||
} | ||
|
||
export interface BaseStateMachineParams { | ||
debug?: boolean; | ||
litNodeClient: LitNodeClient; | ||
litContracts: LitContracts; | ||
} | ||
|
||
export interface StateMachineDefinition | ||
extends Omit<BaseStateMachineParams, 'litNodeClient' | 'litContracts'> { | ||
litNodeClient: LitNodeClient | ConstructorParameters<typeof LitNodeClient>[0]; | ||
litContracts: LitContracts | ConstructorParameters<typeof LitContracts>[0]; | ||
states: StateDefinition[]; | ||
transitions: TransitionDefinition[]; | ||
} | ||
|
||
export interface TransitionParams | ||
extends Omit<BaseTransitionParams, 'onMatch'>, | ||
Partial<Pick<BaseTransitionParams, 'onMatch'>> { | ||
fromState: string; | ||
toState: string; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import { LIT_EVM_CHAINS } from '@lit-protocol/constants'; | ||
|
||
import { OnEvmChainEvent } from '../types'; | ||
|
||
export function getChain(event: OnEvmChainEvent) { | ||
const chain = Object.values(LIT_EVM_CHAINS).find( | ||
(chain) => chain.chainId === event.evmChainId | ||
); | ||
if (!chain) { | ||
throw new Error(`EVM chain with chainId ${event.evmChainId} not found`); | ||
} | ||
|
||
return chain; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
import { ethers } from 'ethers'; | ||
|
||
import { BalanceTransitionDefinition } from '../types'; | ||
|
||
export const ERC20ABI = [ | ||
{ | ||
constant: true, | ||
inputs: [ | ||
{ | ||
name: '_owner', | ||
type: 'address', | ||
}, | ||
], | ||
name: 'balanceOf', | ||
outputs: [ | ||
{ | ||
name: 'balance', | ||
type: 'uint256', | ||
}, | ||
], | ||
payable: false, | ||
type: 'function', | ||
}, | ||
]; | ||
|
||
export async function getERC20Balance( | ||
provider: ethers.providers.Provider, | ||
tokenAddress: string, | ||
tokenDecimals: number, | ||
accountAddress: string | ||
) { | ||
const contract = new ethers.Contract(tokenAddress, ERC20ABI, provider); | ||
const balance = (await contract['balanceOf']( | ||
accountAddress | ||
)) as ethers.BigNumber; | ||
|
||
const adjustedBalance = ethers.utils.parseUnits( | ||
balance.toString(), | ||
18 - tokenDecimals | ||
); | ||
|
||
return adjustedBalance; | ||
} | ||
|
||
export function getBalanceTransitionCheck( | ||
transitionIndex: number, | ||
balance: BalanceTransitionDefinition | ||
): (values: any[]) => Promise<boolean> { | ||
const balanceCheck = async (values: any[]) => { | ||
const { amount, comparator } = balance; | ||
const targetAmount = ethers.utils.parseUnits(amount); | ||
const addressBalance = values[transitionIndex] as | ||
| ethers.BigNumber | ||
| undefined; | ||
|
||
if (!addressBalance) return false; | ||
|
||
switch (comparator) { | ||
case '<': | ||
return addressBalance.lt(targetAmount); | ||
case '<=': | ||
return addressBalance.lte(targetAmount); | ||
case '=': | ||
return addressBalance.eq(targetAmount); | ||
case '!=': | ||
return !addressBalance.eq(targetAmount); | ||
case '>=': | ||
return addressBalance.gte(targetAmount); | ||
case '>': | ||
return addressBalance.gt(targetAmount); | ||
default: | ||
throw new Error(`Unrecognized comparator ${comparator}`); | ||
} | ||
}; | ||
|
||
return balanceCheck; | ||
} |