From 39bafbcfed52fbd80ac312eed6b76586e21bed60 Mon Sep 17 00:00:00 2001 From: wangshijun Date: Sat, 3 Jul 2021 21:22:29 +0800 Subject: [PATCH] chore: basic reward workflow --- api/functions/app.js | 1 + api/routes/auth/reward.js | 49 ++++++++++++++++++++++++++++++++++ src/components/auth/general.js | 1 - src/pages/index.js | 12 +++++++++ 4 files changed, 62 insertions(+), 1 deletion(-) create mode 100644 api/routes/auth/reward.js diff --git a/api/functions/app.js b/api/functions/app.js index d7aceaf..b3060dd 100644 --- a/api/functions/app.js +++ b/api/functions/app.js @@ -73,6 +73,7 @@ walletHandlers.attach(Object.assign({ app: router }, require('../routes/auth/cla walletHandlers.attach(Object.assign({ app: router }, require('../routes/auth/claim_multiple_workflow'))); walletHandlers.attach(Object.assign({ app: router }, require('../routes/auth/error'))); walletHandlers.attach(Object.assign({ app: router }, require('../routes/auth/timeout'))); +walletHandlers.attach(Object.assign({ app: router }, require('../routes/auth/reward'))); walletHandlers.attach(Object.assign({ app: router }, require('../routes/auth/transfer_asset_out'))); walletHandlers.attach(Object.assign({ app: router }, require('../routes/auth/transfer_asset_in'))); walletHandlers.attach(Object.assign({ app: router }, require('../routes/auth/transfer_token_asset_in'))); diff --git a/api/routes/auth/reward.js b/api/routes/auth/reward.js new file mode 100644 index 0000000..66805e6 --- /dev/null +++ b/api/routes/auth/reward.js @@ -0,0 +1,49 @@ +/* eslint-disable no-console */ +const SDK = require('@ocap/sdk'); +const { fromAddress } = require('@ocap/wallet'); +const { fromTokenToUnit } = require('@ocap/util'); + +const { wallet } = require('../../libs/auth'); + +const txCreators = { + TransferV2Tx: async ({ amount }) => ({ + type: 'TransferV2Tx', + data: { + itx: { + to: wallet.address, // 设置为收款人的地址,比如发帖人的 did + value: fromTokenToUnit(amount, 18), + }, + }, + description: 'Transfer some token to application using delegation', + }), +}; + +module.exports = { + action: 'reward', + + claims: { + signature: async ({ userPk, userDid, extraParams: { locale, amount } }) => { + const claim = await txCreators.TransferV2Tx({ userPk, userDid, locale, amount }); + console.log(claim); + return claim; + }, + }, + + onAuth: async ({ userDid, claims }) => { + const claim = claims.find(x => x.type === 'signature'); + + const tx = SDK.decodeTx(claim.origin); + logger.info('reward.auth.tx', tx); + + tx.signature = claim.sig; + if (claim.delegator) { + tx.delegator = claim.delegator; + tx.from = claim.from; + } + const hash = await SDK.sendTransferV2Tx({ tx, wallet: fromAddress(userDid) }); + + // hash 就是交易凭据 + + return { hash, tx: claim.origin }; + }, +}; diff --git a/src/components/auth/general.js b/src/components/auth/general.js index f5cea54..5d08f80 100644 --- a/src/components/auth/general.js +++ b/src/components/auth/general.js @@ -30,7 +30,6 @@ export default function GeneralAuthButton({ button, action, messages, timeout, e responsive action={action} checkFn={api.get} - socketUrl={api.socketUrl} onClose={() => setOpen(false)} checkTimeout={timeout} extraParams={extraParams} diff --git a/src/pages/index.js b/src/pages/index.js index 2315255..319083f 100644 --- a/src/pages/index.js +++ b/src/pages/index.js @@ -7,6 +7,7 @@ import { PlaygroundAction } from '@arcblock/did-playground'; import { UserContext } from '../context/user'; import Layout from '../components/layout'; +import AuthButton from '../components/auth/general'; import getWebWalletUrl from '../libs/util'; @@ -80,6 +81,17 @@ export default function MiniPage() { title={`Send 0.5 ${token.local.symbol}`} webWalletUrl={webWalletUrl} /> +