【问题标题】:Redirect the page after the account received some amount of balance in [payable] method?账户以[payable]方式收到一定余额后重定向页面?
【发布时间】:2021-07-22 17:38:33
【问题描述】:

当我使用payable方式时,页面跳转到钱包审批金额,但是重定向后又返回到旧页面,重定向后下面的代码没有运行。

这里是rust合约函数:

#[payable]
pub fn add_liquidity(&mut self, tokens: u128, account_id: AccountId) -> u128 {
        let amount = env::attached_deposit();
}

这里是 reactjs 代码:

onSubmit={async (values, actions) => {
  try {
        const data = await nearvar.contract.add_liquidity({"tokens": values.buy.toString(), "account_id": "amiyarust.testnet"}, 95000000000000, 500)
        // Page redirects here. Code below is not run. 
        actions.setSubmitting(false)
        history.push(`/hello`)
    } catch (e) {
       console.error(e)
   }

我想在重定向后运行history.push(`/hello`)
但它会刷新页面,并再次呈现表单。 如何解决?

【问题讨论】:

    标签: reactjs nearprotocol


    【解决方案1】:

    您必须在重定向之前在 localStorage 中存储一些内容。

    例如在重定向之前,您预计该帐户会发生什么?

    类似于他们当前的余额,并且您预计它会增加 1 N。

    然后它们被重定向回您的页面,您应该检查此情况,然后继续执行其余代码。

    这里有一个例子:

    https://github.com/near-examples/nearnames/blob/54dd7dd1c6adca4688c76c78db3ea6bd10ed2a13/src/state/near.js#L93-L110

    具体如下: https://github.com/near-examples/nearnames/blob/54dd7dd1c6adca4688c76c78db3ea6bd10ed2a13/src/state/near.js#L106

    在此处重新读取重定向的内容: https://github.com/near-examples/nearnames/blob/54dd7dd1c6adca4688c76c78db3ea6bd10ed2a13/src/state/near.js#L55

    所以这个应用程序“知道”待处理的交易(应该创建的帐户),然后当用户被重定向时,应用程序会检查这些帐户是否真的是通过 TX 创建的。

    然后就可以显示这些链接等...

    【讨论】:

      猜你喜欢
      • 2017-09-09
      • 1970-01-01
      • 1970-01-01
      • 2018-09-21
      • 1970-01-01
      • 2010-09-16
      • 2019-12-04
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多