|
| 1 | +package ton |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "errors" |
| 6 | + "fmt" |
| 7 | + "math/big" |
| 8 | + |
| 9 | + "github.com/xssnick/tonutils-go/address" |
| 10 | + "github.com/xssnick/tonutils-go/ton" |
| 11 | + "github.com/xssnick/tonutils-go/tvm/cell" |
| 12 | + |
| 13 | + "github.com/smartcontractkit/mcms/sdk" |
| 14 | +) |
| 15 | + |
| 16 | +var _ sdk.TimelockInspector = (*timelockInspector)(nil) |
| 17 | + |
| 18 | +// TimelockInspector is an Inspector implementation for Sui, for accessing the MCMS-Timelock contract |
| 19 | +type timelockInspector struct { |
| 20 | + client *ton.APIClient |
| 21 | +} |
| 22 | + |
| 23 | +func NewTimelockInspector(client *ton.APIClient) (sdk.TimelockInspector, error) { |
| 24 | + return &timelockInspector{client}, nil |
| 25 | +} |
| 26 | + |
| 27 | +func (i timelockInspector) GetMinDelay(ctx context.Context, _address string) (uint64, error) { |
| 28 | + // Map to Ton Address type (timelock.address) |
| 29 | + addr, err := address.ParseAddr(_address) |
| 30 | + if err != nil { |
| 31 | + return 0, fmt.Errorf("invalid timelock address: %w", err) |
| 32 | + } |
| 33 | + |
| 34 | + // TODO: mv and import from github.com/smartcontractkit/chainlink-ton/bindings/mcms/timelock |
| 35 | + block, err := i.client.CurrentMasterchainInfo(ctx) |
| 36 | + if err != nil { |
| 37 | + return 0, fmt.Errorf("failed to get current masterchain info: %w", err) |
| 38 | + } |
| 39 | + |
| 40 | + result, err := i.client.RunGetMethod(ctx, block, addr, "getMinDelay") |
| 41 | + if err != nil { |
| 42 | + return 0, fmt.Errorf("error getting getMinDelay: %w", err) |
| 43 | + } |
| 44 | + |
| 45 | + rs, err := result.Slice(0) |
| 46 | + if err != nil { |
| 47 | + return 0, fmt.Errorf("error getting minDelay slice: %w", err) |
| 48 | + } |
| 49 | + |
| 50 | + return rs.LoadUInt(64) |
| 51 | +} |
| 52 | + |
| 53 | +// GetProposers returns the list of addresses with the proposer role |
| 54 | +func (i timelockInspector) GetProposers(ctx context.Context, address string) ([]string, error) { |
| 55 | + return nil, errors.New("unimplemented") |
| 56 | +} |
| 57 | + |
| 58 | +// GetExecutors returns the list of addresses with the executor role |
| 59 | +func (i timelockInspector) GetExecutors(ctx context.Context, address string) ([]string, error) { |
| 60 | + return nil, errors.New("unimplemented") |
| 61 | +} |
| 62 | + |
| 63 | +// GetBypassers returns the list of addresses with the bypasser role |
| 64 | +func (i timelockInspector) GetBypassers(ctx context.Context, address string) ([]string, error) { |
| 65 | + return nil, errors.New("unimplemented") |
| 66 | +} |
| 67 | + |
| 68 | +// GetCancellers returns the list of addresses with the canceller role |
| 69 | +func (i timelockInspector) GetCancellers(ctx context.Context, address string) ([]string, error) { |
| 70 | + return nil, errors.New("unimplemented") |
| 71 | +} |
| 72 | + |
| 73 | +func (i timelockInspector) IsOperation(ctx context.Context, _address string, opID [32]byte) (bool, error) { |
| 74 | + // Map to Ton Address type (timelock.address) |
| 75 | + addr, err := address.ParseAddr(_address) |
| 76 | + if err != nil { |
| 77 | + return false, fmt.Errorf("invalid timelock address: %w", err) |
| 78 | + } |
| 79 | + |
| 80 | + // TODO: mv and import from github.com/smartcontractkit/chainlink-ton/bindings/mcms/timelock |
| 81 | + block, err := i.client.CurrentMasterchainInfo(ctx) |
| 82 | + if err != nil { |
| 83 | + return false, fmt.Errorf("failed to get current masterchain info: %w", err) |
| 84 | + } |
| 85 | + |
| 86 | + _opID, err := mapOpIDParam(opID) |
| 87 | + if err != nil { |
| 88 | + return false, fmt.Errorf("failed to map opID param: %w", err) |
| 89 | + } |
| 90 | + |
| 91 | + result, err := i.client.RunGetMethod(ctx, block, addr, "isOperation", _opID) |
| 92 | + if err != nil { |
| 93 | + return false, fmt.Errorf("error getting isOperation: %w", err) |
| 94 | + } |
| 95 | + |
| 96 | + rs, err := result.Slice(0) |
| 97 | + if err != nil { |
| 98 | + return false, fmt.Errorf("error getting isOperation slice: %w", err) |
| 99 | + } |
| 100 | + |
| 101 | + return rs.LoadBoolBit() |
| 102 | +} |
| 103 | + |
| 104 | +func (i timelockInspector) IsOperationPending(ctx context.Context, _address string, opID [32]byte) (bool, error) { |
| 105 | + // Map to Ton Address type (timelock.address) |
| 106 | + addr, err := address.ParseAddr(_address) |
| 107 | + if err != nil { |
| 108 | + return false, fmt.Errorf("invalid timelock address: %w", err) |
| 109 | + } |
| 110 | + |
| 111 | + // TODO: mv and import from github.com/smartcontractkit/chainlink-ton/bindings/mcms/timelock |
| 112 | + block, err := i.client.CurrentMasterchainInfo(ctx) |
| 113 | + if err != nil { |
| 114 | + return false, fmt.Errorf("failed to get current masterchain info: %w", err) |
| 115 | + } |
| 116 | + |
| 117 | + _opID, err := mapOpIDParam(opID) |
| 118 | + if err != nil { |
| 119 | + return false, fmt.Errorf("failed to map opID param: %w", err) |
| 120 | + } |
| 121 | + |
| 122 | + result, err := i.client.RunGetMethod(ctx, block, addr, "isOperationPending", _opID) |
| 123 | + if err != nil { |
| 124 | + return false, fmt.Errorf("error getting isOperationPending: %w", err) |
| 125 | + } |
| 126 | + |
| 127 | + rs, err := result.Slice(0) |
| 128 | + if err != nil { |
| 129 | + return false, fmt.Errorf("error getting isOperationPending slice: %w", err) |
| 130 | + } |
| 131 | + |
| 132 | + return rs.LoadBoolBit() |
| 133 | +} |
| 134 | + |
| 135 | +func (i timelockInspector) IsOperationReady(ctx context.Context, _address string, opID [32]byte) (bool, error) { |
| 136 | + // Map to Ton Address type (timelock.address) |
| 137 | + addr, err := address.ParseAddr(_address) |
| 138 | + if err != nil { |
| 139 | + return false, fmt.Errorf("invalid timelock address: %w", err) |
| 140 | + } |
| 141 | + |
| 142 | + // TODO: mv and import from github.com/smartcontractkit/chainlink-ton/bindings/mcms/timelock |
| 143 | + block, err := i.client.CurrentMasterchainInfo(ctx) |
| 144 | + if err != nil { |
| 145 | + return false, fmt.Errorf("failed to get current masterchain info: %w", err) |
| 146 | + } |
| 147 | + |
| 148 | + _opID, err := mapOpIDParam(opID) |
| 149 | + if err != nil { |
| 150 | + return false, fmt.Errorf("failed to map opID param: %w", err) |
| 151 | + } |
| 152 | + |
| 153 | + result, err := i.client.RunGetMethod(ctx, block, addr, "isOperationReady", _opID) |
| 154 | + if err != nil { |
| 155 | + return false, fmt.Errorf("error getting isOperationReady: %w", err) |
| 156 | + } |
| 157 | + |
| 158 | + rs, err := result.Slice(0) |
| 159 | + if err != nil { |
| 160 | + return false, fmt.Errorf("error getting isOperationReady slice: %w", err) |
| 161 | + } |
| 162 | + |
| 163 | + return rs.LoadBoolBit() |
| 164 | +} |
| 165 | + |
| 166 | +func (i timelockInspector) IsOperationDone(ctx context.Context, _address string, opID [32]byte) (bool, error) { |
| 167 | + // Map to Ton Address type (timelock.address) |
| 168 | + addr, err := address.ParseAddr(_address) |
| 169 | + if err != nil { |
| 170 | + return false, fmt.Errorf("invalid timelock address: %w", err) |
| 171 | + } |
| 172 | + |
| 173 | + // TODO: mv and import from github.com/smartcontractkit/chainlink-ton/bindings/mcms/timelock |
| 174 | + block, err := i.client.CurrentMasterchainInfo(ctx) |
| 175 | + if err != nil { |
| 176 | + return false, fmt.Errorf("failed to get current masterchain info: %w", err) |
| 177 | + } |
| 178 | + |
| 179 | + _opID, err := mapOpIDParam(opID) |
| 180 | + if err != nil { |
| 181 | + return false, fmt.Errorf("failed to map opID param: %w", err) |
| 182 | + } |
| 183 | + |
| 184 | + result, err := i.client.RunGetMethod(ctx, block, addr, "isOperationDone", _opID) |
| 185 | + if err != nil { |
| 186 | + return false, fmt.Errorf("error getting isOperationDone: %w", err) |
| 187 | + } |
| 188 | + |
| 189 | + rs, err := result.Slice(0) |
| 190 | + if err != nil { |
| 191 | + return false, fmt.Errorf("error getting isOperationDone slice: %w", err) |
| 192 | + } |
| 193 | + |
| 194 | + return rs.LoadBoolBit() |
| 195 | +} |
| 196 | + |
| 197 | +// Help function to map (encode) opID param to cell.Slice |
| 198 | +func mapOpIDParam(opID [32]byte) (*cell.Slice, error) { |
| 199 | + b := cell.BeginCell() |
| 200 | + if err := b.StoreBigUInt(new(big.Int).SetBytes(opID[:]), 256); err != nil { |
| 201 | + return nil, fmt.Errorf("failed to store domain separator: %w", err) |
| 202 | + } |
| 203 | + |
| 204 | + return b.EndCell().BeginParse(), nil |
| 205 | +} |
0 commit comments