前端实现SP代理的终级解决方案

in STEEM CN/中文5 years ago

delegator.jpg

因为涉及到要做SP代理的一个功能,找起文档来真是要命,翻了半天也没有一个文档说明白的。看看官方的文档:

Delegates STEEM POWER, denominated in VESTS, from a delegator to the delegatee. Requires the delegator's private WIF key. Set the delegation to 0 to undelegate.

steem.broadcast.delegateVestingShares(wif, delegator, delegatee, vesting_shares, function(err, result) {
console.log(err, result);
});

wif - the active wif key, used to send a delegation, use the market, send transfers and more...
delegator - the account that sent the delegation
delegatee - the account that gets the delegation
vesting_shares - the number of vests you send, steem to vests calculator Here

看这说明要是能整明白那就是天才了!这估计只是开发员的备忘录了,啥也没说明。以前倒是零星看到些技术文档,那时没留意,现在也找不到了。问了下鱼老板,给了些建议和文档,又是一波折腾,总算是搞定的。

前端实现SP代理主要有三步:

  1. SP要先换算成VESTS。
  2. VESTS整成合规的格式,主要是6位小数加字符串。
  3. 执行代理函数delegateVestingShares

这三步也算是实现SP代理的终极方案了,不用再去查文档了!

第一步. SP要先换算成VESTS。

let s = await that.steem.api.getDynamicGlobalPropertiesAsync()
let sp_to_vests = parseFloat(s.total_vesting_shares) / parseFloat(s.total_vesting_fund_steem))

//从系统的参数中找到两个值,从面计算出换算比例,当前 1 SP = 1935.2059 VESTS

第二步. VESTS整成合规的格式

  let b = '1.8' * sp_to_vests  //1.8个SP
  let c = b.toFixed(6)
  let vests = c+' VESTS'

第三步执行代理函数

async delegate(){
  let wif = '5Kxxxxxx'
  let delegator = 'timool'
  let delegatee = "Tsteem"
  let a = await this.steem.api.getDynamicGlobalPropertiesAsync()
  let sp_to_vests = parseFloat(a.total_vesting_shares) / parseFloat(a.total_vesting_fund_steem)
  let b = '1.8' * sp_to_vests
  let c = b.toFixed(6)
  let vests = c+' VESTS'
  await this.steem.broadcast.delegateVestingSharesAsync(wif, delegator, delegatee, vests)
}

按以上三步来基本就没问题了,想怎么整都可以了。不过要注意最小代理量不能小于1SP,要取消代理直接把代理量设为0即可,取消代理要有7天才能回到原有帐户。

Coin Marketplace

STEEM 0.09
TRX 0.30
JST 0.034
BTC 114465.10
ETH 4124.03
USDT 1.00
SBD 0.59