Skip to content

Commit

Permalink
more eth statements and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
albertolerda committed Oct 10, 2023
1 parent 501af9b commit f0b500f
Show file tree
Hide file tree
Showing 4 changed files with 153 additions and 22 deletions.
44 changes: 25 additions & 19 deletions pkg/ethereum/src/plugins.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { PluginContext, PluginResult } from '@slangroom/core/plugin';
import {JsonableArray } from '@slangroom/shared/jsonable';
import { lex, parse, visit, EthereumRequestKind, type PhraseCst } from '@slangroom/ethereum';
import { Web3 } from 'web3';

Expand Down Expand Up @@ -37,29 +38,34 @@ export const execute = async (

if (kind === EthereumRequestKind.EthereumBalance) {
// TODO: different statement for string and array
const address = ctx.fetch('address');
if (Array.isArray(address)) {
const address = ctx.get('address');
if(address) {
return ctx.pass((await web3.eth.getBalance(address as string)).toString());
}
const addresses = ctx.fetch('addresses');
if (Array.isArray(addresses)) {
const balances = await Promise.all(
address.map((addr) => web3.eth.getBalance(addr as string))
addresses.map((addr) => web3.eth.getBalance(addr as string))
);
return ctx.pass(balances.map((b) => b.toString()));
return ctx.pass(balances.map((b) => b.toString()) as JsonableArray);
} else {
throw new Error("Undefined argument")
}
}
if (kind === EthereumRequestKind.EthereumBytes) {
const tag = ctx.fetch('transaction_id') as string;
const receipt = await web3.eth.getTransactionReceipt(
tag.startsWith('0x') ? tag : '0x' + tag
);
if (!receipt) return ctx.fail("Transaction id doesn't exist");
if (!receipt.status) return ctx.fail('Failed transaction');
try {
const dataRead = receipt.logs[0]?.data?.slice(2);
return ctx.pass(dataRead?.toString() || "");
} catch (e) {
return ctx.fail('Empty transaction');
}
return ctx.pass((await web3.eth.getBalance(address as string)).toString());
}
// if (kind === EthereumRequestKind.EthereumBytes) {
// const tag = ctx.fetch('transaction_id') as string;
// const receipt = await web3.eth.getTransactionReceipt(
// tag.startsWith('0x') ? tag : '0x' + tag
// );
// if (!receipt) return ctx.fail("Transaction id doesn't exist");
// if (!receipt.status) return ctx.fail('Failed transaction');
// try {
// const dataRead = receipt.logs[0]?.data?.slice(2);
// return ctx.pass(dataRead);
// } catch (e) {
// return ctx.fail('Empty transaction');
// }
// }
return ctx.fail('Should not be here');
};

Expand Down
5 changes: 5 additions & 0 deletions pkg/ethereum/src/visitor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,11 @@ class V extends CstVisitor {
if (ctx.id) return EthereumRequestKind.Erc721Id;
throw new Error('Should not be here!!');
}

broadcast() {}
gasPrice() {}
totalSupply() {}
id() {}
}

const Visitor = new V();
Expand Down
3 changes: 0 additions & 3 deletions pkg/ethereum/test/make-ava-happy.ts

This file was deleted.

123 changes: 123 additions & 0 deletions pkg/ethereum/test/plugin.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
import anyTest, {TestFn} from 'ava';
import { Web3 } from 'web3'
import { PluginContextTest } from '@slangroom/core';
import { astify, execute } from '@slangroom/ethereum';

const test = anyTest as TestFn<{web3: Web3}>;


test("read the ethereum nonce", async (t) => {
const { ast, errors} = astify('read the ethereum nonce');
if (errors) {
t.fail();
return;
}
const ctx = new PluginContextTest('http://78.47.38.223:9485', {
address: "0x2D010920b43aFb54f8d5fB51c9354FbC674b28Fc",
});
const res = await execute(ctx, ast);
t.deepEqual(res, {
ok: true,
value: '0',
});
})

test("Ethereum gas price", async (t) => {
const { ast, errors} = astify('read the ethereum suggested gas price');
if (errors) {
t.fail();
return;
}
const ctx = PluginContextTest.connect('http://78.47.38.223:9485');
const res = await execute(ctx, ast);
t.truthy(res.ok)
if(res.ok) {
console.log(res.value);
t.is(typeof res.value, 'string');
}
})

test("Retrieve a zenroom object", async (t) => {
const poem = "000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000674e656c206d657a7a6f2064656c2063616d6d696e206469206e6f7374726120766974610a6d6920726974726f7661692070657220756e612073656c7661206f73637572612c0a6368c3a9206c612064697269747461207669612065726120736d6172726974612e00000000000000000000000000000000000000000000000000"
const { ast, errors} = astify('read the ethereum bytes');
if (errors) {
t.fail();
return;
}
const ctx = new PluginContextTest('http://78.47.38.223:9485', {
transaction_id: '0x0467636a2557a1ccdaf10ce17ee74340096c510acfa9181c85756d43a8bed522'
});
const res = await execute(ctx, ast);
t.deepEqual(res, {
ok: true,
value: poem,
});
})

test("Ethereum balance", async (t) => {
const { ast, errors } = astify("read the ethereum balance");
if (errors) {
t.fail();
return;
}
const ctx = new PluginContextTest('http://78.47.38.223:9485', {
address: "0x2D010920b43aFb54f8d5fB51c9354FbC674b28Fc"
});
const res = await execute(ctx, ast);
t.deepEqual(res, {
ok: true,
value: "1000000000000000000000",
});
})

test("Read the balance of an array of addresses", async (t) => {
const { ast, errors } = astify("read the Ethereum balance");
if (errors) {
t.fail();
return;
}
const ctx = new PluginContextTest('http://78.47.38.223:9485', {
addresses: [
"0xc32510251F77382bb9214144D2c488408Ec2047C",
"0xFf02577F140557190693cFf549025e66119FEA52",
"0x4743879F5e9dc3fcE41E30380365441E8D14CCEc"
]
});
const res = await execute(ctx, ast);
t.truthy(res.ok)
if(res.ok) {
for (const v of (res.value as string[]))
t.is(typeof v, "string")
}
});
/*
test("Ethereum transaction id after broadcast", async (t) => {
const ast = line2Ast("Ethereum transaction id after broadcast of 'signed tx'");
t.deepEqual(ast.value, { kind: EthereumRequestKind.EthereumBroadcast, rawTransaction: 'signed tx'})
})
test("Erc20 method without arg", async (t) => {
const ast = line2Ast("erc20 'symbol' for 'foo'");
t.deepEqual(ast.value, { kind: EthereumRequestKind.Erc20Method, address: 'foo' })
})
test("Erc20 method with arg", async (t) => {
const ast = line2Ast("erc20 'balance' of 'address' for 'foo'");
t.deepEqual(ast.value, { kind: EthereumRequestKind.Erc20Method, address: 'foo', arg: 'address' })
})
test("Erc721 id", async (t) => {
const ast = line2Ast("erc721 id in transaction 'tx id'");
t.deepEqual(ast.value, { kind: EthereumRequestKind.Erc721Id, transactionId: 'tx id' })
})
test("Erc721 asset", async (t) => {
const ast = line2Ast("erc721 asset in 'nft_id' for 'address'");
t.deepEqual(ast.value, { kind: EthereumRequestKind.Erc721Asset, address: 'address', nftId: 'nft_id' })
})
test("Erc721 owner", async (t) => {
const ast = line2Ast("erc721 owner of 'nft_id' for 'address'");
t.deepEqual(ast.value, { kind: EthereumRequestKind.Erc721Owner, address: 'address', nftId: 'nft_id' })
})
*/

0 comments on commit f0b500f

Please sign in to comment.