fix: vrf fixes

This commit is contained in:
ppe
2022-05-15 22:34:15 +02:00
parent 86a1f6288e
commit 6d86046da9
5 changed files with 84 additions and 22 deletions

View File

@@ -149,7 +149,7 @@ export interface Contract<State = unknown> {
*/
bundleInteraction<Input = unknown>(
input: Input,
options: {
options?: {
tags?: Tags;
strict?: boolean;
vrf?: boolean;

View File

@@ -230,15 +230,27 @@ export class HandlerBasedContract<State> implements Contract<State> {
async bundleInteraction<Input>(
input: Input,
options: {
tags: [];
strict: false;
vrf: false;
tags: Tags;
strict: boolean;
vrf: boolean;
} = {
tags: [],
strict: false,
vrf: false
}
): Promise<any | null> {
this.logger.info('Bundle interaction input', input);
if (!this.signer) {
throw new Error("Wallet not connected. Use 'connect' method first.");
}
options = {
tags: [],
strict: false,
vrf: false,
...options
};
const interactionTx = await this.createInteraction(input, options.tags, emptyTransfer, options.strict, options.vrf);
const response = await fetch(`${this._evaluationOptions.bundlerUrl}gateway/sequencer/register`, {
@@ -268,13 +280,7 @@ export class HandlerBasedContract<State> implements Contract<State> {
};
}
private async createInteraction<Input>(
input: Input,
tags: { name: string; value: string }[],
transfer: ArTransfer,
strict: boolean,
vrf = false
) {
private async createInteraction<Input>(input: Input, tags: Tags, transfer: ArTransfer, strict: boolean, vrf = false) {
if (this._evaluationOptions.internalWrites) {
// Call contract and verify if there are any internal writes:
// 1. Evaluate current contract state

View File

@@ -194,7 +194,7 @@ export class DefaultCreateContract implements CreateContract {
};
}
const response = await fetch(`https://gateway.redstone.finance/gateway/contracts/deploy`, {
const response = await fetch(`http://localhost:5666/gateway/contracts/deploy`, {
method: 'POST',
body: JSON.stringify(body),
headers: {

View File

@@ -2,15 +2,13 @@
import Arweave from 'arweave';
import {
LoggerFactory,
MemCache,
RedstoneGatewayContractDefinitionLoader,
RedstoneGatewayInteractionsLoader, sleep,
SmartWeaveNodeFactory, SmartWeaveTags
SmartWeaveNodeFactory
} from '../src';
import { readJSON } from '../../redstone-smartweave-examples/src/_utils';
import { TsLogFactory } from '../src/logging/node/TsLogFactory';
import path from "path";
import knex from "knex";
import fs from "fs";
import {JWKInterface} from "arweave/node/lib/wallet";
const logger = LoggerFactory.INST.create('Contract');
@@ -41,20 +39,40 @@ async function main() {
});
const smartweave = (await SmartWeaveNodeFactory.knexCachedBased(arweave, knexConfig))
.useRedStoneGateway()
.useRedStoneGateway(null, null, "http://localhost:5666/")
.build();
const jwk = readJSON('../redstone-node/.secrets/redstone-jwk.json');
const wallet: JWKInterface = readJSON('.secrets/33F0QHcb22W7LwWR1iRC8Az1ntZG09XQ03YWuw2ABqA.json');
const jsContractSrc = fs.readFileSync(path.join(__dirname, 'data/js/token-pst.js'), 'utf8');
const initialState = fs.readFileSync(path.join(__dirname, 'data/js/token-pst.json'), 'utf8');
// case 1 - full deploy, js contract
const contractTxId = await smartweave.createContract.deploy({
wallet,
initState: initialState,
src: jsContractSrc,
}, true);
logger.info("tx id:", contractTxId);
// connecting to a given contract
const token = smartweave
.contract("_iWbqZMSq_maTbNQKRZ-S_is0Ilj5L0y9UJslZBChnk")
.contract<any>(contractTxId)
// connecting wallet to a contract. It is required before performing any "writeInteraction"
// calling "writeInteraction" without connecting to a wallet first will cause a runtime error.
.connect(jwk);
.connect(wallet)
.setEvaluationOptions({
bundlerUrl: "http://localhost:5666/"
});
const result = await token.bundleInteraction<any>({
function: "vrf",
}, {vrf: true});
const {state} = await token.readState();
logger.info("State", state);
logger.info("State", state.vrf);
/*const result = await token.writeInteraction({
function: "transfer",
@@ -107,5 +125,14 @@ async function main() {
*/
}
function readJSON(path: string): JWKInterface {
const content = fs.readFileSync(path, "utf-8");
try {
return JSON.parse(content);
} catch (e) {
throw new Error(`File "${path}" does not contain a valid JSON`);
}
}
main().catch((e) => console.error(e));

View File

@@ -15,6 +15,35 @@ export async function handle(state, action) {
return { state };
}
if (input.function === 'vrf') {
if (!state.vrf) {
state.vrf = {};
}
state.vrf[SmartWeave.transaction.id] = {
vrf: SmartWeave.vrf.data,
value: SmartWeave.vrf.value,
random_6_1: SmartWeave.vrf.randomInt(6),
random_6_2: SmartWeave.vrf.randomInt(6),
random_6_3: SmartWeave.vrf.randomInt(6),
random_12_1: SmartWeave.vrf.randomInt(12),
random_12_2: SmartWeave.vrf.randomInt(12),
random_12_3: SmartWeave.vrf.randomInt(12),
random_46_1: SmartWeave.vrf.randomInt(46),
random_46_2: SmartWeave.vrf.randomInt(46),
random_46_3: SmartWeave.vrf.randomInt(46),
random_99_1: SmartWeave.vrf.randomInt(99),
random_99_2: SmartWeave.vrf.randomInt(99),
random_99_3: SmartWeave.vrf.randomInt(99),
}
return {state};
}
if (input.function === 'transfer') {
const target = input.target;
const qty = input.qty;