Skip to content

Commit

Permalink
Tx queue (#1124)
Browse files Browse the repository at this point in the history
* Batch commit behind admin auth

* transaction queue initial implementation

* Change test account structure

* Update encode decode so that the output is nicely formatted

* Simply print errors

* switch JSON and table order

* Add stringToHex

* Add stringToU8a

* Changes to test account types

* Make admin API optional

* Fix reloading API

* Remove commented

* remove broken script runner

* Update description

* remove ts-node

* remove ts-node

* remove manual email and name checks

* remove unused contractTx function

* build:typechain

* license

* fix broken merge

* Make sure correct pairs are used when sending txs and correct accounts are passed to queries

* build:typechain

* Move tx queue into its own package

* lint:fix

* build:typechain

* remove trailing commans

* remove commented

* Remove return from immediately returning functions

* Fail immediately if atlasURI not supplied

* Supply function name

* Add error when contract error is unretrieveable

* Remove let

* Use prosopo error handler

* Remove type

* remove rogue logging

* pass Error into prosopo error instance

---------

Co-authored-by: Hugh <hughglynparry@gmail.com>
  • Loading branch information
forgetso and HughParry authored Apr 16, 2024
1 parent fdf3cd8 commit 61fadab
Show file tree
Hide file tree
Showing 46 changed files with 1,335 additions and 394 deletions.
2 changes: 1 addition & 1 deletion contracts/captcha/src/captcha.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"source": {
"hash": "0xf4168d7539ede275d65767189c733756a504f6657609e9e0ba2e6ac654323814",
"hash": "0x082a3b92ea430de7ed186bc7d90d69df7dd9e7916cae5b6e393541ef3d991c1b",
"language": "ink! 4.3.0",
"compiler": "rustc 1.69.0",
"build_info": {
Expand Down
4 changes: 2 additions & 2 deletions contracts/captcha/src/contract-info/captcha.ts

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion contracts/common/src/common.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"source": {
"hash": "0xe4cac088ed09b43809b11b51eea2d7543fdc784b371332886e7922b13d2ab3b4",
"hash": "0x13cc7cae4f185558a84388a1440b6cacec75efdaaa5716c560d5df3f5bbddf4a",
"language": "ink! 4.3.0",
"compiler": "rustc 1.69.0",
"build_info": {
Expand Down
4 changes: 2 additions & 2 deletions contracts/common/src/contract-info/common.ts

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions contracts/proxy/src/contract-info/proxy.ts

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion contracts/proxy/src/proxy.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"source": {
"hash": "0x914ffc648b57fb989d0ee412f7f66775a735a4a57d5beb132967dc715e660224",
"hash": "0x5d40e1e33a0857b98db56378c7c58e06f0c6fdb36a547f0909acffc756d2902b",
"language": "ink! 4.3.0",
"compiler": "rustc 1.69.0",
"build_info": {
Expand Down
158 changes: 41 additions & 117 deletions dev/config/src/projectInfo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,158 +20,82 @@ export const getContractNames = () => {
})
}

export const getRootDir = () => {
return new URL('../../..', import.meta.url).pathname.slice(0, -1)
}
export const getRootDir = () => new URL('../../..', import.meta.url).pathname.slice(0, -1)

export const getCacheDir = () => {
return `${getRootDir()}/.cache`
}
export const getCacheDir = () => `${getRootDir()}/.cache`

export const getTestResultsDir = () => {
return `${getCacheDir()}/test-results`
}
export const getTestResultsDir = () => `${getCacheDir()}/test-results`

export const getDevDir = () => {
return `${getRootDir()}/dev`
}
export const getDevDir = () => `${getRootDir()}/dev`

export const getDemosDir = () => {
return `${getRootDir()}/demos`
}
export const getDemosDir = () => `${getRootDir()}/demos`

export const getContractDir = () => {
return `${getRootDir()}/packages/contract`
}
export const getContractDir = () => `${getRootDir()}/packages/contract`

export const getProtocolDir = () => {
return `${getRootDir()}/protocol`
}
export const getProtocolDir = () => `${getRootDir()}/protocol`

export const getPackagesDir = () => {
return `${getRootDir()}/packages`
}
export const getPackagesDir = () => `${getRootDir()}/packages`

export const getNodeModulesDir = () => {
return `${getRootDir()}/node_modules`
}
export const getNodeModulesDir = () => `${getRootDir()}/node_modules`

export const getConfigPkgDir = () => {
return `${getDevDir()}/config`
}
export const getConfigPkgDir = () => `${getDevDir()}/config`

export const getScriptsPkgDir = () => {
return `${getDevDir()}/scripts`
}
export const getScriptsPkgDir = () => `${getDevDir()}/scripts`

export const getProtocolCliDir = () => {
return `${getProtocolDir()}/dist/cli`
}
export const getProtocolCliDir = () => `${getProtocolDir()}/dist/cli`

export const getProtocolDistDir = () => {
return `${getProtocolDir()}/target/ink`
}
export const getProtocolDistDir = () => `${getProtocolDir()}/target/ink`

export const getClientExampleDir = () => {
return `${getDemosDir()}/client-example`
}
export const getClientExampleDir = () => `${getDemosDir()}/client-example`

export const getClientExampleServerDir = () => {
return `${getDemosDir()}/client-example-server`
}
export const getClientExampleServerDir = () => `${getDemosDir()}/client-example-server`

export const getClientBundleExampleDir = () => {
return `${getDemosDir()}/client-bundle-example`
}
export const getClientBundleExampleDir = () => `${getDemosDir()}/client-bundle-example`

export const getDappExampleDir = () => {
return `${getDemosDir()}/dapp-example`
}
export const getDappExampleDir = () => `${getDemosDir()}/dapp-example`

export const getCommonPkgDir = () => {
return `${getPackagesDir()}/common`
}
export const getCommonPkgDir = () => `${getPackagesDir()}/common`

export const getAccountPkgDir = () => {
return `${getPackagesDir()}/account`
}
export const getAccountPkgDir = () => `${getPackagesDir()}/account`

export const getApiPkgDir = () => {
return `${getPackagesDir()}/api`
}
export const getApiPkgDir = () => `${getPackagesDir()}/api`

export const getCliPkgDir = () => {
return `${getPackagesDir()}/cli`
}
export const getCliPkgDir = () => `${getPackagesDir()}/cli`

export const getUtilPkgDir = () => {
return `${getPackagesDir()}/util`
}
export const getUtilPkgDir = () => `${getPackagesDir()}/util`

export const getDatabasePkgDir = () => {
return `${getPackagesDir()}/database`
}
export const getDatabasePkgDir = () => `${getPackagesDir()}/database`

export const getDatasetsPkgDir = () => {
return `${getPackagesDir()}/datasets`
}
export const getDatasetsPkgDir = () => `${getPackagesDir()}/datasets`

export const getDatasetsFsPkgDir = () => {
return `${getPackagesDir()}/datasets-fs`
}
export const getDatasetsFsPkgDir = () => `${getPackagesDir()}/datasets-fs`

export const getEnvPkgDir = () => {
return `${getPackagesDir()}/env`
}
export const getEnvPkgDir = () => `${getPackagesDir()}/env`

export const getFileServerPkgDir = () => {
return `${getPackagesDir()}/file-server`
}
export const getFileServerPkgDir = () => `${getPackagesDir()}/file-server`

export const getProcaptchaPkgDir = () => {
return `${getPackagesDir()}/procaptcha`
}
export const getProcaptchaPkgDir = () => `${getPackagesDir()}/procaptcha`

export const getProcaptchaReactPkgDir = () => {
return `${getPackagesDir()}/procaptcha-react`
}
export const getProcaptchaReactPkgDir = () => `${getPackagesDir()}/procaptcha-react`

export const getProcaptchaBundlePkgDir = () => {
return `${getPackagesDir()}/procaptcha-bundle`
}
export const getProcaptchaBundlePkgDir = () => `${getPackagesDir()}/procaptcha-bundle`

export const getProcaptchaPoWPkgDir = () => {
return `${getPackagesDir()}/procaptcha-pow`
}
export const getProcaptchaPoWPkgDir = () => `${getPackagesDir()}/procaptcha-pow`

export const getProviderPkgDir = () => {
return `${getPackagesDir()}/provider`
}
export const getProviderPkgDir = () => `${getPackagesDir()}/provider`

export const getServerPkgDir = () => {
return `${getPackagesDir()}/server`
}
export const getServerPkgDir = () => `${getPackagesDir()}/server`

export const getTypesDatabasePkgDir = () => {
return `${getPackagesDir()}/types-database`
}
export const getTypesDatabasePkgDir = () => `${getPackagesDir()}/types-database`

export const getTypesEnvPkgDir = () => {
return `${getPackagesDir()}/types-env`
}
export const getTypesEnvPkgDir = () => `${getPackagesDir()}/types-env`

export const getTypesPkgDir = () => {
return `${getPackagesDir()}/types`
}
export const getTypesPkgDir = () => `${getPackagesDir()}/types`

export const getWebComponentsPkgDir = () => {
return `${getPackagesDir()}/web-components`
}
export const getTxPkgDir = () => `${getPackagesDir()}/tx`

export const getContractsDir = () => {
return `${getRootDir()}/contracts`
}
export const getWebComponentsPkgDir = () => `${getPackagesDir()}/web-components`

export const getProtocolContractsDir = () => {
return `${getProtocolDir()}/contracts`
}
export const getContractsDir = () => `${getRootDir()}/contracts`

export const getProtocolContractsDir = () => `${getProtocolDir()}/contracts`
1 change: 1 addition & 0 deletions dev/config/src/vite/vite.test.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ export default function () {
},
test: {
//root: getRootDir(),
reporters: ['basic'],
include: ['src/**/*.{test,spec}.?(c|m)[jt]s?(x)'],
watch: false,
watchExclude: ['**/node_modules/**', '**/dist/**'],
Expand Down
121 changes: 121 additions & 0 deletions dev/scripts/src/contract/fundDapps.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
// Copyright 2021-2024 Prosopo (UK) Ltd.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
import { Abi } from '@polkadot/api-contract'
import { AbiJSON, Wasm } from '../util/index.js'
import { ContractAbi } from '@prosopo/captcha-contract/contract-info'
import { ContractFile } from '@prosopo/captcha-contract/contract-info'
import { LogLevel, getLogger } from '@prosopo/common'
import { ProviderEnvironment } from '@prosopo/env'
import { TransactionQueue } from '@prosopo/tx'
import { defaultConfig } from '@prosopo/cli'
import { getPairAsync, oneUnit } from '@prosopo/contract'
import { hexToU8a } from '@polkadot/util'
import { loadEnv } from '@prosopo/cli'
import { setupDapp } from '../setup/index.js'
import mongoose, { Model } from 'mongoose'
import path from 'path'

const log = getLogger(LogLevel.enum.info, 'dev.deploy')

interface Emails {
email: string
name: string
url: string
account: string
mnemonic: string
createdAt: number
marketingPreferences: boolean
}

const EmailModelSchema = new mongoose.Schema<Emails>(
{
email: String,
name: String,
url: String,
account: String,
mnemonic: String,
createdAt: Number,
marketingPreferences: Boolean,
},
{ collection: 'emails' }
)

let EmailsModel: typeof Model<Emails>
try {
EmailsModel = mongoose.model('emails')
} catch (error) {
EmailsModel = mongoose.model('emails', EmailModelSchema)
}

export async function run(wasmPath: string | undefined, abiPath: string | undefined, atlasUri: string | undefined) {
if (!atlasUri) {
throw new Error('Atlas URI not found in env')
}
// if wasmPath not provided then default to the captcha contract's wasm
let wasm: Uint8Array
if (wasmPath === undefined) {
log.info('Using wasm from captcha contract')
const jsonContent = JSON.parse(ContractFile)
const hex = jsonContent['source']['wasm']
wasm = hexToU8a(hex)
} else {
log.info('WASM Path', wasmPath)
wasm = await Wasm(path.resolve(wasmPath))
}
// if abiPath not provided then default to the captcha contract's abi
let abi: Abi
if (abiPath === undefined) {
log.info('Using abi from captcha contract')
abi = new Abi(ContractAbi)
} else {
log.info('ABI Path', abiPath)
abi = await AbiJSON(path.resolve(abiPath))
}

await mongoose.connect(atlasUri, { dbName: 'prosopo' })

const dapps = await EmailsModel.find({}).exec()
log.info(dapps)

const config = defaultConfig()
const network = config.networks[config.defaultNetwork]
const secret = config.account.secret
const pair = await getPairAsync(network, secret)
const env = new ProviderEnvironment(config, pair)
await env.isReady()
const queue = new TransactionQueue(env.getApi(), pair, config.logLevel)
if (!config.account.address) {
throw new Error('Account address not found in config')
}
if (!secret) {
throw new Error('Account secret not found in config')
}
const fundAmount = oneUnit(env.getApi())
await Promise.all(
dapps
.filter((dappRecord) => dappRecord.account)
.map((dappRecord) => setupDapp(env, { pair, secret, fundAmount }, dappRecord.account, queue))
)
}
log.info('Loading env from', path.resolve('.'))
loadEnv(path.resolve('.'))
run(process.env.PROSOPO_CAPTCHA_WASM_PATH, process.env.PROSOPO_CAPTCHA_ABI_PATH, process.env._DEV_ONLY_ATLAS_URI)
.then((result) => {
log.info(result)
process.exit(0)
})
.catch((e) => {
console.error(e)
process.exit(1)
})
Loading

0 comments on commit 61fadab

Please sign in to comment.