Skip to content

Commit

Permalink
Merge pull request #2 from OPENSPHERE-Inc/upgrade_symbol_service
Browse files Browse the repository at this point in the history
v0.2.0
  • Loading branch information
hanatyan128 authored Jan 22, 2023
2 parents a740ba0 + 3041606 commit 0db3bf0
Show file tree
Hide file tree
Showing 9 changed files with 136 additions and 105 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,7 @@ Rust コードの深堀はしませんが、`exchange` という関数が JavaSc

上記二つの関数は、後述する JavaScript 側のグローバル関数を呼び出します。

### 3.2. [/src/services/SmartTransactionService.ts](./src/services/SmartTransactionService.ts)
### 3.2. [/src/services/smarttx.ts](./src/services/smarttx.ts)

#### ・call 関数

Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "smart-transaction-poc",
"version": "0.1.0",
"version": "0.2.0",
"description": "Smart Transaction for Symbol PoC",
"main": "dist/index.js",
"author": "Shinichi Hanayama <[email protected]>",
Expand All @@ -10,7 +10,7 @@
"dotenv": "^16.0.3",
"js-base64": "^3.7.3",
"js-sha3": "https://github.com/Propine/js-sha3.git",
"metal-on-symbol": "^0.1.16",
"metal-on-symbol": "^0.2.4",
"rxjs": "^7.6.0",
"simple-exchange-wasm": "file:./webasm/simple-exchange/pkg",
"symbol-sdk": "^2.0.3"
Expand Down
22 changes: 15 additions & 7 deletions src/buyer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import metal from "./metal.json" assert { type: "json" };
import {Logger, MetalService, SymbolService} from "metal-on-symbol";
import assert from "assert";
import init, { exchange } from "simple-exchange-wasm/simple_exchange_wasm.js";
import {SmartTransactionService} from "./services/SmartTransactionService.js";
import {Account} from "symbol-sdk";
import {SmartTransactionService} from "./services/index.js";
import {Account, Deadline} from "symbol-sdk";


const buyAmount = 2.0;
Expand All @@ -15,23 +15,31 @@ assert(process.env.BUYER_PRIVATE_KEY);
const buyerPrivateKey = process.env.BUYER_PRIVATE_KEY;

assert(process.env.NODE_URL);
SymbolService.init({ node_url: process.env.NODE_URL, logging: true })
const symbolService = new SymbolService({ node_url: process.env.NODE_URL });
const metalService = new MetalService(symbolService);
Logger.init({ log_level: Logger.LogLevel.DEBUG });

const main = async () => {
const { networkType } = await SymbolService.getNetwork();
const { networkType, epochAdjustment } = await symbolService.getNetwork();

// Fetch smart transaction
console.log(`Loading smart transaction from Metal: ${metal.metalId}`);
//const smartTx = {
// payload: fs.readFileSync("./webasm/simple-exchange/pkg/simple_exchange_wasm_bg.wasm"),
// targetAddress: Address.createFromPublicKey(metal.targetPublicKey, networkType),
//};
const smartTx = await MetalService.fetchByMetalId(metal.metalId);
const smartTx = await metalService.fetchByMetalId(metal.metalId);

// Init smart transaction
const buyerAccount = Account.createFromPrivateKey(buyerPrivateKey, networkType);
await SmartTransactionService.init(buyerAccount, metal.metalId, smartTx.targetAddress);
const smartTxService = new SmartTransactionService(
symbolService,
buyerAccount,
metal.metalId,
smartTx.targetAddress,
Deadline.create(epochAdjustment, 5)
);
global.symbolLibrary = smartTxService;

// Execute smart transaction
console.log("Executing smart transaction.");
Expand All @@ -47,7 +55,7 @@ const main = async () => {
method_name: "exchange",
arguments: [ buyerAccount.publicKey, buyAmount ],
};
await SmartTransactionService.call(callData);
await smartTxService.call(callData);
};

main()
Expand Down
5 changes: 3 additions & 2 deletions src/global.d.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import {SymbolLibrary} from "./services";

export {};

declare global {
var getAccountBalance: (account: string, mosaic_id: string) => Promise<number>;
var transferMosaic: (from: string, to: string, mosaic_id: string, amount: number, message: string) => Promise<void>;
var symbolLibrary: SymbolLibrary;
}
19 changes: 11 additions & 8 deletions src/seller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,16 @@ dotenv.config();
import {Logger, MetalService, SymbolService} from "metal-on-symbol";
import assert from "assert";
import init, { exchange } from "simple-exchange-wasm/simple_exchange_wasm.js";
import {SmartTransactionService} from "./services/SmartTransactionService.js";
import {Account, AggregateTransaction, Transaction, TransactionType, TransferTransaction} from "symbol-sdk";
import {SmartTransactionService} from "./services/index.js";
import {Account, AggregateTransaction, Deadline, Transaction, TransactionType, TransferTransaction} from "symbol-sdk";


assert(process.env.SELLER_PRIVATE_KEY);
const sellerPrivateKey = process.env.SELLER_PRIVATE_KEY;

assert(process.env.NODE_URL);
SymbolService.init({ node_url: process.env.NODE_URL, logging: true });
const symbolService = new SymbolService({ node_url: process.env.NODE_URL });
const metalService = new MetalService(symbolService);
Logger.init({ log_level: Logger.LogLevel.DEBUG });

const parseCallTxPayload = (payload: string) => {
Expand All @@ -37,7 +38,7 @@ const handleCallerTx = async (account: Account, tx: TransferTransaction) => {

// Fetch smart transaction
console.log(`Loading smart transaction from Metal: ${callTxPayload.metal_id}`);
const smartTx = await MetalService.fetchByMetalId(callTxPayload.metal_id);
const smartTx = await metalService.fetchByMetalId(callTxPayload.metal_id);

// Check smart transaction ownership
if (!smartTx.targetAddress.equals(account.address)) {
Expand All @@ -46,12 +47,14 @@ const handleCallerTx = async (account: Account, tx: TransferTransaction) => {
}

// Init smart transaction
await SmartTransactionService.init(
const smartTxService = new SmartTransactionService(
symbolService,
account,
callTxPayload.metal_id,
smartTx.targetAddress,
callTxPayload.deadline
Deadline.createFromAdjustedValue(callTxPayload.deadline),
);
global.symbolLibrary = smartTxService;

// Execute smart transaction
console.log("Executing smart transaction.");
Expand All @@ -64,7 +67,7 @@ const handleCallerTx = async (account: Account, tx: TransferTransaction) => {

// Cosign and announce transaction
console.log("Validating and fulfilling call of smart transaction.");
await SmartTransactionService.fulfill(callTxPayload);
await smartTxService.fulfill(callTxPayload);

console.log("Handling of call transaction completed.");
} catch (e) {
Expand All @@ -73,7 +76,7 @@ const handleCallerTx = async (account: Account, tx: TransferTransaction) => {
};

const main = async () => {
const { networkType, repositoryFactory } = await SymbolService.getNetwork();
const { networkType, repositoryFactory } = await symbolService.getNetwork();
const sellerAccount = Account.createFromPrivateKey(sellerPrivateKey, networkType);

const isTransferTransaction =
Expand Down
2 changes: 1 addition & 1 deletion src/services/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export * from "./SmartTransactionService.js";
export * from "./smarttx.js";
Loading

0 comments on commit 0db3bf0

Please sign in to comment.