// Solbound v2. Each successful grant or renewal pays the configured fee in SOL. // Record layouts, PDA seeds and owner-only revocations follow v1. #include #include enum { REVOKE = 1, REVOKE_ALL = 2, PAID_GRANT = 3, UPDATE_CONFIG = 4, INIT_CONFIG = 5 }; enum { VAULT_TAG = 1, GRANT_TAG = 2, CONFIG_TAG = 3, STATE_SIZE = 41, GRANT_SIZE = 146, CONFIG_SIZE = 82, MAX_ACCOUNTS = 7, MAX_FEE_CENTS = 10000 }; enum { OK = 0, BAD_EXPIRY = 101, BAD_SCOPE = 102, BAD_PDA = 103, INVALID_STATE = 104, ARITHMETIC_ERROR = 105, UNSUPPORTED_RENT = 106, BAD_TREASURY = 107, BAD_ORACLE = 108, BAD_PRICE = 109, EXPIRED_QUOTE = 110, FEE_LIMIT = 111, BAD_CONFIG = 112, STALE_CONFIG = 113, PAUSED = 114, BAD_AUTHORITY = 115 }; static const SolPubkey SYSTEM_PROGRAM = {{0}}; static const SolPubkey UPGRADEABLE_LOADER = {{0x02, 0xa8, 0xf6, 0x91, 0x4e, 0x88, 0xa1, 0xb0, 0xe2, 0x10, 0x15, 0x3e, 0xf7, 0x63, 0xae, 0x2b, 0x00, 0xc2, 0xb9, 0x3d, 0x16, 0xc1, 0x24, 0xd2, 0xc0, 0x53, 0x7a, 0x10, 0x04, 0x80, 0x00, 0x00}}; // Canonical Solana Pyth Core SOL/USD shard-zero account and its receiver. static const SolPubkey SOL_USD_ACCOUNT = {{0x60, 0x31, 0x47, 0x04, 0x34, 0x0d, 0xed, 0xdf, 0x37, 0x1f, 0xd4, 0x24, 0x72, 0x14, 0x8f, 0x24, 0x8e, 0x9d, 0x1a, 0x6d, 0x1a, 0x5e, 0xb2, 0xac, 0x3a, 0xcd, 0x8b, 0x7f, 0xd5, 0xd6, 0xb2, 0x43}}; static const SolPubkey PYTH_RECEIVER = {{0x0c, 0xb7, 0xfa, 0xbb, 0x52, 0xf7, 0xa6, 0x48, 0xbb, 0x5b, 0x31, 0x7d, 0x9a, 0x01, 0x8b, 0x90, 0x57, 0xcb, 0x02, 0x47, 0x74, 0xfa, 0xfe, 0x01, 0xe6, 0xc4, 0xdf, 0x98, 0xcc, 0x38, 0x58, 0x81}}; static const SolPubkey TREASURY = {{0x07, 0xaa, 0x86, 0xb7, 0xd8, 0xc4, 0x66, 0x46, 0xe5, 0xca, 0xcd, 0xe1, 0x63, 0x13, 0x7e, 0x36, 0x1d, 0x02, 0x48, 0xd9, 0xf3, 0x1e, 0x10, 0x36, 0x80, 0x76, 0x5e, 0x0d, 0xd1, 0x70, 0xea, 0x1d}}; static const uint8_t SOL_USD_FEED[32] = {0xef, 0x0d, 0x8b, 0x6f, 0xda, 0x2c, 0xeb, 0xa4, 0x1d, 0xa1, 0x5d, 0x40, 0x95, 0xd1, 0xda, 0x39, 0x2a, 0x0d, 0x2f, 0x8e, 0xd0, 0xc6, 0xc7, 0xbc, 0x0f, 0x4c, 0xfa, 0xc8, 0xc2, 0x80, 0xb5, 0x6d}; static const uint8_t PRICE_UPDATE_V2[8] = {0x22, 0xf1, 0x23, 0x63, 0x9d, 0x7e, 0xf4, 0xcd}; static const uint64_t CENT_NUMERATOR = 1000000000000000ULL; // $0.01 * 10^8 * 10^9 lamports/SOL. static const uint64_t MAX_LIFETIME = 366UL * 24 * 60 * 60; // Runtime syscall ABI. Both structs match the aligned repr(C) sysvars. typedef struct { uint64_t slot; int64_t start; uint64_t epoch, leader_epoch; int64_t now; } Clock; typedef struct { uint64_t price, threshold_bits; uint8_t burn, padding[7]; } Rent; extern uint64_t sol_get_clock_sysvar(Clock *); extern uint64_t sol_get_rent_sysvar(Rent *); extern void sol_memset_(void *, uint8_t, uint64_t); // LLVM may lower a constant-size zeroing operation to the C ABI. __attribute__((visibility("hidden"))) void *memset(void *dst, int value, size_t size) { sol_memset_(dst, (uint8_t)value, size); return dst; } static uint64_t read_u64(const uint8_t *p) { uint64_t n; __builtin_memcpy(&n, p, sizeof(n)); return n; } static void write_u64(uint8_t *p, uint64_t n) { __builtin_memcpy(p, &n, sizeof(n)); } static bool same(const uint8_t *a, const uint8_t *b) { for (int i = 0; i < 32; i++) if (a[i] != b[i]) return false; return true; } static uint64_t check_pda(const SolParameters *p, const SolAccountInfo *account, SolSignerSeed *seeds, int count, uint8_t *bump) { SolPubkey derived; uint64_t err = sol_try_find_program_address(seeds, count, p->program_id, &derived, bump); if (err) return err; if (!SolPubkey_same(&derived, account->key)) return BAD_PDA; seeds[count] = (SolSignerSeed){bump, 1}; return OK; } // Each grant belongs to precisely one (vault, delegate, audience, scope, asset). static void grant_seeds(SolSignerSeed seeds[7], const SolPubkey *vault, const uint8_t *delegate, const uint8_t *audience, const uint8_t *scope, const uint8_t *asset) { seeds[0] = (SolSignerSeed){(const uint8_t *)"grant", 5}; seeds[1] = (SolSignerSeed){vault->x, 32}; seeds[2] = (SolSignerSeed){delegate, 32}; seeds[3] = (SolSignerSeed){audience, 32}; seeds[4] = (SolSignerSeed){scope, 1}; seeds[5] = (SolSignerSeed){asset, 32}; } static uint64_t system_call(const SolParameters *p, SolAccountMeta *metas, uint64_t count, uint8_t *data, uint64_t length, const SolSignerSeeds *signer) { SolPubkey system = SYSTEM_PROGRAM; SolInstruction ix = {&system, metas, count, data, length}; return sol_invoke_signed(&ix, p->ka, p->ka_num, signer, signer ? 1 : 0); } // Allocate/assign works even if a third party previously sent lamports to the PDA. // The vault pays record deposits only to the canonical registry PDAs. static uint64_t ensure_record(const SolParameters *p, SolAccountInfo *record, uint64_t size, SolSignerSeed *seeds, int count, bool *created) { *created = false; if (!record->is_writable || record->executable) return ERROR_INVALID_ACCOUNT_DATA; if (SolPubkey_same(record->owner, p->program_id)) return record->data_len == size ? OK : INVALID_STATE; if (!SolPubkey_same(record->owner, &SYSTEM_PROGRAM) || record->data_len != 0) return ERROR_INCORRECT_PROGRAM_ID; Rent rent = {0}; uint64_t err = sol_get_rent_sysvar(&rent); if (err) return err; // The current and legacy rent sysvars use exact 1.0 and 2.0 thresholds. uint64_t years; if (rent.threshold_bits == 0x3ff0000000000000UL) years = 1; else if (rent.threshold_bits == 0x4000000000000000UL) years = 2; else return UNSUPPORTED_RENT; uint64_t units = (128 + size) * years; // Maximum allocation in this program is GRANT_SIZE bytes, at most two years. if (rent.price > UINT64_MAX / ((128 + GRANT_SIZE) * 2)) return ARITHMETIC_ERROR; uint64_t minimum = rent.price * units; SolAccountMeta payer = {p->ka[0].key, true, true}; SolAccountMeta target = {record->key, true, false}; if (*record->lamports < minimum) { uint8_t transfer[12] = {2}; write_u64(transfer + 4, minimum - *record->lamports); SolAccountMeta metas[] = {payer, target}; err = system_call(p, metas, 2, transfer, sizeof(transfer), NULL); if (err) return err; } SolSignerSeeds signer = {seeds, count}; target.is_signer = true; uint8_t allocate[12] = {8}; write_u64(allocate + 4, size); err = system_call(p, &target, 1, allocate, sizeof(allocate), &signer); if (err) return err; uint8_t assign[36] = {1}; __builtin_memcpy(assign + 4, p->program_id->x, 32); err = system_call(p, &target, 1, assign, sizeof(assign), &signer); if (err) return err; *created = true; return OK; } static bool valid_state(const SolParameters *p, const SolAccountInfo *state) { return SolPubkey_same(state->owner, p->program_id) && state->data_len == STATE_SIZE && state->data[0] == VAULT_TAG && same(p->ka[0].key->x, state->data + 1); } static uint64_t check_config(const SolParameters *p, const SolAccountInfo *config, SolSignerSeed seeds[2], uint8_t *bump) { seeds[0] = (SolSignerSeed){(const uint8_t *)"config", 6}; return check_pda(p, config, seeds, 1, bump); } static bool valid_config(const SolParameters *p, const SolAccountInfo *config) { return SolPubkey_same(config->owner, p->program_id) && !config->executable && config->data_len == CONFIG_SIZE && config->data[0] == CONFIG_TAG; } static uint64_t init_config(const SolParameters *p) { if (p->ka_num != 4 || p->data_len != 33) return ERROR_INVALID_INSTRUCTION_DATA; if (!p->ka[0].is_writable || !SolPubkey_same(p->ka[2].key, &SYSTEM_PROGRAM) || same(p->data + 1, SYSTEM_PROGRAM.x)) return BAD_CONFIG; // Loader-v3 ProgramData: u32 tag 3, u64 slot, Option (1 + 32 bytes). const SolAccountInfo *code = &p->ka[3]; SolSignerSeed program_seed = {p->program_id->x, 32}; SolPubkey canonical; uint8_t bump; uint64_t err = sol_try_find_program_address(&program_seed, 1, &UPGRADEABLE_LOADER, &canonical, &bump); if (err) return err; if (!SolPubkey_same(code->key, &canonical) || !SolPubkey_same(code->owner, &UPGRADEABLE_LOADER) || code->executable || code->data_len <= 45 || code->data[0] != 3 || code->data[1] || code->data[2] || code->data[3] || code->data[12] != 1 || !same(code->data + 13, p->ka[0].key->x)) return BAD_AUTHORITY; SolAccountInfo *config = &p->ka[1]; SolSignerSeed seeds[2]; err = check_config(p, config, seeds, &bump); if (err) return err; bool created; err = ensure_record(p, config, CONFIG_SIZE, seeds, 2, &created); if (err) return err; if (!created) return BAD_CONFIG; config->data[0] = CONFIG_TAG; __builtin_memcpy(config->data + 1, p->data + 1, 32); __builtin_memcpy(config->data + 33, TREASURY.x, 32); write_u64(config->data + 65, 250); config->data[73] = 0; write_u64(config->data + 74, 1); return OK; } static uint64_t update_config(const SolParameters *p) { if (p->ka_num != 2 || p->data_len != 50) return ERROR_INVALID_INSTRUCTION_DATA; SolAccountInfo *config = &p->ka[1]; SolSignerSeed seeds[2]; uint8_t bump; uint64_t err = check_config(p, config, seeds, &bump); if (err) return err; if (!config->is_writable || !valid_config(p, config)) return BAD_CONFIG; if (!same(config->data + 1, p->ka[0].key->x)) return BAD_AUTHORITY; uint64_t version = read_u64(config->data + 74), cents = read_u64(p->data + 9); if (version != read_u64(p->data + 1) || version == UINT64_MAX) return STALE_CONFIG; if (cents > MAX_FEE_CENTS || p->data[49] > 1 || same(p->data + 17, SYSTEM_PROGRAM.x)) return BAD_CONFIG; __builtin_memcpy(config->data + 33, p->data + 17, 32); write_u64(config->data + 65, cents); config->data[73] = p->data[49]; write_u64(config->data + 74, version + 1); return OK; } static uint64_t collect_service_fee(const SolParameters *p, int64_t now) { const SolAccountInfo *treasury = &p->ka[4], *oracle = &p->ka[5]; const SolAccountInfo *config = &p->ka[6]; SolSignerSeed seeds[2]; uint8_t bump; uint64_t err = check_config(p, config, seeds, &bump); if (err) return err; if (!valid_config(p, config)) return BAD_CONFIG; if (read_u64(config->data + 74) != read_u64(p->data + 122)) return STALE_CONFIG; if (config->data[73]) return PAUSED; if (!same(treasury->key->x, config->data + 33) || !treasury->is_writable || treasury->executable) return BAD_TREASURY; int64_t deadline = (int64_t)read_u64(p->data + 114); if (deadline <= now || deadline - now > 180) return EXPIRED_QUOTE; uint64_t cents = read_u64(config->data + 65), limit = read_u64(p->data + 106); if (cents > MAX_FEE_CENTS) return BAD_CONFIG; if (limit > 1000000000ULL) return FEE_LIMIT; if (!SolPubkey_same(oracle->key, &SOL_USD_ACCOUNT)) return BAD_ORACLE; if (!cents) return OK; // A transfer from the treasury to itself would have no cost. Refuse that paid grant. if (SolPubkey_same(treasury->key, p->ka[0].key)) return BAD_TREASURY; if (!SolPubkey_same(oracle->owner, &PYTH_RECEIVER) || oracle->executable || oracle->data_len != 134) return BAD_ORACLE; const uint8_t *data = oracle->data; for (int i = 0; i < 8; i++) if (data[i] != PRICE_UPDATE_V2[i]) return BAD_ORACLE; // The Borsh Full variant has no extra signer-count byte. Never parse Partial with these offsets. if (data[40] != 1 || !same(data + 41, SOL_USD_FEED)) return BAD_ORACLE; int32_t exponent; __builtin_memcpy(&exponent, data + 89, sizeof(exponent)); int64_t price = (int64_t)read_u64(data + 73), published = (int64_t)read_u64(data + 93); uint64_t confidence = read_u64(data + 81); if (exponent != -8 || price <= 0 || confidence > (uint64_t)price / 100 || published < 0 || published > now || now - published > 120) return BAD_PRICE; uint64_t lamports = 1 + (cents * CENT_NUMERATOR - 1) / (uint64_t)price; if (lamports > limit) return FEE_LIMIT; uint8_t transfer[12] = {2}; write_u64(transfer + 4, lamports); SolAccountMeta metas[] = {{p->ka[0].key, true, true}, {treasury->key, true, false}}; return system_call(p, metas, 2, transfer, sizeof(transfer), NULL); } static uint64_t grant(const SolParameters *p) { if (p->ka_num != 7 || p->data_len != 130) return ERROR_INVALID_INSTRUCTION_DATA; SolAccountInfo *vault = &p->ka[0], *state = &p->ka[1], *grant = &p->ka[2]; if (!vault->is_writable || !SolPubkey_same(p->ka[3].key, &SYSTEM_PROGRAM)) return ERROR_INVALID_ARGUMENT; const uint8_t *scope = p->data + 1, *delegate = p->data + 2, *audience = p->data + 34, *asset = p->data + 66; if (same(delegate, SYSTEM_PROGRAM.x) || same(delegate, vault->key->x) || same(audience, SYSTEM_PROGRAM.x)) return ERROR_INVALID_ARGUMENT; if (*scope > 1 || ((*scope == 0) != same(asset, SYSTEM_PROGRAM.x))) return BAD_SCOPE; Clock clock; uint64_t err = sol_get_clock_sysvar(&clock); if (err) return err; int64_t expiry = (int64_t)read_u64(p->data + 98); if (clock.now < 0 || expiry <= clock.now || (uint64_t)(expiry - clock.now) > MAX_LIFETIME) return BAD_EXPIRY; SolSignerSeed state_seeds[3] = {{(const uint8_t *)"vault", 5}, {vault->key->x, 32}}; SolSignerSeed seeds[7]; grant_seeds(seeds, vault->key, delegate, audience, scope, asset); uint8_t state_bump, grant_bump; err = check_pda(p, state, state_seeds, 2, &state_bump); if (err) return err; err = check_pda(p, grant, seeds, 6, &grant_bump); if (err) return err; // Any later error, including record allocation, rolls the transfer back with the instruction. err = collect_service_fee(p, clock.now); if (err) return err; bool created; err = ensure_record(p, state, STATE_SIZE, state_seeds, 3, &created); if (err) return err; if (created) { state->data[0] = VAULT_TAG; __builtin_memcpy(state->data + 1, vault->key->x, 32); write_u64(state->data + 33, 0); } else if (!valid_state(p, state)) return INVALID_STATE; err = ensure_record(p, grant, GRANT_SIZE, seeds, 7, &created); if (err) return err; if (!created && (grant->data[0] != GRANT_TAG || !same(grant->data + 1, vault->key->x) || !same(grant->data + 33, delegate) || !same(grant->data + 65, audience) || grant->data[97] != *scope || !same(grant->data + 98, asset))) return INVALID_STATE; grant->data[0] = GRANT_TAG; __builtin_memcpy(grant->data + 1, vault->key->x, 32); __builtin_memcpy(grant->data + 33, delegate, 32); __builtin_memcpy(grant->data + 65, audience, 32); grant->data[97] = *scope; __builtin_memcpy(grant->data + 98, asset, 32); write_u64(grant->data + 130, read_u64(state->data + 33)); write_u64(grant->data + 138, expiry); return OK; } static uint64_t revoke_one(const SolParameters *p) { if (p->ka_num != 2 || p->data_len != 1) return ERROR_INVALID_INSTRUCTION_DATA; SolAccountInfo *vault = &p->ka[0], *grant = &p->ka[1]; if (!vault->is_writable || !grant->is_writable || !SolPubkey_same(grant->owner, p->program_id) || grant->data_len != GRANT_SIZE || grant->data[0] != GRANT_TAG || !same(grant->data + 1, vault->key->x)) return INVALID_STATE; SolSignerSeed seeds[7]; uint8_t bump; grant_seeds(seeds, vault->key, grant->data + 33, grant->data + 65, grant->data + 97, grant->data + 98); uint64_t err = check_pda(p, grant, seeds, 6, &bump); if (err) return err; if (*vault->lamports > UINT64_MAX - *grant->lamports) return ARITHMETIC_ERROR; *vault->lamports += *grant->lamports; *grant->lamports = 0; // Clear the serialized length and contents before returning ownership to System. // A same-transaction deposit can no longer revive a closed grant. __builtin_memset(grant->data, 0, GRANT_SIZE); write_u64(grant->data - sizeof(uint64_t), 0); grant->data_len = 0; __builtin_memset(grant->owner->x, 0, 32); return OK; } static uint64_t revoke_all(const SolParameters *p) { if (p->ka_num != 2 || p->data_len != 1) return ERROR_INVALID_INSTRUCTION_DATA; SolAccountInfo *state = &p->ka[1]; if (!state->is_writable || !valid_state(p, state)) return INVALID_STATE; SolSignerSeed seeds[3] = {{(const uint8_t *)"vault", 5}, {p->ka[0].key->x, 32}}; uint8_t bump; uint64_t err = check_pda(p, state, seeds, 2, &bump); if (err) return err; uint64_t epoch = read_u64(state->data + 33); if (epoch == UINT64_MAX) return ARITHMETIC_ERROR; write_u64(state->data + 33, epoch + 1); return OK; } uint64_t entrypoint(const uint8_t *input) { SolAccountInfo accounts[MAX_ACCOUNTS]; SolParameters p = {.ka = accounts}; if (!sol_deserialize(input, &p, MAX_ACCOUNTS) || !p.ka_num || p.ka_num > MAX_ACCOUNTS || !p.data_len) return ERROR_INVALID_INSTRUCTION_DATA; if (!p.ka[0].is_signer) return ERROR_MISSING_REQUIRED_SIGNATURES; if (!SolPubkey_same(p.ka[0].owner, &SYSTEM_PROGRAM) || p.ka[0].data_len || p.ka[0].executable) return ERROR_INVALID_ARGUMENT; switch (p.data[0]) { case PAID_GRANT: return grant(&p); case UPDATE_CONFIG: return update_config(&p); case INIT_CONFIG: return init_config(&p); case REVOKE: return revoke_one(&p); case REVOKE_ALL: return revoke_all(&p); default: return ERROR_INVALID_INSTRUCTION_DATA; } }