/
do_pay.go
54 lines (45 loc) · 1.4 KB
/
do_pay.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
package upload
import (
"fmt"
"github.com/ethereum/go-ethereum/common"
"math/big"
"time"
"github.com/bittorrent/go-btfs/chain"
"github.com/bittorrent/go-btfs/core/commands/storage/upload/sessions"
)
func payInCheque(rss *sessions.RenterSession) error {
for i, hash := range rss.ShardHashes {
shard, err := sessions.GetRenterShard(rss.CtxParams, rss.SsId, hash, i)
if err != nil {
return err
}
c, err := shard.Contracts()
if err != nil {
return err
}
// token: get real amount
//realAmount, err := getRealAmount(c.SignedGuardContract.Amount)
realAmount, err := getRealAmount(c.SignedGuardContract.Amount, rss.Token)
if err != nil {
return err
}
host := c.SignedGuardContract.HostPid
contractId := c.SignedGuardContract.ContractId
fmt.Printf("send cheque: paying... host:%v, amount:%v, contractId:%v, token:%v. \n", host, realAmount.String(), contractId, rss.Token.String())
err = chain.SettleObject.SwapService.Settle(host, realAmount, contractId, rss.Token)
if err != nil {
return err
}
time.Sleep(500 * time.Millisecond)
}
return nil
}
func getRealAmount(amount int64, token common.Address) (*big.Int, error) {
//this is price's rate [Compatible with older versions]
rateObj, err := chain.SettleObject.OracleService.CurrentRate(token)
if err != nil {
return nil, err
}
realAmount := big.NewInt(0).Mul(big.NewInt(amount), rateObj)
return realAmount, nil
}