MCPcopy Index your code
hub / github.com/CovenantSQL/CovenantSQL / ApplyTokenTask

Function ApplyTokenTask

cmd/cql-proxy/api/account.go:197–245  ·  view source on GitHub ↗

ApplyTokenTask handles the token apply process.

(ctx context.Context, cfg *config.Config, db *gorp.DbMap, t *model.Task)

Source from the content-addressed store, hash-verified

195
196// ApplyTokenTask handles the token apply process.
197func ApplyTokenTask(ctx context.Context, cfg *config.Config, db *gorp.DbMap, t *model.Task) (r gin.H, err error) {
198 args := struct {
199 Amount uint64 `json:"amount"`
200 }{}
201 err = json.Unmarshal(t.RawArgs, &args)
202 if err != nil {
203 err = errors.Wrapf(err, "unmarshal task args failed")
204 return
205 }
206
207 p, err := model.GetAccountByID(db, t.Developer, t.Account)
208 if err != nil {
209 err = errors.Wrapf(err, "get account for task failed")
210 return
211 }
212
213 accountAddr, err := p.Account.Get()
214 if err != nil {
215 err = errors.Wrapf(err, "decode task account failed")
216 return
217 }
218
219 txHash, err := client.TransferToken(accountAddr, args.Amount, types.Particle)
220 if err != nil {
221 err = errors.Wrapf(err, "send transfer token rpc failed")
222 return
223 }
224
225 // add record
226 ar, err := model.AddTokenApplyRecord(db, t.Developer, p.Account, args.Amount)
227 if err != nil {
228 err = errors.Wrapf(err, "record token application log failed")
229 return
230 }
231
232 // wait for transaction to complete in several cycles
233 timeoutCtx, cancelCtx := context.WithTimeout(ctx, 3*time.Minute)
234 defer cancelCtx()
235
236 lastState, _ := waitForTxState(timeoutCtx, txHash)
237 r = gin.H{
238 "id": ar.ID,
239 "account": p.Account,
240 "tx": txHash.String(),
241 "state": lastState.String(),
242 }
243
244 return
245}

Callers

nothing calls this directly

Calls 7

GetAccountByIDFunction · 0.92
TransferTokenFunction · 0.92
AddTokenApplyRecordFunction · 0.92
waitForTxStateFunction · 0.85
UnmarshalMethod · 0.80
GetMethod · 0.65
StringMethod · 0.45

Tested by

no test coverage detected