Skip to content

Commit

Permalink
Add contractorCandidates and add SubmitContractorCandidate function
Browse files Browse the repository at this point in the history
  • Loading branch information
go7066 committed Jan 26, 2024
1 parent cb5b6d2 commit d9c152f
Showing 1 changed file with 69 additions and 24 deletions.
93 changes: 69 additions & 24 deletions examples/gno.land/r/demo/teritori/escrow/escrow.gno
Original file line number Diff line number Diff line change
Expand Up @@ -97,25 +97,26 @@ type Milestone struct {
}

type Contract struct {
id uint64
sender string
contractor string
funder string // funder address
escrowToken string // grc20 token
metadata string // store data forforimage, tags, name, description, links for twitter/github...
status ContractStatus
expireAt uint64
funderFeedback string
contractorFeedback string
milestones []Milestone
pausedBy string
conflictHandler string // can be a realm path or a caller address
handlerCandidate string // conflict handler candidate suggested by one party
handlerSuggestor string // the suggestor off the conflict handler candidate
createdAt uint64
budget uint64
funded bool
rejectReason string
id uint64
sender string
contractor string
contractorCandidates []string
funder string // funder address
escrowToken string // grc20 token
metadata string // store data forforimage, tags, name, description, links for twitter/github...
status ContractStatus
expireAt uint64
funderFeedback string
contractorFeedback string
milestones []Milestone
pausedBy string
conflictHandler string // can be a realm path or a caller address
handlerCandidate string // conflict handler candidate suggested by one party
handlerSuggestor string // the suggestor off the conflict handler candidate
createdAt uint64
budget uint64
funded bool
rejectReason string
}

// Escrow State
Expand Down Expand Up @@ -414,8 +415,39 @@ func SubmitFunder(contractId uint64) {
contracts[contractId].funder = caller.String()
}

// Submit candidate as a contractor
func SubmitContractor(contractId uint64) {
// Accept candidate as a contractor
func AcceptContractor(contractId uint64, contractor string) {
caller := std.GetOrigCaller()
if int(contractId) >= len(contracts) {
panic("invalid contract id")
}

contract := contracts[contractId]

if contract.status != CREATED {
panic("can only submit candidate to a CREATED contract")
}

if contract.contractor != "" {
panic("the contract has already a contractor")
}

if caller.String() != contract.funder {
panic("Only contract funder can accept contractor")
}

candidates := contracts[contractId].contractorCandidates
for _, candidate := range candidates {
// Accept the contract if the address already submitted candidate request
if candidate == contractor {
contracts[contractId].status = ACCEPTED
}
}

contracts[contractId].contractor = contractor
}

func SubmitContractorCandidate(contractId uint64) {
caller := std.GetOrigCaller()
if int(contractId) >= len(contracts) {
panic("invalid contract id")
Expand All @@ -435,8 +467,14 @@ func SubmitContractor(contractId uint64) {
panic("you cannot become a contractor of your funded contract")
}

contracts[contractId].status = ACCEPTED
contracts[contractId].contractor = caller.String()
candidates := contracts[contractId].contractorCandidates
for _, candidate := range candidates {
if candidate == caller.String() {
panic("already a contractor candidate")
}
}

contracts[contractId].contractorCandidates = append(candidates, caller.String())
}

// Complete any milestone in review status and pay the needed amount
Expand Down Expand Up @@ -802,10 +840,17 @@ func RenderContract(contractId uint64) string {
}
milestonesText := strings.Join(milestoneEncodes, ",\n")

contractorCandidates = []string{}
for _, candidate := range c.contractorCandidates {
contractorCandidates = append(contractorCandidates, "\""+candidate+"\"")
}
contractorCandidatesText = strings.Join(contractorCandidates, ",")

return fmt.Sprintf(`{
"id": %d,
"sender": "%s",
"contractor": "%s",
"contractorCandidates": [%s],
"funder": "%s",
"escrowToken": "%s",
"metadata": "%s",
Expand All @@ -821,7 +866,7 @@ func RenderContract(contractId uint64) string {
"budget": %d,
"funded": %t,
"rejectReason": "%s"
}`, c.id, c.sender, c.contractor, c.funder, c.escrowToken, strings.ReplaceAll(c.metadata, "\"", "\\\""), c.status.String(),
}`, c.id, c.sender, c.contractor, contractorCandidatesText, c.funder, c.escrowToken, strings.ReplaceAll(c.metadata, "\"", "\\\""), c.status.String(),
c.expireAt, c.funderFeedback, c.contractorFeedback,
milestonesText, c.pausedBy,
c.conflictHandler, c.handlerCandidate, c.handlerSuggestor, c.budget, c.funded, c.rejectReason)
Expand Down

0 comments on commit d9c152f

Please sign in to comment.