Skip to content

Instantly share code, notes, and snippets.

@plinionaves
Last active June 14, 2024 17:38
Show Gist options
  • Save plinionaves/4526860d396f8f3d65f66c264b69b813 to your computer and use it in GitHub Desktop.
Save plinionaves/4526860d396f8f3d65f66c264b69b813 to your computer and use it in GitHub Desktop.
Cartesi Hackaton

Credit Score flow

  1. User or some App request a new Credit Score:
requestCreditScore(string taxId, uint256 loanAmount)
  1. The Smart Contract make an API call through ChainLink, e.g.:

GET https://api.brx.finance/v1/credit-score?taxId=<tax-id>&loanAmount=<loan-amount>

  1. ChainLink fulfill the request

  2. The Smart Contract save it somehow and push a new payload to the InputBox:

bytes memory payload = abi.encode(response);
inputBox.addInput(L2_DAPP, payload);
  1. Cartesi machine processes the payload (do the computations, run the risk model, etc)

  2. Cartesi machine produces some output (maybe a Voucher to put in onchain, or event a Notice)

import json
from eth_abi import decode
# inside handle_advance function
binary = bytes.fromhex(data["payload"][2:])
(json_string,) = decode_abi(["bytes"], binary)
json_string = json_string.decode("utf-8")
json_data = json.loads(json_string) # CustomerData payload
from eth_abi import decode_abi, encode_abi
# pip install eth_abi == 3.0.0
def encode(tax_id: str, loan_amount: float, json_payload):
json_bytes = json.dumps(json_payload, separators=(',', ':')).encode("utf-8")
hex_str = encode_abi(["string", "uint256", "bytes"], [tax_id, loan_amount, json_bytes]).hex()
return f"0x{hex_str}"
def decode(hex: str):
binary = bytes.fromhex(hex[2:])
(tax_id, loan_amount, json_bytes) = decode_abi(["string", "uint256", "bytes"], binary)
json_payload = json.loads(json_bytes.decode("utf-8"))
return (tax_id, loan_amount, json_payload)
# params
tax_id = "26825555000166"
loan_amount = 10000
api_response = {
"tax_id": "26825555000166",
"tax_id_type": "cnpj",
"tax_id_active": True,
"company": {
"start_date": "2017-09-01",
"social_capital": 10000
},
"data": [
{
"source": "serasa",
"score": 800,
"total_debt": 1000
},
{
"source": "scr",
"score": 500,
"total_debt": 2000
}
]
}
# encode and decode
encoded = encode(tax_id, loan_amount, api_response)
print("encoded:", encoded)
# encoded: 0x0000000000000000000000000000000000000000000000000000000000000060000000000000000000000000000000000000000000000000000000000000271000000000000000000000000000000000000000000000000000000000000000a0000000000000000000000000000000000000000000000000000000000000000e323638323535353530303031363600000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000ee7b227461785f6964223a223236383235353535303030313636222c227461785f69645f74797065223a22636e706a222c227461785f69645f616374697665223a747275652c22636f6d70616e79223a7b2273746172745f64617465223a22323031372d30392d3031222c22736f6369616c5f6361706974616c223a31303030302e307d2c2264617461223a5b7b22736f75726365223a22736572617361222c2273636f7265223a3830302c22746f74616c5f64656274223a313030307d2c7b22736f75726365223a22736372222c2273636f7265223a3530302c22746f74616c5f64656274223a323030307d5d7d000000000000000000000000000000000000
decoded = decode(encoded)
print("decoded: ", decoded)
# decoded: ('26825555000166', 10000, {'tax_id': '26825555000166', 'tax_id_type': 'cnpj', 'tax_id_active': True, 'company': {'start_date': '2017-09-01', 'social_capital': 10000.0}, 'data': [{'source': 'serasa', 'score': 800, 'total_debt': 1000}, {'source': 'scr', 'score': 500, 'total_debt': 2000}]})
// response in the CustomerData structure (see types.ts)
bytes memory payload = abi.encode(response);
inputBox.addInput(L2_DAPP, payload);
// SPDX-License-Identifier: Unlicense
pragma solidity ^0.8.25;
interface ICreditScore {
struct CreditScoreInput {
string taxId;
uint256 loanAmount;
}
struct CreditScoreOutput {
string taxId;
uint256 loanAmount;
uint8 score;
bool qualified;
uint256 timestamp;
string[] sources; // serasa, spc, boa_vista, etc
}
}
enum DataSource {
BOA_VISTA = 'boa_vista',
SERASA = 'serasa',
SPC = 'spc',
SCR = 'scr',
QUOD = 'quod',
}
enum TaxIdType {
CPF = 'cpf',
CNPJ = 'cnpj',
// ...
}
interface DataSourceResponse {
source: DataSource
score: number
total_debt: number
}
interface Person {
birth_date: string // 'YYYY-MM-DD'
capital: number
}
interface Company {
start_date: string // 'YYYY-MM-DD'
social_capital: number
}
interface CustomerData {
tax_id: string
tax_id_type: TaxIdType
tax_id_active: boolean
person?: Person
company?: Company
data: DataSourceResponse[]
}
// example
const response: CustomerData = {
tax_id: '12345678901',
tax_id_type: TaxIdType.CPF,
tax_id_active: true,
person: {
birth_date: '1990-06-30',
capital: 10000,
},
data: [
{
source: DataSource.SERASA,
score: 800,
total_debt: 1000,
},
{
source: DataSource.SCR,
score: 500,
total_debt: 2000,
},
],
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment