-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
25 changed files
with
309 additions
and
278 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,2 @@ | ||
export { listenEvents } from "./lock-listener"; | ||
export { listenStakeEvents } from "./stake-listener"; | ||
export { listenEvents } from "./lock-listener/lock-listener"; | ||
export { listenStakeEvents } from "./stake-listener/stake-listener"; |
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,2 @@ | ||
export * from "./lock-listener"; | ||
export * from "./process-fail-safe"; |
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,22 @@ | ||
import { setTimeout } from "node:timers/promises"; | ||
import type { LockEvent } from "../types"; | ||
import type { LogInstance, THandler } from "../types"; | ||
|
||
export const processEventsFailSafe = async ( | ||
chain: THandler, | ||
ev: LockEvent, | ||
log: LogInstance, | ||
processEvent: (chain: THandler, ev: LockEvent) => Promise<void>, | ||
) => { | ||
let success = false; | ||
while (!success) { | ||
try { | ||
await processEvent(chain, ev); | ||
success = true; | ||
} catch (e) { | ||
log.error("Error processing poll events", ev, e); | ||
log.info("Awaiting 2s"); | ||
await setTimeout(2 * 1000); | ||
} | ||
} | ||
}; |
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,2 @@ | ||
export * from "./stake-listener"; | ||
export * from "./stake-tokens"; |
10 changes: 5 additions & 5 deletions
10
src/handler/stake-listener.ts → src/handler/stake-listener/stake-listener.ts
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,59 @@ | ||
import { JsonRpcProvider, Wallet } from "ethers"; | ||
import { | ||
ERC20Staking__factory, | ||
ERC20__factory, | ||
} from "../../contractsTypes/evm"; | ||
import type { IGeneratedWallets, IStakingConfig } from "../../types"; | ||
import type { LogInstance, THandler } from "../types"; | ||
|
||
export async function stakeTokens( | ||
conf: IStakingConfig, | ||
secrets: IGeneratedWallets, | ||
chains: THandler[], | ||
logger: LogInstance, | ||
) { | ||
const others = chains.filter((e) => e.chainType !== "evm"); | ||
const provider = new JsonRpcProvider(conf.rpcURL); | ||
const signer = new Wallet(secrets.evmWallet.privateKey, provider); | ||
const staker = ERC20Staking__factory.connect(conf.contractAddress, signer); | ||
const token = ERC20__factory.connect(conf.coinAddress, signer); | ||
const staked = await staker.stakingBalances(secrets.evmWallet.address); | ||
if (staked > 0n) { | ||
logger.info( | ||
`Already staked ${staked} ${conf.coinSymbol} in contract ${conf.contractAddress}`, | ||
); | ||
return; | ||
} | ||
const amtToStake = await staker.stakingAmount(); | ||
logger.info("Awaiting completion of approve transaction."); | ||
|
||
const approve = await ( | ||
await token.approve(conf.contractAddress, amtToStake * amtToStake) | ||
).wait(); | ||
|
||
logger.info("Approved to stake: ✅"); | ||
if (!approve || approve.status !== 1) { | ||
throw new Error("Failed to approve staking"); | ||
} | ||
|
||
const data = [ | ||
{ | ||
validatorAddress: secrets.evmWallet.address, | ||
chainType: "evm", | ||
}, | ||
...others.map((e) => { | ||
return { | ||
validatorAddress: e.publicKey, | ||
chainType: e.chainType, | ||
}; | ||
}), | ||
]; | ||
|
||
logger.info("Awaiting completion of stake transaction."); | ||
const staking = await (await staker.stakeERC20(data)).wait(); | ||
logger.info("Stake complete: ✅"); | ||
|
||
if (!staking || staking.status !== 1) { | ||
throw new Error("Failed to stake"); | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
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,4 @@ | ||
import type { LockEvent, StakeEvent } from "."; | ||
|
||
export type LockEventIter = (event: LockEvent) => Promise<void>; | ||
export type StakeEventIter = (event: StakeEvent) => Promise<void>; |
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,9 @@ | ||
export * from "./lock-event"; | ||
export * from "./stake-event"; | ||
export * from "./lock-handler"; | ||
export * from "./stake-handler"; | ||
export * from "./event-iterators"; | ||
export * from "./lock-handler"; | ||
export * from "./nft-data"; | ||
export * from "./nft-transfer-details-object"; | ||
export * from "./logger"; |
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,12 @@ | ||
export type LockEvent = { | ||
listenerChain: string; | ||
tokenId: string; | ||
destinationChain: string; | ||
destinationUserAddress: string; | ||
sourceNftContractAddress: string; | ||
tokenAmount: string; | ||
nftType: string; | ||
sourceChain: string; | ||
transactionHash: string; | ||
metaDataUri: 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,31 @@ | ||
import type { TNftTransferDetailsObject } from "."; | ||
import type { LockEventIter, LogInstance, TNftData } from "."; | ||
import type { TSupportedChainTypes, TSupportedChains } from "../../config"; | ||
import type { EventBuilder } from "../event-builder"; | ||
|
||
export interface THandler { | ||
addSelfAsValidator(): Promise<"success" | "failure">; | ||
listenForLockEvents(builder: EventBuilder, cb: LockEventIter): Promise<void>; | ||
pollForLockEvents(builder: EventBuilder, cb: LockEventIter): Promise<void>; | ||
signClaimData( | ||
nfto: TNftTransferDetailsObject, | ||
): Promise<{ signer: string; signature: string }>; | ||
signData(buf: string): Promise<{ signer: string; signature: string }>; | ||
nftData( | ||
tokenId: string, | ||
contract: string, | ||
logger: LogInstance, | ||
): Promise<TNftData>; | ||
validateNftData( | ||
data: TNftData, | ||
): { valid: false; reason: string } | { valid: true }; | ||
chainIdent: TSupportedChains; | ||
selfIsValidator(): Promise<boolean>; | ||
getBalance(): Promise<bigint>; | ||
initialFunds: bigint; | ||
currency: string; | ||
address: string; | ||
chainType: TSupportedChainTypes; | ||
publicKey: string; | ||
decimals: bigint; | ||
} |
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,3 @@ | ||
import type { Logger } from "tslog"; | ||
|
||
export type LogInstance = Logger<unknown>; |
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,6 @@ | ||
export type TNftData = { | ||
name: string; | ||
symbol: string; | ||
metadata: string; | ||
royalty: bigint; | ||
}; |
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,18 @@ | ||
export type TNftTransferDetailsObject = { | ||
tokenId: string; | ||
sourceChain: string; | ||
destinationChain: string; | ||
destinationUserAddress: string; | ||
sourceNftContractAddress: string; | ||
name: string; | ||
symbol: string; | ||
royalty: string; | ||
royaltyReceiver: string; | ||
metadata: string; | ||
transactionHash: string; | ||
tokenAmount: string; | ||
nftType: string; | ||
fee: string; | ||
lockTxChain: string; | ||
imgUri?: 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,7 @@ | ||
import type { TSupportedChainTypes } from "../../config"; | ||
|
||
export type StakeEvent = { | ||
validatorAddress: string; | ||
caller: string; | ||
chainType: TSupportedChainTypes; | ||
}[]; |
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,9 @@ | ||
import type { EventBuilder } from "../event-builder"; | ||
import type { StakeEventIter } from "./event-iterators"; | ||
|
||
export interface TStakingHandler { | ||
listenForStakingEvents( | ||
builder: EventBuilder, | ||
cb: StakeEventIter, | ||
): Promise<void>; | ||
} |
Oops, something went wrong.