|
| 1 | +package reward_manager |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/hex" |
| 5 | + "strings" |
| 6 | + |
| 7 | + bin "github.com/gagliardetto/binary" |
| 8 | + "github.com/gagliardetto/solana-go" |
| 9 | +) |
| 10 | + |
| 11 | +type EvaluateAttestation struct { |
| 12 | + // Instruction Data |
| 13 | + Amount uint64 |
| 14 | + DisbursementID string |
| 15 | + ReceipientEthAddress string |
| 16 | + |
| 17 | + // Used for derivations |
| 18 | + RewardManagerState solana.PublicKey `bin:"-" borsh_skip:"true"` |
| 19 | + Payer solana.PublicKey `bin:"-" borsh_skip:"true"` |
| 20 | + DestinationUserBank solana.PublicKey `bin:"-" borsh_skip:"true"` |
| 21 | + TokenSource solana.PublicKey `bin:"-" borsh_skip:"true"` |
| 22 | + AntiAbuseOracleEthAddress string `bin:"-" borsh_skip:"true"` |
| 23 | + |
| 24 | + // Accounts |
| 25 | + solana.AccountMetaSlice `bin:"-" borsh_skip:"true"` |
| 26 | +} |
| 27 | + |
| 28 | +func NewEvaluateAttestationInstructionBuilder() *EvaluateAttestation { |
| 29 | + data := &EvaluateAttestation{} |
| 30 | + return data |
| 31 | +} |
| 32 | + |
| 33 | +func (inst *EvaluateAttestation) SetDisbursementID(challengedId string, specifier string) *EvaluateAttestation { |
| 34 | + inst.DisbursementID = challengedId + ":" + specifier |
| 35 | + return inst |
| 36 | +} |
| 37 | + |
| 38 | +func (inst *EvaluateAttestation) SetRecipientEthAddress(recipientEthAddress string) *EvaluateAttestation { |
| 39 | + inst.ReceipientEthAddress = recipientEthAddress |
| 40 | + return inst |
| 41 | +} |
| 42 | + |
| 43 | +func (inst *EvaluateAttestation) SetAmount(amount uint64) *EvaluateAttestation { |
| 44 | + inst.Amount = amount |
| 45 | + return inst |
| 46 | +} |
| 47 | + |
| 48 | +func (inst *EvaluateAttestation) SetAntiAbuseOracleEthAddress(antiAbuseOracleAddress string) *EvaluateAttestation { |
| 49 | + inst.AntiAbuseOracleEthAddress = antiAbuseOracleAddress |
| 50 | + return inst |
| 51 | +} |
| 52 | + |
| 53 | +func (inst *EvaluateAttestation) SetRewardManagerState(state solana.PublicKey) *EvaluateAttestation { |
| 54 | + inst.RewardManagerState = state |
| 55 | + return inst |
| 56 | +} |
| 57 | + |
| 58 | +func (inst *EvaluateAttestation) SetTokenSource(tokenSource solana.PublicKey) *EvaluateAttestation { |
| 59 | + inst.TokenSource = tokenSource |
| 60 | + return inst |
| 61 | +} |
| 62 | + |
| 63 | +func (inst *EvaluateAttestation) SetDestinationUserBank(userBank solana.PublicKey) *EvaluateAttestation { |
| 64 | + inst.DestinationUserBank = userBank |
| 65 | + return inst |
| 66 | +} |
| 67 | + |
| 68 | +func (inst *EvaluateAttestation) SetPayer(payer solana.PublicKey) *EvaluateAttestation { |
| 69 | + inst.Payer = payer |
| 70 | + return inst |
| 71 | +} |
| 72 | + |
| 73 | +func (inst EvaluateAttestation) Build() *Instruction { |
| 74 | + authority, _, _ := deriveAuthority(ProgramID, inst.RewardManagerState) |
| 75 | + attestations, _, _ := deriveAttestations(ProgramID, authority, inst.DisbursementID) |
| 76 | + disbursement, _, _ := deriveDisbursement(ProgramID, authority, inst.DisbursementID) |
| 77 | + antiAbuseOracle, _, _ := deriveSender(ProgramID, authority, inst.AntiAbuseOracleEthAddress) |
| 78 | + |
| 79 | + inst.AccountMetaSlice = []*solana.AccountMeta{ |
| 80 | + { |
| 81 | + PublicKey: attestations, |
| 82 | + IsSigner: false, |
| 83 | + IsWritable: true, |
| 84 | + }, |
| 85 | + { |
| 86 | + PublicKey: inst.RewardManagerState, |
| 87 | + IsSigner: false, |
| 88 | + IsWritable: false, |
| 89 | + }, |
| 90 | + { |
| 91 | + PublicKey: authority, |
| 92 | + IsSigner: false, |
| 93 | + IsWritable: false, |
| 94 | + }, |
| 95 | + { |
| 96 | + PublicKey: inst.TokenSource, |
| 97 | + IsSigner: false, |
| 98 | + IsWritable: true, |
| 99 | + }, |
| 100 | + { |
| 101 | + PublicKey: inst.DestinationUserBank, |
| 102 | + IsSigner: false, |
| 103 | + IsWritable: true, |
| 104 | + }, |
| 105 | + { |
| 106 | + PublicKey: disbursement, |
| 107 | + IsSigner: false, |
| 108 | + IsWritable: true, |
| 109 | + }, |
| 110 | + { |
| 111 | + PublicKey: antiAbuseOracle, |
| 112 | + IsSigner: false, |
| 113 | + IsWritable: false, |
| 114 | + }, |
| 115 | + { |
| 116 | + PublicKey: inst.Payer, |
| 117 | + IsSigner: true, |
| 118 | + IsWritable: true, |
| 119 | + }, |
| 120 | + { |
| 121 | + PublicKey: solana.SysVarRentPubkey, |
| 122 | + IsSigner: false, |
| 123 | + IsWritable: false, |
| 124 | + }, |
| 125 | + { |
| 126 | + PublicKey: solana.TokenProgramID, |
| 127 | + IsSigner: false, |
| 128 | + IsWritable: false, |
| 129 | + }, |
| 130 | + { |
| 131 | + PublicKey: solana.SystemProgramID, |
| 132 | + IsSigner: false, |
| 133 | + IsWritable: false, |
| 134 | + }, |
| 135 | + } |
| 136 | + |
| 137 | + return &Instruction{BaseVariant: bin.BaseVariant{ |
| 138 | + Impl: inst, |
| 139 | + TypeID: bin.TypeIDFromUint8(Instruction_EvaluateAttestations), |
| 140 | + }} |
| 141 | +} |
| 142 | + |
| 143 | +func (inst EvaluateAttestation) MarshalWithEncoder(encoder *bin.Encoder) error { |
| 144 | + err := encoder.Encode(inst.Amount) |
| 145 | + if err != nil { |
| 146 | + return err |
| 147 | + } |
| 148 | + |
| 149 | + err = encoder.Encode(inst.DisbursementID) |
| 150 | + if err != nil { |
| 151 | + return err |
| 152 | + } |
| 153 | + |
| 154 | + address, err := hex.DecodeString(strings.TrimPrefix(inst.ReceipientEthAddress, "0x")) |
| 155 | + if err != nil { |
| 156 | + return err |
| 157 | + } |
| 158 | + return encoder.WriteBytes(address, false) |
| 159 | +} |
| 160 | + |
| 161 | +func (inst *EvaluateAttestation) UnmarshalWithDecoder(decoder *bin.Decoder) error { |
| 162 | + return decoder.Decode(&inst) |
| 163 | +} |
| 164 | + |
| 165 | +func NewEvaluateAttestationInstruction( |
| 166 | + challengeId string, |
| 167 | + specifier string, |
| 168 | + recipientEthAddress string, |
| 169 | + amount uint64, |
| 170 | + antiAbuseOracleAddress string, |
| 171 | + rewardManagerState solana.PublicKey, |
| 172 | + tokenSource solana.PublicKey, |
| 173 | + destinationUserBank solana.PublicKey, |
| 174 | + payer solana.PublicKey, |
| 175 | +) *EvaluateAttestation { |
| 176 | + return NewEvaluateAttestationInstructionBuilder(). |
| 177 | + SetRewardManagerState(rewardManagerState). |
| 178 | + SetDisbursementID(challengeId, specifier). |
| 179 | + SetRecipientEthAddress(recipientEthAddress). |
| 180 | + SetAmount(amount). |
| 181 | + SetAntiAbuseOracleEthAddress(antiAbuseOracleAddress). |
| 182 | + SetTokenSource(tokenSource). |
| 183 | + SetDestinationUserBank(destinationUserBank). |
| 184 | + SetPayer(payer) |
| 185 | +} |
0 commit comments