forked from gitter-badger/tao2
-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
67 lines (54 loc) · 2.51 KB
/
main.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
55
56
57
58
59
60
61
62
63
64
65
66
67
package main
import (
"context"
"fmt"
"github.com/Tao-Network/tao2/core/state"
"github.com/Tao-Network/tao2/taoxlending/lendingstate"
"math/big"
"os"
"time"
"github.com/Tao-Network/tao2/accounts/abi/bind"
"github.com/Tao-Network/tao2/common"
"github.com/Tao-Network/tao2/contracts/taox"
"github.com/Tao-Network/tao2/crypto"
"github.com/Tao-Network/tao2/ethclient"
)
func main() {
client, err := ethclient.Dial("http://127.0.0.1:4545/")
if err != nil {
fmt.Println(err, client)
}
MainKey, _ := crypto.HexToECDSA(os.Getenv("LENDING_OWNER_KEY"))
MainAddr := crypto.PubkeyToAddress(MainKey.PublicKey)
nonce, _ := client.NonceAt(context.Background(), MainAddr, nil)
auth := bind.NewKeyedTransactor(MainKey)
auth.Value = big.NewInt(0) // in wei
auth.GasLimit = uint64(4000000) // in units
auth.GasPrice = big.NewInt(250000000000000)
// init trc21 issuer
auth.Nonce = big.NewInt(int64(nonce))
price := new(big.Int)
price.SetString(os.Getenv("PRICE"), 10)
lendContract, _ := taox.NewLendingRelayerRegistration(auth, common.HexToAddress(os.Getenv("LENDING_ADDRESS")), client)
token := common.HexToAddress(os.Getenv("TOKEN_ADDRESS"))
lendingToken := common.HexToAddress(os.Getenv("LENDING_TOKEN_ADDRESS"))
tx, err := lendContract.SetCollateralPrice(token, lendingToken, price)
if err != nil {
fmt.Println("Set price failed!", err)
}
time.Sleep(5 * time.Second)
r, err := client.TransactionReceipt(context.Background(), tx.Hash())
if err != nil {
fmt.Println("Get receipt failed", err)
}
fmt.Println("Done receipt status", r.Status)
collateralState := state.GetLocMappingAtKey(token.Hash(), lendingstate.CollateralMapSlot)
locMapPrices := collateralState.Add(collateralState, lendingstate.CollateralStructSlots["price"])
locLendingTokenPriceByte := crypto.Keccak256(lendingToken.Hash().Bytes(), common.BigToHash(locMapPrices).Bytes())
locCollateralPrice := common.BigToHash(new(big.Int).Add(new(big.Int).SetBytes(locLendingTokenPriceByte), lendingstate.PriceStructSlots["price"]))
locBlockNumber := common.BigToHash(new(big.Int).Add(new(big.Int).SetBytes(locLendingTokenPriceByte), lendingstate.PriceStructSlots["blockNumber"]))
priceByte, err := client.StorageAt(context.Background(), common.HexToAddress(os.Getenv("LENDING_ADDRESS")), locCollateralPrice, nil)
fmt.Println(new(big.Int).SetBytes(priceByte), err)
blockNumberByte, err := client.StorageAt(context.Background(), common.HexToAddress(os.Getenv("LENDING_ADDRESS")), locBlockNumber, nil)
fmt.Println(new(big.Int).SetBytes(blockNumberByte), err)
}