-
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.
feat: add cw-simulate for fetching data without limitation
- Loading branch information
Showing
7 changed files
with
217 additions
and
30 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,4 @@ | ||
.env.* | ||
!.env.example | ||
!.env.example | ||
src/services/contract-simulate/data/final/* | ||
src/services/contract-simulate/data/pending/* |
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
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
Binary file added
BIN
+769 KB
packages/orchestrator/src/services/contract-simulate/data/wasm/cw-app-bitcoin.wasm
Binary file not shown.
146 changes: 146 additions & 0 deletions
146
packages/orchestrator/src/services/contract-simulate/index.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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,146 @@ | ||
import { AppBitcoinClient } from "@oraichain/bitcoin-bridge-contracts-sdk"; | ||
import { | ||
BufferCollection, | ||
compare, | ||
DownloadState, | ||
SimulateCosmWasmClient, | ||
SortedMap, | ||
} from "@oraichain/cw-simulate"; | ||
import fs, { readFileSync } from "fs"; | ||
import path from "path"; | ||
import { setTimeout } from "timers/promises"; | ||
import env from "../../configs/env"; | ||
import { logger } from "../../configs/logger"; | ||
import { ITERATION_DELAY } from "../../constants"; | ||
|
||
class ContractSimulator { | ||
static simulateAppCwBitcoin: AppBitcoinClient; | ||
static simulateClient: SimulateCosmWasmClient = new SimulateCosmWasmClient({ | ||
chainId: "Oraichain", | ||
bech32Prefix: "orai", | ||
}); | ||
static finalState = new DownloadState( | ||
env.cosmos.lcdUrl, | ||
`${__dirname}/data/final` | ||
); | ||
static pendingState = new DownloadState( | ||
env.cosmos.lcdUrl, | ||
`${__dirname}/data/pending` | ||
); | ||
static logger = logger("ContractSimulator"); | ||
static initialized: boolean = false; | ||
static sender: string = "orai1ehmhqcn8erf3dgavrca69zgp4rtxj5kqgtcnyd"; | ||
|
||
static stateCrawler = async () => { | ||
this.logger.info("Starting download new state"); | ||
const source = `${__dirname}/data/pending`; | ||
const dest = `${__dirname}/data/final`; | ||
|
||
if (!fs.existsSync(source)) { | ||
fs.mkdirSync(source, { recursive: true }); | ||
} | ||
|
||
if (!fs.existsSync(dest)) { | ||
fs.mkdirSync(dest, { recursive: true }); | ||
} | ||
|
||
// download and copy to final state | ||
await Promise.all([this.pendingState.saveState(env.cosmos.appBitcoin)]); | ||
this.copyFolderContents(source, dest); | ||
this.logger.info("Success update new state!"); | ||
|
||
// load new state | ||
await Promise.all([ | ||
this.finalState.loadState( | ||
this.simulateClient, | ||
this.sender, | ||
env.cosmos.appBitcoin, | ||
"cw-app-bitcoin" | ||
), | ||
]); | ||
this.simulateAppCwBitcoin = new AppBitcoinClient( | ||
this.simulateClient as any, | ||
this.sender, | ||
env.cosmos.appBitcoin | ||
); | ||
|
||
this.initialized = true; | ||
|
||
await Promise.all([ | ||
this.loadStateAndCode("cw-app-bitcoin.wasm", env.cosmos.appBitcoin), | ||
]); | ||
this.logger.info("Finish state crawler!"); | ||
}; | ||
|
||
static async tryInitializeWithOldData() { | ||
try { | ||
await Promise.all([ | ||
this.loadStateAndCode("cw-app-bitcoin.wasm", env.cosmos.appBitcoin), | ||
]); | ||
this.simulateAppCwBitcoin = new AppBitcoinClient( | ||
this.simulateClient as any, | ||
this.sender, | ||
env.cosmos.appBitcoin | ||
); | ||
this.initialized = true; | ||
} catch (err) { | ||
this.logger.info(`[tryInitializeWithOldData] ${err}`); | ||
} | ||
} | ||
|
||
static async loadStateAndCode(fileName: string, contractAddress: string) { | ||
const code = readFileSync(path.join(__dirname, `data/wasm/${fileName}`)); | ||
const state = readFileSync( | ||
path.join(__dirname, `data/final/${contractAddress}.state`) | ||
); | ||
const { codeId } = await this.simulateClient.upload( | ||
this.sender, | ||
code, | ||
"auto" | ||
); | ||
const raw = SortedMap.rawPack( | ||
new BufferCollection(state as any) as any, | ||
compare | ||
); | ||
await this.simulateClient.loadContract( | ||
env.cosmos.appBitcoin, | ||
{ | ||
codeId: codeId, | ||
admin: this.sender, | ||
created: new Date().getTime(), | ||
creator: "", | ||
label: "", | ||
}, | ||
raw | ||
); | ||
} | ||
|
||
static copyFolderContents = (source: string, destination: string) => { | ||
const files = fs.readdirSync(source); | ||
|
||
files.forEach((file) => { | ||
const currentFilePath = path.join(source, file); | ||
const destinationFilePath = path.join(destination, file); | ||
|
||
if (fs.lstatSync(currentFilePath).isDirectory()) { | ||
this.copyFolderContents(currentFilePath, destinationFilePath); | ||
} else { | ||
fs.copyFileSync(currentFilePath, destinationFilePath); | ||
} | ||
}); | ||
}; | ||
|
||
static setSender(sender: string) { | ||
this.sender = sender; | ||
} | ||
|
||
static async sync() { | ||
this.tryInitializeWithOldData(); | ||
while (true) { | ||
await this.stateCrawler(); | ||
await setTimeout(ITERATION_DELAY.SIMULATOR_INTERVAL); | ||
} | ||
} | ||
} | ||
|
||
export default ContractSimulator; |
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