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>( bundleInteraction<Input = unknown>(
input: Input, input: Input,
options: { options?: {
tags?: Tags; tags?: Tags;
strict?: boolean; strict?: boolean;
vrf?: boolean; vrf?: boolean;

View File

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

View File

@@ -2,15 +2,13 @@
import Arweave from 'arweave'; import Arweave from 'arweave';
import { import {
LoggerFactory, LoggerFactory,
MemCache, SmartWeaveNodeFactory
RedstoneGatewayContractDefinitionLoader,
RedstoneGatewayInteractionsLoader, sleep,
SmartWeaveNodeFactory, SmartWeaveTags
} from '../src'; } from '../src';
import { readJSON } from '../../redstone-smartweave-examples/src/_utils';
import { TsLogFactory } from '../src/logging/node/TsLogFactory'; import { TsLogFactory } from '../src/logging/node/TsLogFactory';
import path from "path"; import path from "path";
import knex from "knex"; import knex from "knex";
import fs from "fs";
import {JWKInterface} from "arweave/node/lib/wallet";
const logger = LoggerFactory.INST.create('Contract'); const logger = LoggerFactory.INST.create('Contract');
@@ -41,20 +39,40 @@ async function main() {
}); });
const smartweave = (await SmartWeaveNodeFactory.knexCachedBased(arweave, knexConfig)) const smartweave = (await SmartWeaveNodeFactory.knexCachedBased(arweave, knexConfig))
.useRedStoneGateway() .useRedStoneGateway(null, null, "http://localhost:5666/")
.build(); .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 // connecting to a given contract
const token = smartweave const token = smartweave
.contract("_iWbqZMSq_maTbNQKRZ-S_is0Ilj5L0y9UJslZBChnk") .contract<any>(contractTxId)
// connecting wallet to a contract. It is required before performing any "writeInteraction" // 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. // 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(); const {state} = await token.readState();
logger.info("State", state); logger.info("State", state.vrf);
/*const result = await token.writeInteraction({ /*const result = await token.writeInteraction({
function: "transfer", 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)); main().catch((e) => console.error(e));

View File

@@ -15,6 +15,35 @@ export async function handle(state, action) {
return { state }; 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') { if (input.function === 'transfer') {
const target = input.target; const target = input.target;
const qty = input.qty; const qty = input.qty;